-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprometheus.go
103 lines (90 loc) · 3.62 KB
/
prometheus.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package prometheus
import (
"strconv"
"time"
"github.com/prometheus/client_golang/prometheus"
vshardrouter "github.com/tarantool/go-vshard-router/v2"
)
// Check that provider implements MetricsProvider interface
var _ vshardrouter.MetricsProvider = (*Provider)(nil)
// Check that provider implements Collector interface
var _ prometheus.Collector = (*Provider)(nil)
// Provider is a struct that implements collector and provider methods.
// It gives users a simple way to use metrics for go-vshard-router.
type Provider struct {
// cronDiscoveryEvent - histogram for cron discovery events.
cronDiscoveryEvent *prometheus.HistogramVec
// retryOnCall - counter for retry calls.
retryOnCall *prometheus.CounterVec
// requestDuration - histogram for map reduce and single request durations.
requestDuration *prometheus.HistogramVec
}
// Describe sends the descriptors of each metric to the provided channel.
func (pp *Provider) Describe(ch chan<- *prometheus.Desc) {
pp.cronDiscoveryEvent.Describe(ch)
pp.retryOnCall.Describe(ch)
pp.requestDuration.Describe(ch)
}
// Collect gathers the metrics and sends them to the provided channel.
func (pp *Provider) Collect(ch chan<- prometheus.Metric) {
pp.cronDiscoveryEvent.Collect(ch)
pp.retryOnCall.Collect(ch)
pp.requestDuration.Collect(ch)
}
// CronDiscoveryEvent records the duration of a cron discovery event with labels.
func (pp *Provider) CronDiscoveryEvent(ok bool, duration time.Duration, reason string) {
pp.cronDiscoveryEvent.With(prometheus.Labels{
"ok": strconv.FormatBool(ok),
"reason": reason,
}).Observe(float64(duration.Milliseconds()))
}
// RetryOnCall increments the retry counter for a specific reason.
func (pp *Provider) RetryOnCall(reason string) {
pp.retryOnCall.With(prometheus.Labels{
"reason": reason,
}).Inc()
}
// RequestDuration records the duration of a request with labels for success and map-reduce usage.
func (pp *Provider) RequestDuration(duration time.Duration, procedure string, ok, mapReduce bool) {
pp.requestDuration.With(prometheus.Labels{
"ok": strconv.FormatBool(ok),
"map_reduce": strconv.FormatBool(mapReduce),
"procedure": procedure,
}).Observe(float64(duration.Milliseconds()))
}
// NewPrometheusProvider - is an experimental function.
// Prometheus Provider is one of the ready-to-use providers implemented
// for go-vshard-router. It can be used to easily integrate metrics into
// your service without worrying about buckets, metric names, or other
// metric options.
//
// The provider implements both the interface required by vshard-router
// and the Prometheus collector interface.
//
// To register it in your Prometheus instance, use:
// registry.MustRegister(provider)
//
// Then, pass it to go-vshard-router so that it can manage the metrics:
//
// vshard_router.NewRouter(ctx, vshard_router.Config{
// Metrics: provider,
// })
//
// This approach simplifies the process of collecting and handling metrics,
// freeing you from manually managing metric-specific configurations.
func NewPrometheusProvider() *Provider {
return &Provider{
cronDiscoveryEvent: prometheus.NewHistogramVec(prometheus.HistogramOpts{
Name: "cron_discovery_event",
Namespace: "vshard",
}, []string{"ok", "reason"}), // Histogram for tracking cron discovery events
retryOnCall: prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "retry_on_call",
Namespace: "vshard",
}, []string{"reason"}), // Counter for retry attempts
requestDuration: prometheus.NewHistogramVec(prometheus.HistogramOpts{
Name: "request_duration",
Namespace: "vshard",
}, []string{"procedure", "ok", "map_reduce"}), // Histogram for request durations
}
}