> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Metrics SDK Lifecycle APIs and Views

> Explains metrics SDK lifecycle, MetricReaders and MetricExporters, push versus pull exports, and Shutdown and ForceFlush behaviors and best practices for reliable metric delivery.

You now understand the high-level metrics pipeline. This article examines the lifecycle of key metrics components — MetricExporters and MetricReaders — and explains how metrics are delivered (pushed or pulled), how shutdowns are handled, and when to use ForceFlush.

Metric exporters are lightweight plugins whose sole responsibility is to transmit metrics to a destination (a backend, collector, or monitoring system). They always work in tandem with a MetricReader: the reader handles aggregation and temporality, while the exporter packages and sends already-summarized metrics. Keep exporters simple — if an exporter receives a format or timing it cannot support, it should log a clear error so users know why data was not exported.

<Callout icon="warning" color="#FF6B6B">
  Exporters should not perform aggregation or alter temporality. If a destination requires a custom metric format that the SDK does not natively support, the exporter must surface this clearly in logs and documentation.
</Callout>

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/d0VZi1GmqxTLJ3bx/images/Prep-Course-OpenTelemetry-Certified-Associate-OTCA-Certification/Recording-Measurements/Metrics-SDK-Lifecycle-APIs-and-Views/metric-exporter-infographic-overview.jpg?fit=max&auto=format&n=d0VZi1GmqxTLJ3bx&q=85&s=3f2e295497c405118386c4edd56c1d5a" alt="The image is an infographic about &#x22;Metric Exporter,&#x22; explaining its role as a plug-in for sending metrics, its collaboration with a MetricReader, and its operation principles like simplicity, error logging, and scheduling." width="1920" height="1080" data-path="images/Prep-Course-OpenTelemetry-Certified-Associate-OTCA-Certification/Recording-Measurements/Metrics-SDK-Lifecycle-APIs-and-Views/metric-exporter-infographic-overview.jpg" />
</Frame>

Why separate MetricReaders and MetricExporters?

* MetricReaders decide when and how to collect metrics (periodic, on-demand) and perform aggregation and temporality.
* MetricExporters remain agnostic of aggregation logic; they receive the reader's summarized metrics and transmit them.
* This separation makes SDKs flexible: you can attach multiple exporters (push and/or pull) to a MeterProvider, each with its own reader configuration.

Key export modes at a glance

| Mode          | Who initiates export                              | Typical use cases                                     | Effect of ForceFlush                                      |
| ------------- | ------------------------------------------------- | ----------------------------------------------------- | --------------------------------------------------------- |
| Push          | MetricReader (periodic or on demand)              | Exporters that send to collectors/backends directly   | ForceFlush triggers immediate collection + export         |
| Pull (scrape) | External scraper (e.g., `https://prometheus.io/`) | Instrumentation that exposes an endpoint for scrapers | ForceFlush has no effect — scrapes happen only on request |

Push-based exporters

In push workflows, a MetricReader controls collection cadence (for example, periodic polling). The reader collects and aggregates the in-memory metrics, then calls the exporter to transmit that summarized data to its destination. Push exporters can also be triggered by immediate requests such as `ForceFlush` or direct application signals (for example, when an application logs a critical incident and requests an immediate export).

Typical push flow:

* Instrument data is recorded in in-memory state.
* MetricReader collects and summarizes those metrics from the SDK.
* MetricExporter packages and sends the summarized metrics to an external process (collector or backend).

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/Eacmk60bHr_VWydJ/images/Prep-Course-OpenTelemetry-Certified-Associate-OTCA-Certification/Recording-Measurements/Metrics-SDK-Lifecycle-APIs-and-Views/push-based-metric-exporter-diagram.jpg?fit=max&auto=format&n=Eacmk60bHr_VWydJ&q=85&s=3511743003c26b540e1fd1e060f59542" alt="The image explains a push-based metric exporter system, detailing how it sends metrics autonomously and works with a paired MetricReader to send data, with periodic or immediate options during errors. The diagram shows the flow from in-memory state to metric exportation and another process." width="1920" height="1080" data-path="images/Prep-Course-OpenTelemetry-Certified-Associate-OTCA-Certification/Recording-Measurements/Metrics-SDK-Lifecycle-APIs-and-Views/push-based-metric-exporter-diagram.jpg" />
</Frame>

Pull-based exporters

Pull-based exporters (commonly called "scrape" exporters) expose an HTTP endpoint that an external scraper requests to retrieve metrics. The exporter is passive and only responds to scrape requests; it does not push data autonomously. Because the scraper controls timing, operations such as ForceFlush do not trigger an immediate push — scrapes occur only when the external system requests them.

Typical pull flow:

