Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting per-request custom metrics in gRPC stats handler. #6092

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions instrumentation/google.golang.org/grpc/otelgrpc/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package otelgrpc // import "go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"

import (
"context"

"google.golang.org/grpc/stats"

"go.opentelemetry.io/otel"
Expand Down Expand Up @@ -36,14 +38,15 @@

// config is a group of options for this instrumentation.
type config struct {
Filter Filter
InterceptorFilter InterceptorFilter
Propagators propagation.TextMapPropagator
TracerProvider trace.TracerProvider
MeterProvider metric.MeterProvider
SpanStartOptions []trace.SpanStartOption
SpanAttributes []attribute.KeyValue
MetricAttributes []attribute.KeyValue
Filter Filter
InterceptorFilter InterceptorFilter
Propagators propagation.TextMapPropagator
TracerProvider trace.TracerProvider
MeterProvider metric.MeterProvider
SpanStartOptions []trace.SpanStartOption
SpanAttributes []attribute.KeyValue
MetricAttributes []attribute.KeyValue
MetricAttributesFn func(ctx context.Context, payload any) []attribute.KeyValue

ReceivedEvent bool
SentEvent bool
Expand Down Expand Up @@ -285,3 +288,19 @@
func WithMetricAttributes(a ...attribute.KeyValue) Option {
return metricAttributesOption{a: a}
}

type metricAttributesFnOption struct {
f func(ctx context.Context, payload any) []attribute.KeyValue
}

func (o metricAttributesFnOption) apply(c *config) {
if o.f != nil {
c.MetricAttributesFn = o.f

Check warning on line 298 in instrumentation/google.golang.org/grpc/otelgrpc/config.go

View check run for this annotation

Codecov / codecov/patch

instrumentation/google.golang.org/grpc/otelgrpc/config.go#L296-L298

Added lines #L296 - L298 were not covered by tests
}
}

// WithMetricAttributesFn returns an Option to add custom attributes to the metrics
// based on the incoming request.
func WithMetricAttributesFn(f func(ctx context.Context, payload any) []attribute.KeyValue) Option {
return metricAttributesFnOption{f: f}

Check warning on line 305 in instrumentation/google.golang.org/grpc/otelgrpc/config.go

View check run for this annotation

Codecov / codecov/patch

instrumentation/google.golang.org/grpc/otelgrpc/config.go#L304-L305

Added lines #L304 - L305 were not covered by tests
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,21 @@
}

switch rs := rs.(type) {
case *stats.Begin:
dmathieu marked this conversation as resolved.
Show resolved Hide resolved
case *stats.InPayload:
if gctx != nil {
messageId = atomic.AddInt64(&gctx.messagesReceived, 1)
dhowden marked this conversation as resolved.
Show resolved Hide resolved

// Run this once on InPayload and record the attributes for the entire RPC.
if c.MetricAttributesFn != nil {
fnMetricAttrs := c.MetricAttributesFn(ctx, rs.Payload)

Check warning on line 156 in instrumentation/google.golang.org/grpc/otelgrpc/stats_handler.go

View check run for this annotation

Codecov / codecov/patch

instrumentation/google.golang.org/grpc/otelgrpc/stats_handler.go#L156

Added line #L156 was not covered by tests

// Record them for the entire RPC.
gctx.metricAttrs = append(gctx.metricAttrs, fnMetricAttrs...)

Check warning on line 159 in instrumentation/google.golang.org/grpc/otelgrpc/stats_handler.go

View check run for this annotation

Codecov / codecov/patch

instrumentation/google.golang.org/grpc/otelgrpc/stats_handler.go#L159

Added line #L159 was not covered by tests

// For this run we need to manually add them to the metricAttrs slice.
metricAttrs = append(metricAttrs, fnMetricAttrs...)

Check warning on line 162 in instrumentation/google.golang.org/grpc/otelgrpc/stats_handler.go

View check run for this annotation

Codecov / codecov/patch

instrumentation/google.golang.org/grpc/otelgrpc/stats_handler.go#L162

Added line #L162 was not covered by tests
}

c.rpcRequestSize.Record(ctx, int64(rs.Length), metric.WithAttributeSet(attribute.NewSet(metricAttrs...)))
}

Expand All @@ -164,6 +175,7 @@
),
)
}

case *stats.OutPayload:
if gctx != nil {
messageId = atomic.AddInt64(&gctx.messagesSent, 1)
Expand All @@ -180,11 +192,12 @@
),
)
}
case *stats.OutTrailer:
dmathieu marked this conversation as resolved.
Show resolved Hide resolved

case *stats.OutHeader:
if p, ok := peer.FromContext(ctx); ok {
span.SetAttributes(peerAttr(p.Addr.String())...)
}

case *stats.End:
var rpcStatusAttr attribute.KeyValue

Expand Down Expand Up @@ -216,6 +229,7 @@
c.rpcRequestsPerRPC.Record(ctx, atomic.LoadInt64(&gctx.messagesReceived), recordOpts...)
c.rpcResponsesPerRPC.Record(ctx, atomic.LoadInt64(&gctx.messagesSent), recordOpts...)
}

default:
return
}
Expand Down
Loading