diff --git a/src/GaugeExporterClient.php b/src/GaugeExporterClient.php index 485a8b9..7ef4520 100644 --- a/src/GaugeExporterClient.php +++ b/src/GaugeExporterClient.php @@ -14,13 +14,13 @@ final class GaugeExporterClient { private string $apiDomain; private ClientInterface $client; - private array $defaultLabels; + private array $systemLabels; - public function __construct(ClientInterface $client, string $apiDomain, array $defaultLabels = []) + public function __construct(ClientInterface $client, string $apiDomain, array $systemLabels = []) { $this->client = $client; $this->apiDomain = trim($apiDomain, " \n\r\t\v\0/"); - $this->defaultLabels = MetricLine::normalizeLabels($defaultLabels); + $this->systemLabels = MetricLine::normalizeLabels($systemLabels); } /** @@ -32,6 +32,7 @@ public function send(MetricBag $metricBag, int $ttlSec): void $body = [ 'ttl' => $ttlSec, 'data' => $this->generateData($metricBag), + 'system_labels' => !empty($this->systemLabels) ? $this->systemLabels : new stdClass(), ]; $request = new Request( 'PUT', @@ -50,9 +51,8 @@ private function generateData(MetricBag $metricBag): array { $result = []; foreach ($metricBag->getLogLines() as $logLine) { - $labels = array_merge($this->defaultLabels, $logLine->getLabels()); $result[] = [ - 'labels' => !empty($labels) ? $labels : new stdClass(), + 'labels' => !empty($logLine->getLabels()) ? $logLine->getLabels() : new stdClass(), 'value' => $logLine->getValue() ]; } diff --git a/tests/GaugeExporterClientTest.php b/tests/GaugeExporterClientTest.php index 7b85aa5..af73167 100755 --- a/tests/GaugeExporterClientTest.php +++ b/tests/GaugeExporterClientTest.php @@ -47,7 +47,7 @@ public function testWholeRequest(): void [ 'method' => 'PUT', 'url' => 'https://example.com/gauge/metric.name', - 'data' => '{"ttl":100,"data":[{"labels":{"key1":"b","key2":"d"},"value":10},{"labels":{"key1":"b","key2":"e"},"value":10}]}', + 'data' => '{"ttl":100,"data":[{"labels":{"key1":"b","key2":"d"},"value":10},{"labels":{"key1":"b","key2":"e"},"value":10}],"system_labels":{}}', ], $this->simplifyRequest($psrClientMock->getLastRequest()), ); @@ -69,7 +69,7 @@ public function testWholeRequestWithEmptyLabelsMetric(): void [ 'method' => 'PUT', 'url' => 'https://example.com/gauge/metric.name', - 'data' => '{"ttl":100,"data":[{"labels":{},"value":10}]}', + 'data' => '{"ttl":100,"data":[{"labels":{},"value":10}],"system_labels":{}}', ], $this->simplifyRequest($psrClientMock->getLastRequest()), ); @@ -90,13 +90,13 @@ public function testWholeRequestWithEmptyBag(): void [ 'method' => 'PUT', 'url' => 'https://example.com/gauge/metric.name', - 'data' => '{"ttl":100,"data":[]}', + 'data' => '{"ttl":100,"data":[],"system_labels":{}}', ], $this->simplifyRequest($psrClientMock->getLastRequest()), ); } - public function testWholeRequestWithDefaultLabels(): void + public function testWholeRequestWithSystemLabels(): void { // Arrange $psrClientMock = new MockClient(); @@ -117,20 +117,20 @@ public function testWholeRequestWithDefaultLabels(): void // Assert $expectedData = [ - ['labels' => ['key2' => 'd', 'key3' => 'default_key3', 'key1' => 'b'], 'value' => 10], - ['labels' => ['key2' => 'default_key2', 'key3' => 'default_key3', 'key1' => 'b'], 'value' => 12], + ['labels' => ['key1' => 'b', 'key2' => 'd'], 'value' => 10], + ['labels' => ['key1' => 'b'], 'value' => 12], ]; $this->assertSame( [ 'method' => 'PUT', 'url' => 'https://example.com/gauge/metric.name', - 'data' => '{"ttl":100,"data":' . json_encode($expectedData) . '}', + 'data' => '{"ttl":100,"data":' . json_encode($expectedData) . ',"system_labels":{"key2":"default_key2","key3":"default_key3"}}', ], $this->simplifyRequest($psrClientMock->getLastRequest()), ); } - public function testWholeRequestWithEmptyBagAndDefaultLabels(): void + public function testWholeRequestWithEmptyBagAndSystemLabels(): void { // Arrange $psrClientMock = new MockClient(); @@ -152,7 +152,7 @@ public function testWholeRequestWithEmptyBagAndDefaultLabels(): void [ 'method' => 'PUT', 'url' => 'https://example.com/gauge/metric.name', - 'data' => '{"ttl":100,"data":[]}', + 'data' => '{"ttl":100,"data":[],"system_labels":{"key2":"default_key2","key3":"default_key3"}}', ], $this->simplifyRequest($psrClientMock->getLastRequest()), );