opencensus-python icon indicating copy to clipboard operation
opencensus-python copied to clipboard

Method on exporter to flush metrics before process exits

Open MikeWoodGCP opened this issue 4 years ago • 4 comments

I believe the method to flush metrics via the exporter exists in go lang binding but I haven't been able to find it via python3 classes. Happy to help implement it if lack of bandwidth is an issue. Also, keen to know if there is a different approach in python. FWIW, I'm using the stackdriver exporter in GCP. Thanks for any consideration or advice.

MikeWoodGCP avatar Dec 16 '20 18:12 MikeWoodGCP

There's a relevant .close method here: https://github.com/census-instrumentation/opencensus-python/blob/master/opencensus/metrics/transport.py#L87

In stackdriver the relevant code is here: https://github.com/census-instrumentation/opencensus-python/blob/07588770a68c259768c9a372c1a5e3b027711a7b/contrib/opencensus-ext-stackdriver/opencensus/ext/stackdriver/stats_exporter/init.py#L404

I'll send a sample cl that adds a close method to the exporter that then calls the close method on the transport exporter thread. Would that help?

shanemhansen avatar Jan 07 '21 20:01 shanemhansen

@shanemhansen @MikeWoodGCP I ran into this while looking into exactly the same issue - did this go anywhere in the end? Thanks!

matthias-baetens avatar Nov 30 '21 10:11 matthias-baetens

@shanemhansen @MikeWoodGCP Let us know if any progress has been made!

Thank you so much!

lucylichenhui avatar Dec 14 '21 13:12 lucylichenhui

As a stopgap, one way to flush is:

import threading
from opencensus.metrics.transport import PeriodicMetricTask

def flush_all_metrics():
    for thread in threading.enumerate():
        if isinstance(thread, PeriodicMetricTask):
            thread.close()
            thread.join(timeout=60)

aaronslin avatar Mar 07 '22 18:03 aaronslin