Skip to content

Commit bee2f50

Browse files
brettmcNevay
andauthored
update default temporality to spec (#1715)
* update default temporality to spec The spec requirements for temporality have changed since our implementation of metrics. Now, Cumulative is the preferred default. A temporality preference of LowMemory is now in spec, which was partially implemented for declarative config, but not for environment-based autoloading. Added some integration tests to validate temporality settings for different preference inputs. Added some non-spec otlp transports (stdout/nd-json and memory/json), since they exercise more of the otlp metrics codebase than the existing InMemory exporters, and made integration testing for temporality easier. * remove invalid default OTEL_METRICS_EXPORTER, fix test name * move test transports under tests/ * remove stream temporality * tests * temporality is a function of instrument kind * temporality selection centralize temporality selection into a trait and re-use across metric exporters remove temporality as a function of stream type (spec now says that temporality is a function of instrument + preference * add check and warning that async callbacks must not return a value ...and add tests that gauges do not have a temporality * add stdout factories * remove unused stdout transport * remove stdout transport, hash _register.php files * update temporality upgrade docs * use AggregationTemporalitySelectorInterface to provide temporality preference based on opentelemetry-java's implementation, the spec-defined preferences are defined in SDK/Metrics/AggregationTemporalitySelector * test gauge temporality, update docs * fix gauge temporality, add tests based on Java's * Update src/SDK/Metrics/AggregationTemporalitySelector.php Co-authored-by: Tobias Bachert <[email protected]> * update test --------- Co-authored-by: Tobias Bachert <[email protected]>
1 parent c7518aa commit bee2f50

36 files changed

+494
-104
lines changed

.github/workflows/php.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ jobs:
8787
uses: actions/cache@v4
8888
with:
8989
path: vendor
90-
key: ${{ runner.os }}-${{ matrix.php-version }}-composer-${{ hashFiles('**/composer.json') }}
90+
key: ${{ runner.os }}-${{ matrix.php-version }}-composer-${{ hashFiles('**/composer.json', '**/_register.php') }}
9191
- name: Cache test tools
9292
id: test-tools-cache
9393
uses: actions/cache@v4

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"readme": "./README.md",
88
"license": "Apache-2.0",
99
"require": {
10-
"php": "^8.1",
10+
"php": "^8.2",
1111
"google/protobuf": "^3.22 || ^4.0",
1212
"nyholm/psr7-server": "^1.1",
1313
"php-http/discovery": "^1.14",

docs/upgrading.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,16 @@ providing a higher priority for the same type).
3030
Methods which previously accepted `Temporality|string`, or `InstrumentType|string` no longer accept strings.
3131
`$advisory` is now a required parameter for `OpenTelemetry\SDK\Metrics\DefaultAggregationProviderInterface`.
3232

33+
If the SDK is configured via autoloading (using environment variables or declarative (yaml) config), the default temporality
34+
is `Cumulative` for all instruments, per [spec](https://github.com/open-telemetry/opentelemetry-specification/blob/v1.48.0/specification/metrics/sdk_exporters/otlp.md#general).
35+
This can be changed by setting the temporality preference via `OTEL_METRIC_TEMPORALITY_PREFERENCE` environment variable. Possible
36+
values are `cumulative`, `delta`, or `lowmemory`.
37+
38+
If the SDK is configured programmatically, an exporter should be configured with an `AggregationTemporalitySelectorInterface`.
39+
The SDK-provided implementations are `AggregationTemporalitySelector::alwaysCumulative()`, `::deltaPreferred()`,
40+
and `::lowMemory()`. The default selector is cumulative.
41+
3342
#### TracerProvider
3443
`TracerProvider` constructor now accepts a `SpanProcessorInterface` as the first argument, rather than an array of
3544
`SpanProcessorInterface`s. If multiple processors are required, they should be added to a `MultiSpanProcessor` (which
3645
is what happened internally in 1.x anyway).
37-

examples/metrics/getting_started.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
declare(strict_types=1);
44

55
use OpenTelemetry\API\Metrics\ObserverInterface;
6-
use OpenTelemetry\SDK\Metrics\Data\Temporality;
6+
use OpenTelemetry\SDK\Metrics\AggregationTemporalitySelector;
77
use OpenTelemetry\SDK\Metrics\MeterProvider;
88
use OpenTelemetry\SDK\Metrics\MetricExporter\ConsoleMetricExporter;
99
use OpenTelemetry\SDK\Metrics\MetricReader\ExportingReader;
@@ -18,7 +18,7 @@
1818
*/
1919

2020
$reader = new ExportingReader(
21-
new ConsoleMetricExporter(Temporality::DELTA)
21+
new ConsoleMetricExporter(AggregationTemporalitySelector::deltaPreferred())
2222
);
2323

2424
$meterProvider = MeterProvider::builder()

examples/metrics/weak-reference-observables.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
declare(strict_types=1);
44

55
use OpenTelemetry\API\Metrics\ObserverInterface;
6-
use OpenTelemetry\SDK\Metrics\Data\Temporality;
6+
use OpenTelemetry\SDK\Metrics\AggregationTemporalitySelector;
77
use OpenTelemetry\SDK\Metrics\MeterProvider;
88
use OpenTelemetry\SDK\Metrics\MetricExporter\ConsoleMetricExporter;
99
use OpenTelemetry\SDK\Metrics\MetricReader\ExportingReader;
@@ -19,7 +19,7 @@
1919
*/
2020

2121
$reader = new ExportingReader(
22-
new ConsoleMetricExporter(Temporality::DELTA)
22+
new ConsoleMetricExporter(AggregationTemporalitySelector::deltaPreferred())
2323
);
2424

2525
$meterProvider = MeterProvider::builder()

files/collector/otel-collector-config.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ receivers:
22
otlp:
33
protocols:
44
grpc:
5+
endpoint: 0.0.0.0:4317
56
http:
7+
endpoint: 0.0.0.0:4318
68
zipkin:
79

810
exporters:

phpstan.neon.dist

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ parameters:
5252
paths:
5353
- src/SDK/Common/InstrumentationScope
5454
-
55-
message: "#.*expects Google\\\\Protobuf\\\\RepeatedField.*#"
55+
message: "#.*RepeatedField.*#"
5656
paths:
5757
- src/Contrib/Otlp
58+
-
59+
message: "#Access to an undefined property .*#"
60+
paths:
61+
- tests/Integration/SDK/Metrics
62+
-
63+
message: "#.*assertSame.*will always evaluate to false.#"
64+
paths:
65+
- tests/Unit/SDK/Metrics/MetricRegistry/MetricRegistryTest.php

rector.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Rector\CodeQuality\Rector\Identical\FlipTypeControlToUseExclusiveTypeRector;
77
use Rector\CodeQuality\Rector\If_\ExplicitBoolCompareRector;
88
use Rector\Config\RectorConfig;
9+
use Rector\Php71\Rector\FuncCall\RemoveExtraParametersRector;
910
use Rector\Php82\Rector\Class_\ReadOnlyClassRector;
1011
use Rector\PHPUnit\Set\PHPUnitSetList;
1112
use Rector\Strict\Rector\Empty_\DisallowedEmptyRuleFixerRector;
@@ -31,5 +32,6 @@
3132
ExplicitBoolCompareRector::class,
3233
LocallyCalledStaticMethodToNonStaticRector::class,
3334
ReadOnlyClassRector::class,
35+
RemoveExtraParametersRector::class
3436
]);
3537
};

