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

otelgrpc: only start span if not filtered out #6695

Merged
merged 15 commits into from
Jan 30, 2025
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
### Fixed

- Use `context.Background()` as default context instead of nil in `go.opentelemetry.io/contrib/bridges/otellogr`. (#6527)
- Don't start spans that never end for filtered out gRPC stats handler in `go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc`. (#6695)

<!-- Released section -->
<!-- Don't change this section unless doing release -->
Expand Down
51 changes: 31 additions & 20 deletions instrumentation/google.golang.org/grpc/otelgrpc/stats_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,26 @@ func (h *serverHandler) TagRPC(ctx context.Context, info *stats.RPCTagInfo) cont

name, attrs := internal.ParseFullMethod(info.FullMethodName)
attrs = append(attrs, RPCSystemGRPC)
ctx, _ = h.tracer.Start(
trace.ContextWithRemoteSpanContext(ctx, trace.SpanContextFromContext(ctx)),
name,
trace.WithSpanKind(trace.SpanKindServer),
trace.WithAttributes(append(attrs, h.config.SpanAttributes...)...),
)

record := true
if h.config.Filter != nil {
record = h.config.Filter(info)
}

if record {
ctx, _ = h.tracer.Start(
trace.ContextWithRemoteSpanContext(ctx, trace.SpanContextFromContext(ctx)),
name,
trace.WithSpanKind(trace.SpanKindServer),
trace.WithAttributes(append(attrs, h.config.SpanAttributes...)...),
)
}

gctx := gRPCContext{
metricAttrs: append(attrs, h.config.MetricAttributes...),
record: true,
}
if h.config.Filter != nil {
gctx.record = h.config.Filter(info)
record: record,
}

return context.WithValue(ctx, gRPCContextKey{}, &gctx)
}

Expand All @@ -99,19 +105,24 @@ func NewClientHandler(opts ...Option) stats.Handler {
func (h *clientHandler) TagRPC(ctx context.Context, info *stats.RPCTagInfo) context.Context {
name, attrs := internal.ParseFullMethod(info.FullMethodName)
attrs = append(attrs, RPCSystemGRPC)
ctx, _ = h.tracer.Start(
ctx,
name,
trace.WithSpanKind(trace.SpanKindClient),
trace.WithAttributes(append(attrs, h.config.SpanAttributes...)...),
)

record := true
if h.config.Filter != nil {
record = h.config.Filter(info)
}

if record {
ctx, _ = h.tracer.Start(
ctx,
name,
trace.WithSpanKind(trace.SpanKindClient),
trace.WithAttributes(append(attrs, h.config.SpanAttributes...)...),
)
}

gctx := gRPCContext{
metricAttrs: append(attrs, h.config.MetricAttributes...),
record: true,
}
if h.config.Filter != nil {
gctx.record = h.config.Filter(info)
record: record,
}

return inject(context.WithValue(ctx, gRPCContextKey{}, &gctx), h.config.Propagators)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
testpb "google.golang.org/grpc/interop/grpc_testing"
"google.golang.org/grpc/stats"
"google.golang.org/grpc/status"

"go.opentelemetry.io/otel/attribute"
Expand All @@ -27,6 +28,7 @@ import (
"go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/sdk/trace/tracetest"
semconv "go.opentelemetry.io/otel/semconv/v1.17.0"
oteltrace "go.opentelemetry.io/otel/trace"

"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc/filters"
Expand Down Expand Up @@ -1536,3 +1538,111 @@ func TestStatsHandlerConcurrentSafeContextCancellation(t *testing.T) {
wg.Wait()
}
}

func TestServerHandlerTagRPC(t *testing.T) {
tests := []struct {
name string
server stats.Handler
ctx context.Context
info *stats.RPCTagInfo
exp bool
}{
{
name: "start a span without filters",
server: otelgrpc.NewServerHandler(otelgrpc.WithTracerProvider(trace.NewTracerProvider())),
ctx: context.Background(),
info: &stats.RPCTagInfo{
FullMethodName: "/grpc.health.v1.Health/Check",
},
exp: true,
},
{
name: "don't start a span with filter and match",
server: otelgrpc.NewServerHandler(otelgrpc.WithTracerProvider(trace.NewTracerProvider()), otelgrpc.WithFilter(func(ri *stats.RPCTagInfo) bool {
return ri.FullMethodName != "/grpc.health.v1.Health/Check"
})),
ctx: context.Background(),
info: &stats.RPCTagInfo{
FullMethodName: "/grpc.health.v1.Health/Check",
},
exp: false,
},
{
name: "start a span with filter and no match",
server: otelgrpc.NewServerHandler(otelgrpc.WithTracerProvider(trace.NewTracerProvider()), otelgrpc.WithFilter(func(ri *stats.RPCTagInfo) bool {
return ri.FullMethodName != "/grpc.health.v1.Health/Check"
})),
ctx: context.Background(),
info: &stats.RPCTagInfo{
FullMethodName: "/app.v1.Service/Get",
},
exp: true,
},
}

for _, tt := range tests {
t.Run(t.Name(), func(t *testing.T) {
ctx := tt.server.TagRPC(tt.ctx, tt.info)

got := oteltrace.SpanFromContext(ctx).IsRecording()

if tt.exp != got {
t.Errorf("expected %t, got %t", tt.exp, got)
}
})
}
}

func TestClientHandlerTagRPC(t *testing.T) {
tests := []struct {
name string
client stats.Handler
ctx context.Context
info *stats.RPCTagInfo
exp bool
}{
{
name: "start a span without filters",
client: otelgrpc.NewClientHandler(otelgrpc.WithTracerProvider(trace.NewTracerProvider())),
ctx: context.Background(),
info: &stats.RPCTagInfo{
FullMethodName: "/grpc.health.v1.Health/Check",
},
exp: true,
},
{
name: "don't start a span with filter and match",
client: otelgrpc.NewClientHandler(otelgrpc.WithTracerProvider(trace.NewTracerProvider()), otelgrpc.WithFilter(func(ri *stats.RPCTagInfo) bool {
return ri.FullMethodName != "/grpc.health.v1.Health/Check"
})),
ctx: context.Background(),
info: &stats.RPCTagInfo{
FullMethodName: "/grpc.health.v1.Health/Check",
},
exp: false,
},
{
name: "start a span with filter and no match",
client: otelgrpc.NewClientHandler(otelgrpc.WithTracerProvider(trace.NewTracerProvider()), otelgrpc.WithFilter(func(ri *stats.RPCTagInfo) bool {
return ri.FullMethodName != "/grpc.health.v1.Health/Check"
})),
ctx: context.Background(),
info: &stats.RPCTagInfo{
FullMethodName: "/app.v1.Service/Get",
},
exp: true,
},
}

for _, tt := range tests {
t.Run(t.Name(), func(t *testing.T) {
ctx := tt.client.TagRPC(tt.ctx, tt.info)

got := oteltrace.SpanFromContext(ctx).IsRecording()

if tt.exp != got {
t.Errorf("expected %t, got %t", tt.exp, got)
}
})
}
}
Loading