Skip to content

Commit

Permalink
optional internal metrics (#1106)
Browse files Browse the repository at this point in the history
adding a configuration option OTEL_PHP_INTERNAL_METRICS_ENABLED which
controls whether the SDK will emit its own metrics (eg batch processor
state).
  • Loading branch information
brettmc authored Aug 30, 2023
1 parent 62a14d1 commit 197a7a4
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 4 deletions.
24 changes: 24 additions & 0 deletions examples/enabling_internal_metrics.php
Original file line number Diff line number Diff line change
@@ -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();
1 change: 0 additions & 1 deletion rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]);
};
1 change: 1 addition & 0 deletions src/SDK/Common/Configuration/Defaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
1 change: 1 addition & 0 deletions src/SDK/Common/Configuration/ValueTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
1 change: 1 addition & 0 deletions src/SDK/Common/Configuration/Variables.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
5 changes: 3 additions & 2 deletions src/SDK/SdkAutoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Context/ScopeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down

0 comments on commit 197a7a4

Please sign in to comment.