0

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 ?

Yafaa Ben Tili
  • 176
  • 1
  • 7

1 Answers1

0

The issue occurs because, Everytime the build_sum_aggregation_metric function is invoked it registers a new view and an exporter. Thus two new exporters are registered each time the function runs. Transfer the exporter and view registration outside of the build_sum_aggregation_metric function to correct this and register the exporter and view at the function launch.

My Code:-

import  logging

import  os

import  json

import  azure.functions  as  func

  

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

  

stats = stats_module.stats

view_manager = stats.view_manager

stats_recorder = stats.stats_recorder

  

instrumentation_key = "InstrumentationKey=3f3d02a1-8ae5-4c89-9b2e-dae6cf090b4f;IngestionEndpoint=https://uksouth-1.in.applicationinsights.azure.com/;LiveEndpoint=https://uksouth.livediagnostics.monitor.azure.com/"

exporter = metrics_exporter.new_metrics_exporter(

connection_string=instrumentation_key

)

  

view_manager.register_exporter(exporter)

  

def  build_sum_aggregation_metric(measure_name, measure_description, measure_unit, view_name, view_description, metric_value):

MEASURE = measure_module.MeasureInt(measure_name,

measure_description,

measure_unit)

  

VIEW = view_module.View(view_name,

view_description,

[],

MEASURE,

aggregation_module.SumAggregation())

  

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)

  

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"

}

)

Output:-

Post Request :-

enter image description here

Local:-

enter image description here

enter image description here

SiddheshDesai
  • 3,668
  • 1
  • 2
  • 11