|
| 1 | +package metrics |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "github.com/prometheus/client_golang/prometheus" |
| 6 | + "github.com/prometheus/client_model/go" |
| 7 | + "github.com/sirupsen/logrus" |
| 8 | + "sync" |
| 9 | +) |
| 10 | + |
| 11 | +var ( |
| 12 | + handlerMetricsByHandlerName = &sync.Map{} |
| 13 | +) |
| 14 | + |
| 15 | +const ( |
| 16 | + failure = "failure" |
| 17 | + success = "success" |
| 18 | + handlerResult = "result" |
| 19 | + handlers = "handlers" |
| 20 | + grabbitPrefix = "grabbit" |
| 21 | +) |
| 22 | + |
| 23 | +type HandlerMetrics struct { |
| 24 | + result *prometheus.CounterVec |
| 25 | + latency prometheus.Summary |
| 26 | +} |
| 27 | + |
| 28 | +func AddHandlerMetrics(handlerName string) { |
| 29 | + handlerMetrics := newHandlerMetrics(handlerName) |
| 30 | + _, exists := handlerMetricsByHandlerName.LoadOrStore(handlerName, handlerMetrics) |
| 31 | + |
| 32 | + if !exists { |
| 33 | + prometheus.MustRegister(handlerMetrics.latency, handlerMetrics.result) |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +func RunHandlerWithMetric(handleMessage func() error, handlerName string, logger logrus.FieldLogger) error { |
| 38 | + handlerMetrics := GetHandlerMetrics(handlerName) |
| 39 | + defer func() { |
| 40 | + if p := recover(); p != nil { |
| 41 | + if handlerMetrics != nil { |
| 42 | + handlerMetrics.result.WithLabelValues(failure).Inc() |
| 43 | + } |
| 44 | + |
| 45 | + panic(p) |
| 46 | + } |
| 47 | + }() |
| 48 | + |
| 49 | + if handlerMetrics == nil { |
| 50 | + logger.WithField("handler", handlerName).Warn("Running with metrics - couldn't find metrics for the given handler") |
| 51 | + return handleMessage() |
| 52 | + } |
| 53 | + |
| 54 | + err := trackTime(handleMessage, handlerMetrics.latency) |
| 55 | + |
| 56 | + if err != nil { |
| 57 | + handlerMetrics.result.WithLabelValues(failure).Inc() |
| 58 | + } else { |
| 59 | + handlerMetrics.result.WithLabelValues(success).Inc() |
| 60 | + } |
| 61 | + |
| 62 | + return err |
| 63 | +} |
| 64 | + |
| 65 | +func GetHandlerMetrics(handlerName string) *HandlerMetrics { |
| 66 | + entry, ok := handlerMetricsByHandlerName.Load(handlerName) |
| 67 | + if ok { |
| 68 | + return entry.(*HandlerMetrics) |
| 69 | + } |
| 70 | + |
| 71 | + return nil |
| 72 | +} |
| 73 | + |
| 74 | +func newHandlerMetrics(handlerName string) *HandlerMetrics { |
| 75 | + return &HandlerMetrics{ |
| 76 | + result: prometheus.NewCounterVec( |
| 77 | + prometheus.CounterOpts{ |
| 78 | + Namespace: grabbitPrefix, |
| 79 | + Subsystem: handlers, |
| 80 | + Name: fmt.Sprintf("%s_result", handlerName), |
| 81 | + Help: fmt.Sprintf("The %s's result", handlerName), |
| 82 | + }, |
| 83 | + []string{handlerResult}), |
| 84 | + latency: prometheus.NewSummary( |
| 85 | + prometheus.SummaryOpts{ |
| 86 | + Namespace: grabbitPrefix, |
| 87 | + Subsystem: handlers, |
| 88 | + Name: fmt.Sprintf("%s_latency", handlerName), |
| 89 | + Help: fmt.Sprintf("The %s's latency", handlerName), |
| 90 | + }), |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func trackTime(functionToTrack func() error, observer prometheus.Observer) error { |
| 95 | + timer := prometheus.NewTimer(observer) |
| 96 | + defer timer.ObserveDuration() |
| 97 | + |
| 98 | + return functionToTrack() |
| 99 | +} |
| 100 | + |
| 101 | +func (hm *HandlerMetrics) GetSuccessCount() (float64, error) { |
| 102 | + return hm.getLabeledCounterValue(success) |
| 103 | +} |
| 104 | + |
| 105 | +func (hm *HandlerMetrics) GetFailureCount() (float64, error) { |
| 106 | + return hm.getLabeledCounterValue(failure) |
| 107 | +} |
| 108 | + |
| 109 | +func (hm *HandlerMetrics) GetLatencySampleCount() (*uint64, error) { |
| 110 | + m := &io_prometheus_client.Metric{} |
| 111 | + err := hm.latency.Write(m) |
| 112 | + if err != nil { |
| 113 | + return nil, err |
| 114 | + } |
| 115 | + |
| 116 | + return m.GetSummary().SampleCount, nil |
| 117 | +} |
| 118 | + |
| 119 | +func (hm *HandlerMetrics) getLabeledCounterValue(label string) (float64, error) { |
| 120 | + m := &io_prometheus_client.Metric{} |
| 121 | + err := hm.result.WithLabelValues(label).Write(m) |
| 122 | + |
| 123 | + if err != nil { |
| 124 | + return 0, err |
| 125 | + } |
| 126 | + |
| 127 | + return m.GetCounter().GetValue(), nil |
| 128 | +} |
0 commit comments