census-instrumentation / opencensus-python

A stats collection and distributed tracing framework
Apache License 2.0
668 stars 250 forks source link

Azure function keeps sending the same metric indefinitely #1202

Closed Yafaa closed 1 year ago

Yafaa commented 1 year ago

I am using Azure function to send metric to Azure Application Insights , after running a request opencensus-ext-azure keep sending the same metric

Steps to reproduce. Running this piece of code inside an azure function and send a request


from opencensus.ext.azure import metrics_exporter
from opencensus.stats import aggregation as aggregation_module
from opencensus.stats import measure as measure_module
from opencensus.stats import stats as stats_module
from opencensus.stats import view as view_module
from opencensus.tags import tag_map as tag_map_module
import os

def build_sum_aggregation_metric(measure_name, measure_description, measure_unit, view_name, view_description, metric_value):
    stats = stats_module.stats
    view_manager = stats.view_manager
    stats_recorder = stats.stats_recorder

    MEASURE = measure_module.MeasureInt(measure_name,
                                        measure_description,
                                        measure_unit)

    VIEW = view_module.View(view_name,
                            view_description,
                            [],
                            MEASURE,
                            aggregation_module.SumAggregation())

    instrumentation_key = os.getenv("APP_INSIGHTS_KEY")
    exporter = metrics_exporter.new_metrics_exporter(
        connection_string=instrumentation_key
    )
    view_manager.register_exporter(exporter)

    view_manager.register_view(VIEW)

    mmap = stats_recorder.new_measurement_map()
    tmap = tag_map_module.TagMap()

    mmap.measure_int_put(MEASURE, metric_value)
    mmap.record(tmap)

build_sum_aggregation_metric("metric 1", "metric 1",
                                     "metric 1", "metric 1", "metric 1", 30)

What is the expected behavior? The metric should be sent only 1 time.

What is the actual behavior? The metric are sent infinite time until I stop the function.

lzchen commented 1 year ago

@Yafaa

This is by design. Metrics are meant to track and collect values over a period of time. After every export interval (default 15s), the current metric value will be exported.