Skip to content

Commit 197a7a4

Browse files
authored
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).
1 parent 62a14d1 commit 197a7a4

File tree

7 files changed

+31
-4
lines changed

7 files changed

+31
-4
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenTelemetry\Example;
6+
7+
use OpenTelemetry\API\Globals;
8+
9+
/**
10+
* The OpenTelemetry SDK is able to emit some metrics about its internal state. For example,
11+
* batch span and log processor state.
12+
* This feature can be enabled via the OTEL_PHP_INTERNAL_METRICS_ENABLED setting.
13+
*/
14+
15+
putenv('OTEL_PHP_INTERNAL_METRICS_ENABLED=true');
16+
putenv('OTEL_PHP_AUTOLOAD_ENABLED=true');
17+
putenv('OTEL_TRACES_EXPORTER=console');
18+
putenv('OTEL_METRICS_EXPORTER=console');
19+
putenv('OTEL_LOGS_EXPORTER=console');
20+
21+
require __DIR__ . '/../vendor/autoload.php';
22+
23+
$tracerProvider = Globals::tracerProvider();
24+
$tracerProvider->getTracer('demo')->spanBuilder('root')->startSpan()->end();

rector.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
FlipTypeControlToUseExclusiveTypeRector::class,
2828
\Rector\Strict\Rector\Empty_\DisallowedEmptyRuleFixerRector::class,
2929
\Rector\CodeQuality\Rector\If_\ExplicitBoolCompareRector::class,
30-
\Rector\RemovingStatic\Rector\ClassMethod\LocallyCalledStaticMethodToNonStaticRector::class,
3130
\Rector\CodeQuality\Rector\ClassMethod\LocallyCalledStaticMethodToNonStaticRector::class,
3231
]);
3332
};

src/SDK/Common/Configuration/Defaults.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ interface Defaults
113113
public const OTEL_PHP_TRACES_PROCESSOR = 'batch';
114114
public const OTEL_PHP_DETECTORS = 'all';
115115
public const OTEL_PHP_AUTOLOAD_ENABLED = 'false';
116+
public const OTEL_PHP_INTERNAL_METRICS_ENABLED = 'false';
116117
public const OTEL_PHP_DISABLED_INSTRUMENTATIONS = [];
117118
public const OTEL_PHP_LOGS_PROCESSOR = 'batch';
118119
}

src/SDK/Common/Configuration/ValueTypes.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,5 +119,6 @@ interface ValueTypes
119119
public const OTEL_PHP_TRACES_PROCESSOR = VariableTypes::ENUM;
120120
public const OTEL_PHP_DETECTORS = VariableTypes::LIST;
121121
public const OTEL_PHP_AUTOLOAD_ENABLED = VariableTypes::BOOL;
122+
public const OTEL_PHP_INTERNAL_METRICS_ENABLED = VariableTypes::BOOL;
122123
public const OTEL_PHP_DISABLED_INSTRUMENTATIONS = VariableTypes::LIST;
123124
}

src/SDK/Common/Configuration/Variables.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,5 +135,6 @@ interface Variables
135135
public const OTEL_PHP_LOGS_PROCESSOR = 'OTEL_PHP_LOGS_PROCESSOR';
136136
public const OTEL_PHP_DETECTORS = 'OTEL_PHP_DETECTORS';
137137
public const OTEL_PHP_AUTOLOAD_ENABLED = 'OTEL_PHP_AUTOLOAD_ENABLED';
138+
public const OTEL_PHP_INTERNAL_METRICS_ENABLED = 'OTEL_PHP_INTERNAL_METRICS_ENABLED'; //whether the SDK should emit its own metrics
138139
public const OTEL_PHP_DISABLED_INSTRUMENTATIONS = 'OTEL_PHP_DISABLED_INSTRUMENTATIONS';
139140
}

src/SDK/SdkAutoloader.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,17 @@ public static function autoload(): bool
4040
//@see https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/sdk-environment-variables.md#general-sdk-configuration
4141
return $configurator->withPropagator($propagator);
4242
}
43+
$emitMetrics = Configuration::getBoolean(Variables::OTEL_PHP_INTERNAL_METRICS_ENABLED);
4344

4445
$exporter = (new ExporterFactory())->create();
4546
$meterProvider = (new MeterProviderFactory())->create();
46-
$spanProcessor = (new SpanProcessorFactory())->create($exporter, $meterProvider);
47+
$spanProcessor = (new SpanProcessorFactory())->create($exporter, $emitMetrics ? $meterProvider : null);
4748
$tracerProvider = (new TracerProviderBuilder())
4849
->addSpanProcessor($spanProcessor)
4950
->setSampler((new SamplerFactory())->create())
5051
->build();
5152

52-
$loggerProvider = (new LoggerProviderFactory())->create($meterProvider);
53+
$loggerProvider = (new LoggerProviderFactory())->create($emitMetrics ? $meterProvider : null);
5354

5455
ShutdownHandler::register([$tracerProvider, 'shutdown']);
5556
ShutdownHandler::register([$meterProvider, 'shutdown']);

tests/Unit/Context/ScopeTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public function test_scope_local_storage_is_preserved_between_attach_and_scope()
116116
$scope['key'] = 'value';
117117
$scope = $storage->scope();
118118
$this->assertNotNull($scope);
119-
$this->assertArrayHasKey('key', $scope); /** @phpstan-ignore-line */
119+
$this->assertArrayHasKey('key', $scope);
120120
$this->assertSame('value', $scope['key']);
121121

122122
unset($scope['key']);

0 commit comments

Comments
 (0)