Skip to content

Commit 401d04c

Browse files
authored
Add support for multi-instrument callbacks (#1202)
* Add support for multi-instrument callbacks https://opentelemetry.io/docs/specs/otel/metrics/api/#multiple-instrument-callbacks * Fix undefined offset on detach of batch callback with repeated instrument * Unify BatchObservableCallback and ObservableCallback * Add missing typehint * Do not bump API dependency Not required, SDK will continue to work with lower API versions, only `::batchObserve()` won't be usable.
1 parent 78bf761 commit 401d04c

11 files changed

+72
-7
lines changed

Metrics/AsynchronousInstrument.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenTelemetry\API\Metrics;
6+
7+
/**
8+
* Marker interface for asynchronous instruments.
9+
*/
10+
interface AsynchronousInstrument
11+
{
12+
}

Metrics/CounterInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use OpenTelemetry\Context\ContextInterface;
88

9-
interface CounterInterface
9+
interface CounterInterface extends SynchronousInstrument
1010
{
1111

1212
/**

Metrics/HistogramInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use OpenTelemetry\Context\ContextInterface;
88

9-
interface HistogramInterface
9+
interface HistogramInterface extends SynchronousInstrument
1010
{
1111

1212
/**

Metrics/MeterInterface.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,40 @@
77
interface MeterInterface
88
{
99

10+
/**
11+
* Reports measurements for multiple asynchronous instrument from a single callback.
12+
*
13+
* The callback receives an {@link ObserverInterface} for each instrument. All provided
14+
* instruments have to be created by this meter.
15+
*
16+
* ```php
17+
* $callback = $meter->batchObserve(
18+
* function(
19+
* ObserverInterface $usageObserver,
20+
* ObserverInterface $pressureObserver,
21+
* ): void {
22+
* [$usage, $pressure] = expensive_system_call();
23+
* $usageObserver->observe($usage);
24+
* $pressureObserver->observe($pressure);
25+
* },
26+
* $meter->createObservableCounter('usage', description: 'count of items used'),
27+
* $meter->createObservableGauge('pressure', description: 'force per unit area'),
28+
* );
29+
* ```
30+
*
31+
* @param callable $callback function responsible for reporting the measurements
32+
* @param AsynchronousInstrument $instrument first instrument to report measurements for
33+
* @param AsynchronousInstrument ...$instruments additional instruments to report measurements for
34+
* @return ObservableCallbackInterface token to detach callback
35+
*
36+
* @see https://opentelemetry.io/docs/specs/otel/metrics/api/#multiple-instrument-callbacks
37+
*/
38+
public function batchObserve(
39+
callable $callback,
40+
AsynchronousInstrument $instrument,
41+
AsynchronousInstrument ...$instruments
42+
): ObservableCallbackInterface;
43+
1044
/**
1145
* Creates a `Counter`.
1246
*

Metrics/Noop/NoopMeter.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,23 @@
44

55
namespace OpenTelemetry\API\Metrics\Noop;
66

7+
use OpenTelemetry\API\Metrics\AsynchronousInstrument;
78
use OpenTelemetry\API\Metrics\CounterInterface;
89
use OpenTelemetry\API\Metrics\HistogramInterface;
910
use OpenTelemetry\API\Metrics\MeterInterface;
11+
use OpenTelemetry\API\Metrics\ObservableCallbackInterface;
1012
use OpenTelemetry\API\Metrics\ObservableCounterInterface;
1113
use OpenTelemetry\API\Metrics\ObservableGaugeInterface;
1214
use OpenTelemetry\API\Metrics\ObservableUpDownCounterInterface;
1315
use OpenTelemetry\API\Metrics\UpDownCounterInterface;
1416

1517
final class NoopMeter implements MeterInterface
1618
{
19+
public function batchObserve(callable $callback, AsynchronousInstrument $instrument, AsynchronousInstrument ...$instruments): ObservableCallbackInterface
20+
{
21+
return new NoopObservableCallback();
22+
}
23+
1724
public function createCounter(string $name, ?string $unit = null, ?string $description = null, array $advisory = []): CounterInterface
1825
{
1926
return new NoopCounter();

Metrics/ObservableCounterInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace OpenTelemetry\API\Metrics;
66

7-
interface ObservableCounterInterface
7+
interface ObservableCounterInterface extends AsynchronousInstrument
88
{
99

1010
/**

Metrics/ObservableGaugeInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace OpenTelemetry\API\Metrics;
66

7-
interface ObservableGaugeInterface
7+
interface ObservableGaugeInterface extends AsynchronousInstrument
88
{
99

1010
/**

Metrics/ObservableUpDownCounterInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace OpenTelemetry\API\Metrics;
66

7-
interface ObservableUpDownCounterInterface
7+
interface ObservableUpDownCounterInterface extends AsynchronousInstrument
88
{
99

1010
/**

Metrics/SynchronousInstrument.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenTelemetry\API\Metrics;
6+
7+
/**
8+
* Marker interface for synchronous instruments.
9+
*/
10+
interface SynchronousInstrument
11+
{
12+
}

Metrics/UpDownCounterInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use OpenTelemetry\Context\ContextInterface;
88

9-
interface UpDownCounterInterface
9+
interface UpDownCounterInterface extends SynchronousInstrument
1010
{
1111

1212
/**

0 commit comments

Comments
 (0)