From ffce4f687fbc3eb543af14b12078eec9f4f5406b Mon Sep 17 00:00:00 2001 From: GCalmels Date: Tue, 30 Jul 2024 07:39:01 +0200 Subject: [PATCH] fix: changes after review --- readme.md | 5 ++- .../config/tracing/tracing.php | 7 ++++ src/Tracing/TogglableTracerProvider.php | 34 +++++++++++++++++++ 3 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 src/Tracing/TogglableTracerProvider.php diff --git a/readme.md b/readme.md index a7235ba..3043aa1 100644 --- a/readme.md +++ b/readme.md @@ -9,7 +9,6 @@ - Trace context propagation from incoming **requests** to **consumers**, **outgoing http calls** and **databases** (using [`sqlcommenter`](https://google.github.io/sqlcommenter/)) - Configurable blacklisting of requests by path to avoid useless traces, eg. `/metrics` or `/_healthz` - Automatic log inclusion with configurable log level and channels -- Disabling tracing with the `NoopTracerProvider` thanks to OTEL_SDK_DISABLED=true #### Metrics @@ -39,7 +38,7 @@ composer require worldia/instrumentation-bundle You will aso need to install an [exporter implementation](https://packagist.org/packages/open-telemetry/exporter-otlp?query=open-telemetry%2Fexporter-) and `APCu` is required by the prometheus exporter. -```` +``` Add to ```bundles.php```: ```php @@ -47,7 +46,7 @@ return [ // Other bundles Instrumentation\InstrumentationBundle::class => ['all' => true], ]; -```` +``` **Minimal configuration** See the complete [configuration reference here](./docs/config-reference.md) or run `bin/console config:dump-reference instrumentation`. diff --git a/src/DependencyInjection/config/tracing/tracing.php b/src/DependencyInjection/config/tracing/tracing.php index 1d207d0..e6e9226 100644 --- a/src/DependencyInjection/config/tracing/tracing.php +++ b/src/DependencyInjection/config/tracing/tracing.php @@ -18,6 +18,7 @@ use Instrumentation\Tracing\Propagation\RegexIncomingTraceHeaderResolver; use Instrumentation\Tracing\Sampling\TogglableSampler; use Instrumentation\Tracing\Serializer\Normalizer\ErrorNormalizer; +use Instrumentation\Tracing\TogglableTracerProvider; use Instrumentation\Tracing\TraceUrlGeneratorInterface; use Instrumentation\Tracing\Twig\Extension\TracingExtension; use OpenTelemetry\API\Trace\TracerProviderInterface; @@ -90,6 +91,12 @@ ]) ->public() + ->set(TogglableTracerProvider::class) + ->decorate(TracerProviderInterface::class) + ->args([ + service('.inner'), + ]) + ->set(MainSpanContextInterface::class, MainSpanContext::class) ->set(TracingHandler::class) diff --git a/src/Tracing/TogglableTracerProvider.php b/src/Tracing/TogglableTracerProvider.php new file mode 100644 index 0000000..801f77b --- /dev/null +++ b/src/Tracing/TogglableTracerProvider.php @@ -0,0 +1,34 @@ + + */ + +namespace Instrumentation\Tracing; + +use OpenTelemetry\API\Trace\NoopTracer; +use OpenTelemetry\API\Trace\TracerInterface; +use OpenTelemetry\API\Trace\TracerProviderInterface; +use OpenTelemetry\SDK\Sdk; + +class TogglableTracerProvider implements TracerProviderInterface +{ + public function __construct(private TracerProviderInterface $decorated) + { + } + + /** + * @param array $attributes + */ + public function getTracer(string $name, string|null $version = null, string|null $schemaUrl = null, iterable $attributes = []): TracerInterface + { + if (Sdk::isDisabled()) { + return new NoopTracer(); + } + + return $this->decorated->getTracer($name, $version, $schemaUrl, $attributes); + } +}