-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtracer.go
149 lines (116 loc) · 4.49 KB
/
tracer.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package tracer
import (
"context"
"path"
"github.com/alexfalkowski/go-service/telemetry/tracer"
"github.com/alexfalkowski/go-service/transport/strings"
middleware "github.com/grpc-ecosystem/go-grpc-middleware/v2"
"go.opentelemetry.io/otel/attribute"
semconv "go.opentelemetry.io/otel/semconv/v1.25.0"
"go.opentelemetry.io/otel/trace"
"google.golang.org/grpc"
"google.golang.org/grpc/status"
)
// UnaryServerInterceptor for tracer.
func UnaryServerInterceptor(t trace.Tracer) grpc.UnaryServerInterceptor {
return func(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error) {
service := path.Dir(info.FullMethod)[1:]
if strings.IsObservable(service) {
return handler(ctx, req)
}
ctx = extract(ctx)
method := path.Base(info.FullMethod)
attrs := []attribute.KeyValue{
semconv.RPCSystemGRPC,
semconv.RPCService(service),
semconv.RPCMethod(method),
}
ctx, span := t.Start(trace.ContextWithRemoteSpanContext(ctx, trace.SpanContextFromContext(ctx)), operationName(info.FullMethod),
trace.WithSpanKind(trace.SpanKindServer), trace.WithAttributes(attrs...))
defer span.End()
ctx = tracer.WithTraceID(ctx, span)
resp, err := handler(ctx, req)
tracer.Error(err, span)
tracer.Meta(ctx, span)
span.SetAttributes(semconv.RPCGRPCStatusCodeKey.Int64(int64(status.Code(err))))
return resp, err
}
}
// StreamServerInterceptor for tracer.
func StreamServerInterceptor(t trace.Tracer) grpc.StreamServerInterceptor {
return func(srv any, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
service := path.Dir(info.FullMethod)[1:]
if strings.IsObservable(service) {
return handler(srv, stream)
}
ctx := extract(stream.Context())
method := path.Base(info.FullMethod)
attrs := []attribute.KeyValue{
semconv.RPCSystemGRPC,
semconv.RPCService(service),
semconv.RPCMethod(method),
}
ctx, span := t.Start(trace.ContextWithRemoteSpanContext(ctx, trace.SpanContextFromContext(ctx)), operationName(info.FullMethod),
trace.WithSpanKind(trace.SpanKindServer), trace.WithAttributes(attrs...))
defer span.End()
ctx = tracer.WithTraceID(ctx, span)
wrappedStream := middleware.WrapServerStream(stream)
wrappedStream.WrappedContext = ctx
err := handler(srv, wrappedStream)
tracer.Error(err, span)
tracer.Meta(ctx, span)
span.SetAttributes(semconv.RPCGRPCStatusCodeKey.Int64(int64(status.Code(err))))
return err
}
}
// UnaryClientInterceptor for tracer.
func UnaryClientInterceptor(t trace.Tracer) grpc.UnaryClientInterceptor {
return func(ctx context.Context, fullMethod string, req, resp any, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
service := path.Dir(fullMethod)[1:]
if strings.IsObservable(service) {
return invoker(ctx, fullMethod, req, resp, cc, opts...)
}
method := path.Base(fullMethod)
attrs := []attribute.KeyValue{
semconv.RPCSystemGRPC,
semconv.RPCService(service),
semconv.RPCMethod(method),
}
ctx, span := t.Start(ctx, operationName(cc.Target()+fullMethod), trace.WithSpanKind(trace.SpanKindClient), trace.WithAttributes(attrs...))
defer span.End()
ctx = tracer.WithTraceID(ctx, span)
ctx = inject(ctx)
err := invoker(ctx, fullMethod, req, resp, cc, opts...)
tracer.Error(err, span)
tracer.Meta(ctx, span)
span.SetAttributes(semconv.RPCGRPCStatusCodeKey.Int64(int64(status.Code(err))))
return err
}
}
// StreamClientInterceptor for tracer.
func StreamClientInterceptor(t trace.Tracer) grpc.StreamClientInterceptor {
return func(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, fullMethod string, streamer grpc.Streamer, opts ...grpc.CallOption) (grpc.ClientStream, error) {
service := path.Dir(fullMethod)[1:]
if strings.IsObservable(service) {
return streamer(ctx, desc, cc, fullMethod, opts...)
}
method := path.Base(fullMethod)
attrs := []attribute.KeyValue{
semconv.RPCSystemGRPC,
semconv.RPCService(service),
semconv.RPCMethod(method),
}
ctx, span := t.Start(ctx, operationName(cc.Target()+fullMethod), trace.WithSpanKind(trace.SpanKindClient), trace.WithAttributes(attrs...))
defer span.End()
ctx = tracer.WithTraceID(ctx, span)
ctx = inject(ctx)
stream, err := streamer(ctx, desc, cc, fullMethod, opts...)
tracer.Error(err, span)
tracer.Meta(ctx, span)
span.SetAttributes(semconv.RPCGRPCStatusCodeKey.Int64(int64(status.Code(err))))
return stream, err
}
}
func operationName(name string) string {
return tracer.OperationName("grpc", "get "+name)
}