* Instrument data remains in in-memory state.
* The pull exporter exposes summarized data on an endpoint.
* An external scraper periodically requests (scrapes) the endpoint and forwards metrics to the backend.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/Eacmk60bHr_VWydJ/images/Prep-Course-OpenTelemetry-Certified-Associate-OTCA-Certification/Recording-Measurements/Metrics-SDK-Lifecycle-APIs-and-Views/pull-metric-exporter-diagram-prometheus.jpg?fit=max&auto=format&n=Eacmk60bHr_VWydJ&q=85&s=671f9bd1ccedfb19db9113d1ed0f2398" alt="The image is a diagram explaining a Pull Metric Exporter, highlighting its passive nature of waiting to be scraped by a process like Prometheus. It shows an in-memory state providing metrics to a PrometheusExporter, which is then accessed by another scraping process." width="1920" height="1080" data-path="images/Prep-Course-OpenTelemetry-Certified-Associate-OTCA-Certification/Recording-Measurements/Metrics-SDK-Lifecycle-APIs-and-Views/pull-metric-exporter-diagram-prometheus.jpg" />
</Frame>

Shutdown and ForceFlush — lifecycle control APIs

Two lifecycle APIs are critical for reliable metric delivery: Shutdown and ForceFlush. Understand their responsibilities and limitations so you can ensure final metrics are delivered during application termination.

Shutdown (MeterProvider)

* `Shutdown` is the termination API on the MeterProvider. It should be called exactly once to perform cleanup and to stop metric processing.
* On shutdown the provider should cascade the `shutdown` call to all registered MetricReaders and MetricExporters.
* SDKs may block synchronously up to a timeout and should report whether shutdown succeeded, failed, or timed out.
* After shutdown, the provider should refuse to create functional meters. Returning no-op meters is preferred so the application can keep running while preventing new metrics from being recorded.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/Eacmk60bHr_VWydJ/images/Prep-Course-OpenTelemetry-Certified-Associate-OTCA-Certification/Recording-Measurements/Metrics-SDK-Lifecycle-APIs-and-Views/shutdown-flow-diagram-forceflush-metrics.jpg?fit=max&auto=format&n=Eacmk60bHr_VWydJ&q=85&s=0d0774959d88f8dfe27b00039875156e" alt="The image is a flow diagram comparing processes before and after a &#x22;Shutdown&#x22; event, with &#x22;ForceFlush&#x22; and &#x22;Metric creation&#x22; involved. It is sourced from KodeKloud." width="1920" height="1080" data-path="images/Prep-Course-OpenTelemetry-Certified-Associate-OTCA-Certification/Recording-Measurements/Metrics-SDK-Lifecycle-APIs-and-Views/shutdown-flow-diagram-forceflush-metrics.jpg" />
</Frame>

ForceFlush

* `ForceFlush` asks push-based MetricReaders (and, transitively, their exporters) to immediately collect and export any buffered metrics.
* It is intended for push-based workflows where you want to ensure buffered metrics are delivered before shutdown or at critical moments.
* Pull exporters (scrape-based) are unaffected by `ForceFlush`; they only expose metrics in response to scrapes. For scrape-based setups, ensure the external scraper performs a final scrape if you need the last metrics exported.

<Callout icon="lightbulb" color="#1CB2FE">
  ForceFlush is intended for push-based workflows (e.g., ensuring buffered metrics are sent before shutdown). For scrape-based setups (Prometheus), ensure the scraper scrapes the endpoint at least once before shutdown if you need the final data exported.
</Callout>

Lifecycle summary and best practices

* Call `Shutdown` on the MeterProvider once to terminate metric processing cleanly and cascade cleanup to all MetricReaders and MetricExporters.
* After shutdown, the provider should return no-op meters to prevent further metric creation while allowing the app to continue running.
* Push exporters:
  * Attempt to `ForceFlush` any pending data during shutdown.
  * Stop timers/background threads, clear buffers, and release resources.
* Pull exporters:
  * Stop exposing the scrape endpoint so subsequent scrapes return no data.
  * Document how consumers should perform a final scrape if they require last-minute metrics.
* Provide explicit feedback about shutdown outcome (success, failure, timeout) to orchestration or monitoring systems.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/d0VZi1GmqxTLJ3bx/images/Prep-Course-OpenTelemetry-Certified-Associate-OTCA-Certification/Recording-Measurements/Metrics-SDK-Lifecycle-APIs-and-Views/meterprovider-shutdown-instructions-summary.jpg?fit=max&auto=format&n=d0VZi1GmqxTLJ3bx&q=85&s=62ac1bcd4fc34713c07d9d737d1866c6" alt="The image is a summary of instructions related to shutting down a MeterProvider, including its effects on MetricReaders and MetricExporters, as well as guidance for push and pull exporters." width="1920" height="1080" data-path="images/Prep-Course-OpenTelemetry-Certified-Associate-OTCA-Certification/Recording-Measurements/Metrics-SDK-Lifecycle-APIs-and-Views/meterprovider-shutdown-instructions-summary.jpg" />
</Frame>

References and further reading

* OpenTelemetry Metrics: [https://opentelemetry.io/docs/](https://opentelemetry.io/docs/) (see Metrics SDK and exporter guidelines)
* Prometheus documentation: [https://prometheus.io/docs/](https://prometheus.io/docs/)

This article covered metrics SDK lifecycle topics: MetricExporters (push and pull), MetricReaders, `ForceFlush`, and `Shutdown` semantics, including best practices for ensuring final metrics are reliably exported.

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/prep-course-opentelemetry-certified-associate-certification-otca/module/6fce855c-4275-48c0-9297-a7f98a292285/lesson/4a118783-522b-4895-8327-fb598928db7c" />
</CardGroup>
