-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracing.go
207 lines (177 loc) · 6.16 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Package tracing is responsible for forwarding and translating span headers for internal requests
package tracing
import (
"context"
"fmt"
"io"
"log"
"net/http"
"net/textproto"
"os"
"strings"
"time"
otnethttp "github.com/opentracing-contrib/go-stdlib/nethttp"
opentracing "github.com/opentracing/opentracing-go"
"github.com/datatrails/go-datatrails-common/environment"
"github.com/datatrails/go-datatrails-common/logger"
zipkinot "github.com/openzipkin-contrib/zipkin-go-opentracing"
zipkin "github.com/openzipkin/zipkin-go"
zipkinhttp "github.com/openzipkin/zipkin-go/reporter/http"
)
const (
requestID = "x-request-id"
otSpanContext = "x-ot-span-context"
prefixTracerState = "x-b3-"
TraceID = prefixTracerState + "traceid"
spanID = prefixTracerState + "spanid"
parentSpanID = prefixTracerState + "parentspanid"
sampled = prefixTracerState + "sampled"
flags = prefixTracerState + "flags"
)
var otHeaders = []string{
requestID,
otSpanContext,
prefixTracerState,
TraceID,
spanID,
parentSpanID,
sampled,
flags,
}
func valueFromCarrier(carrier opentracing.TextMapCarrier, key string) string {
value, found := carrier[key]
if !found || value == "" {
return ""
}
return value
}
func TraceIDFromContext(ctx context.Context) string {
span := opentracing.SpanFromContext(ctx)
if span == nil {
return ""
}
carrier := opentracing.TextMapCarrier{}
err := opentracing.GlobalTracer().Inject(span.Context(), opentracing.TextMap, carrier)
if err != nil {
return ""
}
return valueFromCarrier(carrier, TraceID)
}
func NewSpanContext(ctx context.Context, operationName string) (opentracing.Span, context.Context) {
span := opentracing.StartSpan(operationName)
if span == nil {
return nil, ctx
}
ctx = opentracing.ContextWithSpan(ctx, span)
return span, ctx
}
func StartSpanFromContext(ctx context.Context, name string, options ...opentracing.StartSpanOption) (opentracing.Span, context.Context) {
log := logger.Sugar.FromContext(ctx)
defer log.Close()
log.Debugf("tracing.StartSpanFromContext: %s", name)
tags := make(map[string]interface{})
tags["component"] = "DATATRAILS"
options = append(options, opentracing.Tags(tags))
return opentracing.StartSpanFromContext(ctx, name, options...)
}
func HTTPMiddleware(h http.Handler) http.Handler {
return otnethttp.Middleware(
opentracing.GlobalTracer(),
h,
otnethttp.OperationNameFunc(func(r *http.Request) string {
return "HTTP " + r.Method + ":" + r.URL.EscapedPath() + " >"
}),
)
}
// HeaderMatcher ensures that open tracing headers x-b3-* are forwarded to output requests
func HeaderMatcher(key string) (string, bool) {
key = textproto.CanonicalMIMEHeaderKey(key)
for _, tracingKey := range otHeaders {
if strings.ToLower(key) == tracingKey {
return key, true
}
}
return "", false
}
func trimPodName(p string) string {
a := strings.Split(p, "-")
i := len(a)
// We want the pod name without the trailing instance ID components
// There can be either two ID components (length 10 or 11 and 5) or
// just one (length 5)
if len(a[(i-1)]) == 5 && (len(a[(i-2)]) == 10 || len(a[(i-2)]) == 11) {
// this has two instnace ID components so strip them
return strings.Join(a[:i-2], "-")
}
if i > 1 {
// otherwise just strip one
return strings.Join(a[:i-1], "-")
}
return p
}
func NewTracer() io.Closer {
instanceName, _, _ := strings.Cut(environment.GetOrFatal("POD_NAME"), " ")
nameSpace := environment.GetOrFatal("POD_NAMESPACE")
containerName := environment.GetOrFatal("CONTAINER_NAME")
podName := strings.Join([]string{trimPodName(instanceName), nameSpace, containerName}, ".")
listenStr := fmt.Sprintf("localhost:%s", environment.GetOrFatal("PORT"))
return NewFromEnv(strings.TrimSpace(podName), listenStr, "ZIPKIN_ENDPOINT", "DISABLE_ZIPKIN")
}
// NewFromEnv initialises tracing and returns a closer if tracing is
// configured. If the necessary configuration is not available it is Fatal
// unless disableVar is set and is truthy (strconf.ParseBool -> true). If
// tracing is disabled returns nil
func NewFromEnv(service string, host string, endpointVar, disableVar string) io.Closer {
ze, ok := os.LookupEnv(endpointVar)
if !ok {
if disabled := environment.GetTruthyOrFatal(disableVar); !disabled {
logger.Sugar.Panicf(
"'%s' has not been provided and is not disabled by '%s'",
endpointVar, disableVar)
}
logger.Sugar.Infof("zipkin disabled by '%s'", disableVar)
return nil
}
// zipkin conf is available, disable it if disableVar is truthy
if disabled := environment.GetTruthyOrFatal(disableVar); disabled {
logger.Sugar.Infof("'%s' set, zipkin disabled", disableVar)
return nil
}
return New(service, host, ze)
}
// New initialises tracing
// uses zipkin client tracer
func New(service string, host string, zipkinEndpoint string) io.Closer {
// create our local service endpoint
localEndpoint, err := zipkin.NewEndpoint(service, host)
if err != nil {
logger.Sugar.Panicf("unable to create zipkin local endpoint service '%s' - host '%s': %v", service, host, err)
}
// set up a span reporter
zipkinLogger := log.New(os.Stdout, "zipkin", log.Ldate|log.Ltime|log.Lmicroseconds|log.Llongfile)
reporter := zipkinhttp.NewReporter(zipkinEndpoint, zipkinhttp.Logger(zipkinLogger))
// TODO: One day this should probably be configurable in helm for each service
// For now capture 1 in every 5 traces
rate := 0.2
// This sampler is only used when a service creates new traces (which is rare, only if
// not recieving messages or presenting callable endpoints, e.g. a cron like service)
sampler, err := zipkin.NewBoundarySampler(rate, time.Now().UnixNano())
if err != nil {
logger.Sugar.Panicf("unable to create zipkin sampler: rate %f: %v", rate, err)
}
// initialise the tracer
nativeTracer, err := zipkin.NewTracer(
reporter,
zipkin.WithLocalEndpoint(localEndpoint),
zipkin.WithSharedSpans(false),
zipkin.WithSampler(sampler),
)
if err != nil {
logger.Sugar.Panicf("unable to create zipkin tracer: %v", err)
}
// use zipkin-go-opentracing to wrap our tracer
tracer := zipkinot.Wrap(nativeTracer)
opentracing.SetGlobalTracer(tracer)
// logger.Plain.Core().With(zap.String("service", cfg.ServiceName),)
return reporter
}