Skip to content

Commit

Permalink
fix: ensure that config is used at runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
owenvoke committed Jul 23, 2024
1 parent a61aac4 commit 95916f3
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 48 deletions.
26 changes: 11 additions & 15 deletions src/ConfigConfigurationResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,28 @@

namespace Worksome\LaravelTelemetry;

use Illuminate\Contracts\Config\Repository;
use OpenTelemetry\SDK\Common\Configuration\Resolver\ResolverInterface;

/**
* Resolves Open Telemetry configuration via Laravel config.
*
* The `config()` function is intentionally used to ensure that the latest config is always used.
*/
class ConfigConfigurationResolver implements ResolverInterface
{
private const PREFIX = 'telemetry';

public function __construct(
private readonly Repository $config,
) {
public function retrieveValue(string $variableName)
{
return config()->get($this->variableNameToConfigKey($variableName));
}

private function getKey(string $variableName): string
public function hasVariable(string $variableName): bool
{
return config()->has($this->variableNameToConfigKey($variableName));
}

private function variableNameToConfigKey(string $variableName): string
{
$names = collect(explode('_', $variableName))
->map(fn(string $key) => strtolower($key))
Expand All @@ -28,14 +34,4 @@ private function getKey(string $variableName): string

return implode('.', [self::PREFIX, ...$names]);
}

public function retrieveValue(string $variableName)
{
return $this->config->get($this->getKey($variableName));
}

public function hasVariable(string $variableName): bool
{
return $this->config->has($this->getKey($variableName));
}
}
6 changes: 3 additions & 3 deletions src/LaravelTelemetryServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ private function prepareConfigResolver(): void
{
/** @var LoggerInterface $logger */
$logger = $this->app->get(LoggerInterface::class);
/** @var ConfigConfigurationResolver $configResolver */
$configResolver = $this->app->get(ConfigConfigurationResolver::class);

LoggerHolder::set($logger);
CompositeResolver::instance()->addResolver($configResolver);
CompositeResolver::instance()->addResolver(
new ConfigConfigurationResolver()
);
}
}
5 changes: 0 additions & 5 deletions tests/ExampleTest.php

This file was deleted.

105 changes: 105 additions & 0 deletions tests/Feature/LaravelTelemetryServiceProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

declare(strict_types=1);

use Illuminate\Contracts\Config\Repository as ConfigRepository;
use OpenTelemetry\API\Logs\LoggerInterface;
use OpenTelemetry\API\Logs\LoggerProviderInterface;
use OpenTelemetry\API\Logs\NoopLogger;
use OpenTelemetry\API\Metrics\MeterInterface;
use OpenTelemetry\API\Metrics\MeterProviderInterface;
use OpenTelemetry\API\Metrics\Noop\NoopMeter;
use OpenTelemetry\API\Trace\NoopTracer;
use OpenTelemetry\API\Trace\TracerInterface;
use OpenTelemetry\API\Trace\TracerProviderInterface;
use OpenTelemetry\SDK\Logs\LoggerProviderInterface as LoggerProviderSdkInterface;
use OpenTelemetry\SDK\Metrics\MeterProviderInterface as MeterProviderSdkInterface;
use OpenTelemetry\SDK\Sdk;
use OpenTelemetry\SDK\Trace\TracerProviderInterface as TracerProviderSdkInterface;

it('registers the default config', function () {
/** @var ConfigRepository $config */
$config = $this->app->get(ConfigRepository::class);

expect($config->get('telemetry'))
->sdk->disabled->toBeFalse()
->exporter->otlp->endpoint->toBe('http://localhost:4318');
});

it('can update the config at runtime', function () {
/** @var ConfigRepository $config */
$config = $this->app->get(ConfigRepository::class);

expect(Sdk::isDisabled())->toBeFalse();

$config->set('telemetry.sdk.disabled', true);

expect(Sdk::isDisabled())->toBeTrue();
});

it('registers the logger provider', function () {
expect(
$this->app->get(LoggerProviderInterface::class)
)
->toBeInstanceOf(LoggerProviderInterface::class)
->toBeInstanceOf(LoggerProviderSdkInterface::class);
});

it('registers the metric provider', function () {
expect(
$this->app->get(MeterProviderInterface::class)
)
->toBeInstanceOf(MeterProviderInterface::class)
->toBeInstanceOf(MeterProviderSdkInterface::class);
});

it('registers the tracer provider', function () {
expect(
$this->app->get(TracerProviderInterface::class)
)
->toBeInstanceOf(TracerProviderInterface::class)
->toBeInstanceOf(TracerProviderSdkInterface::class);
});

it('can get a logger from the logger provider', function () {
expect(
$this->app->get(LoggerProviderInterface::class)->getLogger('test')
)
->toBeInstanceOf(LoggerInterface::class);
});

it('can get a meter from the meter provider', function () {
expect(
$this->app->get(MeterProviderInterface::class)->getMeter('test')
)
->toBeInstanceOf(MeterInterface::class);
});

it('can get a tracer from the tracer provider', function () {
expect(
$this->app->get(TracerProviderInterface::class)->getTracer('test')
)
->toBeInstanceOf(TracerInterface::class);
});

it('returns no-op instances when the SDK is disabled', function () {
/** @var ConfigRepository $config */
$config = $this->app->get(ConfigRepository::class);

$config->set('telemetry.sdk.disabled', true);

expect(
$this->app->get(LoggerProviderInterface::class)->getLogger('test')
)
->toBeInstanceOf(NoopLogger::class);

expect(
$this->app->get(MeterProviderInterface::class)->getLogger('test')
)
->toBeInstanceOf(NoopMeter::class);

expect(
$this->app->get(TracerProviderInterface::class)->getLogger('test')
)
->toBeInstanceOf(NoopTracer::class);
});
4 changes: 3 additions & 1 deletion tests/Pest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

use Worksome\LaravelTelemetry\Tests\TestCase;

uses(TestCase::class)->in(__DIR__);
uses(TestCase::class)->in('Feature');
28 changes: 4 additions & 24 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -1,38 +1,18 @@
<?php

declare(strict_types=1);

namespace Worksome\LaravelTelemetry\Tests;

use Illuminate\Database\Eloquent\Factories\Factory;
use Orchestra\Testbench\TestCase as Orchestra;
use Worksome\LaravelTelemetry\LaravelTelemetryServiceProvider;

class TestCase extends Orchestra
abstract class TestCase extends Orchestra
{
protected function setUp(): void
{
parent::setUp();

Factory::guessFactoryNamesUsing(
fn (string $modelName) => 'Worksome\\LaravelTelemetry\\Database\\Factories\\' . class_basename(
$modelName
) . 'Factory'
);
}

protected function getPackageProviders($app)
protected function getPackageProviders($app): array
{
return [
LaravelTelemetryServiceProvider::class,
];
}

public function getEnvironmentSetUp($app)
{
config()->set('database.default', 'testing');

/*
$migration = include __DIR__.'/../database/migrations/create_laravel-telemetry_table.php.stub';
$migration->up();
*/
}
}

0 comments on commit 95916f3

Please sign in to comment.