From 72e5cf634f6b5cc0c887a1ddd3c8c401ff167bd4 Mon Sep 17 00:00:00 2001 From: Stephen Wakely Date: Tue, 5 Aug 2025 17:35:59 +0100 Subject: [PATCH] Supress external env if origin detection is configured off. --- src/DogStatsd.php | 16 +++--- tests/UnitTests/DogStatsd/SocketsTest.php | 65 +++++++++++++++++++---- 2 files changed, 65 insertions(+), 16 deletions(-) diff --git a/src/DogStatsd.php b/src/DogStatsd.php index 70ccb6c..590dcf4 100644 --- a/src/DogStatsd.php +++ b/src/DogStatsd.php @@ -155,14 +155,10 @@ public function __construct(array $config = array()) if (getenv('DD_VERSION')) { $this->globalTags['version'] = getenv('DD_VERSION'); } - // DD_EXTERNAL_ENV can be supplied by the Admission controller for origin detection. - if (getenv('DD_EXTERNAL_ENV')) { - $this->externalData = $this->sanitize(getenv('DD_EXTERNAL_ENV')); - } $this->metricPrefix = isset($config['metric_prefix']) ? "$config[metric_prefix]." : ''; - // by default the telemetry is disable + // by default the telemetry is disabled $this->disable_telemetry = isset($config["disable_telemetry"]) ? $config["disable_telemetry"] : true; $transport_type = !is_null($this->socketPath) ? "uds" : "udp"; $this->telemetry_tags = $this->serializeTags( @@ -176,6 +172,12 @@ public function __construct(array $config = array()) $originDetection = new OriginDetection(); $originDetectionEnabled = $this->isOriginDetectionEnabled($config); + + // DD_EXTERNAL_ENV can be supplied by the Admission controller for origin detection. + if ($originDetectionEnabled && getEnv('DD_EXTERNAL_ENV')) { + $this->externalData = $this->sanitize(getenv('DD_EXTERNAL_ENV')); + } + $containerID = isset($config["container_id"]) ? $config["container_id"] : ""; $this->containerID = $originDetection->getContainerID($containerID, $originDetectionEnabled); } @@ -199,8 +201,8 @@ private function isTrue($value) private function isOriginDetectionEnabled($config) { - if ((isset($config["origin_detection"]) && !$config["origin_detection"])) { - return false; + if (isset($config["origin_detection"])) { + return $config["origin_detection"]; } if (getenv("DD_ORIGIN_DETECTION_ENABLED")) { diff --git a/tests/UnitTests/DogStatsd/SocketsTest.php b/tests/UnitTests/DogStatsd/SocketsTest.php index 12d56e0..afb003e 100644 --- a/tests/UnitTests/DogStatsd/SocketsTest.php +++ b/tests/UnitTests/DogStatsd/SocketsTest.php @@ -1553,9 +1553,10 @@ public function testMetricPrefix() public function testExternalEnv() { putenv("DD_EXTERNAL_ENV=cn-SomeKindOfContainerName"); - $this->disableOriginDetectionLinux(); - $dog = new DogStatsd(array("disable_telemetry" => false)); + $dog = new DogStatsd(array("disable_telemetry" => false, + "origin_detection" => true, + "container_id" => "container")); $dog->gauge('metric', 42); $spy = $this->getSocketSpy(); $this->assertSame( @@ -1563,7 +1564,7 @@ public function testExternalEnv() count($spy->argsFromSocketSendtoCalls), 'Should send 1 UDP message' ); - $expectedUdpMessage = 'metric:42|g|e:cn-SomeKindOfContainerName'; + $expectedUdpMessage = 'metric:42|g|e:cn-SomeKindOfContainerName|c:container'; $argsPassedToSocketSendTo = $spy->argsFromSocketSendtoCalls[0]; $this->assertSameWithTelemetry( @@ -1577,9 +1578,10 @@ public function testExternalEnvInvalidCharacters() { // Environment var contains a new line and a | character.. putenv("DD_EXTERNAL_ENV=it-false,\ncn-nginx-webserver,|pu-75a2b6d5-3949-4afb-ad0d-92ff0674e759"); - $this->disableOriginDetectionLinux(); - $dog = new DogStatsd(array("disable_telemetry" => false)); + $dog = new DogStatsd(array("disable_telemetry" => false, + "origin_detection" => true, + "container_id" => "container")); $dog->gauge('metric', 42, 1.0, array('my_tag' => 'other_value')); $spy = $this->getSocketSpy(); $this->assertSame( @@ -1587,7 +1589,7 @@ public function testExternalEnvInvalidCharacters() count($spy->argsFromSocketSendtoCalls), 'Should send 1 UDP message' ); - $expectedUdpMessage = 'metric:42|g|#my_tag:other_value|e:it-false,cn-nginx-webserver,pu-75a2b6d5-3949-4afb-ad0d-92ff0674e759'; + $expectedUdpMessage = 'metric:42|g|#my_tag:other_value|e:it-false,cn-nginx-webserver,pu-75a2b6d5-3949-4afb-ad0d-92ff0674e759|c:container'; $argsPassedToSocketSendTo = $spy->argsFromSocketSendtoCalls[0]; $this->assertSameWithTelemetry( @@ -1599,10 +1601,55 @@ public function testExternalEnvInvalidCharacters() public function testExternalEnvWithTags() { - $this->disableOriginDetectionLinux(); + putenv("DD_EXTERNAL_ENV=it-false,cn-nginx-webserver,pu-75a2b6d5-3949-4afb-ad0d-92ff0674e759"); + $dog = new DogStatsd(array("disable_telemetry" => false, + "origin_detection" => true, + "container_id" => "container")); + $dog->gauge('metric', 42, 1.0, array('my_tag' => 'other_value')); + $spy = $this->getSocketSpy(); + $this->assertSame( + 1, + count($spy->argsFromSocketSendtoCalls), + 'Should send 1 UDP message' + ); + $expectedUdpMessage = 'metric:42|g|#my_tag:other_value|e:it-false,cn-nginx-webserver,pu-75a2b6d5-3949-4afb-ad0d-92ff0674e759|c:container'; + $argsPassedToSocketSendTo = $spy->argsFromSocketSendtoCalls[0]; + + $this->assertSameWithTelemetry( + $expectedUdpMessage, + $argsPassedToSocketSendTo[1], + "" + ); + } + public function testExternalEnvDisabledOriginDetection() + { putenv("DD_EXTERNAL_ENV=it-false,cn-nginx-webserver,pu-75a2b6d5-3949-4afb-ad0d-92ff0674e759"); - $dog = new DogStatsd(array("disable_telemetry" => false)); + $dog = new DogStatsd(array("disable_telemetry" => false, + "origin_detection" => false)); + $dog->gauge('metric', 42, 1.0, array('my_tag' => 'other_value')); + $spy = $this->getSocketSpy(); + $this->assertSame( + 1, + count($spy->argsFromSocketSendtoCalls), + 'Should send 1 UDP message' + ); + $expectedUdpMessage = 'metric:42|g|#my_tag:other_value'; + $argsPassedToSocketSendTo = $spy->argsFromSocketSendtoCalls[0]; + + $this->assertSameWithTelemetry( + $expectedUdpMessage, + $argsPassedToSocketSendTo[1], + "" + ); + } + + public function testExternalEnvDisabledOriginDetectionContainerID() + { + putenv("DD_EXTERNAL_ENV=it-false,cn-nginx-webserver,pu-75a2b6d5-3949-4afb-ad0d-92ff0674e759"); + $dog = new DogStatsd(array("disable_telemetry" => false, + "origin_detection" => false, + "container_id" => "container")); $dog->gauge('metric', 42, 1.0, array('my_tag' => 'other_value')); $spy = $this->getSocketSpy(); $this->assertSame( @@ -1610,7 +1657,7 @@ public function testExternalEnvWithTags() count($spy->argsFromSocketSendtoCalls), 'Should send 1 UDP message' ); - $expectedUdpMessage = 'metric:42|g|#my_tag:other_value|e:it-false,cn-nginx-webserver,pu-75a2b6d5-3949-4afb-ad0d-92ff0674e759'; + $expectedUdpMessage = 'metric:42|g|#my_tag:other_value|c:container'; $argsPassedToSocketSendTo = $spy->argsFromSocketSendtoCalls[0]; $this->assertSameWithTelemetry(