This repository has been archived by the owner on Sep 9, 2022. It is now read-only.
/
tracer.go
169 lines (148 loc) · 5.47 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
package echo
import (
"errors"
"fmt"
"log"
"net/http"
"time"
"github.com/labstack/echo"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
reporter "github.com/wavefronthq/wavefront-opentracing-sdk-go/reporter"
"github.com/wavefronthq/wavefront-opentracing-sdk-go/tracer"
wfTracer "github.com/wavefronthq/wavefront-opentracing-sdk-go/tracer"
wfApplication "github.com/wavefronthq/wavefront-sdk-go/application"
"github.com/wavefronthq/wavefront-sdk-go/senders"
)
// Tracer Global Tracer
var Tracer opentracing.Tracer
var globalTracerConfig *tracerConfig
// InitTracer initialize tracer object, initialize the GlobalTracer with this newly created,
// tracer object.
// Call utils/wavefront.Tracer to access the tracer object
//For proxy sender send token as empty string
func InitTracer(cfg Config) error {
//Constructing globalTracerConfig from config file
tracerConfig, err := readConfigFromYaml(cfg.CfgFile)
if err != nil {
return fmt.Errorf("error reading config file: %v", err)
}
//Registering routes
tracerConfig, err = readConfigFromYaml(cfg.RoutesFile)
if err != nil {
return fmt.Errorf("error reading routes file: %v", err)
}
var sender senders.Sender
if cfg.DirectCfg != nil {
// Create the direct sender
sender, err = senders.NewDirectSender(cfg.DirectCfg)
if err != nil {
return err
}
} else if cfg.ProxyCfg != nil {
// Create the proxy sender
sender, err = senders.NewProxySender(cfg.ProxyCfg)
if err != nil {
return err
}
} else {
return errors.New("wavefront middleware: one of Direct sender or Proxy sender configuration is required")
}
appTags := wfApplication.New(tracerConfig.Application, tracerConfig.Service)
if len(tracerConfig.Cluster) > 0 {
appTags.Cluster = tracerConfig.Cluster
}
if len(tracerConfig.Shard) > 0 {
appTags.Shard = tracerConfig.Shard
}
if tracerConfig.CustomApplicationTags.StaticTags != nil && len(tracerConfig.CustomApplicationTags.StaticTags) > 0 {
for k, v := range tracerConfig.CustomApplicationTags.StaticTags {
appTags.CustomTags[k] = v
}
}
wfReporter := reporter.New(sender, appTags, reporter.Source(tracerConfig.Source))
//Initialising tracer with RateSampler and DurationSampler if given in conifg
//If values for RateSampler and DurationSampler is not given, it initialises standard tracer with default values
var samplers []wfTracer.Option
if tracerConfig.DurationSampler > 0 {
samplers = append(samplers, wfTracer.WithSampler(tracer.DurationSampler{Duration: time.Duration(tracerConfig.DurationSampler) * time.Second}))
}
if tracerConfig.RateSampler > 0 {
samplers = append(samplers, wfTracer.WithSampler(tracer.RateSampler{Rate: tracerConfig.RateSampler}))
}
Tracer = wfTracer.New(wfReporter, samplers...)
//Instantiating Global Tracer
opentracing.SetGlobalTracer(Tracer)
//Enabling Middleware
cfg.EchoWeb.Use(TracingHandler)
log.Println("wavefront middleware: tracer initialized")
return nil
}
//StartTraceSpan looks for existing context from the headers of the request.
//If context is found, it starts a child span to the parent span,
//Else it starts a root span.
//Tags are added to the respective spans created.
//If parent span exists parent span id is returned along with child span otherwise root span and "" is returned
func StartTraceSpan(c echo.Context, appSpecificOperationName string, tags map[string]string) (opentracing.Span, string) {
var serverSpan opentracing.Span
var parentSpanID string
wireContext, err := opentracing.GlobalTracer().Extract(
opentracing.HTTPHeaders,
opentracing.HTTPHeadersCarrier(c.Request().Header))
if err != nil {
log.Println(err)
}
//Adding parent span Id if exists
sc, ok := wireContext.(tracer.SpanContext)
if ok {
parentSpanID = sc.SpanID
}
// Create the span referring to the RPC client if available.
// If wireContext == nil, a root span will be created.
serverSpan = opentracing.StartSpan(
appSpecificOperationName,
ext.RPCServerOption(wireContext))
//Populating Tags
for key, value := range tags {
if value != "" {
serverSpan.SetTag(key, value)
}
}
return serverSpan, parentSpanID
}
//InjectTracerHTTP injects the current span into the trace.
//By Adding the context of the current span to the headers of the Http request.
func InjectTracerHTTP(tracer opentracing.Tracer, serverSpan opentracing.Span, httpReq *http.Request) error {
err := tracer.Inject(
serverSpan.Context(),
opentracing.HTTPHeaders,
opentracing.HTTPHeadersCarrier(httpReq.Header))
return err
}
//GetTracingHeadersToInjectFromSpan returns the headers
//which Should be added to the request to inject from
//the current span.
//To be used when manually starting span inside process
func GetTracingHeadersToInjectFromSpan(tracer opentracing.Tracer, serverSpan opentracing.Span) map[string]string {
httpReq, _ := http.NewRequest("GET", "serviceUrl", nil)
tracer.Inject(
serverSpan.Context(),
opentracing.HTTPHeaders,
opentracing.HTTPHeadersCarrier(httpReq.Header))
headersToBeInjected := make(map[string]string, len(httpReq.Header))
for key, value := range httpReq.Header {
headersToBeInjected[key] = value[0]
}
return headersToBeInjected
}
//GetTracingHeadersToInjectFromContext returns the headers
//which Should be added to the request to inject from
//the echo context
//To be used when using middleware
func GetTracingHeadersToInjectFromContext(context echo.Context) map[string]string {
tracingHeaders := context.Get("propagationHeaders")
if tracingHeaders == nil {
return nil
}
return tracingHeaders.(map[string]string)
}