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: Add SpanStartOptions behavior to stats_handlers #5431

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -57,11 +57,18 @@ func (h *serverHandler) TagRPC(ctx context.Context, info *stats.RPCTagInfo) cont

name, attrs := internal.ParseFullMethod(info.FullMethodName)
attrs = append(attrs, RPCSystemGRPC)

startOpts := append([]trace.SpanStartOption{
trace.WithSpanKind(trace.SpanKindServer),
trace.WithAttributes(attrs...),
},
h.config.SpanStartOptions...,
)

ctx, _ = h.tracer.Start(
trace.ContextWithRemoteSpanContext(ctx, trace.SpanContextFromContext(ctx)),
name,
trace.WithSpanKind(trace.SpanKindServer),
trace.WithAttributes(attrs...),
startOpts...,
)

gctx := gRPCContext{
Expand Down Expand Up @@ -93,11 +100,18 @@ 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)

startOpts := append([]trace.SpanStartOption{
trace.WithSpanKind(trace.SpanKindClient),
trace.WithAttributes(attrs...),
},
h.config.SpanStartOptions...,
)

ctx, _ = h.tracer.Start(
ctx,
name,
trace.WithSpanKind(trace.SpanKindClient),
trace.WithAttributes(attrs...),
startOpts...,
)

gctx := gRPCContext{
Expand Down
Expand Up @@ -21,6 +21,11 @@ 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"
)

const (
serviceName = "TestGrpcService"
)

func TestStatsHandlerHandleRPCServerErrors(t *testing.T) {
Expand All @@ -38,7 +43,6 @@ func TestStatsHandlerHandleRPCServerErrors(t *testing.T) {
otelgrpc.WithMeterProvider(mp),
)

serviceName := "TestGrpcService"
methodName := serviceName + "/" + name
fullMethodName := "/" + methodName
// call the server handler
Expand All @@ -62,6 +66,64 @@ func TestStatsHandlerHandleRPCServerErrors(t *testing.T) {
}
}

func TestStatsHandlerServerWithSpanOptions(t *testing.T) {
sr := tracetest.NewSpanRecorder()
tp := trace.NewTracerProvider(trace.WithSpanProcessor(sr))
serverHandler := otelgrpc.NewServerHandler(
otelgrpc.WithTracerProvider(tp),
otelgrpc.WithSpanOptions(oteltrace.WithAttributes(attribute.Bool("custom", true))),
)

methodName := serviceName + "/" + "test"
fullMethodName := "/" + methodName
// call the server handler
ctx := serverHandler.TagRPC(context.Background(), &stats.RPCTagInfo{
FullMethodName: fullMethodName,
})

serverHandler.HandleRPC(ctx, &stats.End{})

expected := []attribute.KeyValue{
semconv.RPCSystemGRPC,
semconv.RPCService("TestGrpcService"),
semconv.RPCMethod("test"),
otelgrpc.GRPCStatusCodeKey.Int64(0),
attribute.Bool("custom", true),
}
span, ok := getSpanFromRecorder(sr, methodName)
require.True(t, ok, "missing span %q", methodName)
assert.ElementsMatch(t, expected, span.Attributes())
}

func TestStatsHandlerClientWithSpanOptions(t *testing.T) {
sr := tracetest.NewSpanRecorder()
tp := trace.NewTracerProvider(trace.WithSpanProcessor(sr))
clientHandler := otelgrpc.NewClientHandler(
otelgrpc.WithTracerProvider(tp),
otelgrpc.WithSpanOptions(oteltrace.WithAttributes(attribute.Bool("custom", true))),
)

methodName := serviceName + "/" + "test"
fullMethodName := "/" + methodName
// call the client handler
ctx := clientHandler.TagRPC(context.Background(), &stats.RPCTagInfo{
FullMethodName: fullMethodName,
})

clientHandler.HandleRPC(ctx, &stats.End{})

expected := []attribute.KeyValue{
semconv.RPCSystemGRPC,
semconv.RPCService("TestGrpcService"),
semconv.RPCMethod("test"),
otelgrpc.GRPCStatusCodeKey.Int64(0),
attribute.Bool("custom", true),
}
span, ok := getSpanFromRecorder(sr, methodName)
require.True(t, ok, "missing span %q", methodName)
assert.ElementsMatch(t, expected, span.Attributes())
}

func assertStatsHandlerServerMetrics(t *testing.T, reader metric.Reader, serviceName, name string, code grpc_codes.Code) {
want := metricdata.ScopeMetrics{
Scope: wantInstrumentationScope,
Expand Down