Skip to content

Commit 1139efe

Browse files
authored
Merge pull request #300 from DataDog/ali/fix-origin-detection
[CONTINT-3864] Send both origin the container-id and the ENTITY_ID
2 parents 7e9cde5 + 8c18762 commit 1139efe

File tree

4 files changed

+26
-40
lines changed

4 files changed

+26
-40
lines changed

statsd/end_to_end_udp_test.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,10 @@ func TestContainerIDWithEntityID(t *testing.T) {
110110
resetContainerID()
111111

112112
entityIDEnvName := "DD_ENTITY_ID"
113-
defer func() { os.Unsetenv(entityIDEnvName) }()
113+
defer func() {
114+
os.Unsetenv(entityIDEnvName)
115+
resetContainerID()
116+
}()
114117
os.Setenv(entityIDEnvName, "pod-uid")
115118

116119
expectedTags := []string{"dd.internal.entity_id:pod-uid"}
@@ -123,14 +126,18 @@ func TestContainerIDWithEntityID(t *testing.T) {
123126

124127
sort.Strings(client.tags)
125128
assert.Equal(t, expectedTags, client.tags)
126-
ts.assertContainerID(t, "")
129+
ts.assertContainerID(t, "fake-container-id")
127130
ts.sendAllAndAssert(t, client)
128131
}
129132

130133
func TestContainerIDWithoutEntityID(t *testing.T) {
131134
resetContainerID()
132135
os.Unsetenv("DD_ENTITY_ID")
133136

137+
defer func() {
138+
resetContainerID()
139+
}()
140+
134141
ts, client := newClientAndTestServer(t,
135142
"udp",
136143
"localhost:8765",
@@ -164,7 +171,10 @@ func TestOriginDetectionEnabledWithEntityID(t *testing.T) {
164171
resetContainerID()
165172

166173
entityIDEnvName := "DD_ENTITY_ID"
167-
defer func() { os.Unsetenv(entityIDEnvName) }()
174+
defer func() {
175+
os.Unsetenv(entityIDEnvName)
176+
resetContainerID()
177+
}()
168178
os.Setenv(entityIDEnvName, "pod-uid")
169179

170180
originDetectionEnvName := "DD_ORIGIN_DETECTION_ENABLED"
@@ -181,7 +191,7 @@ func TestOriginDetectionEnabledWithEntityID(t *testing.T) {
181191

182192
sort.Strings(client.tags)
183193
assert.Equal(t, expectedTags, client.tags)
184-
ts.assertContainerID(t, "")
194+
ts.assertContainerID(t, "fake-container-id")
185195
ts.sendAllAndAssert(t, client)
186196
}
187197

statsd/options.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -373,9 +373,10 @@ func WithTelemetryAddr(addr string) Option {
373373
// WithoutOriginDetection disables the client origin detection.
374374
// When enabled, the client tries to discover its container ID and sends it to the Agent
375375
// to enrich the metrics with container tags.
376+
// If the container id is not found and the client is running in a private cgroup namespace, the client
377+
// sends the base cgroup controller inode.
376378
// Origin detection can also be disabled by configuring the environment variabe DD_ORIGIN_DETECTION_ENABLED=false
377379
// The client tries to read the container ID by parsing the file /proc/self/cgroup, this is not supported on Windows.
378-
// The client prioritizes the value passed via DD_ENTITY_ID (if set) over the container ID.
379380
//
380381
// More on this here: https://docs.datadoghq.com/developers/dogstatsd/?tab=kubernetes#origin-detection-over-udp
381382
func WithoutOriginDetection() Option {
@@ -389,9 +390,9 @@ func WithoutOriginDetection() Option {
389390
// This feature requires Datadog Agent version >=6.35.0 && <7.0.0 or Agent versions >=7.35.0.
390391
// When enabled, the client tries to discover its container ID and sends it to the Agent
391392
// to enrich the metrics with container tags.
392-
// Origin detection can be disabled by configuring the environment variabe DD_ORIGIN_DETECTION_ENABLED=false
393-
// The client tries to read the container ID by parsing the file /proc/self/cgroup, this is not supported on Windows.
394-
// The client prioritizes the value passed via DD_ENTITY_ID (if set) over the container ID.
393+
// If the container id is not found and the client is running in a private cgroup namespace, the client
394+
// sends the base cgroup controller inode.
395+
// Origin detection can be disabled by configuring the environment variable DD_ORIGIN_DETECTION_ENABLED=false
395396
//
396397
// More on this here: https://docs.datadoghq.com/developers/dogstatsd/?tab=kubernetes#origin-detection-over-udp
397398
func WithOriginDetection() Option {

statsd/statsd.go

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -454,21 +454,14 @@ func newWithWriter(w Transport, o *Options, writerName string) (*Client, error)
454454
errorHandler: o.errorHandler,
455455
}
456456

457-
hasEntityID := false
458457
// Inject values of DD_* environment variables as global tags.
459458
for _, mapping := range ddEnvTagsMapping {
460459
if value := os.Getenv(mapping.envName); value != "" {
461-
if mapping.envName == ddEntityID {
462-
hasEntityID = true
463-
}
464460
c.tags = append(c.tags, fmt.Sprintf("%s:%s", mapping.tagName, value))
465461
}
466462
}
467463

468-
if !hasEntityID {
469-
initContainerID(o.containerID, isOriginDetectionEnabled(o, hasEntityID), isHostCgroupNamespace())
470-
}
471-
464+
initContainerID(o.containerID, isOriginDetectionEnabled(o), isHostCgroupNamespace())
472465
isUDS := writerName == writerNameUDS
473466

474467
if o.maxBytesPerPayload == 0 {
@@ -888,16 +881,11 @@ func (c *Client) Close() error {
888881

889882
// isOriginDetectionEnabled returns whether the clients should fill the container field.
890883
//
891-
// If DD_ENTITY_ID is set, we don't send the container ID
892-
// If a user-defined container ID is provided, we don't ignore origin detection
893-
// as dd.internal.entity_id is prioritized over the container field for backward compatibility.
894-
// If DD_ENTITY_ID is not set, we try to fill the container field automatically unless
895-
// DD_ORIGIN_DETECTION_ENABLED is explicitly set to false.
896-
func isOriginDetectionEnabled(o *Options, hasEntityID bool) bool {
897-
if !o.originDetection || hasEntityID || o.containerID != "" {
898-
// originDetection is explicitly disabled
899-
// or DD_ENTITY_ID was found
900-
// or a user-defined container ID was provided
884+
// Disable origin detection only in one of the following cases:
885+
// - DD_ORIGIN_DETECTION_ENABLED is explicitly set to false
886+
// - o.originDetection is explicitly set to false, which is true by default
887+
func isOriginDetectionEnabled(o *Options) bool {
888+
if !o.originDetection || o.containerID != "" {
901889
return false
902890
}
903891

statsd/statsd_test.go

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -311,49 +311,36 @@ func Test_isOriginDetectionEnabled(t *testing.T) {
311311
tests := []struct {
312312
name string
313313
o *Options
314-
hasEntityID bool
315314
configEnvVarValue string
316315
want bool
317316
}{
318317
{
319318
name: "nominal case",
320319
o: &Options{originDetection: defaultOriginDetection},
321-
hasEntityID: false,
322320
configEnvVarValue: "",
323321
want: true,
324322
},
325-
{
326-
name: "has entity ID",
327-
o: &Options{originDetection: defaultOriginDetection},
328-
hasEntityID: true,
329-
configEnvVarValue: "",
330-
want: false,
331-
},
332323
{
333324
name: "has user-provided container ID",
334325
o: &Options{containerID: "user-provided"},
335-
hasEntityID: true,
336326
configEnvVarValue: "",
337327
want: false,
338328
},
339329
{
340330
name: "originDetection option disabled",
341331
o: &Options{originDetection: false},
342-
hasEntityID: false,
343332
configEnvVarValue: "",
344333
want: false,
345334
},
346335
{
347336
name: "DD_ORIGIN_DETECTION_ENABLED=false",
348337
o: &Options{originDetection: defaultOriginDetection},
349-
hasEntityID: false,
350338
configEnvVarValue: "false",
351339
want: false,
352340
},
353341
{
354342
name: "invalid DD_ORIGIN_DETECTION_ENABLED value",
355343
o: &Options{originDetection: defaultOriginDetection},
356-
hasEntityID: false,
357344
configEnvVarValue: "invalid",
358345
want: true,
359346
},
@@ -363,7 +350,7 @@ func Test_isOriginDetectionEnabled(t *testing.T) {
363350
os.Setenv("DD_ORIGIN_DETECTION_ENABLED", tt.configEnvVarValue)
364351
defer os.Unsetenv("DD_ORIGIN_DETECTION_ENABLED")
365352

366-
assert.Equal(t, tt.want, isOriginDetectionEnabled(tt.o, tt.hasEntityID))
353+
assert.Equal(t, tt.want, isOriginDetectionEnabled(tt.o))
367354
})
368355
}
369356
}

0 commit comments

Comments
 (0)