Skip to content
This repository was archived by the owner on May 27, 2024. It is now read-only.

Commit bec0a07

Browse files
scott-shields-githubScott Shields
andauthored
Adding suppport for a metric prefix (DataDog#117)
Co-authored-by: Scott Shields <[email protected]>
1 parent ecd57c0 commit bec0a07

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/DogStatsd.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ class DogStatsd
6060
* @var int Number of decimals to use when formatting numbers to strings
6161
*/
6262
private $decimalPrecision;
63+
/**
64+
* @var string The prefix to apply to all metrics
65+
*/
66+
private $metricPrefix;
6367

6468
private static $eventUrl = '/api/v1/events';
6569

@@ -74,7 +78,9 @@ class DogStatsd
7478
* curl_ssl_verify_host,
7579
* curl_ssl_verify_peer,
7680
* api_key and app_key,
77-
* decimal_precision
81+
* global_tags,
82+
* decimal_precision,
83+
* metric_prefix
7884
*
7985
* @param array $config
8086
*/
@@ -104,6 +110,8 @@ public function __construct(array $config = array())
104110
$this->globalTags['dd.internal.entity_id'] = getenv('DD_ENTITY_ID');
105111
}
106112

113+
$this->metricPrefix = isset($config['metric_prefix']) ? "$config[metric_prefix]." : '';
114+
107115
if ($this->apiKey !== null) {
108116
$this->submitEventsOver = 'TCP';
109117
}
@@ -387,7 +395,7 @@ public function send($data, $sampleRate = 1.0, $tags = null)
387395

388396
foreach ($sampledData as $stat => $value) {
389397
$value .= $this->serializeTags($tags);
390-
$this->report("$stat:$value");
398+
$this->report("{$this->metricPrefix}$stat:$value");
391399
}
392400
}
393401

tests/UnitTests/DogStatsd/SocketsTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,6 +1273,20 @@ public function testFloatLocalization()
12731273
setlocale(LC_ALL, $defaultLocale);
12741274
}
12751275

1276+
public function testMetricPrefix()
1277+
{
1278+
$dog = new DogStatsd(array("disable_telemetry" => false, "metric_prefix" => 'test_prefix'));
1279+
1280+
$dog->timing('test', 21.00);
1281+
$this->assertSameWithTelemetry('test_prefix.test:21|ms', $this->getSocketSpy()->argsFromSocketSendtoCalls[0][1]);
1282+
1283+
$dog->gauge('test', 21.22);
1284+
$this->assertSameWithTelemetry('test_prefix.test:21.22|g', $this->getSocketSpy()->argsFromSocketSendtoCalls[1][1], "", array("bytes_sent" => 687, "packets_sent" => 1));
1285+
1286+
$dog->gauge('test', 2000.00);
1287+
$this->assertSameWithTelemetry('test_prefix.test:2000|g', $this->getSocketSpy()->argsFromSocketSendtoCalls[2][1], "", array("bytes_sent" => 691, "packets_sent" => 1));
1288+
}
1289+
12761290

12771291
/**
12781292
* Get a timestamp created from a real date that is deterministic in nature

0 commit comments

Comments
 (0)