-
Notifications
You must be signed in to change notification settings - Fork 36
/
tracing.go
119 lines (96 loc) · 3.46 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
// Copyright © 2019 Banzai Cloud
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package tracing
import (
"context"
"emperror.dev/emperror"
"go.opencensus.io/trace"
"github.com/banzaicloud/cloudinfo/internal/platform/jaeger"
)
// Tracer represents the application specific view of tracing
// It's meant to collect all tracing related operations
type Tracer interface {
// StartSpan starts a span with the given name and context
StartSpan(ctx context.Context, name string) (context.Context, *CiSpan)
// StartWithTags starts a new span and adds the tags to it as attributes
StartWithTags(ctx context.Context, name string, tags map[string]interface{}) (context.Context, *CiSpan)
// StartAndLink starts a new root span and links it with the span from the passed in context
StartAndLink(parentCtx context.Context, name string) (context.Context, *CiSpan)
// EndSpan ends a span in the given context
EndSpan(ctx context.Context)
EndSpanInstance(span *CiSpan)
}
type CiSpan struct {
*trace.Span
}
type ciTracer struct {
}
// StartAndLink starts a new root span and links it to the span in the provided context if any
func (t *ciTracer) StartAndLink(parentCtx context.Context, name string) (context.Context, *CiSpan) {
// start a new root span
newCtx, rootSpan := trace.StartSpan(context.Background(), name)
// get the spant to link from the context if any
if linkSpan := trace.FromContext(parentCtx); linkSpan != nil {
rootSpan.AddLink(trace.Link{
SpanID: linkSpan.SpanContext().SpanID,
TraceID: linkSpan.SpanContext().TraceID,
Type: trace.LinkTypeChild,
})
}
return newCtx, &CiSpan{rootSpan}
}
func (t *ciTracer) EndSpanInstance(span *CiSpan) {
span.End()
}
func (t *ciTracer) StartWithTags(ctx context.Context, name string, tags map[string]interface{}) (context.Context, *CiSpan) {
var attrs []trace.Attribute
ctx, span := t.StartSpan(ctx, name)
for k, v := range tags {
switch v.(type) {
case string:
attrs = append(attrs, trace.StringAttribute(k, v.(string)))
case bool:
attrs = append(attrs, trace.BoolAttribute(k, v.(bool)))
case int64:
attrs = append(attrs, trace.Int64Attribute(k, v.(int64)))
}
}
if len(attrs) > 0 {
span.AddAttributes(attrs...)
}
return ctx, span
}
// EndSpan ends the span in the given context
func (t *ciTracer) EndSpan(ctx context.Context) {
var span *trace.Span
if span = trace.FromContext(ctx); span == nil {
// there's no span in the context
return
}
span.End()
}
func (t *ciTracer) StartSpan(ctx context.Context, name string) (context.Context, *CiSpan) {
c, s := trace.StartSpan(ctx, name)
return c, &CiSpan{s}
}
func NewTracer() Tracer {
return &ciTracer{}
}
func SetupTracing(config jaeger.Config, errorHandler emperror.ErrorHandler) {
exporter, err := jaeger.NewExporter(config, errorHandler)
errorHandler.Handle(err)
trace.RegisterExporter(exporter)
// 2. Configure 100% sample rate, otherwise, few traces will be sampled.
trace.ApplyConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
}