Skip to content
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

Add application insights for request and backend tracing of performance data #515

Merged
merged 29 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
896d9ae
Example for request and backend tracing
tonybaloney Aug 4, 2023
ba13e55
Add bicep for an application insights service connected to the backend
tonybaloney Aug 4, 2023
8f286d7
Quart draft
pamelafox Aug 4, 2023
f4f6a99
Loosen requirements
tonybaloney Aug 4, 2023
83dabe5
Use the new azure package for OTEL
tonybaloney Aug 4, 2023
1283ec5
Remove rich console import
tonybaloney Aug 4, 2023
b23eb61
Fix ask and test
pamelafox Aug 4, 2023
cb6f2ed
Quart deploying now
pamelafox Aug 5, 2023
0225d29
Use semantic
pamelafox Aug 5, 2023
6b1ae1a
Get tests working
pamelafox Aug 5, 2023
479d5aa
Output connection string for azure monitor. Enable instrumentation in…
tonybaloney Aug 6, 2023
413b52e
Revert simple
pamelafox Aug 7, 2023
fb870cc
Typing fixes
pamelafox Aug 7, 2023
67ec7e0
dont use pipe
pamelafox Aug 7, 2023
4821f7a
Simplify tracing setup
tonybaloney Aug 8, 2023
bff1f49
Exclude app insights from output strings
tonybaloney Aug 8, 2023
285e42e
Reorder imports
tonybaloney Aug 9, 2023
f9944fc
Only configure azure monitor if environment variable is set
tonybaloney Aug 9, 2023
9c197a8
Merge branch 'tracing_otel' into pr/pamelafox/503-2
tonybaloney Aug 9, 2023
9368d04
Update app/backend/app.py
tonybaloney Aug 9, 2023
754dd4a
Fix import paths for aio
tonybaloney Aug 9, 2023
8d3e320
Merge branch 'quart_and_otel' into otel_asgi
tonybaloney Aug 10, 2023
4dbc440
Merge branch 'tracing_otel' of github.com:tonybaloney/azure-search-op…
tonybaloney Aug 10, 2023
32bb0a7
Make Application Insights optional, add documentation
tonybaloney Aug 10, 2023
3454808
Reorder imports
tonybaloney Aug 10, 2023
702394d
Pin versions
tonybaloney Aug 10, 2023
dd36170
Merge remote-tracking branch 'origin/main' into tracing_otel
tonybaloney Aug 10, 2023
69d8e29
Trace aiohttp calls
tonybaloney Aug 11, 2023
515b362
reorder imports
tonybaloney Aug 14, 2023
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
13 changes: 12 additions & 1 deletion app/backend/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import openai
from azure.identity import DefaultAzureCredential
from azure.monitor.opentelemetry import configure_azure_monitor
from azure.search.documents import SearchClient
from azure.storage.blob import BlobServiceClient
from flask import (
Expand All @@ -18,6 +19,7 @@
send_file,
send_from_directory,
)
from opentelemetry.instrumentation.flask import FlaskInstrumentor

from approaches.chatreadretrieveread import ChatReadRetrieveReadApproach
from approaches.readdecomposeask import ReadDecomposeAsk
Expand Down Expand Up @@ -45,6 +47,7 @@
CONFIG_CHAT_APPROACHES = "chat_approaches"
CONFIG_BLOB_CLIENT = "blob_client"

APPLICATIONINSIGHTS_CONNECTION_STRING = os.getenv("APPLICATIONINSIGHTS_CONNECTION_STRING")

bp = Blueprint("routes", __name__, static_folder='static')

Expand Down Expand Up @@ -117,12 +120,20 @@ def ensure_openai_token():


def create_app():
if APPLICATIONINSIGHTS_CONNECTION_STRING:
pamelafox marked this conversation as resolved.
Show resolved Hide resolved
configure_azure_monitor()

app = Flask(__name__)
FlaskInstrumentor().instrument_app(app)

# For local debugging, add opentelemetry-exporter-richconsole to requirements.txt and un-comment:
# from opentelemetry.exporter.richconsole import RichConsoleSpanExporter
# provider.add_span_processor(BatchSpanProcessor(RichConsoleSpanExporter()))

