diff --git a/CHANGELOG.md b/CHANGELOG.md index 4bd2c275559..e4374dc115d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - The B3 propagator now extracts from either HTTP encoding of B3 (Single Header or Multiple Header) based on what is contained in the header. Preference is given to Single Header encoding with Multiple Header being the fallback if Single Header is not found or is invalid. This behavior change is made to dynamically support all correctly encoded traces received instead of having to guess the expected encoding prior to receiving. (#882) +- Extend semantic conventions for RPC. (#900) ### Removed diff --git a/api/standard/trace.go b/api/standard/trace.go index 83eae79e8ba..1d148d8df4d 100644 --- a/api/standard/trace.go +++ b/api/standard/trace.go @@ -188,9 +188,15 @@ const ( // Standard attribute keys for RPC. const ( - // The RPC service name. + // A string identifying the remoting system. + RPCSystemKey = kv.Key("rpc.system") + + // The full name of the service being called. RPCServiceKey = kv.Key("rpc.service") + // The name of the method being called. + RPCMethodKey = kv.Key("rpc.method") + // Name of message transmitted or received. RPCNameKey = kv.Key("name") @@ -209,6 +215,8 @@ const ( ) var ( + RPCSystemGRPC = RPCSystemKey.String("grpc") + RPCNameMessage = RPCNameKey.String("message") RPCMessageTypeSent = RPCMessageTypeKey.String("SENT") diff --git a/instrumentation/grpctrace/interceptor.go b/instrumentation/grpctrace/interceptor.go index 3d254c5dccd..d6564825c43 100644 --- a/instrumentation/grpctrace/interceptor.go +++ b/instrumentation/grpctrace/interceptor.go @@ -20,7 +20,7 @@ import ( "context" "io" "net" - "regexp" + "strings" "go.opentelemetry.io/otel/api/standard" @@ -86,7 +86,8 @@ func UnaryClientInterceptor(tracer trace.Tracer) grpc.UnaryClientInterceptor { ctx, method, trace.WithSpanKind(trace.SpanKindClient), trace.WithAttributes(peerInfoFromTarget(cc.Target())...), - trace.WithAttributes(standard.RPCServiceKey.String(serviceFromFullMethod(method))), + trace.WithAttributes(standard.RPCSystemGRPC), + trace.WithAttributes(serviceAndMethodFromFullName(method)...), ) defer span.End() @@ -264,7 +265,8 @@ func StreamClientInterceptor(tracer trace.Tracer) grpc.StreamClientInterceptor { ctx, method, trace.WithSpanKind(trace.SpanKindClient), trace.WithAttributes(peerInfoFromTarget(cc.Target())...), - trace.WithAttributes(standard.RPCServiceKey.String(serviceFromFullMethod(method))), + trace.WithAttributes(standard.RPCSystemGRPC), + trace.WithAttributes(serviceAndMethodFromFullName(method)...), ) Inject(ctx, &metadataCopy) @@ -318,7 +320,8 @@ func UnaryServerInterceptor(tracer trace.Tracer) grpc.UnaryServerInterceptor { info.FullMethod, trace.WithSpanKind(trace.SpanKindServer), trace.WithAttributes(peerInfoFromContext(ctx)...), - trace.WithAttributes(standard.RPCServiceKey.String(serviceFromFullMethod(info.FullMethod))), + trace.WithAttributes(standard.RPCSystemGRPC), + trace.WithAttributes(serviceAndMethodFromFullName(info.FullMethod)...), ) defer span.End() @@ -408,7 +411,8 @@ func StreamServerInterceptor(tracer trace.Tracer) grpc.StreamServerInterceptor { info.FullMethod, trace.WithSpanKind(trace.SpanKindServer), trace.WithAttributes(peerInfoFromContext(ctx)...), - trace.WithAttributes(standard.RPCServiceKey.String(serviceFromFullMethod(info.FullMethod))), + trace.WithAttributes(standard.RPCSystemGRPC), + trace.WithAttributes(serviceAndMethodFromFullName(info.FullMethod)...), ) defer span.End() @@ -450,13 +454,14 @@ func peerInfoFromContext(ctx context.Context) []kv.KeyValue { return peerInfoFromTarget(p.Addr.String()) } -var fullMethodRegexp = regexp.MustCompile(`^\/?(?:\S+\.)?(\S+)\/\S+$`) - -func serviceFromFullMethod(method string) string { - match := fullMethodRegexp.FindStringSubmatch(method) - if len(match) == 0 { - return "" +func serviceAndMethodFromFullName(method string) []kv.KeyValue { + l := strings.LastIndexByte(method, '/') + if l == -1 { + return []kv.KeyValue{} } - return match[1] + return []kv.KeyValue{ + standard.RPCServiceKey.String(method[:l]), + standard.RPCMethodKey.String(method[l+1:]), + } } diff --git a/instrumentation/grpctrace/interceptor_test.go b/instrumentation/grpctrace/interceptor_test.go index 6ec80cba85f..252f81061bb 100644 --- a/instrumentation/grpctrace/interceptor_test.go +++ b/instrumentation/grpctrace/interceptor_test.go @@ -97,7 +97,9 @@ func TestUnaryClientInterceptor(t *testing.T) { { name: "/github.com.serviceName/bar", expectedAttr: map[kv.Key]value.Value{ - standard.RPCServiceKey: value.String("serviceName"), + standard.RPCSystemKey: value.String("grpc"), + standard.RPCServiceKey: value.String("/github.com.serviceName"), + standard.RPCMethodKey: value.String("bar"), standard.NetPeerIPKey: value.String("fake"), standard.NetPeerPortKey: value.String("connection"), }, @@ -117,7 +119,9 @@ func TestUnaryClientInterceptor(t *testing.T) { { name: "/serviceName/bar", expectedAttr: map[kv.Key]value.Value{ - standard.RPCServiceKey: value.String("serviceName"), + standard.RPCSystemKey: value.String("grpc"), + standard.RPCServiceKey: value.String("/serviceName"), + standard.RPCMethodKey: value.String("bar"), standard.NetPeerIPKey: value.String("fake"), standard.NetPeerPortKey: value.String("connection"), }, @@ -137,7 +141,9 @@ func TestUnaryClientInterceptor(t *testing.T) { { name: "serviceName/bar", expectedAttr: map[kv.Key]value.Value{ + standard.RPCSystemKey: value.String("grpc"), standard.RPCServiceKey: value.String("serviceName"), + standard.RPCMethodKey: value.String("bar"), standard.NetPeerIPKey: value.String("fake"), standard.NetPeerPortKey: value.String("connection"), }, @@ -157,7 +163,7 @@ func TestUnaryClientInterceptor(t *testing.T) { { name: "invalidName", expectedAttr: map[kv.Key]value.Value{ - standard.RPCServiceKey: value.String(""), + standard.RPCSystemKey: value.String("grpc"), standard.NetPeerIPKey: value.String("fake"), standard.NetPeerPortKey: value.String("connection"), }, @@ -177,7 +183,9 @@ func TestUnaryClientInterceptor(t *testing.T) { { name: "/github.com.foo.serviceName_123/method", expectedAttr: map[kv.Key]value.Value{ - standard.RPCServiceKey: value.String("serviceName_123"), + standard.RPCSystemKey: value.String("grpc"), + standard.RPCServiceKey: value.String("/github.com.foo.serviceName_123"), + standard.RPCMethodKey: value.String("method"), standard.NetPeerIPKey: value.String("fake"), standard.NetPeerPortKey: value.String("connection"), }, @@ -346,7 +354,9 @@ func TestStreamClientInterceptor(t *testing.T) { attrs := spanData.Attributes expectedAttr := map[kv.Key]string{ - standard.RPCServiceKey: "serviceName", + standard.RPCSystemKey: "grpc", + standard.RPCServiceKey: "/github.com.serviceName", + standard.RPCMethodKey: "bar", standard.NetPeerIPKey: "fake", standard.NetPeerPortKey: "connection", }