forked from go-kratos/kratos
/
statshandler.go
43 lines (36 loc) · 1.05 KB
/
statshandler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package tracing
import (
"context"
"fmt"
"go.opentelemetry.io/otel/trace"
"google.golang.org/grpc/peer"
"google.golang.org/grpc/stats"
)
// ClientHandler is tracing ClientHandler
type ClientHandler struct{}
// HandleConn exists to satisfy gRPC stats.Handler.
func (c *ClientHandler) HandleConn(_ context.Context, _ stats.ConnStats) {
fmt.Println("Handle connection.")
}
// TagConn exists to satisfy gRPC stats.Handler.
func (c *ClientHandler) TagConn(ctx context.Context, _ *stats.ConnTagInfo) context.Context {
return ctx
}
// HandleRPC implements per-RPC tracing and stats instrumentation.
func (c *ClientHandler) HandleRPC(ctx context.Context, rs stats.RPCStats) {
if _, ok := rs.(*stats.OutHeader); !ok {
return
}
p, ok := peer.FromContext(ctx)
if !ok {
return
}
span := trace.SpanFromContext(ctx)
if span.SpanContext().IsValid() {
span.SetAttributes(peerAttr(p.Addr.String())...)
}
}
// TagRPC implements per-RPC context management.
func (c *ClientHandler) TagRPC(ctx context.Context, _ *stats.RPCTagInfo) context.Context {
return ctx
}