From a50ba2c745883fb1a9fdec1444fcf7ba934d0b39 Mon Sep 17 00:00:00 2001 From: Stephen Wakely Date: Thu, 3 Jul 2025 09:50:03 +0100 Subject: [PATCH 01/12] Add Windows to the matrix --- .github/workflows/test.yaml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 8b6b6b9..056968e 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -12,9 +12,10 @@ permissions: jobs: build: - runs-on: ubuntu-latest + runs-on: ${{ matrix.os }} strategy: matrix: + os: [ubuntu-latest, windows-latest] php-versions: ['5.6', '7.0', '7.1', '7.2', '7.3', '7.4', '8.0', '8.2', '8.3'] steps: From 1f9823f611b03f979c1804f8f7cf576c43f8f841 Mon Sep 17 00:00:00 2001 From: Stephen Wakely Date: Thu, 3 Jul 2025 10:00:40 +0100 Subject: [PATCH 02/12] Dont disable origin detection for windows --- tests/UnitTests/DogStatsd/SocketsTest.php | 127 ++++++++++++++-------- 1 file changed, 84 insertions(+), 43 deletions(-) diff --git a/tests/UnitTests/DogStatsd/SocketsTest.php b/tests/UnitTests/DogStatsd/SocketsTest.php index fc32a93..1d62d16 100644 --- a/tests/UnitTests/DogStatsd/SocketsTest.php +++ b/tests/UnitTests/DogStatsd/SocketsTest.php @@ -155,7 +155,8 @@ public function testHostAndPortFromEnvVar() { putenv("DD_AGENT_HOST=myenvvarhost"); putenv("DD_DOGSTATSD_PORT=1234"); - putenv("DD_ORIGIN_DETECTION_ENABLED=false"); + $this->disableOriginDetectionNonWindows(); + $dog = new DogStatsd(); $this->assertSame( 'myenvvarhost', @@ -173,7 +174,7 @@ public function testHostAndPortFromArgs() { putenv("DD_AGENT_HOST=myenvvarhost"); putenv("DD_DOGSTATSD_PORT=1234"); - putenv("DD_ORIGIN_DETECTION_ENABLED=false"); + $this->disableOriginDetectionNonWindows(); $dog = new DogStatsd(array( 'host' => 'myhost', 'port' => 4321 @@ -193,7 +194,7 @@ public function testHostAndPortFromArgs() public function testHostAndPortFromUrl() { putenv("DD_DOGSTATSD_URL=udp://oh.my.address:1234"); - putenv("DD_ORIGIN_DETECTION_ENABLED=false"); + $this->disableOriginDetectionNonWindows(); $dog = new DogStatsd(); $this->assertSame( 'oh.my.address', @@ -317,9 +318,16 @@ public function testMicrotiming() ); } + function disableOriginDetectionNonWindows() { + if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') { + putenv("DD_ORIGIN_DETECTION_ENABLED=false"); + } + } + public function testGauge() { - putenv("DD_ORIGIN_DETECTION_ENABLED=false"); + $this->disableOriginDetectionNonWindows(); + $stat = 'some.gauge_metric'; $value = 5; $sampleRate = 1.0; @@ -353,7 +361,8 @@ public function testGauge() public function testGaugeZero() { - putenv("DD_ORIGIN_DETECTION_ENABLED=false"); + $this->disableOriginDetectionNonWindows(); + $stat = 'some.gauge_metric'; $value = 0; $sampleRate = 1.0; @@ -387,7 +396,8 @@ public function testGaugeZero() public function testHistogram() { - putenv("DD_ORIGIN_DETECTION_ENABLED=false"); + $this->disableOriginDetectionNonWindows(); + $stat = 'some.histogram_metric'; $value = 109; $sampleRate = 1.0; @@ -421,7 +431,8 @@ public function testHistogram() public function testDistribution() { - putenv("DD_ORIGIN_DETECTION_ENABLED=false"); + $this->disableOriginDetectionNonWindows(); + $stat = 'some.distribution_metric'; $value = 7; $sampleRate = 1.0; @@ -463,7 +474,8 @@ public function testDistribution() */ public function testSet($stat, $value, $sampleRate, $tags, $expectedUdpMessage) { - putenv("DD_ORIGIN_DETECTION_ENABLED=false"); + $this->disableOriginDetectionNonWindows(); + $dog = new DogStatsd(array("disable_telemetry" => false)); $dog->set( @@ -535,7 +547,8 @@ public function testServiceCheck( $timestamp, $expectedUdpMessage ) { - putenv("DD_ORIGIN_DETECTION_ENABLED=false"); + $this->disableOriginDetectionNonWindows(); + $dog = new DogStatsd(array("disable_telemetry" => false)); $dog->service_check( @@ -561,7 +574,8 @@ public function testServiceCheck( public function serviceCheckProvider() { - putenv("DD_ORIGIN_DETECTION_ENABLED=false"); + $this->disableOriginDetectionNonWindows(); + $name = 'neat-service'; $status = DogStatsd::CRITICAL; $tags = array('red' => 'balloon', 'green' => 'ham'); @@ -620,7 +634,8 @@ public function serviceCheckProvider() public function testSend() { - putenv("DD_ORIGIN_DETECTION_ENABLED=false"); + $this->disableOriginDetectionNonWindows(); + $sampleRate = 1.0; $tags = array( 'cowboy' => 'hat' @@ -661,7 +676,8 @@ public function testSend() public function testSendSerializesTagAsString() { - putenv("DD_ORIGIN_DETECTION_ENABLED=false"); + $this->disableOriginDetectionNonWindows(); + $data = array( 'foo.metric' => '82|s', ); @@ -685,7 +701,8 @@ public function testSendSerializesTagAsString() public function testSendSerializesMessageWithoutTags() { - putenv("DD_ORIGIN_DETECTION_ENABLED=false"); + $this->disableOriginDetectionNonWindows(); + $data = array( 'foo.metric' => '19872|h', ); @@ -709,7 +726,8 @@ public function testSendSerializesMessageWithoutTags() public function testSendReturnsEarlyWhenPassedEmptyData() { - putenv("DD_ORIGIN_DETECTION_ENABLED=false"); + $this->disableOriginDetectionNonWindows(); + $dog = new DogStatsd(array("disable_telemetry" => false)); $dog->send(array()); @@ -725,7 +743,8 @@ public function testSendReturnsEarlyWhenPassedEmptyData() public function testSendSendsWhenRandCalculationLessThanSampleRate() { - putenv("DD_ORIGIN_DETECTION_ENABLED=false"); + $this->disableOriginDetectionNonWindows(); + global $mt_rand_stub_return_value; global $mt_getrandmax_stub_return_value; @@ -753,7 +772,8 @@ public function testSendSendsWhenRandCalculationLessThanSampleRate() public function testSendSendsWhenRandCalculationEqualToSampleRate() { - putenv("DD_ORIGIN_DETECTION_ENABLED=false"); + $this->disableOriginDetectionNonWindows(); + global $mt_rand_stub_return_value; global $mt_getrandmax_stub_return_value; @@ -781,7 +801,8 @@ public function testSendSendsWhenRandCalculationEqualToSampleRate() public function testSendDoesNotSendWhenRandCalculationGreaterThanSampleRate() { - putenv("DD_ORIGIN_DETECTION_ENABLED=false"); + $this->disableOriginDetectionNonWindows(); + global $mt_rand_stub_return_value; global $mt_getrandmax_stub_return_value; @@ -809,7 +830,8 @@ public function testSendDoesNotSendWhenRandCalculationGreaterThanSampleRate() public function testIncrement() { - putenv("DD_ORIGIN_DETECTION_ENABLED=false"); + $this->disableOriginDetectionNonWindows(); + $stats = array( 'foo.metric', ); @@ -839,7 +861,7 @@ public function testIncrement() public function testDecrement() { - putenv("DD_ORIGIN_DETECTION_ENABLED=false"); + $this->disableOriginDetectionNonWindows(); $stats = array( 'foo.metric', @@ -870,7 +892,7 @@ public function testDecrement() public function testDecrementWithValueGreaterThanOne() { - putenv("DD_ORIGIN_DETECTION_ENABLED=false"); + $this->disableOriginDetectionNonWindows(); $stats = array( 'foo.metric', @@ -901,7 +923,7 @@ public function testDecrementWithValueGreaterThanOne() public function testDecrementWithValueLessThanOne() { - putenv("DD_ORIGIN_DETECTION_ENABLED=false"); + $this->disableOriginDetectionNonWindows(); $stats = array( 'foo.metric', @@ -932,7 +954,7 @@ public function testDecrementWithValueLessThanOne() public function testUpdateStats() { - putenv("DD_ORIGIN_DETECTION_ENABLED=false"); + $this->disableOriginDetectionNonWindows(); $stats = array( 'foo.metric', @@ -983,7 +1005,7 @@ public function testUpdateStats() public function testUpdateStatsWithStringMetric() { - putenv("DD_ORIGIN_DETECTION_ENABLED=false"); + $this->disableOriginDetectionNonWindows(); $stats = 'foo.metric'; $delta = -45; @@ -1040,7 +1062,8 @@ public function testReport() public function testFlushUdp() { - putenv("DD_ORIGIN_DETECTION_ENABLED=false"); + $this->disableOriginDetectionNonWindows(); + $expectedUdpMessage = 'foo'; $dog = new DogStatsd(array("disable_telemetry" => true)); @@ -1109,7 +1132,8 @@ public function testFlushUdp() public function testFlushUds() { - putenv("DD_ORIGIN_DETECTION_ENABLED=false"); + $this->disableOriginDetectionNonWindows(); + $expectedUdsMessage = 'foo'; $expectedUdsSocketPath = '/path/to/some.socket'; @@ -1179,7 +1203,8 @@ public function testFlushUds() public function testEventUdp() { - putenv("DD_ORIGIN_DETECTION_ENABLED=false"); + $this->disableOriginDetectionNonWindows(); + $eventTitle = 'Some event title'; $eventVals = array( 'text' => "Some event text\nthat spans 2 lines", @@ -1226,7 +1251,8 @@ public function testEventUdp() */ public function testEventUdpWithEmptyValues() { - putenv("DD_ORIGIN_DETECTION_ENABLED=false"); + $this->disableOriginDetectionNonWindows(); + $eventTitle = ''; $expectedUdpMessage = "_e{0,0}:|"; @@ -1255,7 +1281,8 @@ public function testEventUdpWithEmptyValues() public function testGlobalTags() { - putenv("DD_ORIGIN_DETECTION_ENABLED=false"); + $this->disableOriginDetectionNonWindows(); + $dog = new DogStatsd(array( 'global_tags' => array( 'my_tag' => 'tag_value', @@ -1282,7 +1309,8 @@ public function testGlobalTags() public function testGlobalTagsWithEntityIdFromEnvVar() { - putenv("DD_ORIGIN_DETECTION_ENABLED=false"); + $this->disableOriginDetectionNonWindows(); + putenv("DD_ENTITY_ID=04652bb7-19b7-11e9-9cc6-42010a9c016d"); $dog = new DogStatsd(array( 'global_tags' => array( @@ -1310,7 +1338,8 @@ public function testGlobalTagsWithEntityIdFromEnvVar() public function testGlobalTagsAreSupplementedWithLocalTags() { - putenv("DD_ORIGIN_DETECTION_ENABLED=false"); + $this->disableOriginDetectionNonWindows(); + $dog = new DogStatsd(array( 'global_tags' => array( 'my_tag' => 'tag_value', @@ -1338,7 +1367,8 @@ public function testGlobalTagsAreSupplementedWithLocalTags() public function testGlobalTagsAreReplacedWithConflictingLocalTags() { - putenv("DD_ORIGIN_DETECTION_ENABLED=false"); + $this->disableOriginDetectionNonWindows(); + $dog = new DogStatsd(array( 'global_tags' => array( 'my_tag' => 'tag_value', @@ -1366,7 +1396,8 @@ public function testGlobalTagsAreReplacedWithConflictingLocalTags() public function testTelemetryDefault() { - putenv("DD_ORIGIN_DETECTION_ENABLED=false"); + $this->disableOriginDetectionNonWindows(); + $dog = new DogStatsd(); $dog->gauge('metric', 42); @@ -1378,7 +1409,8 @@ public function testTelemetryDefault() public function testTelemetryEnable() { - putenv("DD_ORIGIN_DETECTION_ENABLED=false"); + $this->disableOriginDetectionNonWindows(); + $dog = new DogStatsd(array("disable_telemetry" => false)); $dog->gauge('metric', 42); @@ -1390,7 +1422,8 @@ public function testTelemetryEnable() public function testTelemetryAllDataType() { - putenv("DD_ORIGIN_DETECTION_ENABLED=false"); + $this->disableOriginDetectionNonWindows(); + $dog = new DogStatsd(array("disable_telemetry" => false)); $dog->timing('test', 21); @@ -1427,7 +1460,8 @@ public function testTelemetryAllDataType() public function testTelemetryNetworkError() { - putenv("DD_ORIGIN_DETECTION_ENABLED=false"); + $this->disableOriginDetectionNonWindows(); + $dog = new DogStatsd(array("disable_telemetry" => false)); $this->getSocketSpy()->returnErrorOnSend = true; @@ -1446,7 +1480,8 @@ public function testTelemetryNetworkError() public function testDecimalNormalization() { - putenv("DD_ORIGIN_DETECTION_ENABLED=false"); + $this->disableOriginDetectionNonWindows(); + $dog = new DogStatsd(array("disable_telemetry" => false, "decimal_precision" => 5)); $dog->timing('test', 21.00000); @@ -1461,7 +1496,8 @@ public function testDecimalNormalization() public function testFloatLocalization() { - putenv("DD_ORIGIN_DETECTION_ENABLED=false"); + $this->disableOriginDetectionNonWindows(); + $defaultLocale = setlocale(LC_ALL, 0); setlocale(LC_ALL, 'nl_NL'); $dog = new DogStatsd(array("disable_telemetry" => false)); @@ -1473,7 +1509,8 @@ public function testFloatLocalization() public function testSampleRateFloatLocalization() { - putenv("DD_ORIGIN_DETECTION_ENABLED=false"); + $this->disableOriginDetectionNonWindows(); + $defaultLocale = setlocale(LC_ALL, 0); setlocale(LC_ALL, 'de_DE'); $dog = new DogStatsd(array("disable_telemetry" => true)); @@ -1491,7 +1528,8 @@ public function testSampleRateFloatLocalization() public function testMetricPrefix() { - putenv("DD_ORIGIN_DETECTION_ENABLED=false"); + $this->disableOriginDetectionNonWindows(); + $dog = new DogStatsd(array("disable_telemetry" => false, "metric_prefix" => 'test_prefix')); $dog->timing('test', 21.00); @@ -1507,7 +1545,8 @@ public function testMetricPrefix() public function testExternalEnv() { putenv("DD_EXTERNAL_ENV=cn-SomeKindOfContainerName"); - putenv("DD_ORIGIN_DETECTION_ENABLED=false"); + $this->disableOriginDetectionNonWindows(); + $dog = new DogStatsd(array("disable_telemetry" => false)); $dog->gauge('metric', 42); $spy = $this->getSocketSpy(); @@ -1530,7 +1569,8 @@ 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"); - putenv("DD_ORIGIN_DETECTION_ENABLED=false"); + $this->disableOriginDetectionNonWindows(); + $dog = new DogStatsd(array("disable_telemetry" => false)); $dog->gauge('metric', 42, 1.0, array('my_tag' => 'other_value')); $spy = $this->getSocketSpy(); @@ -1551,7 +1591,8 @@ public function testExternalEnvInvalidCharacters() public function testExternalEnvWithTags() { - putenv("DD_ORIGIN_DETECTION_ENABLED=false"); + $this->disableOriginDetectionNonWindows(); + putenv("DD_EXTERNAL_ENV=it-false,cn-nginx-webserver,pu-75a2b6d5-3949-4afb-ad0d-92ff0674e759"); $dog = new DogStatsd(array("disable_telemetry" => false)); $dog->gauge('metric', 42, 1.0, array('my_tag' => 'other_value')); @@ -1576,7 +1617,7 @@ public function testDDTags() putenv("DD_VERSION=1.2.3"); putenv("DD_ENV=prod"); putenv("DD_SERVICE=myService"); - putenv("DD_ORIGIN_DETECTION_ENABLED=false"); + $this->disableOriginDetectionNonWindows(); $dog = new DogStatsd(array( 'global_tags' => array( 'my_tag' => 'tag_value', From 5ba2fb40fc3ecbd9d30e722ece16bdbbfd8ef33b Mon Sep 17 00:00:00 2001 From: Stephen Wakely Date: Thu, 3 Jul 2025 10:12:07 +0100 Subject: [PATCH 03/12] Include input in test desc --- tests/UnitTests/DogStatsd/OriginDetectionTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/UnitTests/DogStatsd/OriginDetectionTest.php b/tests/UnitTests/DogStatsd/OriginDetectionTest.php index 9145f0b..9708f09 100644 --- a/tests/UnitTests/DogStatsd/OriginDetectionTest.php +++ b/tests/UnitTests/DogStatsd/OriginDetectionTest.php @@ -87,7 +87,7 @@ public function testParseContainerID() { ->at($root); $id = $originDetection->readContainerID(vfsStream::url('proc/self/cgroup')); - $this->assertSame($case['expected'], $id); + $this->assertSame($case['expected'], $id, $case['input']); } } From cb2a66cff607e16a9b7281a8c5241db411c8bbe8 Mon Sep 17 00:00:00 2001 From: Stephen Wakely Date: Thu, 3 Jul 2025 11:01:29 +0100 Subject: [PATCH 04/12] Try without suspect test --- tests/UnitTests/DogStatsd/OriginDetectionTest.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/tests/UnitTests/DogStatsd/OriginDetectionTest.php b/tests/UnitTests/DogStatsd/OriginDetectionTest.php index 9708f09..ac2969c 100644 --- a/tests/UnitTests/DogStatsd/OriginDetectionTest.php +++ b/tests/UnitTests/DogStatsd/OriginDetectionTest.php @@ -70,12 +70,6 @@ public function testParseContainerID() { [ 'input' => "1:name=systemd:/docker/34dc0b5e626f2c5c4c5170e34b10e7654ce36f0fcd532739f4445baabea03376.scope", 'expected' => "34dc0b5e626f2c5c4c5170e34b10e7654ce36f0fcd532739f4445baabea03376" - ], - [ - 'input' => "1:name=systemd:/nope -2:pids:/docker/34dc0b5e626f2c5c4c5170e34b10e7654ce36f0fcd532739f4445baabea03376 -3:cpu:/invalid", - 'expected' => "34dc0b5e626f2c5c4c5170e34b10e7654ce36f0fcd532739f4445baabea03376" ] ]; From 05aa7f012ca10bfa34f4833398e1e2d3ba6fa9c3 Mon Sep 17 00:00:00 2001 From: Stephen Wakely Date: Thu, 3 Jul 2025 11:25:43 +0100 Subject: [PATCH 05/12] Properly check Windows --- tests/UnitTests/DogStatsd/SocketsTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/UnitTests/DogStatsd/SocketsTest.php b/tests/UnitTests/DogStatsd/SocketsTest.php index 1d62d16..19480de 100644 --- a/tests/UnitTests/DogStatsd/SocketsTest.php +++ b/tests/UnitTests/DogStatsd/SocketsTest.php @@ -319,7 +319,7 @@ public function testMicrotiming() } function disableOriginDetectionNonWindows() { - if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') { + if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') { putenv("DD_ORIGIN_DETECTION_ENABLED=false"); } } From 80a4dce141259eed78c02c12548ede9bd3fee22a Mon Sep 17 00:00:00 2001 From: Stephen Wakely Date: Thu, 3 Jul 2025 11:31:09 +0100 Subject: [PATCH 06/12] Ensure path exists --- src/OriginDetection.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/OriginDetection.php b/src/OriginDetection.php index d872203..12c5f03 100644 --- a/src/OriginDetection.php +++ b/src/OriginDetection.php @@ -63,6 +63,10 @@ public function parseCgroupNodePath($lines) public function getCgroupInode($cgroupMountPath, $procSelfCgroupPath) { + if (!is_readable($procSelfCgroupPath)) { + return ''; + } + $cgroupControllersPaths = $this->parseCgroupNodePath(file_get_contents($procSelfCgroupPath)); foreach ([self::CGROUPV1BASECONTROLLER , ''] as $controller) { From 72d59af71f1d8d87d16dc0e6b46c19b5bdddc399 Mon Sep 17 00:00:00 2001 From: Stephen Wakely Date: Thu, 3 Jul 2025 11:59:29 +0100 Subject: [PATCH 07/12] Time line --- src/OriginDetection.php | 1 + tests/UnitTests/DogStatsd/OriginDetectionTest.php | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/src/OriginDetection.php b/src/OriginDetection.php index 12c5f03..1d60614 100644 --- a/src/OriginDetection.php +++ b/src/OriginDetection.php @@ -115,6 +115,7 @@ private function parseContainerID($handle) $expContainerID = '/(' . $uuidSource . '|' . $containerSource . '|' . $taskSource . ')(?:.scope)?$/'; while (($line = fgets($handle)) !== false) { + $line = rtrim($line); if (preg_match($expLine, $line, $matches)) { if (count($matches) != 2) { continue; diff --git a/tests/UnitTests/DogStatsd/OriginDetectionTest.php b/tests/UnitTests/DogStatsd/OriginDetectionTest.php index ac2969c..9708f09 100644 --- a/tests/UnitTests/DogStatsd/OriginDetectionTest.php +++ b/tests/UnitTests/DogStatsd/OriginDetectionTest.php @@ -70,6 +70,12 @@ public function testParseContainerID() { [ 'input' => "1:name=systemd:/docker/34dc0b5e626f2c5c4c5170e34b10e7654ce36f0fcd532739f4445baabea03376.scope", 'expected' => "34dc0b5e626f2c5c4c5170e34b10e7654ce36f0fcd532739f4445baabea03376" + ], + [ + 'input' => "1:name=systemd:/nope +2:pids:/docker/34dc0b5e626f2c5c4c5170e34b10e7654ce36f0fcd532739f4445baabea03376 +3:cpu:/invalid", + 'expected' => "34dc0b5e626f2c5c4c5170e34b10e7654ce36f0fcd532739f4445baabea03376" ] ]; From bde99e3a81940da02d2842246f5841c86b2b9368 Mon Sep 17 00:00:00 2001 From: Stephen Wakely Date: Thu, 3 Jul 2025 12:15:43 +0100 Subject: [PATCH 08/12] Add macos to test matrix --- .github/workflows/test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 056968e..aa7f554 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -15,7 +15,7 @@ jobs: runs-on: ${{ matrix.os }} strategy: matrix: - os: [ubuntu-latest, windows-latest] + os: [ubuntu-latest, windows-latest, macos-latest] php-versions: ['5.6', '7.0', '7.1', '7.2', '7.3', '7.4', '8.0', '8.2', '8.3'] steps: From ba7990fd14c476f6a5c3650005cb8936aef28d30 Mon Sep 17 00:00:00 2001 From: Stephen Wakely Date: Thu, 3 Jul 2025 12:58:57 +0100 Subject: [PATCH 09/12] Only disable origin detection in Linux for tests --- tests/UnitTests/DogStatsd/OriginDetectionTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/UnitTests/DogStatsd/OriginDetectionTest.php b/tests/UnitTests/DogStatsd/OriginDetectionTest.php index 9708f09..9145f0b 100644 --- a/tests/UnitTests/DogStatsd/OriginDetectionTest.php +++ b/tests/UnitTests/DogStatsd/OriginDetectionTest.php @@ -87,7 +87,7 @@ public function testParseContainerID() { ->at($root); $id = $originDetection->readContainerID(vfsStream::url('proc/self/cgroup')); - $this->assertSame($case['expected'], $id, $case['input']); + $this->assertSame($case['expected'], $id); } } From a84360acbaf362dc44d3b937842ded4298d7939f Mon Sep 17 00:00:00 2001 From: Stephen Wakely Date: Thu, 3 Jul 2025 14:41:30 +0100 Subject: [PATCH 10/12] Only disable origin detection in tests on linux --- tests/UnitTests/DogStatsd/SocketsTest.php | 90 +++++++++++------------ 1 file changed, 45 insertions(+), 45 deletions(-) diff --git a/tests/UnitTests/DogStatsd/SocketsTest.php b/tests/UnitTests/DogStatsd/SocketsTest.php index 19480de..1b04015 100644 --- a/tests/UnitTests/DogStatsd/SocketsTest.php +++ b/tests/UnitTests/DogStatsd/SocketsTest.php @@ -155,7 +155,7 @@ public function testHostAndPortFromEnvVar() { putenv("DD_AGENT_HOST=myenvvarhost"); putenv("DD_DOGSTATSD_PORT=1234"); - $this->disableOriginDetectionNonWindows(); + $this->disableOriginDetectionLinux(); $dog = new DogStatsd(); $this->assertSame( @@ -174,7 +174,7 @@ public function testHostAndPortFromArgs() { putenv("DD_AGENT_HOST=myenvvarhost"); putenv("DD_DOGSTATSD_PORT=1234"); - $this->disableOriginDetectionNonWindows(); + $this->disableOriginDetectionLinux(); $dog = new DogStatsd(array( 'host' => 'myhost', 'port' => 4321 @@ -194,7 +194,7 @@ public function testHostAndPortFromArgs() public function testHostAndPortFromUrl() { putenv("DD_DOGSTATSD_URL=udp://oh.my.address:1234"); - $this->disableOriginDetectionNonWindows(); + $this->disableOriginDetectionLinux(); $dog = new DogStatsd(); $this->assertSame( 'oh.my.address', @@ -318,15 +318,15 @@ public function testMicrotiming() ); } - function disableOriginDetectionNonWindows() { - if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') { + function disableOriginDetectionLinux() { + if (strtoupper(substr(PHP_OS, 0, 3)) == 'LIN') { putenv("DD_ORIGIN_DETECTION_ENABLED=false"); } } public function testGauge() { - $this->disableOriginDetectionNonWindows(); + $this->disableOriginDetectionLinux(); $stat = 'some.gauge_metric'; $value = 5; @@ -361,7 +361,7 @@ public function testGauge() public function testGaugeZero() { - $this->disableOriginDetectionNonWindows(); + $this->disableOriginDetectionLinux(); $stat = 'some.gauge_metric'; $value = 0; @@ -396,7 +396,7 @@ public function testGaugeZero() public function testHistogram() { - $this->disableOriginDetectionNonWindows(); + $this->disableOriginDetectionLinux(); $stat = 'some.histogram_metric'; $value = 109; @@ -431,7 +431,7 @@ public function testHistogram() public function testDistribution() { - $this->disableOriginDetectionNonWindows(); + $this->disableOriginDetectionLinux(); $stat = 'some.distribution_metric'; $value = 7; @@ -474,7 +474,7 @@ public function testDistribution() */ public function testSet($stat, $value, $sampleRate, $tags, $expectedUdpMessage) { - $this->disableOriginDetectionNonWindows(); + $this->disableOriginDetectionLinux(); $dog = new DogStatsd(array("disable_telemetry" => false)); @@ -547,7 +547,7 @@ public function testServiceCheck( $timestamp, $expectedUdpMessage ) { - $this->disableOriginDetectionNonWindows(); + $this->disableOriginDetectionLinux(); $dog = new DogStatsd(array("disable_telemetry" => false)); @@ -574,7 +574,7 @@ public function testServiceCheck( public function serviceCheckProvider() { - $this->disableOriginDetectionNonWindows(); + $this->disableOriginDetectionLinux(); $name = 'neat-service'; $status = DogStatsd::CRITICAL; @@ -634,7 +634,7 @@ public function serviceCheckProvider() public function testSend() { - $this->disableOriginDetectionNonWindows(); + $this->disableOriginDetectionLinux(); $sampleRate = 1.0; $tags = array( @@ -676,7 +676,7 @@ public function testSend() public function testSendSerializesTagAsString() { - $this->disableOriginDetectionNonWindows(); + $this->disableOriginDetectionLinux(); $data = array( 'foo.metric' => '82|s', @@ -701,7 +701,7 @@ public function testSendSerializesTagAsString() public function testSendSerializesMessageWithoutTags() { - $this->disableOriginDetectionNonWindows(); + $this->disableOriginDetectionLinux(); $data = array( 'foo.metric' => '19872|h', @@ -726,7 +726,7 @@ public function testSendSerializesMessageWithoutTags() public function testSendReturnsEarlyWhenPassedEmptyData() { - $this->disableOriginDetectionNonWindows(); + $this->disableOriginDetectionLinux(); $dog = new DogStatsd(array("disable_telemetry" => false)); @@ -743,7 +743,7 @@ public function testSendReturnsEarlyWhenPassedEmptyData() public function testSendSendsWhenRandCalculationLessThanSampleRate() { - $this->disableOriginDetectionNonWindows(); + $this->disableOriginDetectionLinux(); global $mt_rand_stub_return_value; global $mt_getrandmax_stub_return_value; @@ -772,7 +772,7 @@ public function testSendSendsWhenRandCalculationLessThanSampleRate() public function testSendSendsWhenRandCalculationEqualToSampleRate() { - $this->disableOriginDetectionNonWindows(); + $this->disableOriginDetectionLinux(); global $mt_rand_stub_return_value; global $mt_getrandmax_stub_return_value; @@ -801,7 +801,7 @@ public function testSendSendsWhenRandCalculationEqualToSampleRate() public function testSendDoesNotSendWhenRandCalculationGreaterThanSampleRate() { - $this->disableOriginDetectionNonWindows(); + $this->disableOriginDetectionLinux(); global $mt_rand_stub_return_value; global $mt_getrandmax_stub_return_value; @@ -830,7 +830,7 @@ public function testSendDoesNotSendWhenRandCalculationGreaterThanSampleRate() public function testIncrement() { - $this->disableOriginDetectionNonWindows(); + $this->disableOriginDetectionLinux(); $stats = array( 'foo.metric', @@ -861,7 +861,7 @@ public function testIncrement() public function testDecrement() { - $this->disableOriginDetectionNonWindows(); + $this->disableOriginDetectionLinux(); $stats = array( 'foo.metric', @@ -892,7 +892,7 @@ public function testDecrement() public function testDecrementWithValueGreaterThanOne() { - $this->disableOriginDetectionNonWindows(); + $this->disableOriginDetectionLinux(); $stats = array( 'foo.metric', @@ -923,7 +923,7 @@ public function testDecrementWithValueGreaterThanOne() public function testDecrementWithValueLessThanOne() { - $this->disableOriginDetectionNonWindows(); + $this->disableOriginDetectionLinux(); $stats = array( 'foo.metric', @@ -954,7 +954,7 @@ public function testDecrementWithValueLessThanOne() public function testUpdateStats() { - $this->disableOriginDetectionNonWindows(); + $this->disableOriginDetectionLinux(); $stats = array( 'foo.metric', @@ -1005,7 +1005,7 @@ public function testUpdateStats() public function testUpdateStatsWithStringMetric() { - $this->disableOriginDetectionNonWindows(); + $this->disableOriginDetectionLinux(); $stats = 'foo.metric'; $delta = -45; @@ -1062,7 +1062,7 @@ public function testReport() public function testFlushUdp() { - $this->disableOriginDetectionNonWindows(); + $this->disableOriginDetectionLinux(); $expectedUdpMessage = 'foo'; @@ -1132,7 +1132,7 @@ public function testFlushUdp() public function testFlushUds() { - $this->disableOriginDetectionNonWindows(); + $this->disableOriginDetectionLinux(); $expectedUdsMessage = 'foo'; $expectedUdsSocketPath = '/path/to/some.socket'; @@ -1203,7 +1203,7 @@ public function testFlushUds() public function testEventUdp() { - $this->disableOriginDetectionNonWindows(); + $this->disableOriginDetectionLinux(); $eventTitle = 'Some event title'; $eventVals = array( @@ -1251,7 +1251,7 @@ public function testEventUdp() */ public function testEventUdpWithEmptyValues() { - $this->disableOriginDetectionNonWindows(); + $this->disableOriginDetectionLinux(); $eventTitle = ''; @@ -1281,7 +1281,7 @@ public function testEventUdpWithEmptyValues() public function testGlobalTags() { - $this->disableOriginDetectionNonWindows(); + $this->disableOriginDetectionLinux(); $dog = new DogStatsd(array( 'global_tags' => array( @@ -1309,7 +1309,7 @@ public function testGlobalTags() public function testGlobalTagsWithEntityIdFromEnvVar() { - $this->disableOriginDetectionNonWindows(); + $this->disableOriginDetectionLinux(); putenv("DD_ENTITY_ID=04652bb7-19b7-11e9-9cc6-42010a9c016d"); $dog = new DogStatsd(array( @@ -1338,7 +1338,7 @@ public function testGlobalTagsWithEntityIdFromEnvVar() public function testGlobalTagsAreSupplementedWithLocalTags() { - $this->disableOriginDetectionNonWindows(); + $this->disableOriginDetectionLinux(); $dog = new DogStatsd(array( 'global_tags' => array( @@ -1367,7 +1367,7 @@ public function testGlobalTagsAreSupplementedWithLocalTags() public function testGlobalTagsAreReplacedWithConflictingLocalTags() { - $this->disableOriginDetectionNonWindows(); + $this->disableOriginDetectionLinux(); $dog = new DogStatsd(array( 'global_tags' => array( @@ -1396,7 +1396,7 @@ public function testGlobalTagsAreReplacedWithConflictingLocalTags() public function testTelemetryDefault() { - $this->disableOriginDetectionNonWindows(); + $this->disableOriginDetectionLinux(); $dog = new DogStatsd(); $dog->gauge('metric', 42); @@ -1409,7 +1409,7 @@ public function testTelemetryDefault() public function testTelemetryEnable() { - $this->disableOriginDetectionNonWindows(); + $this->disableOriginDetectionLinux(); $dog = new DogStatsd(array("disable_telemetry" => false)); $dog->gauge('metric', 42); @@ -1422,7 +1422,7 @@ public function testTelemetryEnable() public function testTelemetryAllDataType() { - $this->disableOriginDetectionNonWindows(); + $this->disableOriginDetectionLinux(); $dog = new DogStatsd(array("disable_telemetry" => false)); @@ -1460,7 +1460,7 @@ public function testTelemetryAllDataType() public function testTelemetryNetworkError() { - $this->disableOriginDetectionNonWindows(); + $this->disableOriginDetectionLinux(); $dog = new DogStatsd(array("disable_telemetry" => false)); $this->getSocketSpy()->returnErrorOnSend = true; @@ -1480,7 +1480,7 @@ public function testTelemetryNetworkError() public function testDecimalNormalization() { - $this->disableOriginDetectionNonWindows(); + $this->disableOriginDetectionLinux(); $dog = new DogStatsd(array("disable_telemetry" => false, "decimal_precision" => 5)); @@ -1496,7 +1496,7 @@ public function testDecimalNormalization() public function testFloatLocalization() { - $this->disableOriginDetectionNonWindows(); + $this->disableOriginDetectionLinux(); $defaultLocale = setlocale(LC_ALL, 0); setlocale(LC_ALL, 'nl_NL'); @@ -1509,7 +1509,7 @@ public function testFloatLocalization() public function testSampleRateFloatLocalization() { - $this->disableOriginDetectionNonWindows(); + $this->disableOriginDetectionLinux(); $defaultLocale = setlocale(LC_ALL, 0); setlocale(LC_ALL, 'de_DE'); @@ -1528,7 +1528,7 @@ public function testSampleRateFloatLocalization() public function testMetricPrefix() { - $this->disableOriginDetectionNonWindows(); + $this->disableOriginDetectionLinux(); $dog = new DogStatsd(array("disable_telemetry" => false, "metric_prefix" => 'test_prefix')); @@ -1545,7 +1545,7 @@ public function testMetricPrefix() public function testExternalEnv() { putenv("DD_EXTERNAL_ENV=cn-SomeKindOfContainerName"); - $this->disableOriginDetectionNonWindows(); + $this->disableOriginDetectionLinux(); $dog = new DogStatsd(array("disable_telemetry" => false)); $dog->gauge('metric', 42); @@ -1569,7 +1569,7 @@ 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->disableOriginDetectionNonWindows(); + $this->disableOriginDetectionLinux(); $dog = new DogStatsd(array("disable_telemetry" => false)); $dog->gauge('metric', 42, 1.0, array('my_tag' => 'other_value')); @@ -1591,7 +1591,7 @@ public function testExternalEnvInvalidCharacters() public function testExternalEnvWithTags() { - $this->disableOriginDetectionNonWindows(); + $this->disableOriginDetectionLinux(); putenv("DD_EXTERNAL_ENV=it-false,cn-nginx-webserver,pu-75a2b6d5-3949-4afb-ad0d-92ff0674e759"); $dog = new DogStatsd(array("disable_telemetry" => false)); @@ -1617,7 +1617,7 @@ public function testDDTags() putenv("DD_VERSION=1.2.3"); putenv("DD_ENV=prod"); putenv("DD_SERVICE=myService"); - $this->disableOriginDetectionNonWindows(); + $this->disableOriginDetectionLinux(); $dog = new DogStatsd(array( 'global_tags' => array( 'my_tag' => 'tag_value', From 7dbfff4e73051bfec7dbdcbc630d39603a8ddaee Mon Sep 17 00:00:00 2001 From: Stephen Wakely Date: Thu, 3 Jul 2025 14:41:48 +0100 Subject: [PATCH 11/12] Handle missing errors from file_get_contents --- src/OriginDetection.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/OriginDetection.php b/src/OriginDetection.php index 1d60614..464f3f7 100644 --- a/src/OriginDetection.php +++ b/src/OriginDetection.php @@ -63,11 +63,14 @@ public function parseCgroupNodePath($lines) public function getCgroupInode($cgroupMountPath, $procSelfCgroupPath) { - if (!is_readable($procSelfCgroupPath)) { + // phpcs:disable + $content = @file_get_contents($procSelfCgroupPath); + // phpcs:enable + if ($content == false) { return ''; } - $cgroupControllersPaths = $this->parseCgroupNodePath(file_get_contents($procSelfCgroupPath)); + $cgroupControllersPaths = $this->parseCgroupNodePath($content); foreach ([self::CGROUPV1BASECONTROLLER , ''] as $controller) { if (!isset($cgroupControllersPaths[$controller])) { From 676fa785e98d0753028ce7a6ad5e4cdeb1505bb2 Mon Sep 17 00:00:00 2001 From: Stephen Wakely Date: Thu, 3 Jul 2025 14:45:53 +0100 Subject: [PATCH 12/12] Tidy disableOriginDetectionLinux --- tests/UnitTests/DogStatsd/SocketsTest.php | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/tests/UnitTests/DogStatsd/SocketsTest.php b/tests/UnitTests/DogStatsd/SocketsTest.php index 1b04015..12d56e0 100644 --- a/tests/UnitTests/DogStatsd/SocketsTest.php +++ b/tests/UnitTests/DogStatsd/SocketsTest.php @@ -107,6 +107,20 @@ protected function tear_down() { parent::tear_down(); } + + /** + * Origin detection via cgroups only works on Linux. On Linux this could + * interfere with the tests and the container ID is non deterministic, + * so for Linux we disable origin detection. Other OSes shouldn't pick + * up the container Id, so we keep origin detection enabled to ensure it + * doesn't raise unwanted errors. + */ + function disableOriginDetectionLinux() { + if (strtoupper(substr(PHP_OS, 0, 5)) == 'LINUX') { + putenv("DD_ORIGIN_DETECTION_ENABLED=false"); + } + } + static function getPrivate($object, $property) { $reflector = new ReflectionProperty(get_class($object), $property); $reflector->setAccessible(true); @@ -318,12 +332,6 @@ public function testMicrotiming() ); } - function disableOriginDetectionLinux() { - if (strtoupper(substr(PHP_OS, 0, 3)) == 'LIN') { - putenv("DD_ORIGIN_DETECTION_ENABLED=false"); - } - } - public function testGauge() { $this->disableOriginDetectionLinux();