opentelemetry-rust icon indicating copy to clipboard operation
opentelemetry-rust copied to clipboard

Is there a way to force_flush() metrics via the otlp exporter?

Open nstinus opened this issue 2 years ago • 2 comments

Hi,

I have a short lived process and I want to make sure metrics are pushed before it exits. I have setup short intervals and added a sleep before it exits but ideally, I would want to be able to force_flush() the metrics before exiting. Is this currently possible?

Possible related reference: https://opentelemetry.io/docs/reference/specification/metrics/sdk/#forceflush

nstinus avatar Mar 01 '23 17:03 nstinus

I was wondering the same. In the Quickstart example, would dropping the otlp_exporter (at the end of main) block the process until metrics are sent or lose metrics (as the process ends without waiting for the metrics to be sent)?

The drop code looks like it would just lose metrics.

I think the big upside of using an exporter (instead of being scraped by prometheus etc.) is the ability to flush metrics during shutdown, so how can we fix that?

Reading the examples, it seems the way to achieve this is to call BasicController.stop().

However that is hard to discover, as the opentelemetry_otlp::OtlpMetricPipeline.build() returns a Result<BasicController> but you cannot click on it because it is not publicly exported.

Would you accept a PR that re-exports BasicController and adjusts the Readme to call BasicController.stop() and global::shutdown_tracer_provider()?

kosta avatar Mar 15 '23 09:03 kosta

I'm confused by this as well.. it seems there used to be a global::force_flush_tracer_provider() function.. but now it has been removed... I'm not sure what was meant to replace it. My use-case is for an aws lambda... it sleeps after a run, so unless I send out the traces before it sleeps, I may or may not get any results on the next call.

canadiannomad avatar Jul 01 '23 04:07 canadiannomad

https://github.com/open-telemetry/opentelemetry-rust/blob/main/opentelemetry-otlp/examples/basic-otlp/src/main.rs#L139 shows an example to shutdown. same approach can be used for force_flush.

cijothomas avatar May 20 '24 12:05 cijothomas