/
router.go
96 lines (80 loc) · 2.43 KB
/
router.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
package instrumentation
import (
"fmt"
ddmux "gopkg.in/DataDog/dd-trace-go.v1/contrib/gorilla/mux"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
)
const (
datadogServiceSuffix = "app"
)
// Tracer is a "controller" to a ddtrace.tracer.
//
// Tracer is not exactly a "wrapper" because the tracer is a
// private/global entity in the tracer library and it's not directly
// accessible.
// - https://godoc.org/gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer#Start
// - https://github.com/DataDog/dd-trace-go/blob/v1.19.0/ddtrace/tracer/tracer.go
//
// Tracer specifies an implementation of the Datadog tracer which allows
// starting and propagating spans.
type Tracer struct {
Enabled bool
Environment string
Options []tracer.StartOption
globalServiceName string
}
// NewTracer returns a new tracer with the giver configuration and an optional
// list of ddtrace's tracer.StartOptions.
//
// NewTracer assigns universal the version of the service that is running, and will be applied to all spans,
// regardless of whether span service name and config service name match.
func NewTracer(config *Config, options ...tracer.StartOption) *Tracer {
serviceName := globalServiceName(config.ServiceName)
options = append(
options,
tracer.WithService(serviceName),
tracer.WithGlobalTag("env", config.environment),
tracer.WithUniversalVersion(config.ServiceVersion),
)
if config.RuntimeMetricsEnabled {
options = append(options, tracer.WithRuntimeMetrics())
}
if config.CodeHotspotsEnabled {
options = append(
options,
tracer.WithProfilerCodeHotspots(true),
tracer.WithProfilerEndpoints(true),
)
}
return &Tracer{
Enabled: config.Enabled,
Environment: config.environment,
globalServiceName: serviceName,
Options: options,
}
}
// Start starts the current tracer.
func (t *Tracer) Start() {
if !t.Enabled {
return
}
tracer.Start(t.Options...)
}
// Stop stops the current tracer.
func (t *Tracer) Stop() {
if !t.Enabled {
return
}
tracer.Stop()
}
// Router returns an instrumented-mux-compatible router instance traced
// with the global tracer.
//
// Returning a Router is part of the Tracer API to ensure a single entry-point
// for the instrumentation features.
func (t *Tracer) Router() *ddmux.Router {
return ddmux.NewRouter(ddmux.WithServiceName(t.globalServiceName))
}
func globalServiceName(serviceName string) string {
return fmt.Sprintf("%s-%s", serviceName, datadogServiceSuffix)
}