From bab137f30a20b05191c7bc7764638041c965808d Mon Sep 17 00:00:00 2001 From: ajatprabha Date: Tue, 16 Apr 2024 15:49:01 +0530 Subject: [PATCH] add bucket boundaries acc to seconds instrumentation --- otelcourier/metric.go | 21 +++++++++++++-------- otelcourier/metric_test.go | 2 +- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/otelcourier/metric.go b/otelcourier/metric.go index 3ed699f..c17570a 100644 --- a/otelcourier/metric.go +++ b/otelcourier/metric.go @@ -5,24 +5,28 @@ import ( "fmt" "time" + prom "github.com/prometheus/client_golang/prometheus" "go.opentelemetry.io/otel/metric" semconv "go.opentelemetry.io/otel/semconv/v1.24.0" ) -var metricFlowNames = map[tracePath]string{ - tracePublisher: "publish", - traceSubscriber: "subscribe", - traceUnsubscriber: "unsubscribe", - traceCallback: "subscribe.callback", +var metricFlows = map[tracePath]struct { + name string + boundaries []float64 +}{ + tracePublisher: {name: "publish", boundaries: prom.ExponentialBucketsRange(0.0001, 1, 10)}, + traceSubscriber: {name: "subscribe", boundaries: prom.ExponentialBucketsRange(0.001, 1, 7)}, + traceUnsubscriber: {name: "unsubscribe", boundaries: prom.ExponentialBucketsRange(0.001, 1, 7)}, + traceCallback: {name: "subscribe.callback", boundaries: prom.ExponentialBucketsRange(0.001, 10, 15)}, } func (t *OTel) initRecorders() { - for path, flow := range metricFlowNames { + for path, flow := range metricFlows { if !t.tracePaths.match(path) { continue } - t.rc[path] = t.newRecorder(flow) + t.rc[path] = t.newRecorder(flow.name, flow.boundaries) } if t.infoHandler != nil { @@ -43,7 +47,7 @@ func (t *OTel) initInfoHandler() { type recordersOp func(*recorders) error -func (t *OTel) newRecorder(flow string) *recorders { +func (t *OTel) newRecorder(flow string, boundaries []float64) *recorders { var rs recorders for _, op := range []recordersOp{ @@ -70,6 +74,7 @@ func (t *OTel) newRecorder(flow string) *recorders { fmt.Sprintf("courier.%s.latency", flow), metric.WithDescription(fmt.Sprintf("Latency of %s calls", flow)), metric.WithUnit("s"), + metric.WithExplicitBucketBoundaries(boundaries...), ) r.latency = lt diff --git a/otelcourier/metric_test.go b/otelcourier/metric_test.go index e59b8bc..218f9ed 100644 --- a/otelcourier/metric_test.go +++ b/otelcourier/metric_test.go @@ -22,5 +22,5 @@ func Test_recorderOpsWithoutInitialization(t *testing.T) { func Test_recorderPanicsWithInvalidFlowName(t *testing.T) { ot := &OTel{meter: metric.NewMeterProvider().Meter(tracerName)} - assert.Panics(t, func() { _ = ot.newRecorder("invalid%flow") }) + assert.Panics(t, func() { _ = ot.newRecorder("invalid%flow", nil) }) }