Repository: traefik/traefik (language: go)

Issue:
Allow use of socket for Datadog metrics
### Welcome!

- [X] Yes, I've searched similar issues on [GitHub](https://github.com/traefik/traefik/issues) and didn't find any.
- [X] Yes, I've searched similar issues on the [Traefik community forum](https://community.traefik.io) and didn't find any.

### What did you expect to see?

Basically a copy of #5502, but this issue was exclusively for 1.7 and therefore closed. When using the DataDog agent on Kubernetes, you usually have one agent per host. But on Kubernetes you can't address them via `localhost` unless you are using the host network. That's why they added the possibility to send traces via a local socket that gets mounted by the agent and the service that should send traces.

At the moment Traefik only supports setting an IP for the DataDog agent, I want to be able to send the traces to it via a UNIX socket.


Discussion / hints:

Fix patch:
diff --git a/docs/content/observability/tracing/datadog.md b/docs/content/observability/tracing/datadog.md
index 85e0ce469d..7bd346c5c6 100644
--- a/docs/content/observability/tracing/datadog.md
+++ b/docs/content/observability/tracing/datadog.md
@@ -23,24 +23,46 @@ tracing:
 
 #### `localAgentHostPort`
 
-_Required, Default="127.0.0.1:8126"_
+_Optional, Default="localhost:8126"_
 
 Local Agent Host Port instructs the reporter to send spans to the Datadog Agent at this address (host:port).
 
 ```yaml tab="File (YAML)"
 tracing:
   datadog:
-    localAgentHostPort: 127.0.0.1:8126
+    localAgentHostPort: localhost:8126
 ```
 
 ```toml tab="File (TOML)"
 [tracing]
   [tracing.datadog]
-    localAgentHostPort = "127.0.0.1:8126"
+    localAgentHostPort = "localhost:8126"
 ```
 
 ```bash tab="CLI"
---tracing.datadog.localAgentHostPort=127.0.0.1:8126
+--tracing.datadog.localAgentHostPort=localhost:8126
+```
+
+#### `localAgentSocket`
+
+_Optional, Default=""_
+
+Local Agent Socket instructs the reporter to send spans to the Datadog Agent at this UNIX socket.
+
+```yaml tab="File (YAML)"
+tracing:
+  datadog:
+    localAgentSocket: /var/run/datadog/apm.socket
+```
+
+```toml tab="File (TOML)"
+[tracing]
+  [tracing.datadog]
+    localAgentSocket = "/var/run/datadog/apm.socket"
+```
+
+```bash tab="CLI"
+--tracing.datadog.localAgentSocket=/var/run/datadog/apm.socket
 ```
 
 #### `debug`
diff --git a/docs/content/reference/static-configuration/cli-ref.md b/docs/content/reference/static-configuration/cli-ref.md
index f393c66266..80c9436aa9 100644
--- a/docs/content/reference/static-configuration/cli-ref.md
+++ b/docs/content/reference/static-configuration/cli-ref.md
@@ -990,6 +990,9 @@ Sets a list of key:value tags on all spans.
 `--tracing.datadog.localagenthostport`:  
 Sets the Datadog Agent host:port. (Default: ```localhost:8126```)
 
+`--tracing.datadog.localagentsocket`:  
+Sets the socket for the Datadog Agent.
+
 `--tracing.datadog.parentidheadername`:  
 Sets the header name used to store the parent ID.
 
diff --git a/docs/content/reference/static-configuration/env-ref.md b/docs/content/reference/static-configuration/env-ref.md
index 8a43556ca7..4edfeb4112 100644
--- a/docs/content/reference/static-configuration/env-ref.md
+++ b/docs/content/reference/static-configuration/env-ref.md
@@ -990,6 +990,9 @@ Sets a list of key:value tags on all spans.
 `TRAEFIK_TRACING_DATADOG_LOCALAGENTHOSTPORT`:  
 Sets the Datadog Agent host:port. (Default: ```localhost:8126```)
 
+`TRAEFIK_TRACING_DATADOG_LOCALAGENTSOCKET`:  
+Sets the socket for the Datadog Agent.
+
 `TRAEFIK_TRACING_DATADOG_PARENTIDHEADERNAME`:  
 Sets the header name used to store the parent ID.
 
diff --git a/docs/content/reference/static-configuration/file.toml b/docs/content/reference/static-configuration/file.toml
index 3ad3d95c59..bf3d13125d 100644
--- a/docs/content/reference/static-configuration/file.toml
+++ b/docs/content/reference/static-configuration/file.toml
@@ -365,6 +365,7 @@
     sampleRate = 42.0
   [tracing.datadog]
     localAgentHostPort = "foobar"
