-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatadog.go
71 lines (57 loc) · 1.73 KB
/
datadog.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
package telemetry
import (
"net"
"os"
"github.com/hashicorp/go-hclog"
"github.com/opentracing/opentracing-go"
"github.com/pkg/errors"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/opentracer"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
)
var (
missingDataDogEnvironmentVar = errors.New("missing `DD_ENV` environment vars")
missingDataDogTraceAgentPortVar = errors.New("missing `DD_TRACE_AGENT_PORT` environment vars")
missingDataDogAgentHost = errors.New("missing `DD_AGENT_HOST` environment vars")
missingApplicationReleaseVersion = errors.New("missing `DD_VERSION` environment vars")
missingServiceReleaseVersion = errors.New("missing `DD_SERVICE` environment vars")
)
type DatadogOptions struct {
Service, Env, Port, Host, Version string
}
func (o DatadogOptions) validate() error {
if o.Service == "" {
return missingServiceReleaseVersion
}
if o.Version == "" {
return missingApplicationReleaseVersion
}
if o.Env == "" {
return missingDataDogEnvironmentVar
}
if o.Port == "" {
return missingDataDogTraceAgentPortVar
}
if o.Host == "" {
return missingDataDogAgentHost
}
return nil
}
func WithDatadog(log hclog.Logger, options *DatadogOptions) TracerOption {
return func(o opentracing.Tracer) opentracing.Tracer {
if _, ok := os.LookupEnv("DATADOG_ENABLED"); !ok {
return o
}
if err := options.validate(); err != nil {
log.Error("Tracing options invalid", err)
panic(err)
}
return opentracer.New(
tracer.WithService(log.Name()),
tracer.WithServiceVersion(options.Version),
tracer.WithEnv(options.Env),
tracer.WithServiceVersion(options.Version),
tracer.WithAgentAddr(net.JoinHostPort(options.Host, options.Port)),
tracer.WithSampler(tracer.NewRateSampler(1)),
)
}
}