# Use the current user identity to authenticate with Azure OpenAI, Cognitive Search and Blob Storage (no secrets needed,
# just use 'az login' locally, and managed identity when deployed on Azure). If you need to use keys, use separate AzureKeyCredential instances with the
# keys for each service
# If you encounter a blocking error during a DefaultAzureCredntial resolution, you can exclude the problematic credential by using a parameter (ex. exclude_shared_token_cache_credential=True)
# If you encounter a blocking error during a DefaultAzureCredential resolution, you can exclude the problematic credential by using a parameter (ex. exclude_shared_token_cache_credential=True)
azure_credential = DefaultAzureCredential(exclude_shared_token_cache_credential = True)

# Set up clients for Cognitive Search and Storage
Expand Down
5 changes: 5 additions & 0 deletions app/backend/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@ openai[datalib]==0.27.8
tiktoken==0.4.0
azure-search-documents==11.4.0b6
azure-storage-blob==12.14.1

azure-monitor-opentelemetry --pre
tonybaloney marked this conversation as resolved.
Show resolved Hide resolved
opentelemetry-instrumentation-flask
opentelemetry-instrumentation-requests
opentelemetry-instrumentation-aiohttp-client
17 changes: 17 additions & 0 deletions infra/core/monitor/applicationinsights.bicep
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
param name string
param location string = resourceGroup().location
param tags object = {}
tonybaloney marked this conversation as resolved.
Show resolved Hide resolved

resource applicationInsights 'Microsoft.Insights/components@2020-02-02' = {
name: name
location: location
tags: tags
kind: 'web'
properties: {
Application_Type: 'web'
}
}

output connectionString string = applicationInsights.properties.ConnectionString
output instrumentationKey string = applicationInsights.properties.InstrumentationKey
output name string = applicationInsights.name
17 changes: 17 additions & 0 deletions infra/core/monitor/monitoring.bicep
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
param applicationInsightsName string
param location string = resourceGroup().location
param tags object = {}

module applicationInsights 'applicationinsights.bicep' = {
name: 'applicationinsights'
params: {
name: applicationInsightsName
location: location
tags: tags
}
}

output applicationInsightsConnectionString string = applicationInsights.outputs.connectionString
output applicationInsightsInstrumentationKey string = applicationInsights.outputs.instrumentationKey
output applicationInsightsName string = applicationInsights.outputs.name

15 changes: 15 additions & 0 deletions infra/main.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ param appServicePlanName string = ''
param backendServiceName string = ''
param resourceGroupName string = ''

param applicationInsightsName string = ''

param searchServiceName string = ''
param searchServiceResourceGroupName string = ''
param searchServiceResourceGroupLocation string = location
Expand Down Expand Up @@ -84,6 +86,18 @@ resource storageResourceGroup 'Microsoft.Resources/resourceGroups@2021-04-01' ex
name: !empty(storageResourceGroupName) ? storageResourceGroupName : resourceGroup.name
}

// Monitor application with Azure Monitor
module monitoring './core/monitor/monitoring.bicep' = {
name: 'monitoring'
scope: resourceGroup
params: {
location: location
tags: tags
applicationInsightsName: !empty(applicationInsightsName) ? applicationInsightsName : '${abbrs.insightsComponents}${resourceToken}'
}
}


// Create an App Service Plan to group applications under the same payment plan and SKU
module appServicePlan 'core/host/appserviceplan.bicep' = {
name: 'appserviceplan'
Expand Down Expand Up @@ -123,6 +137,7 @@ module backend 'core/host/appservice.bicep' = {
AZURE_OPENAI_GPT_DEPLOYMENT: gptDeploymentName
AZURE_OPENAI_CHATGPT_DEPLOYMENT: chatGptDeploymentName
AZURE_OPENAI_EMB_DEPLOYMENT: embeddingDeploymentName
APPLICATIONINSIGHTS_CONNECTION_STRING: monitoring.outputs.applicationInsightsConnectionString
tonybaloney marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand Down