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