metrics.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. package metrics
  2. import (
  3. "runtime"
  4. "strings"
  5. "time"
  6. "github.com/hashicorp/go-immutable-radix"
  7. )
  8. type Label struct {
  9. Name string
  10. Value string
  11. }
  12. func (m *Metrics) SetGauge(key []string, val float32) {
  13. m.SetGaugeWithLabels(key, val, nil)
  14. }
  15. func (m *Metrics) SetGaugeWithLabels(key []string, val float32, labels []Label) {
  16. if m.HostName != "" {
  17. if m.EnableHostnameLabel {
  18. labels = append(labels, Label{"host", m.HostName})
  19. } else if m.EnableHostname {
  20. key = insert(0, m.HostName, key)
  21. }
  22. }
  23. if m.EnableTypePrefix {
  24. key = insert(0, "gauge", key)
  25. }
  26. if m.ServiceName != "" {
  27. if m.EnableServiceLabel {
  28. labels = append(labels, Label{"service", m.ServiceName})
  29. } else {
  30. key = insert(0, m.ServiceName, key)
  31. }
  32. }
  33. allowed, labelsFiltered := m.allowMetric(key, labels)
  34. if !allowed {
  35. return
  36. }
  37. m.sink.SetGaugeWithLabels(key, val, labelsFiltered)
  38. }
  39. func (m *Metrics) EmitKey(key []string, val float32) {
  40. if m.EnableTypePrefix {
  41. key = insert(0, "kv", key)
  42. }
  43. if m.ServiceName != "" {
  44. key = insert(0, m.ServiceName, key)
  45. }
  46. allowed, _ := m.allowMetric(key, nil)
  47. if !allowed {
  48. return
  49. }
  50. m.sink.EmitKey(key, val)
  51. }
  52. func (m *Metrics) IncrCounter(key []string, val float32) {
  53. m.IncrCounterWithLabels(key, val, nil)
  54. }
  55. func (m *Metrics) IncrCounterWithLabels(key []string, val float32, labels []Label) {
  56. if m.HostName != "" && m.EnableHostnameLabel {
  57. labels = append(labels, Label{"host", m.HostName})
  58. }
  59. if m.EnableTypePrefix {
  60. key = insert(0, "counter", key)
  61. }
  62. if m.ServiceName != "" {
  63. if m.EnableServiceLabel {
  64. labels = append(labels, Label{"service", m.ServiceName})
  65. } else {
  66. key = insert(0, m.ServiceName, key)
  67. }
  68. }
  69. allowed, labelsFiltered := m.allowMetric(key, labels)
  70. if !allowed {
  71. return
  72. }
  73. m.sink.IncrCounterWithLabels(key, val, labelsFiltered)
  74. }
  75. func (m *Metrics) AddSample(key []string, val float32) {
  76. m.AddSampleWithLabels(key, val, nil)
  77. }
  78. func (m *Metrics) AddSampleWithLabels(key []string, val float32, labels []Label) {
  79. if m.HostName != "" && m.EnableHostnameLabel {
  80. labels = append(labels, Label{"host", m.HostName})
  81. }
  82. if m.EnableTypePrefix {
  83. key = insert(0, "sample", key)
  84. }
  85. if m.ServiceName != "" {
  86. if m.EnableServiceLabel {
  87. labels = append(labels, Label{"service", m.ServiceName})
  88. } else {
  89. key = insert(0, m.ServiceName, key)
  90. }
  91. }
  92. allowed, labelsFiltered := m.allowMetric(key, labels)
  93. if !allowed {
  94. return
  95. }
  96. m.sink.AddSampleWithLabels(key, val, labelsFiltered)
  97. }
  98. func (m *Metrics) MeasureSince(key []string, start time.Time) {
  99. m.MeasureSinceWithLabels(key, start, nil)
  100. }
  101. func (m *Metrics) MeasureSinceWithLabels(key []string, start time.Time, labels []Label) {
  102. if m.HostName != "" && m.EnableHostnameLabel {
  103. labels = append(labels, Label{"host", m.HostName})
  104. }
  105. if m.EnableTypePrefix {
  106. key = insert(0, "timer", key)
  107. }
  108. if m.ServiceName != "" {
  109. if m.EnableServiceLabel {
  110. labels = append(labels, Label{"service", m.ServiceName})
  111. } else {
  112. key = insert(0, m.ServiceName, key)
  113. }
  114. }
  115. allowed, labelsFiltered := m.allowMetric(key, labels)
  116. if !allowed {
  117. return
  118. }
  119. now := time.Now()
  120. elapsed := now.Sub(start)
  121. msec := float32(elapsed.Nanoseconds()) / float32(m.TimerGranularity)
  122. m.sink.AddSampleWithLabels(key, msec, labelsFiltered)
  123. }
  124. // UpdateFilter overwrites the existing filter with the given rules.
  125. func (m *Metrics) UpdateFilter(allow, block []string) {
  126. m.UpdateFilterAndLabels(allow, block, m.AllowedLabels, m.BlockedLabels)
  127. }
  128. // UpdateFilterAndLabels overwrites the existing filter with the given rules.
  129. func (m *Metrics) UpdateFilterAndLabels(allow, block, allowedLabels, blockedLabels []string) {
  130. m.filterLock.Lock()
  131. defer m.filterLock.Unlock()
  132. m.AllowedPrefixes = allow
  133. m.BlockedPrefixes = block
  134. if allowedLabels == nil {
  135. // Having a white list means we take only elements from it
  136. m.allowedLabels = nil
  137. } else {
  138. m.allowedLabels = make(map[string]bool)
  139. for _, v := range allowedLabels {
  140. m.allowedLabels[v] = true
  141. }
  142. }
  143. m.blockedLabels = make(map[string]bool)
  144. for _, v := range blockedLabels {
  145. m.blockedLabels[v] = true
  146. }
  147. m.AllowedLabels = allowedLabels
  148. m.BlockedLabels = blockedLabels
  149. m.filter = iradix.New()
  150. for _, prefix := range m.AllowedPrefixes {
  151. m.filter, _, _ = m.filter.Insert([]byte(prefix), true)
  152. }
  153. for _, prefix := range m.BlockedPrefixes {
  154. m.filter, _, _ = m.filter.Insert([]byte(prefix), false)
  155. }
  156. }
  157. // labelIsAllowed return true if a should be included in metric
  158. // the caller should lock m.filterLock while calling this method
  159. func (m *Metrics) labelIsAllowed(label *Label) bool {
  160. labelName := (*label).Name
  161. if m.blockedLabels != nil {
  162. _, ok := m.blockedLabels[labelName]
  163. if ok {
  164. // If present, let's remove this label
  165. return false
  166. }
  167. }
  168. if m.allowedLabels != nil {
  169. _, ok := m.allowedLabels[labelName]
  170. return ok
  171. }
  172. // Allow by default
  173. return true
  174. }
  175. // filterLabels return only allowed labels
  176. // the caller should lock m.filterLock while calling this method
  177. func (m *Metrics) filterLabels(labels []Label) []Label {
  178. if labels == nil {
  179. return nil
  180. }
  181. toReturn := []Label{}
  182. for _, label := range labels {
  183. if m.labelIsAllowed(&label) {
  184. toReturn = append(toReturn, label)
  185. }
  186. }
  187. return toReturn
  188. }
  189. // Returns whether the metric should be allowed based on configured prefix filters
  190. // Also return the applicable labels
  191. func (m *Metrics) allowMetric(key []string, labels []Label) (bool, []Label) {
  192. m.filterLock.RLock()
  193. defer m.filterLock.RUnlock()
  194. if m.filter == nil || m.filter.Len() == 0 {
  195. return m.Config.FilterDefault, m.filterLabels(labels)
  196. }
  197. _, allowed, ok := m.filter.Root().LongestPrefix([]byte(strings.Join(key, ".")))
  198. if !ok {
  199. return m.Config.FilterDefault, m.filterLabels(labels)
  200. }
  201. return allowed.(bool), m.filterLabels(labels)
  202. }
  203. // Periodically collects runtime stats to publish
  204. func (m *Metrics) collectStats() {
  205. for {
  206. time.Sleep(m.ProfileInterval)
  207. m.emitRuntimeStats()
  208. }
  209. }
  210. // Emits various runtime statsitics
  211. func (m *Metrics) emitRuntimeStats() {
  212. // Export number of Goroutines
  213. numRoutines := runtime.NumGoroutine()
  214. m.SetGauge([]string{"runtime", "num_goroutines"}, float32(numRoutines))
  215. // Export memory stats
  216. var stats runtime.MemStats
  217. runtime.ReadMemStats(&stats)
  218. m.SetGauge([]string{"runtime", "alloc_bytes"}, float32(stats.Alloc))
  219. m.SetGauge([]string{"runtime", "sys_bytes"}, float32(stats.Sys))
  220. m.SetGauge([]string{"runtime", "malloc_count"}, float32(stats.Mallocs))
  221. m.SetGauge([]string{"runtime", "free_count"}, float32(stats.Frees))
  222. m.SetGauge([]string{"runtime", "heap_objects"}, float32(stats.HeapObjects))
  223. m.SetGauge([]string{"runtime", "total_gc_pause_ns"}, float32(stats.PauseTotalNs))
  224. m.SetGauge([]string{"runtime", "total_gc_runs"}, float32(stats.NumGC))
  225. // Export info about the last few GC runs
  226. num := stats.NumGC
  227. // Handle wrap around
  228. if num < m.lastNumGC {
  229. m.lastNumGC = 0
  230. }
  231. // Ensure we don't scan more than 256
  232. if num-m.lastNumGC >= 256 {
  233. m.lastNumGC = num - 255
  234. }
  235. for i := m.lastNumGC; i < num; i++ {
  236. pause := stats.PauseNs[i%256]
  237. m.AddSample([]string{"runtime", "gc_pause_ns"}, float32(pause))
  238. }
  239. m.lastNumGC = num
  240. }
  241. // Inserts a string value at an index into the slice
  242. func insert(i int, v string, s []string) []string {
  243. s = append(s, "")
  244. copy(s[i+1:], s[i:])
  245. s[i] = v
  246. return s
  247. }