github.com/songzhibin97/gkit@v1.2.13/trace/stats_handler.go (about)

     1  package trace
     2  
     3  import (
     4  	"context"
     5  
     6  	"go.opentelemetry.io/otel/trace"
     7  	"google.golang.org/grpc/peer"
     8  	"google.golang.org/grpc/stats"
     9  )
    10  
    11  // ClientHandler 客户端追踪
    12  type ClientHandler struct{}
    13  
    14  // HandleConn exists to satisfy gRPC stats.Handler.
    15  func (c *ClientHandler) HandleConn(ctx context.Context, cs stats.ConnStats) {}
    16  
    17  // TagConn exists to satisfy gRPC stats.Handler.
    18  func (c *ClientHandler) TagConn(ctx context.Context, cti *stats.ConnTagInfo) context.Context {
    19  	return ctx
    20  }
    21  
    22  // HandleRPC implements per-RPC tracing and stats instrumentation.
    23  func (c *ClientHandler) HandleRPC(ctx context.Context, rs stats.RPCStats) {
    24  	if _, ok := rs.(*stats.OutHeader); ok {
    25  		if p, ok := peer.FromContext(ctx); ok {
    26  			remoteAddr := p.Addr.String()
    27  			if span := trace.SpanFromContext(ctx); span.SpanContext().IsValid() {
    28  				span.SetAttributes(peerAttr(remoteAddr)...)
    29  			}
    30  		}
    31  	}
    32  }
    33  
    34  // TagRPC implements per-RPC context management.
    35  func (c *ClientHandler) TagRPC(ctx context.Context, rti *stats.RPCTagInfo) context.Context {
    36  	return ctx
    37  }