项目作者: viant

项目描述 :
Operation metric for go
高级语言: Go
项目地址: git://github.com/viant/gmetric.git
创建时间: 2016-11-17T20:20:22Z
项目社区:https://github.com/viant/gmetric

开源协议:Apache License 2.0

下载


Application operation performance metric (gmetric)

Application operation performance metric.
GoDoc

This library is compatible with Go 1.12+

Please refer to CHANGELOG.md if you encounter breaking changes.

Motivation

The goal of this project is to provide metrics to measure various aspect of the application behaviours with minimum overhead.
Application metrics can be viewed as cumulative or recent rolling window values.
All metrics are locking and memory allocation free to provide best possible performance.

Usage

Single metric counter
  1. package mypackage
  2. import (
  3. "errors"
  4. "github.com/viant/gmetric"
  5. "github.com/viant/gmetric/stat"
  6. "log"
  7. "math/rand"
  8. "net/http"
  9. "testing"
  10. "time"
  11. )
  12. func OperationCounterUsage() {
  13. metrics := gmetric.New()
  14. handler := gmetric.NewHandler("/v1/metrics", metrics)
  15. http.Handle("/v1/metrics", handler)
  16. //basic single counter
  17. counter := metrics.OperationCounter("pkg.myapp", "mySingleCounter1", "my description", time.Microsecond, time.Minute, 2)
  18. go runBasicTasks(counter)
  19. err := http.ListenAndServe(":8080", http.DefaultServeMux)
  20. if err != nil {
  21. log.Fatal(err)
  22. }
  23. }
  24. func runBasicTasks(counter *gmetric.Operation) {
  25. for i := 0; i < 1000; i++ {
  26. runBasicTask(counter)
  27. }
  28. }
  29. func runBasicTask(counter *gmetric.Operation) {
  30. onDone := counter.Begin(time.Now())
  31. defer func() {
  32. onDone(time.Now())
  33. }()
  34. time.Sleep(time.Nanosecond)
  35. }
  1. open http://127.0.0.1:8080/v1/metrics/counters
  2. ## or
  3. open http://127.0.0.1:8080/v1/metrics/counter/mySingleCounter1
Multi metric counter
  1. package gmetric_test
  2. import (
  3. "errors"
  4. "github.com/viant/gmetric"
  5. "github.com/viant/gmetric/counter/base"
  6. "github.com/viant/gmetric/stat"
  7. "log"
  8. "math/rand"
  9. "net/http"
  10. "testing"
  11. "time"
  12. )
  13. const (
  14. NoSuchKey = "noSuchKey"
  15. MyStatsCacheHit = "cacheHit"
  16. MyStatsCacheCollision = "cacheCollision"
  17. )
  18. //MultiStateStatTestProvider represents multi stats value provider
  19. type MultiStateStatTestProvider struct{ *base.Provider }
  20. //Map maps value int slice index
  21. func (p *MultiStateStatTestProvider) Map(key interface{}) int {
  22. textKey, ok := key.(string)
  23. if !ok {
  24. return p.Provider.Map(key)
  25. }
  26. switch textKey {
  27. case NoSuchKey:
  28. return 1
  29. case MyStatsCacheHit:
  30. return 2
  31. case MyStatsCacheCollision:
  32. return 3
  33. }
  34. return -1
  35. }
  36. func OperationMulriCounterUsage() {
  37. metrics := gmetric.New()
  38. handler := gmetric.NewHandler("/v1/metrics", metrics)
  39. http.Handle("/v1/metrics", handler)
  40. counter := metrics.MultiOperationCounter("pkg.myapp", "myMultiCounter1", "my description", time.Microsecond, time.Minute, 2, &MultiStateStatTestProvider{})
  41. go runMultiStateTasks(counter)
  42. err := http.ListenAndServe(":8080", http.DefaultServeMux)
  43. if err != nil {
  44. log.Fatal(err)
  45. }
  46. }
  47. func runMultiStateTasks(counter *gmetric.Operation) {
  48. for i := 0; i < 1000; i++ {
  49. runMultiStateTask(counter)
  50. }
  51. }
  52. func runMultiStateTask(counter *gmetric.Operation) {
  53. stats := stat.New()
  54. onDone := counter.Begin(time.Now())
  55. defer func() {
  56. onDone(time.Now(), stats)
  57. }()
  58. time.Sleep(time.Nanosecond)
  59. //simulate task metrics state
  60. rnd := rand.NewSource(time.Now().UnixNano())
  61. state := rnd.Int63() % 3
  62. switch state {
  63. case 0:
  64. stats.Append(NoSuchKey)
  65. case 1:
  66. stats.Append(MyStatsCacheHit)
  67. case 2:
  68. stats.Append(MyStatsCacheHit)
  69. stats.Append(MyStatsCacheCollision)
  70. }
  71. if rnd.Int63() % 10 == 0 {
  72. stats.Append(errors.New("test error"))
  73. }
  74. }
  1. ### all operations counters
  2. open http://127.0.0.1:8080/v1/metrics/operations
  3. ## indivual operation multi counter
  4. open http://127.0.0.1:8080/v1/metrics/operations/myMultiCounter1
  5. ## cumulative indivual operation multi counter metric value
  6. open http://127.0.0.1:8080/v1/metrics/operations/myMultiCounter1/cumulative/count
  7. open http://127.0.0.1:8080/v1/metrics/operations/myMultiCounter1/cumulative/min
  8. open http://127.0.0.1:8080/v1/metrics/operations/myMultiCounter1/cumulative/avg
  9. open http://127.0.0.1:8080/v1/metrics/operations/myMultiCounter1/cumulative/max
  10. open http://127.0.0.1:8080/v1/metrics/operations/myMultiCounter1/cumulative/error
  11. open http://127.0.0.1:8080/v1/metrics/operations/myMultiCounter1/cumulative/noSuchKey
  12. open http://127.0.0.1:8080/v1/metrics/operations/myMultiCounter1/cumulative/cacheCollision
  13. open http://127.0.0.1:8080/v1/metrics/operations/myMultiCounter1/cumulative/cacheCollision.pct
  14. ## recent counters
  15. open http://127.0.0.1:8080/v1/metrics/operations/myMultiCounter1/recent/count
  16. open http://127.0.0.1:8080/v1/metrics/operations/myMultiCounter1/recent/min
  17. open http://127.0.0.1:8080/v1/metrics/operations/myMultiCounter1/recent/avg
  18. open http://127.0.0.1:8080/v1/metrics/operations/myMultiCounter1/recent/max
  19. open http://127.0.0.1:8080/v1/metrics/operations/myMultiCounter1/recent/counter
  20. open http://127.0.0.1:8080/v1/metrics/operations/myMultiCounter1/recent/error
  21. open http://127.0.0.1:8080/v1/metrics/operations/myMultiCounter1/recent/noSuchKey
  22. open http://127.0.0.1:8080/v1/metrics/operations/myMultiCounter1/recent/noSuchKey.pct
  23. open http://127.0.0.1:8080/v1/metrics/operations/myMultiCounter1/recent/cacheCollision

Example metric:

Example metrics

License

The source code is made available under the terms of the Apache License, Version 2, as stated in the file LICENSE.

Individual files may be made available under their own specific license,
all compatible with Apache License, Version 2. Please see individual files for details.

Credits and Acknowledgements

Library Author: Adrian Witas