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']);