diff --git a/pkg/api/telemetry.go b/pkg/api/telemetry.go index bb344222..70cd3833 100644 --- a/pkg/api/telemetry.go +++ b/pkg/api/telemetry.go @@ -3,6 +3,7 @@ package api import ( "context" "strings" + "time" apicontext "github.com/xmtp/xmtp-node-go/pkg/api/message/v1/context" "github.com/xmtp/xmtp-node-go/pkg/metrics" @@ -30,8 +31,9 @@ func (ti *TelemetryInterceptor) Unary() grpc.UnaryServerInterceptor { info *grpc.UnaryServerInfo, handler grpc.UnaryHandler, ) (interface{}, error) { + start := time.Now().UTC() res, err := handler(ctx, req) - ti.record(ctx, info.FullMethod, err) + ti.record(ctx, info.FullMethod, time.Since(start), err) return res, err } } @@ -43,13 +45,14 @@ func (ti *TelemetryInterceptor) Stream() grpc.StreamServerInterceptor { info *grpc.StreamServerInfo, handler grpc.StreamHandler, ) error { + start := time.Now().UTC() err := handler(srv, stream) - ti.record(stream.Context(), info.FullMethod, err) + ti.record(stream.Context(), info.FullMethod, time.Since(start), err) return err } } -func (ti *TelemetryInterceptor) record(ctx context.Context, fullMethod string, err error) { +func (ti *TelemetryInterceptor) record(ctx context.Context, fullMethod string, duration time.Duration, err error) { serviceName, methodName := splitMethodName(fullMethod) ri := apicontext.NewRequesterInfo(ctx) fields := append( @@ -85,7 +88,7 @@ func (ti *TelemetryInterceptor) record(ctx context.Context, fullMethod string, e } logFn("api request", fields...) - metrics.EmitAPIRequest(ctx, ti.log, fields) + metrics.EmitAPIRequest(ctx, ti.log, fields, duration) } func splitMethodName(fullMethodName string) (serviceName string, methodName string) { diff --git a/pkg/metrics/api.go b/pkg/metrics/api.go index ca9907f0..5eb95ee8 100644 --- a/pkg/metrics/api.go +++ b/pkg/metrics/api.go @@ -41,7 +41,16 @@ var apiRequests = prometheus.NewCounterVec( apiRequestTagKeys, ) -func EmitAPIRequest(ctx context.Context, log *zap.Logger, fields []zapcore.Field) { +var apiRequestsDuration = prometheus.NewHistogramVec( + prometheus.HistogramOpts{ + Name: "xmtp_api_request_duration_ms", + Help: "Duration of api request (ms)", + Buckets: []float64{1, 10, 100, 500, 1000, 5000, 10000, 50000, 100000}, + }, + apiRequestTagKeys, +) + +func EmitAPIRequest(ctx context.Context, log *zap.Logger, fields []zapcore.Field, duration time.Duration) { labels := prometheus.Labels{} for _, field := range fields { if !apiRequestTagKeysByName[field.Key] { @@ -55,6 +64,7 @@ func EmitAPIRequest(ctx context.Context, log *zap.Logger, fields []zapcore.Field } } apiRequests.With(labels).Inc() + apiRequestsDuration.With(labels).Observe(float64(duration / time.Millisecond)) } var subscribeTopicsLength = prometheus.NewHistogramVec( diff --git a/pkg/metrics/metrics.go b/pkg/metrics/metrics.go index 88018f65..4b005000 100644 --- a/pkg/metrics/metrics.go +++ b/pkg/metrics/metrics.go @@ -57,6 +57,7 @@ func registerCollectors(reg prometheus.Registerer) { BootstrapPeers, StoredMessages, apiRequests, + apiRequestsDuration, subscribeTopicsLength, publishedEnvelopeSize, publishedEnvelopeCount,