-
Notifications
You must be signed in to change notification settings - Fork 288
/
tracer.go
79 lines (61 loc) · 1.9 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
package tracer
import (
"context"
"fmt"
"log"
"strings"
"github.com/windmilleng/tilt/internal/logger"
"github.com/pkg/errors"
"github.com/opentracing/opentracing-go"
zipkin "github.com/openzipkin/zipkin-go-opentracing"
)
const windmillTracerHostPort = "opentracing.windmill.build:9411"
type zipkinLogger struct {
ctx context.Context
}
func (zl zipkinLogger) Log(keyvals ...interface{}) error {
logger.Get(zl.ctx).Debugf("%v", keyvals)
return nil
}
var _ zipkin.Logger = zipkinLogger{}
func Init(ctx context.Context) (func() error, error) {
collector, err := zipkin.NewHTTPCollector(fmt.Sprintf("http://%s/api/v1/spans", windmillTracerHostPort), zipkin.HTTPLogger(zipkinLogger{ctx}))
if err != nil {
return nil, errors.Wrap(err, "unable to create zipkin collector")
}
recorder := zipkin.NewRecorder(collector, true, "0.0.0.0:0", "tilt")
tracer, err := zipkin.NewTracer(recorder)
if err != nil {
return nil, errors.Wrap(err, "unable to create tracer")
}
opentracing.SetGlobalTracer(tracer)
return collector.Close, nil
}
func TraceID(ctx context.Context) (string, error) {
spanContext := opentracing.SpanFromContext(ctx)
if spanContext == nil {
return "", errors.New("cannot get traceid - there is no span context")
}
zipkinSpanContext, ok := spanContext.Context().(zipkin.SpanContext)
if !ok {
return "", errors.New("cannot get traceid - span context was not a zipkin span context")
}
return zipkinSpanContext.TraceID.ToHex(), nil
}
// TagStrToMap converts a user-passed string of tags of the form `key1=val1,key2=val2` to a map.
func TagStrToMap(tagStr string) map[string]string {
if tagStr == "" {
return nil
}
res := make(map[string]string)
pairs := strings.Split(tagStr, ",")
for _, p := range pairs {
elems := strings.Split(strings.TrimSpace(p), "=")
if len(elems) != 2 {
log.Printf("got malformed trace tag: %s", p)
continue
}
res[elems[0]] = elems[1]
}
return res
}