Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added additional tags adding possibility #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
32 changes: 32 additions & 0 deletions DatadogStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ class DatadogStorage implements StatsStorage
*/
private $datadog;

/**
* @var array
*/
private $additionalTags = [];

public function __construct($config = 'datadog:')
{
if (false === class_exists(DogStatsd::class)) {
Expand All @@ -47,6 +52,8 @@ public function pushConsumerStats(ConsumerStats $stats): void
'consumerId' => $stats->getConsumerId(),
];

$tags = $this->addAdditionalTags($tags);

if ($stats->getFinishedAtMs()) {
$values['finishedAtMs'] = $stats->getFinishedAtMs();
}
Expand Down Expand Up @@ -77,6 +84,8 @@ public function pushSentMessageStats(SentMessageStats $stats): void
$tags['command'] = $properties[Config::COMMAND];
}

$tags = $this->addAdditionalTags($tags);

$this->datadog->increment($this->config['metric.messages.sent'], 1, $tags);
}

Expand All @@ -87,6 +96,8 @@ public function pushConsumedMessageStats(ConsumedMessageStats $stats): void
'status' => $stats->getStatus(),
];

$tags = $this->addAdditionalTags($tags);

if (ConsumedMessageStats::STATUS_FAILED === $stats->getStatus()) {
$this->datadog->increment($this->config['metric.messages.failed'], 1, $tags);
}
Expand Down Expand Up @@ -170,4 +181,25 @@ private function prepareConfig($config): array
'metric.consumers.memoryUsage' => 'enqueue.consumers.memoryUsage',
], $config);
}

public function getAdditionalTags(): array
{
return $this->additionalTags;
}

public function setAdditionalTags(array $additionalTags): self
{
$this->additionalTags = $additionalTags;

return $this;
}

private function addAdditionalTags(array $tags): array
{
foreach ($this->additionalTags as $tagKey => $tagValue) {
$tags[$tagKey] = $tagValue;
}

return $tags;
}
}
20 changes: 20 additions & 0 deletions Tests/DatadogStorageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

use Enqueue\Monitoring\DatadogStorage;
use PHPUnit\Framework\TestCase;

class DatadogStorageTest extends TestCase
{
public function testCanAddTagsCorrect(): void
{
$additionalTags = ['environment' => 'staging'];

$storage = new DatadogStorage();
$storage->setAdditionalTags($additionalTags);

$storageTags = $storage->getAdditionalTags();
$this->assertEquals($additionalTags, $storageTags);
}
}