-
Notifications
You must be signed in to change notification settings - Fork 739
Description
Is your feature request related to a problem?
Summary
Add a SynchronousExportingMetricReader
to complement the existing PeriodicExportingMetricReader
, supporting on-demand batch exporting of metrics rather than timer-based collection.
Problem Statement
The current PeriodicExportingMetricReader
uses fixed intervals, which doesn't fit all use cases. There are scenarios which require explicit control over when metrics are collected and exported. It also means there is a feature gap between metrics compared to logs and traces, which can handle batch exporting.
Describe the solution you'd like
Solution
SynchronousExportingMetricReader
provides:
- On-demand collection via explicit
collect()
calls - Efficient batching of metrics before export
- Configurable queue and batch sizes
- No background threads, reducing resource usage
Real-World Use Cases
- Serverless Functions - Lambda/Azure Functions need to export metrics before completion with no persistent background processes
- Batch Jobs/ETL - Data pipelines that collect metrics at specific processing stages
- CLI Tools - Short-lived applications that report metrics before exit
- Web Applications - Metrics tied to specific HTTP requests rather than time intervals
- Microservice Health Checks - Services reporting metrics on-demand when probed
- Testing Environments - Precise control over metric collection during tests
- Game Development - Collection at frame boundaries or level completions
Example Usage
reader = SynchronousExportingMetricReader(
exporter,
max_export_batch_size=5,
max_queue_size=1000
)
meter_provider = MeterProvider(resource=resource, metric_readers=[reader])
meter = metrics.get_meter("my-meter")
counter = meter.create_counter("my_counter")
# Record and explicitly collect
counter.add(1)
reader.collect() # Adds to queue and exports when batch size reached
Describe alternatives you've considered
Alternatives Considered
It could be possible to achieve similar behaviour with the PeriodicExportingMetricReader
(setting collection interval to infinite and calling collect manually). However, it does not explicitly implement a queue and batching system. This function would bring feature parity between logs and traces.
Additional Context
References
PR with new SynchronousExportingMetricReader
#4542
Would you like to implement a fix?
Yes