Skip to content

Commit 1c9aab6

Browse files
matej-gMrAlias
andauthored
Extend semantic convetions for RPC (#900)
* Extend semantic convetions for RPC * Update changelog * make service attribute conform to spec * Update api/standard/trace.go Co-authored-by: Tyler Yahn <[email protected]> Co-authored-by: Tyler Yahn <[email protected]>
1 parent 918c654 commit 1c9aab6

File tree

4 files changed

+42
-18
lines changed

4 files changed

+42
-18
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
2828
- The B3 propagator now extracts from either HTTP encoding of B3 (Single Header or Multiple Header) based on what is contained in the header.
2929
Preference is given to Single Header encoding with Multiple Header being the fallback if Single Header is not found or is invalid.
3030
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)
31+
- Extend semantic conventions for RPC. (#900)
3132

3233
### Removed
3334

api/standard/trace.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,15 @@ const (
188188

189189
// Standard attribute keys for RPC.
190190
const (
191-
// The RPC service name.
191+
// A string identifying the remoting system.
192+
RPCSystemKey = kv.Key("rpc.system")
193+
194+
// The full name of the service being called.
192195
RPCServiceKey = kv.Key("rpc.service")
193196

197+
// The name of the method being called.
198+
RPCMethodKey = kv.Key("rpc.method")
199+
194200
// Name of message transmitted or received.
195201
RPCNameKey = kv.Key("name")
196202

@@ -209,6 +215,8 @@ const (
209215
)
210216

211217
var (
218+
RPCSystemGRPC = RPCSystemKey.String("grpc")
219+
212220
RPCNameMessage = RPCNameKey.String("message")
213221

214222
RPCMessageTypeSent = RPCMessageTypeKey.String("SENT")

instrumentation/grpctrace/interceptor.go

+17-12
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"context"
2121
"io"
2222
"net"
23-
"regexp"
23+
"strings"
2424

2525
"go.opentelemetry.io/otel/api/standard"
2626

@@ -86,7 +86,8 @@ func UnaryClientInterceptor(tracer trace.Tracer) grpc.UnaryClientInterceptor {
8686
ctx, method,
8787
trace.WithSpanKind(trace.SpanKindClient),
8888
trace.WithAttributes(peerInfoFromTarget(cc.Target())...),
89-
trace.WithAttributes(standard.RPCServiceKey.String(serviceFromFullMethod(method))),
89+
trace.WithAttributes(standard.RPCSystemGRPC),
90+
trace.WithAttributes(serviceAndMethodFromFullName(method)...),
9091
)
9192
defer span.End()
9293

@@ -264,7 +265,8 @@ func StreamClientInterceptor(tracer trace.Tracer) grpc.StreamClientInterceptor {
264265
ctx, method,
265266
trace.WithSpanKind(trace.SpanKindClient),
266267
trace.WithAttributes(peerInfoFromTarget(cc.Target())...),
267-
trace.WithAttributes(standard.RPCServiceKey.String(serviceFromFullMethod(method))),
268+
trace.WithAttributes(standard.RPCSystemGRPC),
269+
trace.WithAttributes(serviceAndMethodFromFullName(method)...),
268270
)
269271

270272
Inject(ctx, &metadataCopy)
@@ -318,7 +320,8 @@ func UnaryServerInterceptor(tracer trace.Tracer) grpc.UnaryServerInterceptor {
318320
info.FullMethod,
319321
trace.WithSpanKind(trace.SpanKindServer),
320322
trace.WithAttributes(peerInfoFromContext(ctx)...),
321-
trace.WithAttributes(standard.RPCServiceKey.String(serviceFromFullMethod(info.FullMethod))),
323+
trace.WithAttributes(standard.RPCSystemGRPC),
324+
trace.WithAttributes(serviceAndMethodFromFullName(info.FullMethod)...),
322325
)
323326
defer span.End()
324327

@@ -408,7 +411,8 @@ func StreamServerInterceptor(tracer trace.Tracer) grpc.StreamServerInterceptor {
408411
info.FullMethod,
409412
trace.WithSpanKind(trace.SpanKindServer),
410413
trace.WithAttributes(peerInfoFromContext(ctx)...),
411-
trace.WithAttributes(standard.RPCServiceKey.String(serviceFromFullMethod(info.FullMethod))),
414+
trace.WithAttributes(standard.RPCSystemGRPC),
415+
trace.WithAttributes(serviceAndMethodFromFullName(info.FullMethod)...),
412416
)
413417
defer span.End()
414418

@@ -450,13 +454,14 @@ func peerInfoFromContext(ctx context.Context) []kv.KeyValue {
450454
return peerInfoFromTarget(p.Addr.String())
451455
}
452456

453-
var fullMethodRegexp = regexp.MustCompile(`^\/?(?:\S+\.)?(\S+)\/\S+$`)
454-
455-
func serviceFromFullMethod(method string) string {
456-
match := fullMethodRegexp.FindStringSubmatch(method)
457-
if len(match) == 0 {
458-
return ""
457+
func serviceAndMethodFromFullName(method string) []kv.KeyValue {
458+
l := strings.LastIndexByte(method, '/')
459+
if l == -1 {
460+
return []kv.KeyValue{}
459461
}
460462

461-
return match[1]
463+
return []kv.KeyValue{
464+
standard.RPCServiceKey.String(method[:l]),
465+
standard.RPCMethodKey.String(method[l+1:]),
466+
}
462467
}

instrumentation/grpctrace/interceptor_test.go

+15-5
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,9 @@ func TestUnaryClientInterceptor(t *testing.T) {
9797
{
9898
name: "/g.yxqyang.asia.serviceName/bar",
9999
expectedAttr: map[kv.Key]value.Value{
100-
standard.RPCServiceKey: value.String("serviceName"),
100+
standard.RPCSystemKey: value.String("grpc"),
101+
standard.RPCServiceKey: value.String("/g.yxqyang.asia.serviceName"),
102+
standard.RPCMethodKey: value.String("bar"),
101103
standard.NetPeerIPKey: value.String("fake"),
102104
standard.NetPeerPortKey: value.String("connection"),
103105
},
@@ -117,7 +119,9 @@ func TestUnaryClientInterceptor(t *testing.T) {
117119
{
118120
name: "/serviceName/bar",
119121
expectedAttr: map[kv.Key]value.Value{
120-
standard.RPCServiceKey: value.String("serviceName"),
122+
standard.RPCSystemKey: value.String("grpc"),
123+
standard.RPCServiceKey: value.String("/serviceName"),
124+
standard.RPCMethodKey: value.String("bar"),
121125
standard.NetPeerIPKey: value.String("fake"),
122126
standard.NetPeerPortKey: value.String("connection"),
123127
},
@@ -137,7 +141,9 @@ func TestUnaryClientInterceptor(t *testing.T) {
137141
{
138142
name: "serviceName/bar",
139143
expectedAttr: map[kv.Key]value.Value{
144+
standard.RPCSystemKey: value.String("grpc"),
140145
standard.RPCServiceKey: value.String("serviceName"),
146+
standard.RPCMethodKey: value.String("bar"),
141147
standard.NetPeerIPKey: value.String("fake"),
142148
standard.NetPeerPortKey: value.String("connection"),
143149
},
@@ -157,7 +163,7 @@ func TestUnaryClientInterceptor(t *testing.T) {
157163
{
158164
name: "invalidName",
159165
expectedAttr: map[kv.Key]value.Value{
160-
standard.RPCServiceKey: value.String(""),
166+
standard.RPCSystemKey: value.String("grpc"),
161167
standard.NetPeerIPKey: value.String("fake"),
162168
standard.NetPeerPortKey: value.String("connection"),
163169
},
@@ -177,7 +183,9 @@ func TestUnaryClientInterceptor(t *testing.T) {
177183
{
178184
name: "/g.yxqyang.asia.foo.serviceName_123/method",
179185
expectedAttr: map[kv.Key]value.Value{
180-
standard.RPCServiceKey: value.String("serviceName_123"),
186+
standard.RPCSystemKey: value.String("grpc"),
187+
standard.RPCServiceKey: value.String("/g.yxqyang.asia.foo.serviceName_123"),
188+
standard.RPCMethodKey: value.String("method"),
181189
standard.NetPeerIPKey: value.String("fake"),
182190
standard.NetPeerPortKey: value.String("connection"),
183191
},
@@ -346,7 +354,9 @@ func TestStreamClientInterceptor(t *testing.T) {
346354

347355
attrs := spanData.Attributes
348356
expectedAttr := map[kv.Key]string{
349-
standard.RPCServiceKey: "serviceName",
357+
standard.RPCSystemKey: "grpc",
358+
standard.RPCServiceKey: "/g.yxqyang.asia.serviceName",
359+
standard.RPCMethodKey: "bar",
350360
standard.NetPeerIPKey: "fake",
351361
standard.NetPeerPortKey: "connection",
352362
}

0 commit comments

Comments
 (0)