Style:trace (#2017)

* Update metadata.go

* Update span.go

* Update statsHandler.go
status-code-override
songzhibin97 3 years ago committed by chenzhihui
parent 9abcff9d30
commit 72ba4e5c43
  1. 18
      middleware/tracing/metadata.go
  2. 16
      middleware/tracing/span.go
  3. 17
      middleware/tracing/statsHandler.go

@ -26,16 +26,16 @@ func (b Metadata) Inject(ctx context.Context, carrier propagation.TextMapCarrier
// Extract returns a copy of parent with the metadata from the carrier added. // Extract returns a copy of parent with the metadata from the carrier added.
func (b Metadata) Extract(parent context.Context, carrier propagation.TextMapCarrier) context.Context { func (b Metadata) Extract(parent context.Context, carrier propagation.TextMapCarrier) context.Context {
name := carrier.Get(serviceHeader) name := carrier.Get(serviceHeader)
if name != "" { if name == "" {
if md, ok := metadata.FromServerContext(parent); ok { return parent
md.Set(serviceHeader, name)
} else {
md := metadata.New()
md.Set(serviceHeader, name)
parent = metadata.NewServerContext(parent, md)
}
} }
if md, ok := metadata.FromServerContext(parent); ok {
md.Set(serviceHeader, name)
return parent
}
md := metadata.New()
md.Set(serviceHeader, name)
parent = metadata.NewServerContext(parent, md)
return parent return parent
} }

@ -22,10 +22,12 @@ func setClientSpan(ctx context.Context, span trace.Span, m interface{}) {
var remote string var remote string
var operation string var operation string
var rpcKind string var rpcKind string
if tr, ok := transport.FromClientContext(ctx); ok { tr, ok := transport.FromClientContext(ctx)
if ok {
operation = tr.Operation() operation = tr.Operation()
rpcKind = tr.Kind().String() rpcKind = tr.Kind().String()
if tr.Kind() == transport.KindHTTP { switch tr.Kind() {
case transport.KindHTTP:
if ht, ok := tr.(http.Transporter); ok { if ht, ok := tr.(http.Transporter); ok {
method := ht.Request().Method method := ht.Request().Method
route := ht.PathTemplate() route := ht.PathTemplate()
@ -35,7 +37,7 @@ func setClientSpan(ctx context.Context, span trace.Span, m interface{}) {
attrs = append(attrs, semconv.HTTPTargetKey.String(path)) attrs = append(attrs, semconv.HTTPTargetKey.String(path))
remote = ht.Request().Host remote = ht.Request().Host
} }
} else if tr.Kind() == transport.KindGRPC { case transport.KindGRPC:
remote, _ = parseTarget(tr.Endpoint()) remote, _ = parseTarget(tr.Endpoint())
} }
} }
@ -57,10 +59,12 @@ func setServerSpan(ctx context.Context, span trace.Span, m interface{}) {
var remote string var remote string
var operation string var operation string
var rpcKind string var rpcKind string
if tr, ok := transport.FromServerContext(ctx); ok { tr, ok := transport.FromServerContext(ctx)
if ok {
operation = tr.Operation() operation = tr.Operation()
rpcKind = tr.Kind().String() rpcKind = tr.Kind().String()
if tr.Kind() == transport.KindHTTP { switch tr.Kind() {
case transport.KindHTTP:
if ht, ok := tr.(http.Transporter); ok { if ht, ok := tr.(http.Transporter); ok {
method := ht.Request().Method method := ht.Request().Method
route := ht.PathTemplate() route := ht.PathTemplate()
@ -70,7 +74,7 @@ func setServerSpan(ctx context.Context, span trace.Span, m interface{}) {
attrs = append(attrs, semconv.HTTPTargetKey.String(path)) attrs = append(attrs, semconv.HTTPTargetKey.String(path))
remote = ht.Request().RemoteAddr remote = ht.Request().RemoteAddr
} }
} else if tr.Kind() == transport.KindGRPC { case transport.KindGRPC:
if p, ok := peer.FromContext(ctx); ok { if p, ok := peer.FromContext(ctx); ok {
remote = p.Addr.String() remote = p.Addr.String()
} }

@ -22,13 +22,16 @@ func (c *ClientHandler) TagConn(ctx context.Context, cti *stats.ConnTagInfo) con
// HandleRPC implements per-RPC tracing and stats instrumentation. // HandleRPC implements per-RPC tracing and stats instrumentation.
func (c *ClientHandler) HandleRPC(ctx context.Context, rs stats.RPCStats) { func (c *ClientHandler) HandleRPC(ctx context.Context, rs stats.RPCStats) {
if _, ok := rs.(*stats.OutHeader); ok { if _, ok := rs.(*stats.OutHeader); !ok {
if p, ok := peer.FromContext(ctx); ok { return
remoteAddr := p.Addr.String() }
if span := trace.SpanFromContext(ctx); span.SpanContext().IsValid() { p, ok := peer.FromContext(ctx)
span.SetAttributes(peerAttr(remoteAddr)...) if !ok {
} return
} }
remoteAddr := p.Addr.String()
if span := trace.SpanFromContext(ctx); span.SpanContext().IsValid() {
span.SetAttributes(peerAttr(remoteAddr)...)
} }
} }

Loading…
Cancel
Save