Skip to content

Commit

Permalink
System labels support
Browse files Browse the repository at this point in the history
  • Loading branch information
solodkiy committed Oct 1, 2024
1 parent f379af2 commit 2e38085
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
10 changes: 5 additions & 5 deletions src/GaugeExporterClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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',
Expand All @@ -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()
];
}
Expand Down
18 changes: 9 additions & 9 deletions tests/GaugeExporterClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
);
Expand All @@ -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()),
);
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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()),
);
Expand Down

0 comments on commit 2e38085

Please sign in to comment.