Skip to content

Commit

Permalink
feat: update to entirely rely on OTEL config
Browse files Browse the repository at this point in the history
  • Loading branch information
owenvoke committed Jul 22, 2024
1 parent 89ec35c commit 8d0ca5b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 61 deletions.
4 changes: 3 additions & 1 deletion config/telemetry.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
// The Configuration is based on OpenTelemetry's naming convention.
return [

'enabled' => env('LARAVEL_TELEMETRY_ENABLED', true),
'sdk' => [
'disabled' => ! env('LARAVEL_TELEMETRY_ENABLED', true)
],

'exporter' => [
'otlp' => [
Expand Down
77 changes: 17 additions & 60 deletions src/LaravelTelemetryServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,9 @@
use OpenTelemetry\API\LoggerHolder;
use OpenTelemetry\API\Metrics\MeterProviderInterface;
use OpenTelemetry\API\Trace\TracerProviderInterface;
use OpenTelemetry\SDK\Common\Attribute\Attributes;
use OpenTelemetry\SDK\Common\Configuration\Resolver\CompositeResolver;
use OpenTelemetry\SDK\Common\Instrumentation\InstrumentationScopeFactory;
use OpenTelemetry\SDK\Common\Time\ClockFactory;
use OpenTelemetry\SDK\Metrics\Exemplar\ExemplarFilter\WithSampledTraceExemplarFilter;
use OpenTelemetry\SDK\Metrics\MeterProvider;
use OpenTelemetry\SDK\Metrics\MeterProviderFactory;
use OpenTelemetry\SDK\Metrics\MeterProviderInterface as MeterProviderSdkInterface;
use OpenTelemetry\SDK\Metrics\MetricReader\ExportingReader;
use OpenTelemetry\SDK\Metrics\StalenessHandler\NoopStalenessHandlerFactory;
use OpenTelemetry\SDK\Metrics\View\CriteriaViewRegistry;
use OpenTelemetry\SDK\Registry;
use OpenTelemetry\SDK\Resource\ResourceInfoFactory;
use OpenTelemetry\SDK\Trace\TracerProviderFactory;
use OpenTelemetry\SDK\Trace\TracerProviderInterface as TracerProviderSdkInterface;
use Psr\Log\LoggerInterface;
Expand All @@ -32,34 +23,12 @@ class LaravelTelemetryServiceProvider extends ServiceProvider
public function register(): void
{
$this->app->singleton(MeterProviderSdkInterface::class, function () {
if (! $this->app->make(Repository::class)->get('telemetry.enabled')) {
return null;
}

return new MeterProvider(
null,
ResourceInfoFactory::defaultResource(),
ClockFactory::getDefault(),
Attributes::factory(),
new InstrumentationScopeFactory(Attributes::factory()),
[
new ExportingReader(
Registry::metricExporterFactory('otlp')->create(),
),
],
new CriteriaViewRegistry(),
new WithSampledTraceExemplarFilter(),
new NoopStalenessHandlerFactory(),
);
return (new MeterProviderFactory())->create();
});

$this->app->bind(MeterProviderInterface::class, MeterProviderSdkInterface::class);

$this->app->singleton(TracerProviderSdkInterface::class, function () {
if (! $this->app->make(Repository::class)->get('telemetry.enabled')) {
return null;
}

return (new TracerProviderFactory())->create();
});

Expand All @@ -77,42 +46,19 @@ public function boot(): void
'telemetry',
);

$this->app->beforeResolving(MeterProviderInterface::class, function () {
if (! $this->app->make(Repository::class)->get('telemetry.enabled')) {
return;
}

/** @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);
});

$this->app->beforeResolving(TracerProviderInterface::class, function () {
if (! $this->app->make(Repository::class)->get('telemetry.enabled')) {
return;
}

/** @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);
});
$this->app->beforeResolving(MeterProviderInterface::class, $this->prepareConfigResolver(...));
$this->app->beforeResolving(TracerProviderInterface::class, $this->prepareConfigResolver(...));

$this->callAfterResolving(Dispatcher::class, function (Dispatcher $event) {
if (! $this->app->make(Repository::class)->get('telemetry.enabled')) {
if ($this->app->make(Repository::class)->get('telemetry.sdk.disabled')) {
return;
}

$event->listen(WorkerStopping::class, WorkerStoppingFlush::class);
});

$this->app->terminating(function () {
if (! $this->app->make(Repository::class)->get('telemetry.enabled')) {
if ($this->app->make(Repository::class)->get('telemetry.sdk.disabled')) {
return;
}

Expand All @@ -129,4 +75,15 @@ public function boot(): void
}
});
}

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);
}
}

0 comments on commit 8d0ca5b

Please sign in to comment.