Skip to content

feat: Synchronous Metrics Reader and Exporter #4549

@Jayclifford345

Description

@Jayclifford345

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

  1. Serverless Functions - Lambda/Azure Functions need to export metrics before completion with no persistent background processes
  2. Batch Jobs/ETL - Data pipelines that collect metrics at specific processing stages
  3. CLI Tools - Short-lived applications that report metrics before exit
  4. Web Applications - Metrics tied to specific HTTP requests rather than time intervals
  5. Microservice Health Checks - Services reporting metrics on-demand when probed
  6. Testing Environments - Precise control over metric collection during tests
  7. 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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions