traefik/pkg/tracing/datadog/datadog.go

85 lines
3.9 KiB
Go
Raw Normal View History

package datadog
import (
"io"
"net"
"os"
"github.com/opentracing/opentracing-go"
2022-11-21 17:36:05 +00:00
"github.com/rs/zerolog/log"
2023-02-03 14:24:05 +00:00
"github.com/traefik/traefik/v3/pkg/logs"
ddtracer "gopkg.in/DataDog/dd-trace-go.v1/ddtrace/opentracer"
datadog "gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
)
2020-05-11 10:06:07 +00:00
// Name sets the name of this tracer.
const Name = "datadog"
2020-05-11 10:06:07 +00:00
// Config provides configuration settings for a datadog tracer.
type Config struct {
2022-11-25 09:50:06 +00:00
LocalAgentHostPort string `description:"Sets the Datadog Agent host:port." json:"localAgentHostPort,omitempty" toml:"localAgentHostPort,omitempty" yaml:"localAgentHostPort,omitempty"`
2023-03-21 11:00:46 +00:00
LocalAgentSocket string `description:"Sets the socket for the Datadog Agent." json:"localAgentSocket,omitempty" toml:"localAgentSocket,omitempty" yaml:"localAgentSocket,omitempty"`
2022-09-12 13:14:08 +00:00
GlobalTags map[string]string `description:"Sets a list of key:value tags on all spans." json:"globalTags,omitempty" toml:"globalTags,omitempty" yaml:"globalTags,omitempty" export:"true"`
Debug bool `description:"Enables Datadog debug." json:"debug,omitempty" toml:"debug,omitempty" yaml:"debug,omitempty" export:"true"`
PrioritySampling bool `description:"Enables priority sampling. When using distributed tracing, this option must be enabled in order to get all the parts of a distributed trace sampled." json:"prioritySampling,omitempty" toml:"prioritySampling,omitempty" yaml:"prioritySampling,omitempty" export:"true"`
TraceIDHeaderName string `description:"Sets the header name used to store the trace ID." json:"traceIDHeaderName,omitempty" toml:"traceIDHeaderName,omitempty" yaml:"traceIDHeaderName,omitempty" export:"true"`
ParentIDHeaderName string `description:"Sets the header name used to store the parent ID." json:"parentIDHeaderName,omitempty" toml:"parentIDHeaderName,omitempty" yaml:"parentIDHeaderName,omitempty" export:"true"`
SamplingPriorityHeaderName string `description:"Sets the header name used to store the sampling priority." json:"samplingPriorityHeaderName,omitempty" toml:"samplingPriorityHeaderName,omitempty" yaml:"samplingPriorityHeaderName,omitempty" export:"true"`
BagagePrefixHeaderName string `description:"Sets the header name prefix used to store baggage items in a map." json:"bagagePrefixHeaderName,omitempty" toml:"bagagePrefixHeaderName,omitempty" yaml:"bagagePrefixHeaderName,omitempty" export:"true"`
}
// SetDefaults sets the default values.
func (c *Config) SetDefaults() {
host, ok := os.LookupEnv("DD_AGENT_HOST")
if !ok {
host = "localhost"
}
port, ok := os.LookupEnv("DD_TRACE_AGENT_PORT")
if !ok {
port = "8126"
}
c.LocalAgentHostPort = net.JoinHostPort(host, port)
}
2020-05-11 10:06:07 +00:00
// Setup sets up the tracer.
func (c *Config) Setup(serviceName string) (opentracing.Tracer, io.Closer, error) {
2022-11-21 17:36:05 +00:00
logger := log.With().Str(logs.TracingProviderName, Name).Logger()
opts := []datadog.StartOption{
2023-10-27 07:14:05 +00:00
datadog.WithService(serviceName),
datadog.WithDebugMode(c.Debug),
datadog.WithPropagator(datadog.NewPropagator(&datadog.PropagatorConfig{
TraceHeader: c.TraceIDHeaderName,
ParentHeader: c.ParentIDHeaderName,
PriorityHeader: c.SamplingPriorityHeaderName,
BaggagePrefix: c.BagagePrefixHeaderName,
})),
2022-11-21 17:36:05 +00:00
datadog.WithLogger(logs.NewDatadogLogger(logger)),
}
2022-09-12 13:14:08 +00:00
if c.LocalAgentSocket != "" {
opts = append(opts, datadog.WithUDS(c.LocalAgentSocket))
} else {
opts = append(opts, datadog.WithAgentAddr(c.LocalAgentHostPort))
}
2022-09-12 13:14:08 +00:00
for k, v := range c.GlobalTags {
opts = append(opts, datadog.WithGlobalTag(k, v))
}
if c.PrioritySampling {
opts = append(opts, datadog.WithPrioritySampling())
}
2022-11-21 17:36:05 +00:00
tracer := ddtracer.New(opts...)
// Without this, child spans are getting the NOOP tracer
opentracing.SetGlobalTracer(tracer)
2022-11-21 17:36:05 +00:00
logger.Debug().Msg("Datadog tracer configured")
return tracer, nil, nil
}