Skip to content
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 @@ -44,6 +44,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- The deprecated `SemVersion` function is removed in `go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin/test`, use `Version` function instead. (#7087)
- The deprecated `SemVersion` function is removed in `go.opentelemetry.io/contrib/instrumentation/github.com/labstack/echo/otelecho`, use `Version` function instead. (#7089)
- The deprecated `SemVersion` function is removed in `go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws`, use `Version` function instead. (#7154)
- The deprecated `UnaryServerInterceptor` in `go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc` is removed, use `NewServerHandler` instead. (#7115)

<!-- Released section -->
<!-- Don't change this section unless doing release -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,6 @@ func BenchmarkNoInstrumentation(b *testing.B) {
benchmark(b, nil, nil)
}

func BenchmarkUnaryServerInterceptor(b *testing.B) {
benchmark(b, nil, []grpc.ServerOption{
grpc.UnaryInterceptor(otelgrpc.UnaryServerInterceptor(
otelgrpc.WithTracerProvider(tracerProvider),
)),
})
}

func BenchmarkStreamServerInterceptor(b *testing.B) {
benchmark(b, nil, []grpc.ServerOption{
grpc.StreamInterceptor(otelgrpc.StreamServerInterceptor(
Expand Down
88 changes: 5 additions & 83 deletions instrumentation/google.golang.org/grpc/otelgrpc/interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"io"
"net"
"strconv"
"time"

"google.golang.org/grpc"
grpc_codes "google.golang.org/grpc/codes"
Expand All @@ -23,7 +22,6 @@ import (
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc/internal"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/metric"
semconv "go.opentelemetry.io/otel/semconv/v1.17.0"
"go.opentelemetry.io/otel/trace"
)
Expand Down Expand Up @@ -75,7 +73,7 @@ func UnaryClientInterceptor(opts ...Option) grpc.UnaryClientInterceptor {
return invoker(ctx, method, req, reply, cc, callOpts...)
}

name, attr, _ := telemetryAttributes(method, cc.Target())
name, attr := telemetryAttributes(method, cc.Target())

startOpts := append([]trace.SpanStartOption{
trace.WithSpanKind(trace.SpanKindClient),
Expand Down Expand Up @@ -235,7 +233,7 @@ func StreamClientInterceptor(opts ...Option) grpc.StreamClientInterceptor {
return streamer(ctx, desc, cc, method, callOpts...)
}

name, attr, _ := telemetryAttributes(method, cc.Target())
name, attr := telemetryAttributes(method, cc.Target())

startOpts := append([]trace.SpanStartOption{
trace.WithSpanKind(trace.SpanKindClient),
Expand Down Expand Up @@ -265,81 +263,6 @@ func StreamClientInterceptor(opts ...Option) grpc.StreamClientInterceptor {
}
}

// UnaryServerInterceptor returns a grpc.UnaryServerInterceptor suitable
// for use in a grpc.NewServer call.
//
// Deprecated: Use [NewServerHandler] instead.
func UnaryServerInterceptor(opts ...Option) grpc.UnaryServerInterceptor {
cfg := newConfig(opts, "server")
tracer := cfg.TracerProvider.Tracer(
ScopeName,
trace.WithInstrumentationVersion(Version()),
)

return func(
ctx context.Context,
req interface{},
info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler,
) (interface{}, error) {
i := &InterceptorInfo{
UnaryServerInfo: info,
Type: UnaryServer,
}
if cfg.InterceptorFilter != nil && !cfg.InterceptorFilter(i) {
return handler(ctx, req)
}

ctx = extract(ctx, cfg.Propagators)
name, attr, metricAttrs := telemetryAttributes(info.FullMethod, peerFromCtx(ctx))

startOpts := append([]trace.SpanStartOption{
trace.WithSpanKind(trace.SpanKindServer),
trace.WithAttributes(attr...),
},
cfg.SpanStartOptions...,
)

ctx, span := tracer.Start(
trace.ContextWithRemoteSpanContext(ctx, trace.SpanContextFromContext(ctx)),
name,
startOpts...,
)
defer span.End()

if cfg.ReceivedEvent {
messageReceived.Event(ctx, 1, req)
}

before := time.Now()

resp, err := handler(ctx, req)

s, _ := status.FromError(err)
if err != nil {
statusCode, msg := serverStatus(s)
span.SetStatus(statusCode, msg)
if cfg.SentEvent {
messageSent.Event(ctx, 1, s.Proto())
}
} else {
if cfg.SentEvent {
messageSent.Event(ctx, 1, resp)
}
}
grpcStatusCodeAttr := statusCodeAttr(s.Code())
span.SetAttributes(grpcStatusCodeAttr)

// Use floating point division here for higher precision (instead of Millisecond method).
elapsedTime := float64(time.Since(before)) / float64(time.Millisecond)

metricAttrs = append(metricAttrs, grpcStatusCodeAttr)
cfg.rpcDuration.Record(ctx, elapsedTime, metric.WithAttributeSet(attribute.NewSet(metricAttrs...)))

return resp, err
}
}

// serverStream wraps around the embedded grpc.ServerStream, and intercepts the RecvMsg and
// SendMsg method call.
type serverStream struct {
Expand Down Expand Up @@ -417,7 +340,7 @@ func StreamServerInterceptor(opts ...Option) grpc.StreamServerInterceptor {
}

ctx = extract(ctx, cfg.Propagators)
name, attr, _ := telemetryAttributes(info.FullMethod, peerFromCtx(ctx))
name, attr := telemetryAttributes(info.FullMethod, peerFromCtx(ctx))

startOpts := append([]trace.SpanStartOption{
trace.WithSpanKind(trace.SpanKindServer),
Expand Down Expand Up @@ -449,16 +372,15 @@ func StreamServerInterceptor(opts ...Option) grpc.StreamServerInterceptor {

// telemetryAttributes returns a span name and span and metric attributes from
// the gRPC method and peer address.
func telemetryAttributes(fullMethod, peerAddress string) (string, []attribute.KeyValue, []attribute.KeyValue) {
func telemetryAttributes(fullMethod, peerAddress string) (string, []attribute.KeyValue) {
name, methodAttrs := internal.ParseFullMethod(fullMethod)
peerAttrs := peerAttr(peerAddress)

attrs := make([]attribute.KeyValue, 0, 1+len(methodAttrs)+len(peerAttrs))
attrs = append(attrs, RPCSystemGRPC)
attrs = append(attrs, methodAttrs...)
metricAttrs := attrs[:1+len(methodAttrs)]
attrs = append(attrs, peerAttrs...)
return name, attrs, metricAttrs
return name, attrs
}

// peerAttr returns attributes about the peer address.
Expand Down
128 changes: 0 additions & 128 deletions instrumentation/google.golang.org/grpc/otelgrpc/test/grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ import (
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc/internal/test"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/sdk/instrumentation"
"go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/metric/metricdata"
"go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest"
"go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/sdk/trace/tracetest"
semconv "go.opentelemetry.io/otel/semconv/v1.17.0"
Expand Down Expand Up @@ -86,11 +83,6 @@ func TestInterceptors(t *testing.T) {
clientStreamSR := tracetest.NewSpanRecorder()
clientStreamTP := trace.NewTracerProvider(trace.WithSpanProcessor(clientStreamSR))

serverUnarySR := tracetest.NewSpanRecorder()
serverUnaryTP := trace.NewTracerProvider(trace.WithSpanProcessor(serverUnarySR))
serverUnaryMetricReader := metric.NewManualReader()
serverUnaryMP := metric.NewMeterProvider(metric.WithReader(serverUnaryMetricReader))

serverStreamSR := tracetest.NewSpanRecorder()
serverStreamTP := trace.NewTracerProvider(trace.WithSpanProcessor(serverStreamSR))

Expand All @@ -110,12 +102,6 @@ func TestInterceptors(t *testing.T) {
)),
},
[]grpc.ServerOption{
//nolint:staticcheck // Interceptors are deprecated and will be removed in the next release.
grpc.UnaryInterceptor(otelgrpc.UnaryServerInterceptor(
otelgrpc.WithTracerProvider(serverUnaryTP),
otelgrpc.WithMeterProvider(serverUnaryMP),
otelgrpc.WithMessageEvents(otelgrpc.ReceivedEvents, otelgrpc.SentEvents),
)),
//nolint:staticcheck // Interceptors are deprecated and will be removed in the next release.
grpc.StreamInterceptor(otelgrpc.StreamServerInterceptor(
otelgrpc.WithTracerProvider(serverStreamTP),
Expand All @@ -135,11 +121,6 @@ func TestInterceptors(t *testing.T) {
checkStreamClientSpans(t, clientStreamSR.Ended(), listener.Addr().String())
})

t.Run("UnaryServerSpans", func(t *testing.T) {
checkUnaryServerSpans(t, serverUnarySR.Ended())
checkUnaryServerRecords(t, serverUnaryMetricReader)
})

t.Run("StreamServerSpans", func(t *testing.T) {
checkStreamServerSpans(t, serverStreamSR.Ended())
})
Expand Down Expand Up @@ -568,74 +549,6 @@ func checkStreamServerSpans(t *testing.T, spans []trace.ReadOnlySpan) {
}, pingPong.Attributes())
}

func checkUnaryServerSpans(t *testing.T, spans []trace.ReadOnlySpan) {
require.Len(t, spans, 2)

emptySpan := spans[0]
assert.False(t, emptySpan.EndTime().IsZero())
assert.Equal(t, "grpc.testing.TestService/EmptyCall", emptySpan.Name())
assertEvents(t, []trace.Event{
{
Name: "message",
Attributes: []attribute.KeyValue{
otelgrpc.RPCMessageIDKey.Int(1),
otelgrpc.RPCMessageTypeKey.String("RECEIVED"),
},
},
{
Name: "message",
Attributes: []attribute.KeyValue{
otelgrpc.RPCMessageIDKey.Int(1),
otelgrpc.RPCMessageTypeKey.String("SENT"),
},
},
}, emptySpan.Events())

port, ok := findAttribute(emptySpan.Attributes(), semconv.NetSockPeerPortKey)
assert.True(t, ok)
assert.ElementsMatch(t, []attribute.KeyValue{
semconv.RPCMethod("EmptyCall"),
semconv.RPCService("grpc.testing.TestService"),
otelgrpc.RPCSystemGRPC,
otelgrpc.GRPCStatusCodeKey.Int64(int64(codes.OK)),
semconv.NetSockPeerAddr("127.0.0.1"),
port,
}, emptySpan.Attributes())

largeSpan := spans[1]
assert.False(t, largeSpan.EndTime().IsZero())
assert.Equal(t, "grpc.testing.TestService/UnaryCall", largeSpan.Name())
assertEvents(t, []trace.Event{
{
Name: "message",
Attributes: []attribute.KeyValue{
otelgrpc.RPCMessageIDKey.Int(1),
otelgrpc.RPCMessageTypeKey.String("RECEIVED"),
// largeReqSize from "go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc/test" + 12 (overhead).
},
},
{
Name: "message",
Attributes: []attribute.KeyValue{
otelgrpc.RPCMessageIDKey.Int(1),
otelgrpc.RPCMessageTypeKey.String("SENT"),
// largeRespSize from "go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc/test" + 8 (overhead).
},
},
}, largeSpan.Events())

port, ok = findAttribute(largeSpan.Attributes(), semconv.NetSockPeerPortKey)
assert.True(t, ok)
assert.ElementsMatch(t, []attribute.KeyValue{
semconv.RPCMethod("UnaryCall"),
semconv.RPCService("grpc.testing.TestService"),
otelgrpc.RPCSystemGRPC,
otelgrpc.GRPCStatusCodeKey.Int64(int64(codes.OK)),
semconv.NetSockPeerAddr("127.0.0.1"),
port,
}, largeSpan.Attributes())
}

func assertEvents(t *testing.T, expected, actual []trace.Event) bool { //nolint:unparam
if !assert.Len(t, actual, len(expected)) {
return false
Expand All @@ -654,47 +567,6 @@ func assertEvents(t *testing.T, expected, actual []trace.Event) bool { //nolint:
return !failed
}

func checkUnaryServerRecords(t *testing.T, reader metric.Reader) {
rm := metricdata.ResourceMetrics{}
err := reader.Collect(context.Background(), &rm)
assert.NoError(t, err)
require.Len(t, rm.ScopeMetrics, 1)

want := metricdata.ScopeMetrics{
Scope: wantInstrumentationScope,
Metrics: []metricdata.Metrics{
{
Name: "rpc.server.duration",
Description: "Measures the duration of inbound RPC.",
Unit: "ms",
Data: metricdata.Histogram[float64]{
Temporality: metricdata.CumulativeTemporality,
DataPoints: []metricdata.HistogramDataPoint[float64]{
{
Attributes: attribute.NewSet(
semconv.RPCMethod("EmptyCall"),
semconv.RPCService("grpc.testing.TestService"),
otelgrpc.RPCSystemGRPC,
otelgrpc.GRPCStatusCodeKey.Int64(int64(codes.OK)),
),
},
{
Attributes: attribute.NewSet(
semconv.RPCMethod("UnaryCall"),
semconv.RPCService("grpc.testing.TestService"),
otelgrpc.RPCSystemGRPC,
otelgrpc.GRPCStatusCodeKey.Int64(int64(codes.OK)),
),
},
},
},
},
},
}

metricdatatest.AssertEqual(t, want, rm.ScopeMetrics[0], metricdatatest.IgnoreTimestamp(), metricdatatest.IgnoreValue())
}

func findAttribute(kvs []attribute.KeyValue, key attribute.Key) (attribute.KeyValue, bool) { //nolint:unparam
for _, kv := range kvs {
if kv.Key == key {
Expand Down
Loading