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

refactor(config): Prototype of config separation and auto loading #111

Closed
Closed
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
59 changes: 1 addition & 58 deletions src/DependencyInjection/OpenTelemetryExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,11 @@

namespace FriendsOfOpenTelemetry\OpenTelemetryBundle\DependencyInjection;

use FriendsOfOpenTelemetry\OpenTelemetryBundle\Instrumentation\InstrumentationTypeEnum;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension;

/**
* @phpstan-type InstrumentationConfig array{
* type?: string,
* tracing: TracingInstrumentationConfig,
* metering: MeteringInstrumentationConfig,
* }
* @phpstan-type TracingInstrumentationConfig array{
* enabled: bool,
* tracer: ?string,
* }
* @phpstan-type MeteringInstrumentationConfig array{
* enabled: bool,
* meter: ?string,
* }
*/
final class OpenTelemetryExtension extends ConfigurableExtension
{
protected function loadInternal(array $mergedConfig, ContainerBuilder $container): void
Expand All @@ -33,11 +17,10 @@ protected function loadInternal(array $mergedConfig, ContainerBuilder $container
$loader->load('services_logs.php');
$loader->load('services_metrics.php');
$loader->load('services_traces.php');
$loader->load('services_tracing_instrumentation.php');

$this->loadServiceParams($mergedConfig['service'], $container);
$this->loadInstrumentationParams($mergedConfig['instrumentation'], $container);

(new OpenTelemetryInstrumentationExtension())($mergedConfig['instrumentation'], $container, $loader);
(new OpenTelemetryTracesExtension())($mergedConfig['traces'], $container);
(new OpenTelemetryMetricsExtension())($mergedConfig['metrics'], $container);
(new OpenTelemetryLogsExtension())($mergedConfig['logs'], $container);
Expand Down Expand Up @@ -66,44 +49,4 @@ private function loadServiceParams(array $config, ContainerBuilder $container):
$config['environment'],
]);
}

