Repository: traefik/traefik
File: pkg/tracing/datadog/datadog.go (Go)

Code before:
	}

	port, ok := os.LookupEnv("DD_TRACE_AGENT_PORT")
	if !ok {
		port = "8126"
	}

	c.LocalAgentHostPort = net.JoinHostPort(host, port)
}

// Setup sets up the tracer.
func (c *Config) Setup(serviceName string) (opentracing.Tracer, io.Closer, error) {
	if c.GlobalTag != "" {
		log.WithoutContext().Warn(`Datadog: option "globalTag" is deprecated, please use "globalTags" instead.`)

		key, value, _ := strings.Cut(c.GlobalTag, ":")

		// Don't override a tag already defined with the new option.
		if _, ok := c.GlobalTags[key]; !ok {
			if c.GlobalTags == nil {
				c.GlobalTags = make(map[string]string)
			}

			c.GlobalTags[key] = value
		}
	}

	opts := []datadog.StartOption{
		datadog.WithAgentAddr(c.LocalAgentHostPort),
		datadog.WithServiceName(serviceName),
		datadog.WithDebugMode(c.Debug),
		datadog.WithPropagator(datadog.NewPropagator(&datadog.PropagatorConfig{
			TraceHeader:    c.TraceIDHeaderName,
			ParentHeader:   c.ParentIDHeaderName,
			PriorityHeader: c.SamplingPriorityHeaderName,
			BaggagePrefix:  c.BagagePrefixHeaderName,
		})),
	}

	for k, v := range c.GlobalTags {
		opts = append(opts, datadog.WithGlobalTag(k, v))
	}

	if c.PrioritySampling {
		opts = append(opts, datadog.WithPrioritySampling())
	}
	tracer := ddtracer.New(opts...)

	// Without this, child spans are getting the NOOP tracer
	opentracing.SetGlobalTracer(tracer)


Review comment (refactor):
imho, it would have been better to make them mutually exclusive as soon as possible in the flow. It is highly unlikely the user purposefully uses both options at the same time, so we would do them a favor by detecting it, telling them to clean up their conf, and stopping.
But whatever, the option is going to disappear soon anyway.

Code after:
	}

	port, ok := os.LookupEnv("DD_TRACE_AGENT_PORT")
	if !ok {
		port = "8126"
	}

	c.LocalAgentHostPort = net.JoinHostPort(host, port)
}

// Setup sets up the tracer.
func (c *Config) Setup(serviceName string) (opentracing.Tracer, io.Closer, error) {
	opts := []datadog.StartOption{
		datadog.WithAgentAddr(c.LocalAgentHostPort),
		datadog.WithServiceName(serviceName),
		datadog.WithDebugMode(c.Debug),
		datadog.WithPropagator(datadog.NewPropagator(&datadog.PropagatorConfig{
			TraceHeader:    c.TraceIDHeaderName,
			ParentHeader:   c.ParentIDHeaderName,
			PriorityHeader: c.SamplingPriorityHeaderName,
			BaggagePrefix:  c.BagagePrefixHeaderName,
		})),
	}

	for k, v := range c.GlobalTags {
		opts = append(opts, datadog.WithGlobalTag(k, v))
	}

	if c.GlobalTag != "" {
		log.WithoutContext().Warn(`Datadog: option "globalTag" is deprecated, please use "globalTags" instead.`)

		key, value, _ := strings.Cut(c.GlobalTag, ":")

		// Don't override a tag already defined with the new option.
		if _, ok := c.GlobalTags[key]; !ok {
			opts = append(opts, datadog.WithGlobalTag(key, value))
		}
	}

	if c.PrioritySampling {
		opts = append(opts, datadog.WithPrioritySampling())
	}
	tracer := ddtracer.New(opts...)

	// Without this, child spans are getting the NOOP tracer
	opentracing.SetGlobalTracer(tracer)

	log.WithoutContext().Debug("Datadog tracer configured")

	return tracer, nil, nil
}