8 func (m *Metrics) SetGauge(key []string, val float32) {
9 if m.HostName != "" && m.EnableHostname {
10 key = insert(0, m.HostName, key)
12 if m.EnableTypePrefix {
13 key = insert(0, "gauge", key)
15 if m.ServiceName != "" {
16 key = insert(0, m.ServiceName, key)
18 m.sink.SetGauge(key, val)
21 func (m *Metrics) EmitKey(key []string, val float32) {
22 if m.EnableTypePrefix {
23 key = insert(0, "kv", key)
25 if m.ServiceName != "" {
26 key = insert(0, m.ServiceName, key)
28 m.sink.EmitKey(key, val)
31 func (m *Metrics) IncrCounter(key []string, val float32) {
32 if m.EnableTypePrefix {
33 key = insert(0, "counter", key)
35 if m.ServiceName != "" {
36 key = insert(0, m.ServiceName, key)
38 m.sink.IncrCounter(key, val)
41 func (m *Metrics) AddSample(key []string, val float32) {
42 if m.EnableTypePrefix {
43 key = insert(0, "sample", key)
45 if m.ServiceName != "" {
46 key = insert(0, m.ServiceName, key)
48 m.sink.AddSample(key, val)
51 func (m *Metrics) MeasureSince(key []string, start time.Time) {
52 if m.EnableTypePrefix {
53 key = insert(0, "timer", key)
55 if m.ServiceName != "" {
56 key = insert(0, m.ServiceName, key)
59 elapsed := now.Sub(start)
60 msec := float32(elapsed.Nanoseconds()) / float32(m.TimerGranularity)
61 m.sink.AddSample(key, msec)
64 // Periodically collects runtime stats to publish
65 func (m *Metrics) collectStats() {
67 time.Sleep(m.ProfileInterval)
72 // Emits various runtime statsitics
73 func (m *Metrics) emitRuntimeStats() {
74 // Export number of Goroutines
75 numRoutines := runtime.NumGoroutine()
76 m.SetGauge([]string{"runtime", "num_goroutines"}, float32(numRoutines))
78 // Export memory stats
79 var stats runtime.MemStats
80 runtime.ReadMemStats(&stats)
81 m.SetGauge([]string{"runtime", "alloc_bytes"}, float32(stats.Alloc))
82 m.SetGauge([]string{"runtime", "sys_bytes"}, float32(stats.Sys))
83 m.SetGauge([]string{"runtime", "malloc_count"}, float32(stats.Mallocs))
84 m.SetGauge([]string{"runtime", "free_count"}, float32(stats.Frees))
85 m.SetGauge([]string{"runtime", "heap_objects"}, float32(stats.HeapObjects))
86 m.SetGauge([]string{"runtime", "total_gc_pause_ns"}, float32(stats.PauseTotalNs))
87 m.SetGauge([]string{"runtime", "total_gc_runs"}, float32(stats.NumGC))
89 // Export info about the last few GC runs
93 if num < m.lastNumGC {
97 // Ensure we don't scan more than 256
98 if num-m.lastNumGC >= 256 {
99 m.lastNumGC = num - 255
102 for i := m.lastNumGC; i < num; i++ {
103 pause := stats.PauseNs[i%256]
104 m.AddSample([]string{"runtime", "gc_pause_ns"}, float32(pause))
109 // Inserts a string value at an index into the slice
110 func insert(i int, v string, s []string) []string {