+    localAgentSocket = "foobar"
     globalTag = "foobar"
     [tracing.datadog.globalTags]
       tag1 = "foobar"
diff --git a/docs/content/reference/static-configuration/file.yaml b/docs/content/reference/static-configuration/file.yaml
index dbe316d4ad..b323f8b724 100644
--- a/docs/content/reference/static-configuration/file.yaml
+++ b/docs/content/reference/static-configuration/file.yaml
@@ -389,6 +389,7 @@ tracing:
     sampleRate: 42
   datadog:
     localAgentHostPort: foobar
+    localAgentSocket: foobar
     globalTag: foobar
     globalTags:
       tag1: foobar
diff --git a/pkg/config/dynamic/fixtures/sample.toml b/pkg/config/dynamic/fixtures/sample.toml
index fdde014c76..460912a990 100644
--- a/pkg/config/dynamic/fixtures/sample.toml
+++ b/pkg/config/dynamic/fixtures/sample.toml
@@ -171,6 +171,7 @@
     samplingType = "foobar"
     samplingParam = 42.0
     localAgentHostPort = "foobar"
+    localAgentSocket = "foobar"
     gen128Bit = true
     propagation = "foobar"
     traceContextHeaderName = "foobar"
@@ -182,6 +183,7 @@
     sampleRate = 42.0
   [tracing.datadog]
     localAgentHostPort = "foobar"
+    localAgentSocket = "foobar"
     globalTag = "foobar"
     debug = true
     prioritySampling = true
diff --git a/pkg/tracing/datadog/datadog.go b/pkg/tracing/datadog/datadog.go
index f8878943fa..ffde8d7872 100644
--- a/pkg/tracing/datadog/datadog.go
+++ b/pkg/tracing/datadog/datadog.go
@@ -18,6 +18,7 @@ const Name = "datadog"
 // Config provides configuration settings for a datadog tracer.
 type Config struct {
 	LocalAgentHostPort string `description:"Sets the Datadog Agent host:port." json:"localAgentHostPort,omitempty" toml:"localAgentHostPort,omitempty" yaml:"localAgentHostPort,omitempty"`
+	LocalAgentSocket   string `description:"Sets the socket for the Datadog Agent." json:"localAgentSocket,omitempty" toml:"localAgentSocket,omitempty" yaml:"localAgentSocket,omitempty"`
 	// Deprecated: use GlobalTags instead.
 	GlobalTag                  string            `description:"Sets a key:value tag on all spans." json:"globalTag,omitempty" toml:"globalTag,omitempty" yaml:"globalTag,omitempty" export:"true"`
 	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"`
@@ -47,7 +48,6 @@ func (c *Config) SetDefaults() {
 // 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{
@@ -58,6 +58,12 @@ func (c *Config) Setup(serviceName string) (opentracing.Tracer, io.Closer, error
 		})),
 	}
 
+	if c.LocalAgentSocket != "" {
+		opts = append(opts, datadog.WithUDS(c.LocalAgentSocket))
+	} else {
+		opts = append(opts, datadog.WithAgentAddr(c.LocalAgentHostPort))
+	}
+
 	for k, v := range c.GlobalTags {
 		opts = append(opts, datadog.WithGlobalTag(k, v))
 	}


Test patch:
diff --git a/pkg/redactor/redactor_config_test.go b/pkg/redactor/redactor_config_test.go
index a3f7504aae..368f2d9564 100644
--- a/pkg/redactor/redactor_config_test.go
+++ b/pkg/redactor/redactor_config_test.go
@@ -899,6 +899,7 @@ func TestDo_staticConfiguration(t *testing.T) {
 		},
 		Datadog: &datadog.Config{
 			LocalAgentHostPort:         "foobar",
+			LocalAgentSocket:           "foobar",
 			GlobalTag:                  "foobar",
 			Debug:                      true,
 			PrioritySampling:           true,
diff --git a/pkg/redactor/testdata/anonymized-static-config.json b/pkg/redactor/testdata/anonymized-static-config.json
index 79ca15a67f..09e0d796d8 100644
--- a/pkg/redactor/testdata/anonymized-static-config.json
+++ b/pkg/redactor/testdata/anonymized-static-config.json
@@ -390,6 +390,7 @@
     },
     "datadog": {
       "localAgentHostPort": "xxxx",
+      "localAgentSocket": "xxxx",
       "globalTag": "foobar",
       "debug": true,
       "prioritySampling": true,