Skip to content

Commit

Permalink
feat: add metric for api request duration (#338)
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven Normore authored Jan 23, 2024
1 parent 1189bb4 commit a15ebd3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
11 changes: 7 additions & 4 deletions pkg/api/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
}
Expand All @@ -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(
Expand Down Expand Up @@ -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) {
Expand Down
12 changes: 11 additions & 1 deletion pkg/metrics/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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] {
Expand All @@ -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(
Expand Down
1 change: 1 addition & 0 deletions pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func registerCollectors(reg prometheus.Registerer) {
BootstrapPeers,
StoredMessages,
apiRequests,
apiRequestsDuration,
subscribeTopicsLength,
publishedEnvelopeSize,
publishedEnvelopeCount,
Expand Down

0 comments on commit a15ebd3

Please sign in to comment.