start.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package metrics
  2. import (
  3. "os"
  4. "time"
  5. )
  6. // Config is used to configure metrics settings
  7. type Config struct {
  8. ServiceName string // Prefixed with keys to seperate services
  9. HostName string // Hostname to use. If not provided and EnableHostname, it will be os.Hostname
  10. EnableHostname bool // Enable prefixing gauge values with hostname
  11. EnableRuntimeMetrics bool // Enables profiling of runtime metrics (GC, Goroutines, Memory)
  12. EnableTypePrefix bool // Prefixes key with a type ("counter", "gauge", "timer")
  13. TimerGranularity time.Duration // Granularity of timers.
  14. ProfileInterval time.Duration // Interval to profile runtime metrics
  15. }
  16. // Metrics represents an instance of a metrics sink that can
  17. // be used to emit
  18. type Metrics struct {
  19. Config
  20. lastNumGC uint32
  21. sink MetricSink
  22. }
  23. // Shared global metrics instance
  24. var globalMetrics *Metrics
  25. func init() {
  26. // Initialize to a blackhole sink to avoid errors
  27. globalMetrics = &Metrics{sink: &BlackholeSink{}}
  28. }
  29. // DefaultConfig provides a sane default configuration
  30. func DefaultConfig(serviceName string) *Config {
  31. c := &Config{
  32. ServiceName: serviceName, // Use client provided service
  33. HostName: "",
  34. EnableHostname: true, // Enable hostname prefix
  35. EnableRuntimeMetrics: true, // Enable runtime profiling
  36. EnableTypePrefix: false, // Disable type prefix
  37. TimerGranularity: time.Millisecond, // Timers are in milliseconds
  38. ProfileInterval: time.Second, // Poll runtime every second
  39. }
  40. // Try to get the hostname
  41. name, _ := os.Hostname()
  42. c.HostName = name
  43. return c
  44. }
  45. // New is used to create a new instance of Metrics
  46. func New(conf *Config, sink MetricSink) (*Metrics, error) {
  47. met := &Metrics{}
  48. met.Config = *conf
  49. met.sink = sink
  50. // Start the runtime collector
  51. if conf.EnableRuntimeMetrics {
  52. go met.collectStats()
  53. }
  54. return met, nil
  55. }
  56. // NewGlobal is the same as New, but it assigns the metrics object to be
  57. // used globally as well as returning it.
  58. func NewGlobal(conf *Config, sink MetricSink) (*Metrics, error) {
  59. metrics, err := New(conf, sink)
  60. if err == nil {
  61. globalMetrics = metrics
  62. }
  63. return metrics, err
  64. }
  65. // Proxy all the methods to the globalMetrics instance
  66. func SetGauge(key []string, val float32) {
  67. globalMetrics.SetGauge(key, val)
  68. }
  69. func EmitKey(key []string, val float32) {
  70. globalMetrics.EmitKey(key, val)
  71. }
  72. func IncrCounter(key []string, val float32) {
  73. globalMetrics.IncrCounter(key, val)
  74. }
  75. func AddSample(key []string, val float32) {
  76. globalMetrics.AddSample(key, val)
  77. }
  78. func MeasureSince(key []string, start time.Time) {
  79. globalMetrics.MeasureSince(key, start)
  80. }