1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2024-12-12 10:04:29 +02:00

Merge pull request #782 from tensorchen/remove_dup_key

using api/standard keys instead of custom keys
This commit is contained in:
Tyler Yahn 2020-06-01 09:17:26 -07:00 committed by GitHub
commit 823703a6bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 73 additions and 82 deletions

View File

@ -22,6 +22,8 @@ import (
"net"
"regexp"
"go.opentelemetry.io/otel/api/standard"
"github.com/golang/protobuf/proto"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
@ -33,17 +35,7 @@ import (
"go.opentelemetry.io/otel/api/trace"
)
var (
rpcServiceKey = kv.Key("rpc.service")
netPeerIPKey = kv.Key("net.peer.ip")
netPeerPortKey = kv.Key("net.peer.port")
messageTypeKey = kv.Key("message.type")
messageIDKey = kv.Key("message.id")
messageUncompressedSizeKey = kv.Key("message.uncompressed_size")
)
type messageType string
type messageType kv.KeyValue
// Event adds an event of the messageType to the span associated with the
// passed context with id and size (if message is a proto message).
@ -51,21 +43,21 @@ func (m messageType) Event(ctx context.Context, id int, message interface{}) {
span := trace.SpanFromContext(ctx)
if p, ok := message.(proto.Message); ok {
span.AddEvent(ctx, "message",
messageTypeKey.String(string(m)),
messageIDKey.Int(id),
messageUncompressedSizeKey.Int(proto.Size(p)),
kv.KeyValue(m),
standard.RPCMessageIDKey.Int(id),
standard.RPCMessageUncompressedSizeKey.Int(proto.Size(p)),
)
} else {
span.AddEvent(ctx, "message",
messageTypeKey.String(string(m)),
messageIDKey.Int(id),
kv.KeyValue(m),
standard.RPCMessageIDKey.Int(id),
)
}
}
const (
messageSent messageType = "SENT"
messageReceived messageType = "RECEIVED"
var (
messageSent = messageType(standard.RPCMessageTypeSent)
messageReceived = messageType(standard.RPCMessageTypeReceived)
)
// UnaryClientInterceptor returns a grpc.UnaryClientInterceptor suitable
@ -93,7 +85,7 @@ func UnaryClientInterceptor(tracer trace.Tracer) grpc.UnaryClientInterceptor {
ctx, method,
trace.WithSpanKind(trace.SpanKindClient),
trace.WithAttributes(peerInfoFromTarget(cc.Target())...),
trace.WithAttributes(rpcServiceKey.String(serviceFromFullMethod(method))),
trace.WithAttributes(standard.RPCServiceKey.String(serviceFromFullMethod(method))),
)
defer span.End()
@ -271,7 +263,7 @@ func StreamClientInterceptor(tracer trace.Tracer) grpc.StreamClientInterceptor {
ctx, method,
trace.WithSpanKind(trace.SpanKindClient),
trace.WithAttributes(peerInfoFromTarget(cc.Target())...),
trace.WithAttributes(rpcServiceKey.String(serviceFromFullMethod(method))),
trace.WithAttributes(standard.RPCServiceKey.String(serviceFromFullMethod(method))),
)
Inject(ctx, &metadataCopy)
@ -325,7 +317,7 @@ func UnaryServerInterceptor(tracer trace.Tracer) grpc.UnaryServerInterceptor {
info.FullMethod,
trace.WithSpanKind(trace.SpanKindServer),
trace.WithAttributes(peerInfoFromContext(ctx)...),
trace.WithAttributes(rpcServiceKey.String(serviceFromFullMethod(info.FullMethod))),
trace.WithAttributes(standard.RPCServiceKey.String(serviceFromFullMethod(info.FullMethod))),
)
defer span.End()
@ -415,7 +407,7 @@ func StreamServerInterceptor(tracer trace.Tracer) grpc.StreamServerInterceptor {
info.FullMethod,
trace.WithSpanKind(trace.SpanKindServer),
trace.WithAttributes(peerInfoFromContext(ctx)...),
trace.WithAttributes(rpcServiceKey.String(serviceFromFullMethod(info.FullMethod))),
trace.WithAttributes(standard.RPCServiceKey.String(serviceFromFullMethod(info.FullMethod))),
)
defer span.End()
@ -442,8 +434,8 @@ func peerInfoFromTarget(target string) []kv.KeyValue {
}
return []kv.KeyValue{
netPeerIPKey.String(host),
netPeerPortKey.String(port),
standard.NetPeerIPKey.String(host),
standard.NetPeerPortKey.String(port),
}
}

View File

@ -19,6 +19,8 @@ import (
"testing"
"time"
"go.opentelemetry.io/otel/api/standard"
"github.com/golang/protobuf/proto"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@ -93,100 +95,100 @@ func TestUnaryClientInterceptor(t *testing.T) {
{
name: "/github.com.serviceName/bar",
expectedAttr: map[kv.Key]value.Value{
rpcServiceKey: value.String("serviceName"),
netPeerIPKey: value.String("fake"),
netPeerPortKey: value.String("connection"),
standard.RPCServiceKey: value.String("serviceName"),
standard.NetPeerIPKey: value.String("fake"),
standard.NetPeerPortKey: value.String("connection"),
},
eventsAttr: []map[kv.Key]value.Value{
{
messageTypeKey: value.String("SENT"),
messageIDKey: value.Int(1),
messageUncompressedSizeKey: value.Int(proto.Size(proto.Message(req))),
standard.RPCMessageTypeKey: value.String("SENT"),
standard.RPCMessageIDKey: value.Int(1),
standard.RPCMessageUncompressedSizeKey: value.Int(proto.Size(proto.Message(req))),
},
{
messageTypeKey: value.String("RECEIVED"),
messageIDKey: value.Int(1),
messageUncompressedSizeKey: value.Int(proto.Size(proto.Message(reply))),
standard.RPCMessageTypeKey: value.String("RECEIVED"),
standard.RPCMessageIDKey: value.Int(1),
standard.RPCMessageUncompressedSizeKey: value.Int(proto.Size(proto.Message(reply))),
},
},
},
{
name: "/serviceName/bar",
expectedAttr: map[kv.Key]value.Value{
rpcServiceKey: value.String("serviceName"),
netPeerIPKey: value.String("fake"),
netPeerPortKey: value.String("connection"),
standard.RPCServiceKey: value.String("serviceName"),
standard.NetPeerIPKey: value.String("fake"),
standard.NetPeerPortKey: value.String("connection"),
},
eventsAttr: []map[kv.Key]value.Value{
{
messageTypeKey: value.String("SENT"),
messageIDKey: value.Int(1),
messageUncompressedSizeKey: value.Int(proto.Size(proto.Message(req))),
standard.RPCMessageTypeKey: value.String("SENT"),
standard.RPCMessageIDKey: value.Int(1),
standard.RPCMessageUncompressedSizeKey: value.Int(proto.Size(proto.Message(req))),
},
{
messageTypeKey: value.String("RECEIVED"),
messageIDKey: value.Int(1),
messageUncompressedSizeKey: value.Int(proto.Size(proto.Message(reply))),
standard.RPCMessageTypeKey: value.String("RECEIVED"),
standard.RPCMessageIDKey: value.Int(1),
standard.RPCMessageUncompressedSizeKey: value.Int(proto.Size(proto.Message(reply))),
},
},
},
{
name: "serviceName/bar",
expectedAttr: map[kv.Key]value.Value{
rpcServiceKey: value.String("serviceName"),
netPeerIPKey: value.String("fake"),
netPeerPortKey: value.String("connection"),
standard.RPCServiceKey: value.String("serviceName"),
standard.NetPeerIPKey: value.String("fake"),
standard.NetPeerPortKey: value.String("connection"),
},
eventsAttr: []map[kv.Key]value.Value{
{
messageTypeKey: value.String("SENT"),
messageIDKey: value.Int(1),
messageUncompressedSizeKey: value.Int(proto.Size(proto.Message(req))),
standard.RPCMessageTypeKey: value.String("SENT"),
standard.RPCMessageIDKey: value.Int(1),
standard.RPCMessageUncompressedSizeKey: value.Int(proto.Size(proto.Message(req))),
},
{
messageTypeKey: value.String("RECEIVED"),
messageIDKey: value.Int(1),
messageUncompressedSizeKey: value.Int(proto.Size(proto.Message(reply))),
standard.RPCMessageTypeKey: value.String("RECEIVED"),
standard.RPCMessageIDKey: value.Int(1),
standard.RPCMessageUncompressedSizeKey: value.Int(proto.Size(proto.Message(reply))),
},
},
},
{
name: "invalidName",
expectedAttr: map[kv.Key]value.Value{
rpcServiceKey: value.String(""),
netPeerIPKey: value.String("fake"),
netPeerPortKey: value.String("connection"),
standard.RPCServiceKey: value.String(""),
standard.NetPeerIPKey: value.String("fake"),
standard.NetPeerPortKey: value.String("connection"),
},
eventsAttr: []map[kv.Key]value.Value{
{
messageTypeKey: value.String("SENT"),
messageIDKey: value.Int(1),
messageUncompressedSizeKey: value.Int(proto.Size(proto.Message(req))),
standard.RPCMessageTypeKey: value.String("SENT"),
standard.RPCMessageIDKey: value.Int(1),
standard.RPCMessageUncompressedSizeKey: value.Int(proto.Size(proto.Message(req))),
},
{
messageTypeKey: value.String("RECEIVED"),
messageIDKey: value.Int(1),
messageUncompressedSizeKey: value.Int(proto.Size(proto.Message(reply))),
standard.RPCMessageTypeKey: value.String("RECEIVED"),
standard.RPCMessageIDKey: value.Int(1),
standard.RPCMessageUncompressedSizeKey: value.Int(proto.Size(proto.Message(reply))),
},
},
},
{
name: "/github.com.foo.serviceName_123/method",
expectedAttr: map[kv.Key]value.Value{
rpcServiceKey: value.String("serviceName_123"),
netPeerIPKey: value.String("fake"),
netPeerPortKey: value.String("connection"),
standard.RPCServiceKey: value.String("serviceName_123"),
standard.NetPeerIPKey: value.String("fake"),
standard.NetPeerPortKey: value.String("connection"),
},
eventsAttr: []map[kv.Key]value.Value{
{
messageTypeKey: value.String("SENT"),
messageIDKey: value.Int(1),
messageUncompressedSizeKey: value.Int(proto.Size(proto.Message(req))),
standard.RPCMessageTypeKey: value.String("SENT"),
standard.RPCMessageIDKey: value.Int(1),
standard.RPCMessageUncompressedSizeKey: value.Int(proto.Size(proto.Message(req))),
},
{
messageTypeKey: value.String("RECEIVED"),
messageIDKey: value.Int(1),
messageUncompressedSizeKey: value.Int(proto.Size(proto.Message(reply))),
standard.RPCMessageTypeKey: value.String("RECEIVED"),
standard.RPCMessageIDKey: value.Int(1),
standard.RPCMessageUncompressedSizeKey: value.Int(proto.Size(proto.Message(reply))),
},
},
},
@ -342,9 +344,9 @@ func TestStreamClientInterceptor(t *testing.T) {
attrs := spanData.Attributes
expectedAttr := map[kv.Key]string{
rpcServiceKey: "serviceName",
netPeerIPKey: "fake",
netPeerPortKey: "connection",
standard.RPCServiceKey: "serviceName",
standard.NetPeerIPKey: "fake",
standard.NetPeerPortKey: "connection",
}
for _, attr := range attrs {
@ -365,10 +367,10 @@ func TestStreamClientInterceptor(t *testing.T) {
msgID := i/2 + 1
validate := func(eventName string, attrs []kv.KeyValue) {
for _, attr := range attrs {
if attr.Key == messageTypeKey && attr.Value.AsString() != eventName {
if attr.Key == standard.RPCMessageTypeKey && attr.Value.AsString() != eventName {
t.Errorf("invalid event on index: %d expecting %s event, receive %s event", i, eventName, attr.Value.AsString())
}
if attr.Key == messageIDKey && attr.Value != value.Int(msgID) {
if attr.Key == standard.RPCMessageIDKey && attr.Value != value.Int(msgID) {
t.Errorf("invalid id for message event expected %d received %d", msgID, attr.Value.AsInt32())
}
}

View File

@ -22,6 +22,8 @@ import (
"strings"
"sync"
"go.opentelemetry.io/otel/api/standard"
"google.golang.org/grpc/codes"
"go.opentelemetry.io/otel/api/global"
@ -150,7 +152,7 @@ func (ct *clientTracer) span(hook string) trace.Span {
}
func (ct *clientTracer) getConn(host string) {
ct.start("http.getconn", "http.getconn", HostKey.String(host))
ct.start("http.getconn", "http.getconn", standard.HTTPHostKey.String(host))
}
func (ct *clientTracer) gotConn(info httptrace.GotConnInfo) {
@ -170,7 +172,7 @@ func (ct *clientTracer) gotFirstResponseByte() {
}
func (ct *clientTracer) dnsStart(info httptrace.DNSStartInfo) {
ct.start("http.dns", "http.dns", HostKey.String(info.Host))
ct.start("http.dns", "http.dns", standard.HTTPHostKey.String(info.Host))
}
func (ct *clientTracer) dnsDone(info httptrace.DNSDoneInfo) {

View File

@ -26,11 +26,6 @@ import (
"go.opentelemetry.io/otel/api/trace"
)
var (
HostKey = kv.Key("http.host")
URLKey = kv.Key("http.url")
)
// Returns the Attributes, Context Entries, and SpanContext that were encoded by Inject.
func Extract(ctx context.Context, req *http.Request) ([]kv.KeyValue, []kv.KeyValue, trace.SpanContext) {
ctx = propagation.ExtractHTTP(ctx, global.Propagators(), req.Header)