-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtracer.go
214 lines (174 loc) · 5.61 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package tracer
import (
"context"
"fmt"
"path"
"github.com/alexfalkowski/go-service/env"
"github.com/alexfalkowski/go-service/meta"
"github.com/alexfalkowski/go-service/telemetry/tracer"
"github.com/alexfalkowski/go-service/transport/strings"
"github.com/alexfalkowski/go-service/version"
middleware "github.com/grpc-ecosystem/go-grpc-middleware"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
semconv "go.opentelemetry.io/otel/semconv/v1.19.0"
"go.opentelemetry.io/otel/trace"
"go.uber.org/fx"
"google.golang.org/grpc"
"google.golang.org/grpc/status"
)
// Params for tracer.
type Params struct {
fx.In
Lifecycle fx.Lifecycle
Config *tracer.Config
Environment env.Environment
Version version.Version
}
// NewTracer for tracer.
func NewTracer(params Params) (Tracer, error) {
return tracer.NewTracer(params.Lifecycle, "grpc", params.Environment, params.Version, params.Config)
}
// Tracer for tracer.
type Tracer trace.Tracer
// UnaryServerInterceptor for tracer.
func UnaryServerInterceptor(tracer 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.IsHealth(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 := tracer.Start(
trace.ContextWithRemoteSpanContext(ctx, trace.SpanContextFromContext(ctx)),
operationName(info.FullMethod),
trace.WithSpanKind(trace.SpanKindServer),
trace.WithAttributes(attrs...),
)
defer span.End()
resp, err := handler(ctx, req)
if err != nil {
s, _ := status.FromError(err)
span.SetStatus(codes.Error, s.Message())
span.RecordError(err)
}
for k, v := range meta.Attributes(ctx) {
span.SetAttributes(attribute.Key(k).String(v))
}
span.SetAttributes(semconv.RPCGRPCStatusCodeKey.Int64(int64(status.Code(err))))
return resp, err
}
}
// StreamServerInterceptor for tracer.
func StreamServerInterceptor(tracer 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.IsHealth(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 := tracer.Start(
trace.ContextWithRemoteSpanContext(ctx, trace.SpanContextFromContext(ctx)),
operationName(info.FullMethod),
trace.WithSpanKind(trace.SpanKindServer),
trace.WithAttributes(attrs...),
)
defer span.End()
wrappedStream := middleware.WrapServerStream(stream)
wrappedStream.WrappedContext = ctx
err := handler(srv, wrappedStream)
if err != nil {
s, _ := status.FromError(err)
span.SetStatus(codes.Error, s.Message())
span.RecordError(err)
}
for k, v := range meta.Attributes(ctx) {
span.SetAttributes(attribute.Key(k).String(v))
}
span.SetAttributes(semconv.RPCGRPCStatusCodeKey.Int64(int64(status.Code(err))))
return err
}
}
// UnaryClientInterceptor for tracer.
func UnaryClientInterceptor(tracer 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.IsHealth(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 := tracer.Start(
ctx,
operationName(fullMethod),
trace.WithSpanKind(trace.SpanKindClient),
trace.WithAttributes(attrs...),
)
defer span.End()
ctx = inject(ctx)
err := invoker(ctx, fullMethod, req, resp, cc, opts...)
if err != nil {
s, _ := status.FromError(err)
span.SetStatus(codes.Error, s.Message())
span.RecordError(err)
}
for k, v := range meta.Attributes(ctx) {
span.SetAttributes(attribute.Key(k).String(v))
}
span.SetAttributes(semconv.RPCGRPCStatusCodeKey.Int64(int64(status.Code(err))))
return err
}
}
// StreamClientInterceptor for tracer.
func StreamClientInterceptor(tracer 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.IsHealth(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 := tracer.Start(
ctx,
operationName(fullMethod),
trace.WithSpanKind(trace.SpanKindClient),
trace.WithAttributes(attrs...),
)
defer span.End()
ctx = inject(ctx)
stream, err := streamer(ctx, desc, cc, fullMethod, opts...)
if err != nil {
s, _ := status.FromError(err)
span.SetStatus(codes.Error, s.Message())
span.RecordError(err)
}
for k, v := range meta.Attributes(ctx) {
span.SetAttributes(attribute.Key(k).String(v))
}
span.SetAttributes(semconv.RPCGRPCStatusCodeKey.Int64(int64(status.Code(err))))
return stream, err
}
}
func operationName(fullMethod string) string {
return fmt.Sprintf("get %s", fullMethod)
}