Skip to content

Commit 94d589e

Browse files
authored
Merge pull request #8 from PerfectThymeTech/marvinbuss/open_telemetry
Add Open Telemetry
2 parents 015098b + 4559f16 commit 94d589e

File tree

6 files changed

+37
-3
lines changed

6 files changed

+37
-3
lines changed

.github/workflows/functionApp.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
uses: ./.github/workflows/_functionAppDeployTemplate.yml
2727
name: "Function App Deploy"
2828
needs: [function_test]
29-
# if: github.event_name == 'push' || github.event_name == 'release'
29+
if: github.event_name == 'push' || github.event_name == 'release'
3030
with:
3131
environment: "dev"
3232
python_version: "3.10"

code/function/fastapp/main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from fastapi import FastAPI
22
from fastapp.api.v1.api_v1 import api_v1_router
33
from fastapp.core.config import settings
4+
from fastapp.utils import setup_tracer
45

56

67
def get_app() -> FastAPI:
@@ -24,7 +25,7 @@ def get_app() -> FastAPI:
2425
@app.on_event("startup")
2526
async def startup_event():
2627
"""Gracefully start the application before the server reports readiness."""
27-
pass
28+
setup_tracer(app=app)
2829

2930

3031
@app.on_event("shutdown")

code/function/fastapp/utils.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
import logging
22
from logging import Logger
33

4+
# from azure.identity import ManagedIdentityCredential
5+
from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter
6+
from fastapi import FastAPI
47
from fastapp.core.config import settings
8+
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
9+
from opentelemetry.sdk.resources import SERVICE_NAME, Resource
10+
from opentelemetry.sdk.trace import TracerProvider
11+
from opentelemetry.sdk.trace.export import BatchSpanProcessor
512

613

714
def setup_logging(module) -> Logger:
@@ -20,3 +27,20 @@ def setup_logging(module) -> Logger:
2027
)
2128
logger.addHandler(logger_stream_handler)
2229
return logger
30+
31+
32+
def setup_tracer(app: FastAPI):
33+
"""Setup tracer for Open Telemetry.
34+
35+
app (FastAPI): The app to be instrumented by Open Telemetry.
36+
RETURNS (None): Nothing is being returned.
37+
"""
38+
if settings.APPLICATIONINSIGHTS_CONNECTION_STRING:
39+
# credential = ManagedIdentityCredential()
40+
exporter = AzureMonitorTraceExporter.from_connection_string(
41+
settings.APPLICATIONINSIGHTS_CONNECTION_STRING,
42+
# credential=credential
43+
)
44+
tracer = TracerProvider(resource=Resource({SERVICE_NAME: "api"}))
45+
tracer.add_span_processor(BatchSpanProcessor(exporter))
46+
FastAPIInstrumentor.instrument_app(app, tracer_provider=tracer)

code/function/requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
# The Python Worker is managed by Azure Functions platform
33
# Manually managing azure-functions-worker may cause unexpected issues
44

5+
# azure-identity~=1.13.0
56
azure-functions~=1.14.0
67
fastapi~=0.96.1
78
aiohttp~=3.8.4
9+
opentelemetry-instrumentation-fastapi==0.39b0
10+
azure-monitor-opentelemetry-exporter==1.0.0b14

code/infra/logging.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ resource "azurerm_application_insights" "application_insights" {
1010
force_customer_storage_for_profiler = false
1111
internet_ingestion_enabled = true
1212
internet_query_enabled = true
13-
local_authentication_disabled = true
13+
local_authentication_disabled = false
1414
retention_in_days = 90
1515
sampling_percentage = 100
1616
workspace_id = azurerm_log_analytics_workspace.log_analytics_workspace.id

code/infra/roleassignments.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,9 @@ resource "azurerm_role_assignment" "function_role_assignment_key_vault" {
99
role_definition_name = "Key Vault Secrets User"
1010
principal_id = azapi_resource.function.identity[0].principal_id
1111
}
12+
13+
# resource "azurerm_role_assignment" "function_role_assignment_application_insights" {
14+
# scope = azurerm_application_insights.application_insights.id
15+
# role_definition_name = "Monitoring Metrics Publisher"
16+
# principal_id = azapi_resource.function.identity[0].principal_id
17+
# }

0 commit comments

Comments
 (0)