Skip to content

Add Open Telemetry #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/functionApp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion code/function/fastapp/main.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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")
Expand Down
24 changes: 24 additions & 0 deletions code/function/fastapp/utils.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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)
3 changes: 3 additions & 0 deletions code/function/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion code/infra/logging.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions code/infra/roleassignments.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
# }