Skip to content

Commit

Permalink
passes tags to all instances; cleans up PHPDoc
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmastech committed Jul 8, 2024
1 parent d809a3c commit 0324e29
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 11 deletions.
40 changes: 29 additions & 11 deletions src/AdapterManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Cosmastech\StatsDClientAdapter\Adapters\Datadog\DatadogStatsDClientAdapter;
use Cosmastech\StatsDClientAdapter\Adapters\InMemory\InMemoryClientAdapter;
use Cosmastech\StatsDClientAdapter\Adapters\League\LeagueStatsDClientAdapter;
use Cosmastech\StatsDClientAdapter\Adapters\StatsDClientAdapter;
use Cosmastech\StatsDClientAdapter\Clients\Datadog\DatadogLoggingClient;
use Cosmastech\StatsDClientAdapter\Utility\SampleRateDecider\SampleRateSendDecider;
use DataDog\DogStatsd;
Expand All @@ -16,6 +17,9 @@
use League\StatsD\Exception\ConfigurationException;

/**
* @property array<int, StatsDClientAdapter> $instances
* @method StatsDClientAdapter instance($name = null)
*
* @mixin \Cosmastech\StatsDClientAdapter\Adapters\StatsDClientAdapter
*/
class AdapterManager extends MultipleInstanceManager
Expand All @@ -34,7 +38,7 @@ class AdapterManager extends MultipleInstanceManager
/**
* @var array<mixed, mixed>
*/
protected array $defaultTags = [];
protected array $defaultTags;

/**
* @inheritDoc
Expand Down Expand Up @@ -71,18 +75,32 @@ public function getInstanceConfig($name)
}

/**
* @return array<mixed, mixed>
* @param array<mixed, mixed> $tags
* @return void
*/
protected function getDefaultTagsFromConfig(): array
public function setDefaultTags(array $tags): void
{
return $this->config->get('statsd-adapter.default_tags', []);
$this->defaultTags = $tags;

foreach ($this->instances as $instance) {
$instance->setDefaultTags($this->defaultTags);
}
}

public function setDefaultTags(array $tags): void
/**
* @return array<mixed, mixed>
*/
public function getDefaultTags(): array
{
$this->defaultTags = $tags;
return $this->defaultTags ?? $this->getDefaultTagsFromConfig();
}


/**
* @return array<mixed, mixed>
*/
protected function getDefaultTagsFromConfig(): array
{
return $this->config->get('statsd-adapter.default_tags', []);
}

/**
Expand All @@ -93,7 +111,7 @@ protected function createMemoryAdapter(array $config): InMemoryClientAdapter
{
$wrapperClock = new WrapperClock(FactoryImmutable::getDefaultInstance());

return new InMemoryClientAdapter($wrapperClock, $this->getDefaultTagsFromConfig());
return new InMemoryClientAdapter($wrapperClock, $this->getDefaultTags());
}

/**
Expand All @@ -119,7 +137,7 @@ protected function createLogDatadogAdapter(array $config): DatadogStatsDClientAd

return new DatadogStatsDClientAdapter(
new DatadogLoggingClient($this->app->make('log'), $logLevel, $config),
$this->getDefaultTagsFromConfig()
$this->getDefaultTags()
);
}

Expand All @@ -129,7 +147,7 @@ protected function createLogDatadogAdapter(array $config): DatadogStatsDClientAd
*/
protected function createDatadogAdapter(array $config): DatadogStatsDClientAdapter
{
return new DatadogStatsDClientAdapter(new DogStatsd($config), $this->getDefaultTagsFromConfig());
return new DatadogStatsDClientAdapter(new DogStatsd($config), $this->getDefaultTags());
}

/**
Expand All @@ -146,7 +164,7 @@ protected function createLeagueAdapter(array $config): LeagueStatsDClientAdapter
return new LeagueStatsDClientAdapter(
$leagueClient,
new SampleRateSendDecider(),
$this->getDefaultTagsFromConfig()
$this->getDefaultTags()
);
}
}
31 changes: 31 additions & 0 deletions tests/AdapterManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,35 @@ public function getDefaultTags_withDefaultTagsSet_returnsDefaultTagsArray(): voi
// Then
self::assertSame(["abc" => true], $adapterManager->getDefaultTags());
}

#[Test]
public function setDefaultTags_passesTagsToExistingInstance(): void
{
// Given
$adapterManager = $this->createAdapterManager();

/** @var InMemoryClientAdapter $inMemoryClientAdapter */
$inMemoryClientAdapter = $adapterManager->instance("memory");

// When
$adapterManager->setDefaultTags(["abc" => "hello"]);

// Then
self::assertSame(["abc" => "hello"], $inMemoryClientAdapter->getDefaultTags());
}

public function setDefaultTags_passesToNewInstance(): void
{
// Given
$adapterManager = $this->createAdapterManager();

// And an instance is created
$adapterManager->instance("log_datadog");

// When
$adapterManager->setDefaultTags(["abc" => "123"]);

// Then
self::assertEquals(["abc" => "123"], $adapterManager->instance("memory"));
}
}

0 comments on commit 0324e29

Please sign in to comment.