Skip to content

Commit 3f3186a

Browse files
committed
Add MeasurementProcessor specification to Metrics SDK
TODO Closes open-telemetry#4298.
1 parent 50027a1 commit 3f3186a

File tree

1 file changed

+76
-2
lines changed

1 file changed

+76
-2
lines changed

specification/metrics/sdk.md

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ linkTitle: SDK
5151
* [Instrument advisory parameters](#instrument-advisory-parameters)
5252
* [Instrument enabled](#instrument-enabled)
5353
- [Attribute limits](#attribute-limits)
54+
- [MeasurementProcessor](#measurementprocessor)
55+
* [MeasurementProcessor operations](#measurementprocessor-operations)
56+
+ [OnMeasure](#onmeasure)
57+
+ [Shutdown](#shutdown-1)
58+
+ [ForceFlush](#forceflush-1)
5459
- [Exemplar](#exemplar)
5560
* [ExemplarFilter](#exemplarfilter)
5661
+ [AlwaysOn](#alwayson)
@@ -64,9 +69,9 @@ linkTitle: SDK
6469
- [MetricReader](#metricreader)
6570
* [MetricReader operations](#metricreader-operations)
6671
+ [Collect](#collect)
67-
+ [Shutdown](#shutdown-1)
72+
+ [Shutdown](#shutdown-2)
6873
* [Periodic exporting MetricReader](#periodic-exporting-metricreader)
69-
+ [ForceFlush](#forceflush-1)
74+
+ [ForceFlush](#forceflush-2)
7075
- [MetricExporter](#metricexporter)
7176
* [Push Metric Exporter](#push-metric-exporter)
7277
+ [Interface Definition](#interface-definition)
@@ -986,6 +991,75 @@ Attributes which belong to Metrics are exempt from the
986991
time. Attribute truncation or deletion could affect identity of metric time
987992
series and the topic requires further analysis.
988993

994+
## MeasurementProcessor
995+
996+
`MeasurementProcessor` is an interface which allows hooks when a `Measurement` is recorded by an `Instrument`.
997+
998+
`MeasurementProcessors` can be registered directly on SDK `MeterProvider` and they are invoked in the same order as they were registered.
999+
1000+
SDK MUST allow users to implement and configure custom processors.
1001+
1002+
The following diagram shows `MeasurementProcessor`'s relationship to other components in the SDK:
1003+
1004+
```plaintext
1005+
+------------------+
1006+
| MeterProvider | +----------------------+ +-----------------+
1007+
| Meter A | Measurements... | | Metrics... | |
1008+
| Instrument X |-----------------> MeasurementProcessor +------------> In-memory state |
1009+
| Instrument Y + | | | |
1010+
| Meter B | +----------------------+ +-----------------+
1011+
| Instrument Z |
1012+
| ... | +----------------------+ +-----------------+
1013+
| ... | Measurements... | | Metrics... | |
1014+
| ... |-----------------> MeasurementProcessor +------------> In-memory state |
1015+
| ... | | | | |
1016+
| ... | +----------------------+ +-----------------+
1017+
+------------------+
1018+
```
1019+
1020+
### MeasurementProcessor operations
1021+
1022+
#### OnMeasure
1023+
1024+
`OnMeasure` is called when a `Measurement` is recorded. This method is called synchronously on the thread that emitted the `Measurement`, therefore it SHOULD NOT block or throw exceptions.
1025+
1026+
**Parameters:**
1027+
1028+
* `measurement` - a [Measurement](./api.md#measurement) that was recorded
1029+
* `context` - the resolved `Context` (the explicitly passed `Context` or the current `Contex`)
1030+
1031+
**Returns:** Void
1032+
1033+
For a `MeasurementProcessor` registered directly on SDK `MeterProvider`, the `measurement` mutations MUST be visible in next registered processors.
1034+
1035+
A `MeasuremenetProcessor` may freely modify `measurement` for the duration of the `OnMeasure` call.
1036+
1037+
#### Shutdown
1038+
1039+
Shuts down the processor. Called when the SDK is shut down. This is an opportunity for the processor to do any cleanup required.
1040+
1041+
`Shutdown` SHOULD be called only once for each`MeasurementProcessor` instance. After the call to `Shutdow`, subsequent calls to `OnMeasure` are not allowed. SDKs SHOULD ignore these calls gracefully, if possible.
1042+
1043+
`Shutdown` SHOULD provide a way to let the caller know whether it succeeded, failed or timed out.
1044+
1045+
`Shutdown` MUST include the effects of `ForceFlush`.
1046+
1047+
`Shutdown` SHOULD complete or abort within some timeout. `Shutdown` can be implemented as a blocking API or an asynchronous API which notifies the caller via a callback or an event. OpenTelemetry SDK authors can decide if they want to make the shutdown timeout configurable.
1048+
1049+
#### ForceFlush
1050+
1051+
This is a hint to ensure that any tasks associated with `Measurements` for which the `MeasurementProcessor` had already received events prior to the call to `ForceFlush` SHOULD be completed as soon as possible, preferably before returning from this method.
1052+
1053+
<!-- TODO: Should we mingle with the Exporter concept here? For metrics, the only thing we care is that Measuremenets can be processed before aggregation happens -->
1054+
1055+
In particular, if any `MeasurementProcessor` has any associated exporter, it SHOULD try to call the exporter's `Export` with all `Measurements` for which this was not already done and then invoke `ForceFlush` on it. If a timeout is specified (see below), the `MeasurementProcessor` MUST prioritize honoring the timeout over finishing all calls. It MAY skip or abort some or all `Export` or `ForceFlush` calls it has made to achieve this goal.
1056+
1057+
`ForceFlush` SHOULD provide a way to let the caller know whether it succeeded, failed or timed out.
1058+
1059+
`ForceFlush` SHOULD only be called in cases where it is absolutely necessary, such as when using some FaaS providers that may suspend the process after an invocation, but before the `MeasurementProcessor` exports the emitted `Measuremenets`.
1060+
1061+
`ForceFlush` SHOULD complete or abort within some timeout. `ForceFlush` can be implemented as a blocking API or an asynchronous API which notifies the caller via a callback or an event. OpenTelemetry SDK authors can decide if they want to make the flush timeout configurable.
1062+
9891063
## Exemplar
9901064

9911065
**Status**: [Stable](../document-status.md)

0 commit comments

Comments
 (0)