1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Writing to application insights from FastAPI with managed identity

Discussão em 'Python' iniciado por Stack, Setembro 8, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to log from a FastAPI application to Azure application insights. It is working with a connection string, but I would like it to be working with managed identity. The code below does not fail - no errors or anything. But it does not log anything. Any suggestions to sove the problem, or how to troubleshoot as I get no errors:

    from fastapi import FastAPI,Request
    from fastapi.middleware.cors import CORSMiddleware
    from fastapi_azure_auth import SingleTenantAzureAuthorizationCodeBearer
    import uvicorn
    from fastapi import FastAPI, Security
    import os
    from typing import Dict
    from azure.identity import DefaultAzureCredential
    import logging
    from azure.monitor.opentelemetry import configure_azure_monitor
    from opentelemetry import trace,metrics

    from settings import Settings

    from pydantic import AnyHttpUrl,BaseModel

    from contextlib import asynccontextmanager
    from typing import AsyncGenerator

    from fastapi_azure_auth.user import User

    settings = Settings()

    @asynccontextmanager
    async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
    """
    Load OpenID config on startup.
    """
    await azure_scheme.openid_config.load_config()
    yield


    app = FastAPI(
    swagger_ui_oauth2_redirect_url='/oauth2-redirect',
    swagger_ui_init_oauth={
    'usePkceWithAuthorizationCodeGrant': True,
    'clientId': settings.xx,
    'scopes': settings.xx,
    },
    )

    if settings.BACKEND_CORS_ORIGINS:
    app.add_middleware(
    CORSMiddleware,
    allow_origins=[str(origin) for origin in settings.BACKEND_CORS_ORIGINS],
    allow_credentials=True,
    allow_methods=['*'],
    allow_headers=['*'],
    )

    azure_scheme = SingleTenantAzureAuthorizationCodeBearer(
    app_client_id=settings.xx,
    tenant_id=settings.xx,
    scopes=settings.xx,
    )

    class User(BaseModel):
    name: str
    roles: list[str] = []


    logger = logging.getLogger(__name__)
    logger.setLevel(logging.INFO)

    credential = DefaultAzureCredential()

    configure_azure_monitor(
    credential=credential,
    connection_string="InstrumentationKey=xx-xx-xx-xx-xx"
    )

    @app.get("/log", dependencies=[Security(azure_scheme)])
    async def root():
    print("Yo test")
    logger.info("Segato5", extra={"custom_dimension": "Kam_value","test1": "val1"})

    meter = metrics.get_meter_provider().get_meter(__name__)
    counter = meter.create_counter("segato2")
    counter.add(8)

    return {"whoIsTheBest": "!!"}

    if __name__ == '__main__':
    uvicorn.run('main:app', reload=True)

    Continue reading...

Compartilhe esta Página