I want to send some metric data to Azure appinsight, I am using Azure function ,this is my code :
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
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)
And here is my azure function :
def main(req: func.HttpRequest) -> func.HttpResponse:
req_body = req.get_json()
build_sum_aggregation_metric("metric 1","metric 1","metric 1", "metric 1", "metric 1", 5)
build_sum_aggregation_metric("metric 2","metric 2","metric 2", "metric 2", "metric 2", 10)
response ={ "value" : "metric sent" }
return func.HttpResponse(
json.dumps(response),
headers={
"Content-Type": "application/json"
}
)
After doing a request I thought that this will only send the request 1 time but in Application Insights count it shows it keeps sending the metric multiple time until I stop the function even thought I did not run any request is that a normal behavior , and there is a way to send only the metric 1 time for each request ?