Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions src/DogStatsd.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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);
}
Expand All @@ -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"];
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit rusty on PHP, but would that be worth doing a boolean test?

return $config["origin_detection"] == true;

or is this array access enough for what we want?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is testing if the value is truthy, rather than strict true, so you could configure 'origin_detection' => 42 and it would be true. From my understanding it is more idiomatic PHP to accept truthy values rather than expect a strict true to be passed in this situation.

}

if (getenv("DD_ORIGIN_DETECTION_ENABLED")) {
Expand Down
65 changes: 56 additions & 9 deletions tests/UnitTests/DogStatsd/SocketsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1553,17 +1553,18 @@ 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(
1,
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(
Expand All @@ -1577,17 +1578,18 @@ 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(
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';
$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(
Expand All @@ -1599,18 +1601,63 @@ 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(
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';
$expectedUdpMessage = 'metric:42|g|#my_tag:other_value|c:container';
$argsPassedToSocketSendTo = $spy->argsFromSocketSendtoCalls[0];

$this->assertSameWithTelemetry(
Expand Down
Loading