microsoft / sample-app-aoai-chatGPT

Sample code for a simple web chat experience through Azure OpenAI, including Azure OpenAI On Your Data.
MIT License
1.57k stars 2.43k forks source link

Integrating Azure Application Insights using opentelemetry-instrumentation in the Sample-app-aoai-chatGPT #495

Open DWDNZ001 opened 8 months ago

DWDNZ001 commented 8 months ago

Integrating Azure Application Insights with the Sample-app-aoai-chatGPT

This guide walks through the process of integrating Azure Application Insights into the AOAI sample app using OpenTelemetry. This integration allows for enhanced monitoring and observability of the application. (Don't forget to run .\start.cmd to at the end to rebuild the vite / JS files before commiting and syncing your changes to Github so it gets deployed correctly to the web app!! )

Keep an eye on the Python package numbers for opentelemetry packages, don't forget the distro ! If Pip is up to date then it should warn you..

Prerequisites An Azure Application Insights instance. A fork of the AOAI sample app repository in Github and a copy of Visual Studio Code or Code spaces connected to the repo.

Configuration Steps

1. Configure Azure Application Insights After creating your Application Insights instance in Azure, add the APPLICATIONINSIGHTS_CONNECTION_STRING to your Azure Web App Service configuration for the sample web applications.

Key Setting:

APPLICATIONINSIGHTS_CONNECTION_STRING :

2. Update Local Environment

In your local environment, within the Visual Studio Code or your preferred editor, navigate to the root of your fork of the AOAI sample app repository.

3. Install Required Python Packages Run the following command to install necessary Python packages for OpenTelemetry:

pip install opentelemetry-distro==0.43b0 opentelemetry-instrumentation==0.43b0 opentelemetry-instrumentation-flask==0.43b0 opentelemetry-sdk==1.22.0 opentelemetry-instrumentation-requests==0.43b0 azure-monitor-opentelemetry-exporter==1.0.0b18

4. Update requirements.txt Modify the requirements.txt file under the /static directory to include the new packages:

azure-identity==1.14.0 opentelemetry-distro==0.43b0 opentelemetry-sdk==1.22.0 opentelemetry-instrumentation==0.43b0 opentelemetry-instrumentation-flask==0.43b0 opentelemetry-instrumentation-requests==0.43b0 azure-monitor-opentelemetry-exporter==1.0.0b18 Flask==2.3.2 openai==0.27.7 azure-search-documents==11.4.0b6 azure-storage-blob==12.17.0 python-dotenv==1.0.0 azure-cosmos==4.5.0 fpdf==1.7.2 python-docx==0.8.11 pymongo==4.5.0

5. Update app.py Modify app.py to include OpenTelemetry configuration. Replace the existing imports and setup with the following code:

5. Update app.py

Modify app.py to include OpenTelemetry configuration. Replace the existing imports and setup with the following code:

import json
import os
import logging
import requests
import openai
from azure.identity import DefaultAzureCredential
from flask import Flask, Response, request, jsonify, send_from_directory
from dotenv import load_dotenv
from backend.auth.auth_utils import get_authenticated user_details
from backend.history.cosmosdbservice import CosmosConversationClient

# OpenTelemetry imports and setup
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter
from opentelemetry.instrumentation.flask import FlaskInstrumentor
from opentelemetry.instrumentation.requests import RequestsInstrumentor

load_dotenv()

APPLICATIONINSIGHTS_CONNECTION_STRING = os.environ.get("APPLICATIONINSIGHTS_CONNECTION_STRING")

# Configure logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

# Configure OpenTelemetry with Azure Monitor Exporter
trace.set_tracer_provider(TracerProvider())
tracer_provider = trace.get_tracer_provider()
tracer_provider.add_span_processor(
    BatchSpanProcessor(AzureMonitorTraceExporter(
        connection_string=APPLICATIONINSIGHTS_CONNECTION_STRING)
    )
)

app = Flask(__name__, static_folder="static")

# Enable Flask auto-instrumentation
FlaskInstrumentor().instrument_app(app)

# Enable Requests auto-instrumentation
RequestsInstrumentor().instrument()

The rest of the app.py code..

6. Run the Application Execute the ./start.cmd script to start the application. This will apply the changes and start the Flask server with OpenTelemetry integration.

7. Finalize Changes Ensure to save, commit, and sync these changes to your forked private repository.

DWDNZ001 commented 8 months ago

Feel free to remove thi if it's already been sorted...

basspro-asif commented 1 month ago

current app is using the quart implementation? how we can integrate with?