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

View File

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

View File

@ -22,6 +22,8 @@ import (
"strings" "strings"
"sync" "sync"
"go.opentelemetry.io/otel/api/standard"
"google.golang.org/grpc/codes" "google.golang.org/grpc/codes"
"go.opentelemetry.io/otel/api/global" "go.opentelemetry.io/otel/api/global"
@ -150,7 +152,7 @@ func (ct *clientTracer) span(hook string) trace.Span {
} }
func (ct *clientTracer) getConn(host string) { 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) { func (ct *clientTracer) gotConn(info httptrace.GotConnInfo) {
@ -170,7 +172,7 @@ func (ct *clientTracer) gotFirstResponseByte() {
} }
func (ct *clientTracer) dnsStart(info httptrace.DNSStartInfo) { 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) { func (ct *clientTracer) dnsDone(info httptrace.DNSDoneInfo) {

View File

@ -26,11 +26,6 @@ import (
"go.opentelemetry.io/otel/api/trace" "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. // 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) { func Extract(ctx context.Context, req *http.Request) ([]kv.KeyValue, []kv.KeyValue, trace.SpanContext) {
ctx = propagation.ExtractHTTP(ctx, global.Propagators(), req.Header) ctx = propagation.ExtractHTTP(ctx, global.Propagators(), req.Header)