Skip to content

Azure Function OpenTelemetry instrumentation of FastAPI #42453

@claria

Description

@claria
  • Package Name: azure-monitor-opentelemetry
  • Package Version:
  • Operating System: Windows/Linux
  • Python Version: 3.11.9

Describe the bug

I am testing to use opentelemetry for a fastapi app running in azure function. I use the auto instrumentation of fastapi given by the package opentelemetry-instrumentation-fastapi.

While it is working and all logs and spans are exported to Azure AppInsights, the specific entry in the requests table is created in an unfortunate way.

I have one route "/hello/{name}". As you can see from the spans created from opentelemetry the spans get the name "GET /hello/{name}" while the entry in the requests table gets the name "GET /hello/1" (with the actual url). This destroys the grouping in appInsights as can bee seen in the screenshots:

To Reproduce

function_app.py

import logging

import azure.functions as func
import fastapi
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor

fastapi_app = fastapi.FastAPI()

FastAPIInstrumentor.instrument_app(fastapi_app)

logger = logging.getLogger(__name__)


@fastapi_app.get("/hello/{name}")
async def get_name(name: str):
    logger.info(f"Hello {name}")
    return {
        "name": name,
    }


app = func.AsgiFunctionApp(app=fastapi_app,
                           http_auth_level=func.AuthLevel.ANONYMOUS,
                           function_name='fastapi')
import logging

import azure.functions as func
import fastapi
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor

fastapi_app = fastapi.FastAPI()

FastAPIInstrumentor.instrument_app(fastapi_app)

logger = logging.getLogger(__name__)


@fastapi_app.get("/hello/{name}")
async def get_name(name: str):
    logger.info(f"Hello {name}")
    return {
        "name": name,
    }


app = func.AsgiFunctionApp(app=fastapi_app,
                           http_auth_level=func.AuthLevel.ANONYMOUS,
                           function_name='fastapi')

host.json:

{
  "version": "2.0",
  "extensions": {
    "http": {
      "routePrefix": ""
    }
  },
  "telemetryMode": "OpenTelemetry"
}

local.settings.json

{
  "IsEncrypted": false,
  "Values": {
    "PYTHON_ENABLE_INIT_INDEXING": "1",
    "PYTHON_ENABLE_OPENTELEMETRY": true,
    "PYTHON_APPLICATIONINSIGHTS_ENABLE_TELEMETRY": true,
    "FUNCTIONS_WORKER_RUNTIME": "python",
    "AzureWebJobsStorage": "XXXXXXXXXXXXXXXXXXXXXX",
    "APPLICATIONINSIGHTS_CONNECTION_STRING": "XXXXXXXXXXXXXXXXXXXXXXX"
  }
}

requirements.txt:

azure-functions==1.24.0b3
fastapi==0.116.1
azure-monitor-opentelemetry==1.6.13
opentelemetry-instrumentation-fastapi==0.57.b0

Expected behavior
Use the route name (with route param template) or the actual endpoint (function) name.

Screenshots
Image

Image

Additional context
Add any other context about the problem here.

Metadata

Metadata

Assignees

Labels

Monitor - DistroMonitor OpenTelemetry DistroService AttentionWorkflow: This issue is responsible by Azure service team.customer-reportedIssues that are reported by GitHub users external to the Azure organization.needs-author-feedbackWorkflow: More information is needed from author to address the issue.no-recent-activityThere has been no recent activity on this issue.questionThe issue doesn't require a change to the product in order to be resolved. Most issues start as that

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions