metrics.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. Copyright 2014 CoreOS, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // Package metrics provides metrics view of variables which is exposed through
  14. // expvar package.
  15. //
  16. // Naming conventions:
  17. // 1. volatile path components should be kept as deep into the hierarchy as possible
  18. // 2. each path component should have a clear and well-defined purpose
  19. // 3. components.separated.with.dot, and put package prefix at the head
  20. // 4. words_separated_with_underscore, and put clarifiers last, e.g., requests_total
  21. package metrics
  22. import (
  23. "bytes"
  24. "expvar"
  25. "fmt"
  26. )
  27. // Counter is a number that increases over time monotonically.
  28. type Counter struct{ i *expvar.Int }
  29. func NewCounter(name string) *Counter {
  30. return &Counter{i: expvar.NewInt(name)}
  31. }
  32. func (c *Counter) Add() { c.i.Add(1) }
  33. func (c *Counter) AddBy(delta int64) { c.i.Add(delta) }
  34. func (c *Counter) String() string { return c.i.String() }
  35. // Gauge returns instantaneous value that is expected to fluctuate over time.
  36. type Gauge struct{ i *expvar.Int }
  37. func NewGauge(name string) *Gauge {
  38. return &Gauge{i: expvar.NewInt(name)}
  39. }
  40. func (g *Gauge) Set(value int64) { g.i.Set(value) }
  41. func (g *Gauge) String() string { return g.i.String() }
  42. type nilVar struct{}
  43. func (v *nilVar) String() string { return "nil" }
  44. // Map aggregates Counters and Gauges.
  45. type Map struct{ *expvar.Map }
  46. func NewMap(name string) *Map {
  47. return &Map{Map: expvar.NewMap(name)}
  48. }
  49. // GetMap returns the map if it exists, or inits the given name map if it does
  50. // not exist.
  51. func GetMap(name string) *Map {
  52. if m, ok := expvar.Get(name).(*expvar.Map); ok {
  53. return &Map{Map: m}
  54. }
  55. return NewMap(name)
  56. }
  57. func (m *Map) NewCounter(key string) *Counter {
  58. c := &Counter{i: new(expvar.Int)}
  59. m.Set(key, c)
  60. return c
  61. }
  62. func (m *Map) NewGauge(key string) *Gauge {
  63. g := &Gauge{i: new(expvar.Int)}
  64. m.Set(key, g)
  65. return g
  66. }
  67. // TODO: remove the var from the map to avoid memory boom
  68. func (m *Map) Delete(key string) { m.Set(key, &nilVar{}) }
  69. // String returns JSON format string that represents the group.
  70. // It does not print out nilVar.
  71. func (m *Map) String() string {
  72. var b bytes.Buffer
  73. fmt.Fprintf(&b, "{")
  74. first := true
  75. m.Do(func(kv expvar.KeyValue) {
  76. v := kv.Value.String()
  77. if v == "nil" {
  78. return
  79. }
  80. if !first {
  81. fmt.Fprintf(&b, ", ")
  82. }
  83. fmt.Fprintf(&b, "%q: %v", kv.Key, v)
  84. first = false
  85. })
  86. fmt.Fprintf(&b, "}")
  87. return b.String()
  88. }