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).
This commit is contained in:
Brett McBride 2023-08-31 09:23:20 +10:00 committed by GitHub
parent 62a14d1c20
commit 197a7a4c2c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 31 additions and 4 deletions

View File

@ -0,0 +1,24 @@
<?php
declare(strict_types=1);
namespace OpenTelemetry\Example;
use OpenTelemetry\API\Globals;
/**
* The OpenTelemetry SDK is able to emit some metrics about its internal state. For example,
* batch span and log processor state.
* This feature can be enabled via the OTEL_PHP_INTERNAL_METRICS_ENABLED setting.
*/
putenv('OTEL_PHP_INTERNAL_METRICS_ENABLED=true');
putenv('OTEL_PHP_AUTOLOAD_ENABLED=true');
putenv('OTEL_TRACES_EXPORTER=console');
putenv('OTEL_METRICS_EXPORTER=console');
putenv('OTEL_LOGS_EXPORTER=console');
require __DIR__ . '/../vendor/autoload.php';
$tracerProvider = Globals::tracerProvider();
$tracerProvider->getTracer('demo')->spanBuilder('root')->startSpan()->end();

View File

@ -27,7 +27,6 @@ return static function (RectorConfig $rectorConfig): void {
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,
]);
};

View File

@ -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';
}

View File

@ -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;
}

View File

@ -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';
}

View File

@ -40,16 +40,17 @@ class SdkAutoloader
//@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']);

View File

@ -116,7 +116,7 @@ class ScopeTest extends TestCase
$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']);