Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using new metrics lib. #38

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 21 additions & 13 deletions common/stats/aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package stats

import (
"strings"
"sync"
"time"
)
Expand Down Expand Up @@ -50,9 +51,8 @@ func newCollectedStatUnescaped(name string) *collectedStat {
// previously. Useful for reporters that have low throughput ie stathat.
type Aggregator struct {
// Holds all of our stats based on stat.Name
sl sync.RWMutex
stats map[string]*statHolder

sl sync.RWMutex
stats map[string]*statHolder
reporters []reporter
}

Expand Down Expand Up @@ -167,22 +167,30 @@ func (a *Aggregator) report(st []*collectedStat) {
}
}

func (r *Aggregator) Inc(component string, stat string, value int64, rate float32) {
r.add(component, stat, counterKind, value)
func (r *Aggregator) Inc(value int64, stat ...string) {
component := stat[0]
newstat := strings.Join(stat[1:], ".")
r.add(component, newstat, counterKind, value)
}

func (r *Aggregator) Gauge(component string, stat string, value int64, rate float32) {
r.add(component, stat, gaugeKind, value)
func (r *Aggregator) Gauge(value int64, stat ...string) {
component := stat[0]
newstat := strings.Join(stat[1:], ".")
r.add(component, newstat, gaugeKind, value)
}

func (r *Aggregator) Measure(component string, stat string, value int64, rate float32) {
r.add(component, stat, valueKind, value)
func (r *Aggregator) Measure(value int64, stat ...string) {
component := stat[0]
newstat := strings.Join(stat[1:], ".")
r.add(component, newstat, valueKind, value)
}

func (r *Aggregator) Time(component string, stat string, value time.Duration, rate float32) {
r.add(component, stat, durationKind, value)
func (r *Aggregator) Time(value time.Duration, stat ...string) {
component := stat[0]
newstat := strings.Join(stat[1:], ".")
r.add(component, newstat, durationKind, value)
}

func (r *Aggregator) NewTimer(component string, stat string, rate float32) *Timer {
return newTimer(r, component, stat, rate)
func (r *Aggregator) NewTimer(stat ...string) *Timer {
return newTimer(r, stat...)
}
8 changes: 4 additions & 4 deletions common/stats/mem.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ func StartReportingMemoryAndGC(reporter Statter, d time.Duration) {

prefix := "runtime"

reporter.Measure(prefix, "allocated", int64(ms.Alloc), 1.0)
reporter.Measure(prefix, "allocated.heap", int64(ms.HeapAlloc), 1.0)
reporter.Time(prefix, "gc.pause", time.Duration(ms.PauseNs[(ms.NumGC+255)%256]), 1.0)
reporter.Measure(int64(ms.Alloc), prefix, "allocated")
reporter.Measure(int64(ms.HeapAlloc), prefix, "allocated.heap")
reporter.Time(time.Duration(ms.PauseNs[(ms.NumGC+255)%256]), prefix, "gc.pause")

// GC CPU percentage.
reporter.Measure(prefix, "gc.cpufraction", int64(ms.GCCPUFraction*100), 1.0)
reporter.Measure(int64(ms.GCCPUFraction*100), prefix, "gc.cpufraction")
}
}
}
103 changes: 103 additions & 0 deletions common/stats/metricsstatter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package stats

import (
"os"
"path"
"strings"
"time"

"github.com/armon/go-metrics"
"gopkg.in/inconshreveable/log15.v2"
)

func NewMetricsStatter(config Config) Statter {
inm := metrics.NewInmemSink(10*time.Second, time.Minute)
metrics.DefaultInmemSignal(inm)

var fanout metrics.FanoutSink

if config.Statsd != nil && config.Statsd.StatsdUdpTarget != "" {
sink, err := metrics.NewStatsdSink(config.Statsd.StatsdUdpTarget)
if err != nil {
log15.Error("Couldn't create statsd reporter", "err", err)
}
fanout = append(fanout, sink)
}
prefix := "iron"
if config.Statsd != nil && len(config.Statsd.Prefix) > 0 {
prefix = config.Statsd.Prefix + ".iron"
}
metricsConfig := metrics.DefaultConfig(prefix)
metricsConfig.EnableRuntimeMetrics = false // Use another metrics instance for runtime stats reporting
metricsConfig.EnableHostname = false

m := new(MetricsStatter)
m.hostname = whoami()
m.servicePrefix = path.Base(os.Args[0])

if len(fanout) > 0 {
if !config.NoHostname {
metricsConfig.ServiceName = strings.Join([]string{prefix, m.servicePrefix, m.hostname}, ".")
}
fanout = append(fanout, inm)
metrics.NewGlobal(metricsConfig, fanout)
} else {
metrics.NewGlobal(metricsConfig, inm)
}

if config.GCStats >= 0 {
if config.GCStats == 0 {
config.GCStats = 1
}
metricsConfig.EnableRuntimeMetrics = true
metricsConfig.ProfileInterval = time.Duration(config.Interval * float64(time.Second))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe time.Duration(config.Interval) * time.Second instead of time.Duration(config.Interval * float64(time.Second)) to omit float64() ?

if len(fanout) > 0 {
metricsConfig.ServiceName = strings.Join([]string{prefix, m.servicePrefix, m.hostname}, ".")
metrics.New(metricsConfig, fanout)
} else {
metrics.New(metricsConfig, inm)
}
}

log15.Info("Statter configured", "servicePrefix", m.servicePrefix)

return m
}

type MetricsStatter struct {
hostname string
servicePrefix string
}

func (m *MetricsStatter) template(stat ...string) []string {
newstat := []string{m.servicePrefix}
for _, token := range stat {
switch token {
case "@hostname":
newstat = append(newstat, m.hostname)
default:
newstat = append(newstat, token)
}
}
return newstat
}

func (m *MetricsStatter) Inc(value int64, stat ...string) {
newstat := m.template(stat...)
metrics.IncrCounter(newstat, float32(value))
}
func (m *MetricsStatter) Gauge(value int64, stat ...string) {
newstat := m.template(stat...)
metrics.SetGauge(newstat, float32(value))
}
func (m *MetricsStatter) Measure(value int64, stat ...string) {
newstat := m.template(stat...)
metrics.AddSample(newstat, float32(value))
}
func (m *MetricsStatter) Time(value time.Duration, stat ...string) {
newstat := m.template(stat...)
metrics.AddSample(newstat, float32(value.Nanoseconds()/1e6)) //Report ms to statsd
}
func (m *MetricsStatter) NewTimer(stat ...string) *Timer {
return newTimer(m, m.template(stat...)...)
}
90 changes: 90 additions & 0 deletions common/stats/multistatter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package stats

import (
"time"

"gopkg.in/inconshreveable/log15.v2"
)

type MultiStatter struct {
statters []Statter
}

func NewMultiStatter(config Config) Statter {
s := new(MultiStatter)

var reporters []reporter
if config.StatHat != nil && config.StatHat.Email != "" {
reporters = append(reporters, config.StatHat)
}

if config.NewRelic != nil && config.NewRelic.LicenseKey != "" {
// NR wants version?
// can get it out of the namespace? roll it here?
reporters = append(reporters, NewNewRelicReporter("1.0", config.NewRelic.LicenseKey))
}

if config.Log15 != nil {
reporters = append(reporters, NewLogReporter())
}

if len(reporters) > 0 {
ag := newAggregator(reporters)
s.statters = append(s.statters, ag)
go func() {
for range time.Tick(time.Duration(config.Interval * float64(time.Second))) {
ag.report(nil)
}
}()
}

if config.Statsd != nil && config.Statsd.StatsdUdpTarget != "" {
std, err := NewStatsd(config.Statsd)
if err == nil {
s.statters = append(s.statters, std)
} else {
log15.Error("Couldn't create statsd reporter", "err", err)
}
}

if len(reporters) == 0 && config.Statsd == nil && config.History == 0 {
return &NilStatter{}
}

if config.GCStats >= 0 {
if config.GCStats == 0 {
config.GCStats = 1
}
go StartReportingMemoryAndGC(s, time.Duration(config.GCStats)*time.Second)
}

return s
}

func (s *MultiStatter) Inc(value int64, stat ...string) {
for _, st := range s.statters {
st.Inc(value, stat...)
}
}

func (s *MultiStatter) Gauge(value int64, stat ...string) {
for _, st := range s.statters {
st.Gauge(value, stat...)
}
}

func (s *MultiStatter) Measure(value int64, stat ...string) {
for _, st := range s.statters {
st.Measure(value, stat...)
}
}

func (s *MultiStatter) Time(value time.Duration, stat ...string) {
for _, st := range s.statters {
st.Time(value, stat...)
}
}

func (s *MultiStatter) NewTimer(stat ...string) *Timer {
return newTimer(s, stat...)
}
Loading