src/Config/SDK/ComponentProvider/Metrics/MetricExporterOtlpFile.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use OpenTelemetry\Contrib\Otlp\ContentTypes;
1414
use OpenTelemetry\Contrib\Otlp\MetricExporter;
1515
use OpenTelemetry\SDK\Common\Services\Loader;
16-
use OpenTelemetry\SDK\Metrics\Data\Temporality;
16+
use OpenTelemetry\SDK\Metrics\AggregationTemporalitySelector;
1717
use OpenTelemetry\SDK\Metrics\MetricExporterInterface;
1818
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
1919
use Symfony\Component\Config\Definition\Builder\NodeBuilder;
@@ -35,16 +35,16 @@ public function createPlugin(array $properties, Context $context): MetricExporte
3535
{
3636
$endpoint = OutputStreamParser::parse($properties['output_stream']);
3737

38-
$temporality = match ($properties['temporality_preference']) {
39-
'cumulative' => Temporality::CUMULATIVE,
40-
'delta' => Temporality::DELTA,
41-
'lowmemory' => null,
38+
$selector = match ($properties['temporality_preference']) {
39+
'cumulative' => AggregationTemporalitySelector::alwaysCumulative(),
40+
'delta' => AggregationTemporalitySelector::deltaPreferred(),
41+
'lowmemory' => AggregationTemporalitySelector::lowMemory(),
4242
};
4343

4444
return new MetricExporter(Loader::transportFactory('stream')->create(
4545
endpoint: $endpoint,
4646
contentType: ContentTypes::NDJSON,
47-
), $temporality);
47+
), $selector);
4848
}
4949

5050
public function getConfig(ComponentProviderRegistry $registry, NodeBuilder $builder): ArrayNodeDefinition

src/Config/SDK/ComponentProvider/Metrics/MetricExporterOtlpGrpc.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use OpenTelemetry\Contrib\Otlp\Protocols;
1717
use OpenTelemetry\SDK\Common\Configuration\Parser\MapParser;
1818
use OpenTelemetry\SDK\Common\Services\Loader;
19-
use OpenTelemetry\SDK\Metrics\Data\Temporality;
19+
use OpenTelemetry\SDK\Metrics\AggregationTemporalitySelector;
2020
use OpenTelemetry\SDK\Metrics\MetricExporterInterface;
2121
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
2222
use Symfony\Component\Config\Definition\Builder\NodeBuilder;
@@ -49,10 +49,10 @@ public function createPlugin(array $properties, Context $context): MetricExporte
4949

5050
$headers = array_column($properties['headers'], 'value', 'name') + MapParser::parse($properties['headers_list']);
5151

52-
$temporality = match ($properties['temporality_preference']) {
53-
'cumulative' => Temporality::CUMULATIVE,
54-
'delta' => Temporality::DELTA,
55-
'lowmemory' => null,
52+
$selector = match ($properties['temporality_preference']) {
53+
'cumulative' => AggregationTemporalitySelector::alwaysCumulative(),
54+
'delta' => AggregationTemporalitySelector::deltaPreferred(),
55+
'lowmemory' => AggregationTemporalitySelector::lowMemory(),
5656
};
5757

5858
return new MetricExporter(Loader::transportFactory($protocol)->create(
@@ -64,7 +64,7 @@ public function createPlugin(array $properties, Context $context): MetricExporte
6464
cacert: $properties['certificate_file'],
6565
cert: $properties['client_certificate_file'],
6666
key: $properties['client_certificate_file'],
67-
), $temporality);
67+
), $selector);
6868
}
6969

7070
public function getConfig(ComponentProviderRegistry $registry, NodeBuilder $builder): ArrayNodeDefinition

0 commit comments

Comments
 (0)