-
Notifications
You must be signed in to change notification settings - Fork 1
/
tracing.go
191 lines (162 loc) · 4.74 KB
/
tracing.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
package totem
import (
"context"
"net"
"os"
"strconv"
"strings"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
otelcodes "go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/sdk/resource"
tracesdk "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.12.0"
"go.opentelemetry.io/otel/trace"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/peer"
"google.golang.org/grpc/status"
)
const TracerName = "totem"
func TracerProvider(opts ...resource.Option) (_tp trace.TracerProvider) {
defer func() {
if _tp == nil {
_tp = otel.GetTracerProvider()
}
}()
if !TracingEnabled {
return nil
}
resourceOpts := []resource.Option{
resource.WithFromEnv(),
resource.WithTelemetrySDK(),
}
var exporter tracesdk.SpanExporter
switch os.Getenv("OTEL_TRACES_EXPORTER") {
case "otlp":
var err error
exporter, err = otlptracegrpc.New(context.Background())
if err != nil {
return nil
}
default:
return nil
}
res, err := resource.New(context.Background(), append(resourceOpts, opts...)...)
if err != nil {
return nil
}
return tracesdk.NewTracerProvider(tracesdk.WithResource(res), tracesdk.WithBatcher(exporter))
}
// Controls whether or not tracing is enabled. Must only be set once at
// startup. Defaults to false.
var TracingEnabled = false
func init() {
if v, err := strconv.ParseBool(os.Getenv("TOTEM_TRACING_ENABLED")); err == nil {
TracingEnabled = v
}
}
// internal helper methods from otelgrpc below
// -------------------------------------------
// spanInfo returns a span name and all appropriate attributes from the gRPC
// method and peer address.
func spanInfo(fullMethod, peerAddress string) (string, []attribute.KeyValue) {
attrs := []attribute.KeyValue{otelgrpc.RPCSystemGRPC}
name, mAttrs := parseFullMethod(fullMethod)
attrs = append(attrs, mAttrs...)
attrs = append(attrs, peerAttr(peerAddress)...)
return name, attrs
}
// peerAttr returns attributes about the peer address.
func peerAttr(addr string) []attribute.KeyValue {
host, port, err := net.SplitHostPort(addr)
if err != nil {
return []attribute.KeyValue(nil)
}
if host == "" {
host = "127.0.0.1"
}
return []attribute.KeyValue{
semconv.NetPeerIPKey.String(host),
semconv.NetPeerPortKey.String(port),
}
}
// peerFromCtx returns a peer address from a context, if one exists.
func peerFromCtx(ctx context.Context) string {
p, ok := peer.FromContext(ctx)
if !ok {
return ""
}
return p.Addr.String()
}
// ParseFullMethod returns a span name following the OpenTelemetry semantic
// conventions as well as all applicable span attribute.KeyValue attributes based
// on a gRPC's FullMethod.
func parseFullMethod(fullMethod string) (string, []attribute.KeyValue) {
name := strings.TrimLeft(fullMethod, "/")
parts := strings.SplitN(name, "/", 2)
if len(parts) != 2 {
// Invalid format, does not follow `/package.service/method`.
return name, []attribute.KeyValue(nil)
}
var attrs []attribute.KeyValue
if service := parts[0]; service != "" {
attrs = append(attrs, semconv.RPCServiceKey.String(service))
}
if method := parts[1]; method != "" {
attrs = append(attrs, semconv.RPCMethodKey.String(method))
}
return name, attrs
}
// statusCodeAttr returns status code attribute based on given gRPC code
func statusCodeAttr(c codes.Code) attribute.KeyValue {
return otelgrpc.GRPCStatusCodeKey.Int64(int64(c))
}
func recordErrorStatus(span trace.Span, stat *status.Status) {
if !TracingEnabled {
return
}
span.SetAttributes(statusCodeAttr(stat.Code()))
span.SetStatus(otelcodes.Error, stat.Message())
span.RecordError(stat.Err())
}
func recordError(span trace.Span, err error) {
if !TracingEnabled {
return
}
span.SetAttributes(statusCodeAttr(status.Code(err)))
span.SetStatus(otelcodes.Error, err.Error())
span.RecordError(err)
}
func recordSuccess(span trace.Span) {
if !TracingEnabled {
return
}
span.SetAttributes(statusCodeAttr(codes.OK))
}
// unexported code copied from otelgrpc/metadata_supplier.go
type metadataSupplier struct {
metadata *metadata.MD
}
// assert that metadataSupplier implements the TextMapCarrier interface.
var _ propagation.TextMapCarrier = &metadataSupplier{}
func (s *metadataSupplier) Get(key string) string {
values := s.metadata.Get(key)
if len(values) == 0 {
return ""
}
return values[0]
}
func (s *metadataSupplier) Set(key string, value string) {
s.metadata.Set(key, value)
}
func (s *metadataSupplier) Keys() []string {
out := make([]string, 0, len(*s.metadata))
for key := range *s.metadata {
out = append(out, key)
}
return out
}