diff --git a/.github/workflows/functionApp.yml b/.github/workflows/functionApp.yml index db7f0ca..f03355b 100644 --- a/.github/workflows/functionApp.yml +++ b/.github/workflows/functionApp.yml @@ -26,7 +26,7 @@ jobs: uses: ./.github/workflows/_functionAppDeployTemplate.yml name: "Function App Deploy" needs: [function_test] - # if: github.event_name == 'push' || github.event_name == 'release' + if: github.event_name == 'push' || github.event_name == 'release' with: environment: "dev" python_version: "3.10" diff --git a/code/function/fastapp/main.py b/code/function/fastapp/main.py index 488ef81..8834cfa 100644 --- a/code/function/fastapp/main.py +++ b/code/function/fastapp/main.py @@ -1,6 +1,7 @@ from fastapi import FastAPI from fastapp.api.v1.api_v1 import api_v1_router from fastapp.core.config import settings +from fastapp.utils import setup_tracer def get_app() -> FastAPI: @@ -24,7 +25,7 @@ def get_app() -> FastAPI: @app.on_event("startup") async def startup_event(): """Gracefully start the application before the server reports readiness.""" - pass + setup_tracer(app=app) @app.on_event("shutdown") diff --git a/code/function/fastapp/utils.py b/code/function/fastapp/utils.py index d140f1a..dba9aa4 100644 --- a/code/function/fastapp/utils.py +++ b/code/function/fastapp/utils.py @@ -1,7 +1,14 @@ import logging from logging import Logger +# from azure.identity import ManagedIdentityCredential +from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter +from fastapi import FastAPI from fastapp.core.config import settings +from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor +from opentelemetry.sdk.resources import SERVICE_NAME, Resource +from opentelemetry.sdk.trace import TracerProvider +from opentelemetry.sdk.trace.export import BatchSpanProcessor def setup_logging(module) -> Logger: @@ -20,3 +27,20 @@ def setup_logging(module) -> Logger: ) logger.addHandler(logger_stream_handler) return logger + + +def setup_tracer(app: FastAPI): + """Setup tracer for Open Telemetry. + + app (FastAPI): The app to be instrumented by Open Telemetry. + RETURNS (None): Nothing is being returned. + """ + if settings.APPLICATIONINSIGHTS_CONNECTION_STRING: + # credential = ManagedIdentityCredential() + exporter = AzureMonitorTraceExporter.from_connection_string( + settings.APPLICATIONINSIGHTS_CONNECTION_STRING, + # credential=credential + ) + tracer = TracerProvider(resource=Resource({SERVICE_NAME: "api"})) + tracer.add_span_processor(BatchSpanProcessor(exporter)) + FastAPIInstrumentor.instrument_app(app, tracer_provider=tracer) diff --git a/code/function/requirements.txt b/code/function/requirements.txt index e380e21..25a209e 100644 --- a/code/function/requirements.txt +++ b/code/function/requirements.txt @@ -2,6 +2,9 @@ # The Python Worker is managed by Azure Functions platform # Manually managing azure-functions-worker may cause unexpected issues +# azure-identity~=1.13.0 azure-functions~=1.14.0 fastapi~=0.96.1 aiohttp~=3.8.4 +opentelemetry-instrumentation-fastapi==0.39b0 +azure-monitor-opentelemetry-exporter==1.0.0b14 diff --git a/code/infra/logging.tf b/code/infra/logging.tf index f3b6637..209e890 100644 --- a/code/infra/logging.tf +++ b/code/infra/logging.tf @@ -10,7 +10,7 @@ resource "azurerm_application_insights" "application_insights" { force_customer_storage_for_profiler = false internet_ingestion_enabled = true internet_query_enabled = true - local_authentication_disabled = true + local_authentication_disabled = false retention_in_days = 90 sampling_percentage = 100 workspace_id = azurerm_log_analytics_workspace.log_analytics_workspace.id diff --git a/code/infra/roleassignments.tf b/code/infra/roleassignments.tf index 86d553b..cbe287d 100644 --- a/code/infra/roleassignments.tf +++ b/code/infra/roleassignments.tf @@ -9,3 +9,9 @@ resource "azurerm_role_assignment" "function_role_assignment_key_vault" { role_definition_name = "Key Vault Secrets User" principal_id = azapi_resource.function.identity[0].principal_id } + +# resource "azurerm_role_assignment" "function_role_assignment_application_insights" { +# scope = azurerm_application_insights.application_insights.id +# role_definition_name = "Monitoring Metrics Publisher" +# principal_id = azapi_resource.function.identity[0].principal_id +# }