Skip to content

Commit

Permalink
clean logging
Browse files Browse the repository at this point in the history
  • Loading branch information
cdaguerre committed Jan 13, 2025
1 parent 27be902 commit bec5380
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 22 deletions.
6 changes: 6 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ OTEL_EXPORTER_OTLP_ENDPOINT=http://<your-telemetry-collector>:4318
Enable the extension. See the complete [configuration reference here](./docs/config-reference.md) or run ```bin/console config:dump-reference instrumentation```.
```yaml
instrumentation: ~

monolog:
handlers:
otel:
type: service
id: Instrumentation\Logging\OtelHandler
```
### Usage
Expand Down
18 changes: 3 additions & 15 deletions src/DependencyInjection/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

namespace Instrumentation\DependencyInjection;

use Instrumentation\Logging\OtelHandler;
use Instrumentation\Tracing\Bridge\TraceUrlGenerator;
use Instrumentation\Tracing\Bridge\TraceUrlGeneratorInterface;
use Instrumentation\Tracing\Doctrine\Instrumentation\DBAL\Middleware as InstrumentationMiddleware;
Expand Down Expand Up @@ -41,8 +40,10 @@ public function load(array $configs, ContainerBuilder $container): void
$container->setParameter('service.name', $config['resource']['service.name']);

$this->loadSemConv($config['resource'], $container);
$this->loadLogging($config['logging'], $container);

if ($this->isConfigEnabled($container, $config['logging'])) {
$this->loadLogging($config['baggage'], $container);
}
if ($this->isConfigEnabled($container, $config['baggage'])) {
$this->loadBaggage($config['baggage'], $container);
}
Expand All @@ -56,17 +57,6 @@ public function load(array $configs, ContainerBuilder $container): void

public function prepend(ContainerBuilder $container): void
{
if ($container->hasExtension('monolog')) {
$container->prependExtensionConfig('monolog', [
'handlers' => [
'otel' => [
'type' => 'service',
'id' => OtelHandler::class,
],
],
]);
}

if ($container->hasExtension('twig')) {
$container->prependExtensionConfig('twig', [
'paths' => [
Expand Down Expand Up @@ -200,8 +190,6 @@ protected function loadLogging(array $config, ContainerBuilder $container): void
{
$loader = $this->getLoader('logging', $container);

$container->setParameter('logging.enabled', $this->isConfigEnabled($container, $config));

$loader->load('logging.php');
}

Expand Down
2 changes: 0 additions & 2 deletions src/DependencyInjection/config/logging/logging.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

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

return static function (ContainerConfigurator $container) {
Expand All @@ -39,7 +38,6 @@

->set(Logging\OtelHandler::class)
->args([
'$enabled' => param('logging.enabled'),
'$loggerProvider' => service(LoggerProviderInterface::class),
'$level' => Level::Debug,
'$bubble' => true,
Expand Down
6 changes: 1 addition & 5 deletions src/Logging/OtelHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class OtelHandler extends AbstractProcessingHandler
**/
private array $loggers = [];

public function __construct(private readonly bool $enabled, private readonly API\LoggerProviderInterface $loggerProvider, Level $level, bool $bubble = true)
public function __construct(private readonly API\LoggerProviderInterface $loggerProvider, Level $level, bool $bubble = true)
{
parent::__construct($level, $bubble);

Expand All @@ -50,10 +50,6 @@ protected function getDefaultFormatter(): FormatterInterface

protected function write(LogRecord $record): void
{
if (!$this->enabled) {
return;
}

$formatted = $record['formatted'];
$logRecord = (new API\LogRecord())
->setTimestamp((int) $record['datetime']->format('Uu') * 1000) // @phpstan-ignore-line
Expand Down

0 comments on commit bec5380

Please sign in to comment.