项目作者: takashabe

项目描述 :
collecting metrics
高级语言: Go
项目地址: git://github.com/takashabe/go-metrics.git
创建时间: 2017-06-09T10:30:47Z
项目社区:https://github.com/takashabe/go-metrics

开源协议:MIT License

下载


go-metrics

GoDoc
CircleCI
Go Report Card

Collecting metrics. The metrics values can be categorized into several types.

Installation

  1. go get -u github.com/takashabe/go-metrics

Usage

  • saveMetrics
    • collect metrics
  • forwardConsole, forwardUDP
    • forward to the specified io.Writer

detail see at example/main.go

  1. package main
  2. import (
  3. "context"
  4. "os"
  5. "time"
  6. "github.com/takashabe/go-metrics/collect"
  7. "github.com/takashabe/go-metrics/forward"
  8. )
  9. func main() {
  10. // collect metrics
  11. collector := collect.NewSimpleCollector()
  12. for i := 0; i < 10; i++ {
  13. saveMetrics(collector)
  14. }
  15. // metrics send to console
  16. cctx, ccancel := context.WithCancel(context.Background())
  17. forwardConsole(cctx, collector)
  18. // metrics send to udp server
  19. // must running server
  20. uctx, ucancel := context.WithCancel(context.Background())
  21. forwardUDP(uctx, collector)
  22. time.Sleep(2 * time.Second)
  23. ccancel()
  24. ucancel()
  25. // output console and udp socket (prepare reformat by jq):
  26. `
  27. {
  28. "cnt": 10,
  29. "histogram.95percentile": 1499763733746145300,
  30. "histogram.avg": 1499763733746128600,
  31. "histogram.count": 10,
  32. "histogram.max": 1499763733746146600,
  33. "histogram.median": 1499763733746140200,
  34. "history": [
  35. "2017-07-11 18:02:13.746027874 +0900 JST",
  36. "2017-07-11 18:02:13.746132309 +0900 JST",
  37. "2017-07-11 18:02:13.74613555 +0900 JST",
  38. "2017-07-11 18:02:13.746137325 +0900 JST",
  39. "2017-07-11 18:02:13.746138707 +0900 JST",
  40. "2017-07-11 18:02:13.746140146 +0900 JST",
  41. "2017-07-11 18:02:13.746141455 +0900 JST",
  42. "2017-07-11 18:02:13.746142766 +0900 JST",
  43. "2017-07-11 18:02:13.746144055 +0900 JST",
  44. "2017-07-11 18:02:13.746146669 +0900 JST"
  45. ],
  46. "recent": 1499763733746146600
  47. }`
  48. }
  49. func saveMetrics(c collect.Collector) {
  50. now := time.Now()
  51. c.Add("cnt", 1)
  52. c.Gauge("recent", float64(now.UnixNano()))
  53. c.Histogram("histogram", float64(now.UnixNano()))
  54. c.Set("history", now.String())
  55. }
  56. func forwardConsole(ctx context.Context, c collect.Collector) {
  57. writer, err := forward.NewSimpleWriter(c, os.Stdout)
  58. if err != nil {
  59. panic(err)
  60. }
  61. writer.AddMetrics(c.GetMetricsKeys()...)
  62. writer.RunStream(ctx) // metrics will be sent every seconds
  63. }
  64. func forwardUDP(ctx context.Context, c collect.Collector) {
  65. writer, err := forward.NewNetWriter(c, ":1234")
  66. if err != nil {
  67. panic(err)
  68. }
  69. writer.AddMetrics(c.GetMetricsKeys()...)
  70. writer.RunStream(ctx) // metrics will be sent every seconds
  71. }

Metrics type

Type Detail
Counter Used to count things
Gauge A particular value at a particular time
Histogram Represents a statistical distribution of a series of values.
Each histogram are count, average, minimum, maximum, median and 95th percentile
Set Used to count the value of unique in a group
Snapshot A particular value set at a particular time