/**
* @param array{
* cache: InstrumentationConfig,
* console: InstrumentationConfig,
* doctrine: InstrumentationConfig,
* http_client: InstrumentationConfig,
* http_kernel: InstrumentationConfig,
* mailer: InstrumentationConfig,
* messenger: InstrumentationConfig,
* twig: InstrumentationConfig,
* } $config
*/
private function loadInstrumentationParams(array $config, ContainerBuilder $container): void
{
foreach ($config as $name => $instrumentation) {
$container->setParameter(
sprintf('open_telemetry.instrumentation.%s.tracing.enabled', $name),
$instrumentation['tracing']['enabled'],
);
if (isset($instrumentation['type'])) {
$container->setParameter(
sprintf('open_telemetry.instrumentation.%s.type', $name),
InstrumentationTypeEnum::from($instrumentation['type']),
);
}
$container->setParameter(
sprintf('open_telemetry.instrumentation.%s.tracing.tracer', $name),
$instrumentation['tracing']['tracer'] ?? 'default_tracer',
);
$container->setParameter(
sprintf('open_telemetry.instrumentation.%s.metering.enabled', $name),
$instrumentation['metering']['enabled'],
);
$container->setParameter(
sprintf('open_telemetry.instrumentation.%s.metering.meter', $name),
$instrumentation['metering']['meter'] ?? 'default_meter',
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace FriendsOfOpenTelemetry\OpenTelemetryBundle\DependencyInjection;

use FriendsOfOpenTelemetry\OpenTelemetryBundle\Instrumentation\InstrumentationTypeEnum;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* @phpstan-type InstrumentationConfig array{
* type?: string,
* tracing: TracingInstrumentationConfig,
* metering: MeteringInstrumentationConfig,
* }
* @phpstan-type TracingInstrumentationConfig array{
* enabled: bool,
* tracer: ?string,
* }
* @phpstan-type MeteringInstrumentationConfig array{
* enabled: bool,
* meter: ?string,
* }
*/
final class OpenTelemetryInstrumentationExtension
{
/**
* @param array{
* cache: InstrumentationConfig,
* console: InstrumentationConfig,
* doctrine: InstrumentationConfig,
* http_client: InstrumentationConfig,
* http_kernel: InstrumentationConfig,
* mailer: InstrumentationConfig,
* messenger: InstrumentationConfig,
* twig: InstrumentationConfig,
* } $config
*/
public function __invoke(array $config, ContainerBuilder $container, LoaderInterface $loader): void
{
foreach ($config as $name => $instrumentation) {
$container->setParameter(
sprintf('open_telemetry.instrumentation.%s.tracing.enabled', $name),
$instrumentation['tracing']['enabled'],
);

if (isset($instrumentation['type'])) {
$container->setParameter(
sprintf('open_telemetry.instrumentation.%s.type', $name),
InstrumentationTypeEnum::from($instrumentation['type']),
);
}
$container->setParameter(
sprintf('open_telemetry.instrumentation.%s.tracing.tracer', $name),
$instrumentation['tracing']['tracer'] ?? 'default_tracer',
);
$container->setParameter(
sprintf('open_telemetry.instrumentation.%s.metering.enabled', $name),
$instrumentation['metering']['enabled'],
);
$container->setParameter(
sprintf('open_telemetry.instrumentation.%s.metering.meter', $name),
$instrumentation['metering']['meter'] ?? 'default_meter',
);

if ($instrumentation['tracing']['enabled']) {
$loader->load('instrumentation/'.$name.'.php');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Look

}
}
}
}
23 changes: 23 additions & 0 deletions src/Resources/config/instrumentation/cache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

use FriendsOfOpenTelemetry\OpenTelemetryBundle\Instrumentation\Symfony\Cache\TagAwareTraceableCacheAdapter;
use FriendsOfOpenTelemetry\OpenTelemetryBundle\Instrumentation\Symfony\Cache\TraceableCacheAdapter;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

use function Symfony\Component\DependencyInjection\Loader\Configurator\service;

return static function (ContainerConfigurator $container): void {
$container->services()
->defaults()
->private()
->set('open_telemetry.instrumentation.cache.trace.adapter', TraceableCacheAdapter::class)
->arg('$tracer', service('open_telemetry.traces.default_tracer'))
->abstract()
->tag('monolog.logger', ['channel' => 'open_telemetry'])

->set('open_telemetry.instrumentation.cache.trace.tag_aware_adapter', TagAwareTraceableCacheAdapter::class)
->arg('$tracer', service('open_telemetry.traces.default_tracer'))
->abstract()
->tag('monolog.logger', ['channel' => 'open_telemetry'])
;
};
16 changes: 16 additions & 0 deletions src/Resources/config/instrumentation/console.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

use FriendsOfOpenTelemetry\OpenTelemetryBundle\Instrumentation\Symfony\Console\TraceableConsoleEventSubscriber;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

use function Symfony\Component\DependencyInjection\Loader\Configurator\service;

return static function (ContainerConfigurator $container): void {
$container->services()
->defaults()
->private()
->set('open_telemetry.instrumentation.console.trace.event_subscriber', TraceableConsoleEventSubscriber::class)
->arg('$tracer', service('open_telemetry.traces.default_tracer'))
->tag('kernel.event_subscriber')
;
};
17 changes: 17 additions & 0 deletions src/Resources/config/instrumentation/doctrine.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

use FriendsOfOpenTelemetry\OpenTelemetryBundle\Instrumentation\Doctrine\Middleware\TraceableMiddleware as TraceableDoctrineMiddleware;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

use function Symfony\Component\DependencyInjection\Loader\Configurator\service;

return static function (ContainerConfigurator $container): void {
$container->services()
->defaults()
->private()
->set('open_telemetry.instrumentation.doctrine.trace.middleware', TraceableDoctrineMiddleware::class)
->arg('$tracer', service('open_telemetry.traces.default_tracer'))
->tag('doctrine.middleware')
->tag('monolog.logger', ['channel' => 'open_telemetry'])
;
};
16 changes: 16 additions & 0 deletions src/Resources/config/instrumentation/http_client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

use FriendsOfOpenTelemetry\OpenTelemetryBundle\Instrumentation\Symfony\HttpClient\TraceableHttpClient;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

use function Symfony\Component\DependencyInjection\Loader\Configurator\service;

return static function (ContainerConfigurator $container): void {
$container->services()
->defaults()
->private()
->set('open_telemetry.instrumentation.http_client.trace.client', TraceableHttpClient::class)
->arg('$tracer', service('open_telemetry.traces.default_tracer'))
->tag('monolog.logger', ['channel' => 'open_telemetry'])
;
};
25 changes: 25 additions & 0 deletions src/Resources/config/instrumentation/http_kernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

use FriendsOfOpenTelemetry\OpenTelemetryBundle\Instrumentation\Symfony\Framework\Routing\TraceableRouteLoader;
use FriendsOfOpenTelemetry\OpenTelemetryBundle\Instrumentation\Symfony\HttpKernel\TraceableHttpKernelEventSubscriber;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

use function Symfony\Component\DependencyInjection\Loader\Configurator\service;

return static function (ContainerConfigurator $container): void {
$container->services()
->defaults()
->private()
->set('open_telemetry.instrumentation.http_kernel.trace.event_subscriber', TraceableHttpKernelEventSubscriber::class)
->arg('$tracer', service('open_telemetry.traces.default_tracer'))
->arg('$propagator', service('open_telemetry.propagator_text_map.noop'))
->arg('$propagationGetter', service('open_telemetry.propagation_getter.headers'))
->tag('kernel.event_subscriber')
->tag('monolog.logger', ['channel' => 'open_telemetry'])

->set('open_telemetry.instrumentation.http_kernel.trace.route_loader', TraceableRouteLoader::class)
->decorate('routing.loader')
->arg('$loader', service('.inner'))
->tag('monolog.logger', ['channel' => 'open_telemetry'])
;
};
31 changes: 31 additions & 0 deletions src/Resources/config/instrumentation/mailer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

use FriendsOfOpenTelemetry\OpenTelemetryBundle\Instrumentation\Symfony\Mailer\TraceableMailer;
use FriendsOfOpenTelemetry\OpenTelemetryBundle\Instrumentation\Symfony\Mailer\TraceableMailerTransport;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

use function Symfony\Component\DependencyInjection\Loader\Configurator\service;

return static function (ContainerConfigurator $container): void {
$container->services()
->defaults()
->private()
->set('open_telemetry.instrumentation.mailer.trace.transports', TraceableMailerTransport::class)
->decorate('mailer.transports')
->arg('$tracer', service('open_telemetry.traces.default_tracer'))
->arg('$transport', service('.inner'))
->tag('monolog.logger', ['channel' => 'open_telemetry'])

->set('open_telemetry.instrumentation.mailer.trace.default_transport', TraceableMailerTransport::class)
->decorate('mailer.default_transport')
->arg('$tracer', service('open_telemetry.traces.default_tracer'))
->arg('$transport', service('.inner'))
->tag('monolog.logger', ['channel' => 'open_telemetry'])

->set('open_telemetry.instrumentation.mailer.trace.mailer', TraceableMailer::class)
->decorate('mailer.mailer')
->arg('$tracer', service('open_telemetry.traces.default_tracer'))
->arg('$mailer', service('.inner'))
->tag('monolog.logger', ['channel' => 'open_telemetry'])
;
};
31 changes: 31 additions & 0 deletions src/Resources/config/instrumentation/messenger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

use FriendsOfOpenTelemetry\OpenTelemetryBundle\Instrumentation\Symfony\Messenger\TraceableMessengerMiddleware;
use FriendsOfOpenTelemetry\OpenTelemetryBundle\Instrumentation\Symfony\Messenger\TraceableMessengerTransport;
use FriendsOfOpenTelemetry\OpenTelemetryBundle\Instrumentation\Symfony\Messenger\TraceableMessengerTransportFactory;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

use function Symfony\Component\DependencyInjection\Loader\Configurator\service;

return static function (ContainerConfigurator $container): void {
$container->services()
->defaults()
->private()
->set('open_telemetry.instrumentation.messenger.trace.transport', TraceableMessengerTransport::class)
->arg('$tracer', service('open_telemetry.traces.default_tracer'))
->tag('monolog.logger', ['channel' => 'open_telemetry'])

->set('open_telemetry.instrumentation.messenger.trace.transport_factory', TraceableMessengerTransportFactory::class)
->arg('$tracer', service('open_telemetry.traces.default_tracer'))
->arg('$transportFactory', service('messenger.transport_factory'))
->tag('messenger.transport_factory')
->tag('kernel.reset', ['method' => 'reset'])
->tag('monolog.logger', ['channel' => 'open_telemetry'])
->alias('messenger.transport.open_telemetry_tracer.factory', 'open_telemetry.instrumentation.messenger.trace.transport_factory')

->set('open_telemetry.instrumentation.messenger.trace.middleware', TraceableMessengerMiddleware::class)
->arg('$tracer', service('open_telemetry.traces.default_tracer'))
->tag('monolog.logger', ['channel' => 'open_telemetry'])
->alias('messenger.middleware.open_telemetry_tracer', 'open_telemetry.instrumentation.messenger.trace.middleware')
;
};
17 changes: 17 additions & 0 deletions src/Resources/config/instrumentation/twig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

use FriendsOfOpenTelemetry\OpenTelemetryBundle\Instrumentation\Twig\TraceableTwigExtension;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

use function Symfony\Component\DependencyInjection\Loader\Configurator\service;

return static function (ContainerConfigurator $container): void {
$container->services()
->defaults()
->private()
->set('open_telemetry.instrumentation.twig.trace.extension', TraceableTwigExtension::class)
->arg('$tracer', service('open_telemetry.traces.default_tracer'))
->tag('twig.extension')
->tag('monolog.logger', ['channel' => 'open_telemetry'])
;
};