|
10 | 10 | from quart.sessions import SecureCookieSessionInterface |
11 | 11 | from azure.appconfiguration.provider import load |
12 | 12 | from azure.identity import DefaultAzureCredential |
| 13 | +from azure.monitor.opentelemetry import configure_azure_monitor |
13 | 14 | from featuremanagement.aio import FeatureManager |
14 | 15 | from featuremanagement import TargetingContext |
| 16 | +from featuremanagement.azuremonitor import TargetingSpanProcessor |
15 | 17 |
|
16 | | -try: |
17 | | - from azure.monitor.opentelemetry import configure_azure_monitor # pylint: disable=ungrouped-imports |
18 | 18 |
|
19 | | - # Configure Azure Monitor |
20 | | - configure_azure_monitor(connection_string=os.getenv("APPLICATIONINSIGHTS_CONNECTION_STRING")) |
21 | | -except ImportError: |
22 | | - pass |
| 19 | +# A callback for assigning a TargetingContext for both Telemetry logs and Feature Flag evaluation |
| 20 | +async def my_targeting_accessor() -> TargetingContext: |
| 21 | + session_id = "" |
| 22 | + if "Session-ID" in request.headers: |
| 23 | + session_id = request.headers["Session-ID"] |
| 24 | + return TargetingContext(user_id=session_id) |
| 25 | + |
| 26 | + |
| 27 | +# Configure Azure Monitor |
| 28 | +configure_azure_monitor( |
| 29 | + connection_string=os.getenv("APPLICATIONINSIGHTS_CONNECTION_STRING"), |
| 30 | + span_processors=[TargetingSpanProcessor(targeting_context_accessor=my_targeting_accessor)], |
| 31 | +) |
23 | 32 |
|
24 | 33 | app = Quart(__name__) |
25 | 34 | app.session_interface = SecureCookieSessionInterface() |
|
28 | 37 | endpoint = os.environ.get("APPCONFIGURATION_ENDPOINT_STRING") |
29 | 38 | credential = DefaultAzureCredential() |
30 | 39 |
|
31 | | - |
32 | | -async def my_targeting_accessor() -> TargetingContext: |
33 | | - session_id = "" |
34 | | - if "Session-ID" in request.headers: |
35 | | - session_id = request.headers["Session-ID"] |
36 | | - return TargetingContext(user_id=session_id) |
37 | | - |
38 | | - |
39 | 40 | # Connecting to Azure App Configuration using AAD |
40 | 41 | config = load(endpoint=endpoint, credential=credential, feature_flag_enabled=True, feature_flag_refresh_enabled=True) |
41 | 42 |
|
|
0 commit comments