Skip to content

Commit

Permalink
feat(metrics): custom histogram buckets configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
thejoeejoee committed Sep 5, 2024
1 parent ca938e0 commit 5eb3677
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
4 changes: 4 additions & 0 deletions components/metrics/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@ type PrometheusMetricsBuilder struct {

Namespace string
Subsystem string
// Buckets defines the histogram buckets for Prometheus metrics, defaulted if nil.
Buckets []float64
}

// AddPrometheusRouterMetrics is a convenience function that acts on the message router to add the metrics middleware
// to all its handlers. The handlers' publishers and subscribers are also decorated.
// The default buckets are used for the handler execution time histogram (use your own provisioning
// with NewRouterMiddlewareWithConfig if needed).
func (b PrometheusMetricsBuilder) AddPrometheusRouterMetrics(r *message.Router) {
r.AddPublisherDecorators(b.DecoratePublisher)
r.AddSubscriberDecorators(b.DecorateSubscriber)
Expand Down
10 changes: 7 additions & 3 deletions components/metrics/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ var (
labelSuccess,
}

// handlerExecutionTimeBuckets are one order of magnitude smaller than default buckets (5ms~10s),
// defaultHandlerExecutionTimeBuckets are one order of magnitude smaller than default buckets (5ms~10s),
// because the handler execution times are typically shorter (µs~ms range).
handlerExecutionTimeBuckets = []float64{
defaultHandlerExecutionTimeBuckets = []float64{
0.0005,
0.001,
0.0025,
Expand Down Expand Up @@ -64,13 +64,17 @@ func (b PrometheusMetricsBuilder) NewRouterMiddleware() HandlerPrometheusMetrics
var err error
m := HandlerPrometheusMetricsMiddleware{}

if b.Buckets == nil {
b.Buckets = defaultHandlerExecutionTimeBuckets
}

m.handlerExecutionTimeSeconds, err = b.registerHistogramVec(prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: b.Namespace,
Subsystem: b.Subsystem,
Name: "handler_execution_time_seconds",
Help: "The total time elapsed while executing the handler function in seconds",
Buckets: handlerExecutionTimeBuckets,
Buckets: b.Buckets,
},
handlerLabelKeys,
))
Expand Down
3 changes: 3 additions & 0 deletions docs/content/docs/metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ Example use of `AddPrometheusRouterMetrics`:

In the snippet above, we have left the `namespace` and `subsystem` arguments empty. The Prometheus client library [uses these](https://godoc.org/github.com/prometheus/client_golang/prometheus#BuildFQName) to prefix the metric names. You may want to use namespace or subsystem, but be aware that this will impact the metric names and you will have to adjust the Grafana dashboard accordingly.

The `PrometheusMetricsBuilder` allows for custom configuration of histogram buckets for handler metrics middleware by setting the `Buckets` field.
If `Buckets` is not provided, default values will be used.

Standalone publishers and subscribers may also be decorated through the use of dedicated methods of `PrometheusMetricBuilder`:

{{% render-md %}}
Expand Down

0 comments on commit 5eb3677

Please sign in to comment.