@noob I tried to work through this issue and the following are some of the observations based on your code snippet provided. (note - I worked on a sample repro using the Azure function development through VSCode locally, but the concept should also apply to deployed Azure Function).
I see a number of deviations from the documented sample as available here - Metric instrument usage.
I used the code snippet provided in the question and the documented sample, and the following worked for me where customMetric is being tracked for a HTTP triggered Azure Function written in Python. I have added comments to the updated lines of code.:
import azure.functions as func
import logging
from opentelemetry import metrics
from opentelemetry.sdk.metrics import MeterProvider
from azure.monitor.opentelemetry.exporter import AzureMonitorMetricExporter
from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader #-----UPDATED-------
import os
app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS)
exporter = AzureMonitorMetricExporter.from_connection_string(os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"])
reader = PeriodicExportingMetricReader(exporter, export_interval_millis=5000) #-----UPDATED---
meter_provider = MeterProvider(metric_readers=[reader]) #-----UPDATED-------
metrics.set_meter_provider(meter_provider) #-----UPDATED-------
meter = metrics.get_meter_provider().get_meter("sample") #-----UPDATED-------
# Create a counter metric
requests_counter = meter.create_counter(
name="requests",
description="Number of requests",
unit="1")
@app.route(route="HTTPTriggerFunc")
def HTTPTriggerFunc(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
#open telemetry start *****************************************************
# Record a metric to track requests
requests_counter.add(1, {"environment": "production"})
meter_provider.force_flush() #-----UPDATED----
#---open telemetry end *****************************************************
name = req.params.get('name')
if not name:
try:
req_body = req.get_json()
except ValueError:
pass
else:
name = req_body.get('name')
if name:
return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.")
else:
return func.HttpResponse(
"This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
status_code=200
)
While most of the code in the snippet above is boiler plate code as available when creating a HTTP trigger python-based Azure function, the lines marked with #-----UPDATED-------
shows the updates made in addition to what you had provided in the question.
After sending a couple of telemetry, I could see the metrics in customMetrics table in ApplicationInsights Workspace using the query below:
customMetrics
| where customDimensions == '{"environment":"production"}'
And also, metrics explorer had this listed under the azure.applicationinsights Metric namespace
I hope the sample code snippet shared will also answer the queries you had.
Please let me know if you have any questions.
If the answer helped, please click Accept answer so that it can help others in the community looking for help on similar topics.