From 197a7a4c2ca848e1e71c4ec1188f7491ce6d9acc Mon Sep 17 00:00:00 2001 From: Brett McBride Date: Thu, 31 Aug 2023 09:23:20 +1000 Subject: [PATCH] optional internal metrics (#1106) adding a configuration option OTEL_PHP_INTERNAL_METRICS_ENABLED which controls whether the SDK will emit its own metrics (eg batch processor state). --- examples/enabling_internal_metrics.php | 24 +++++++++++++++++++++ rector.php | 1 - src/SDK/Common/Configuration/Defaults.php | 1 + src/SDK/Common/Configuration/ValueTypes.php | 1 + src/SDK/Common/Configuration/Variables.php | 1 + src/SDK/SdkAutoloader.php | 5 +++-- tests/Unit/Context/ScopeTest.php | 2 +- 7 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 examples/enabling_internal_metrics.php diff --git a/examples/enabling_internal_metrics.php b/examples/enabling_internal_metrics.php new file mode 100644 index 000000000..ea3588a43 --- /dev/null +++ b/examples/enabling_internal_metrics.php @@ -0,0 +1,24 @@ +getTracer('demo')->spanBuilder('root')->startSpan()->end(); diff --git a/rector.php b/rector.php index e550e3de5..347905025 100644 --- a/rector.php +++ b/rector.php @@ -27,7 +27,6 @@ FlipTypeControlToUseExclusiveTypeRector::class, \Rector\Strict\Rector\Empty_\DisallowedEmptyRuleFixerRector::class, \Rector\CodeQuality\Rector\If_\ExplicitBoolCompareRector::class, - \Rector\RemovingStatic\Rector\ClassMethod\LocallyCalledStaticMethodToNonStaticRector::class, \Rector\CodeQuality\Rector\ClassMethod\LocallyCalledStaticMethodToNonStaticRector::class, ]); }; diff --git a/src/SDK/Common/Configuration/Defaults.php b/src/SDK/Common/Configuration/Defaults.php index e144de074..3ad59a63e 100644 --- a/src/SDK/Common/Configuration/Defaults.php +++ b/src/SDK/Common/Configuration/Defaults.php @@ -113,6 +113,7 @@ interface Defaults public const OTEL_PHP_TRACES_PROCESSOR = 'batch'; public const OTEL_PHP_DETECTORS = 'all'; public const OTEL_PHP_AUTOLOAD_ENABLED = 'false'; + public const OTEL_PHP_INTERNAL_METRICS_ENABLED = 'false'; public const OTEL_PHP_DISABLED_INSTRUMENTATIONS = []; public const OTEL_PHP_LOGS_PROCESSOR = 'batch'; } diff --git a/src/SDK/Common/Configuration/ValueTypes.php b/src/SDK/Common/Configuration/ValueTypes.php index 96dce43d9..81f6b0142 100644 --- a/src/SDK/Common/Configuration/ValueTypes.php +++ b/src/SDK/Common/Configuration/ValueTypes.php @@ -119,5 +119,6 @@ interface ValueTypes public const OTEL_PHP_TRACES_PROCESSOR = VariableTypes::ENUM; public const OTEL_PHP_DETECTORS = VariableTypes::LIST; public const OTEL_PHP_AUTOLOAD_ENABLED = VariableTypes::BOOL; + public const OTEL_PHP_INTERNAL_METRICS_ENABLED = VariableTypes::BOOL; public const OTEL_PHP_DISABLED_INSTRUMENTATIONS = VariableTypes::LIST; } diff --git a/src/SDK/Common/Configuration/Variables.php b/src/SDK/Common/Configuration/Variables.php index 705c212aa..797c1651c 100644 --- a/src/SDK/Common/Configuration/Variables.php +++ b/src/SDK/Common/Configuration/Variables.php @@ -135,5 +135,6 @@ interface Variables public const OTEL_PHP_LOGS_PROCESSOR = 'OTEL_PHP_LOGS_PROCESSOR'; public const OTEL_PHP_DETECTORS = 'OTEL_PHP_DETECTORS'; public const OTEL_PHP_AUTOLOAD_ENABLED = 'OTEL_PHP_AUTOLOAD_ENABLED'; + public const OTEL_PHP_INTERNAL_METRICS_ENABLED = 'OTEL_PHP_INTERNAL_METRICS_ENABLED'; //whether the SDK should emit its own metrics public const OTEL_PHP_DISABLED_INSTRUMENTATIONS = 'OTEL_PHP_DISABLED_INSTRUMENTATIONS'; } diff --git a/src/SDK/SdkAutoloader.php b/src/SDK/SdkAutoloader.php index a426d1712..53079a915 100644 --- a/src/SDK/SdkAutoloader.php +++ b/src/SDK/SdkAutoloader.php @@ -40,16 +40,17 @@ public static function autoload(): bool //@see https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/sdk-environment-variables.md#general-sdk-configuration return $configurator->withPropagator($propagator); } + $emitMetrics = Configuration::getBoolean(Variables::OTEL_PHP_INTERNAL_METRICS_ENABLED); $exporter = (new ExporterFactory())->create(); $meterProvider = (new MeterProviderFactory())->create(); - $spanProcessor = (new SpanProcessorFactory())->create($exporter, $meterProvider); + $spanProcessor = (new SpanProcessorFactory())->create($exporter, $emitMetrics ? $meterProvider : null); $tracerProvider = (new TracerProviderBuilder()) ->addSpanProcessor($spanProcessor) ->setSampler((new SamplerFactory())->create()) ->build(); - $loggerProvider = (new LoggerProviderFactory())->create($meterProvider); + $loggerProvider = (new LoggerProviderFactory())->create($emitMetrics ? $meterProvider : null); ShutdownHandler::register([$tracerProvider, 'shutdown']); ShutdownHandler::register([$meterProvider, 'shutdown']); diff --git a/tests/Unit/Context/ScopeTest.php b/tests/Unit/Context/ScopeTest.php index 7f0c2b0ce..08149ada5 100644 --- a/tests/Unit/Context/ScopeTest.php +++ b/tests/Unit/Context/ScopeTest.php @@ -116,7 +116,7 @@ public function test_scope_local_storage_is_preserved_between_attach_and_scope() $scope['key'] = 'value'; $scope = $storage->scope(); $this->assertNotNull($scope); - $this->assertArrayHasKey('key', $scope); /** @phpstan-ignore-line */ + $this->assertArrayHasKey('key', $scope); $this->assertSame('value', $scope['key']); unset($scope['key']);