Style:trace (#2017)

* Update metadata.go

* Update span.go

* Update statsHandler.go
pull/2021/head
songzhibin97 3 years ago committed by GitHub
parent 9e66ac2f5b
commit 92ba6a3209
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 10
      middleware/tracing/metadata.go
  2. 16
      middleware/tracing/span.go
  3. 11
      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 == "" {
return parent
}
if md, ok := metadata.FromServerContext(parent); ok { if md, ok := metadata.FromServerContext(parent); ok {
md.Set(serviceHeader, name) md.Set(serviceHeader, name)
} else { return parent
}
md := metadata.New() md := metadata.New()
md.Set(serviceHeader, name) md.Set(serviceHeader, name)
parent = metadata.NewServerContext(parent, md) 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,15 +22,18 @@ 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
}
p, ok := peer.FromContext(ctx)
if !ok {
return
}
remoteAddr := p.Addr.String() remoteAddr := p.Addr.String()
if span := trace.SpanFromContext(ctx); span.SpanContext().IsValid() { if span := trace.SpanFromContext(ctx); span.SpanContext().IsValid() {
span.SetAttributes(peerAttr(remoteAddr)...) span.SetAttributes(peerAttr(remoteAddr)...)
} }
} }
}
}
// TagRPC implements per-RPC context management. // TagRPC implements per-RPC context management.
func (c *ClientHandler) TagRPC(ctx context.Context, rti *stats.RPCTagInfo) context.Context { func (c *ClientHandler) TagRPC(ctx context.Context, rti *stats.RPCTagInfo) context.Context {

Loading…
Cancel
Save