Use the Aspire dashboard with Python apps
Dette indhold er ikke tilgængeligt i dit sprog endnu.
The Aspire dashboard provides a great user experience for viewing telemetry, and is available as a standalone container image that can be used with any OpenTelemetry-enabled app. In this article, you’ll learn how to:
- Start the Aspire dashboard in standalone mode.
- Use the Aspire dashboard with a Python app.
Prerequisites
Section titled “Prerequisites”To complete this tutorial, you need the following:
- Container runtime.
- You can use an alternative container runtime, but the commands in this article are for Docker.
- Python 3.9 or higher locally installed.
- A sample application.
Sample application
Section titled “Sample application”This tutorial can be completed using either Flask or FastAPI. A sample application in each framework is provided to help you follow along with this tutorial. Download or clone the sample application to your local workstation.
git clone https://github.com/Azure-Samples/msdocs-python-flask-webapp-quickstartgit clone https://github.com/Azure-Samples/msdocs-python-fastapi-webapp-quickstart.gitTo run the application locally, follow the framework-specific instructions for Flask or FastAPI.
To run the application locally:
-
Go to the application folder:
Change directory to the Flask app cd msdocs-python-flask-webapp-quickstart -
Create a virtual environment for the app:
Terminal window python -m venv venvsource venv/bin/activateTerminal window python -m venv venv.\venv\Scripts\Activate.ps1 -
Install the dependencies:
Terminal window pip install -r requirements.txt -
Run the app:
Terminal window flask run -
Browse to the sample application at
http://localhost:5000in a web browser.
-
Go to the application folder:
Change directory to the FastAPI app cd msdocs-python-fastapi-webapp-quickstart -
Create a virtual environment for the app:
Terminal window python -m venv venvsource venv/bin/activateTerminal window python -m venv venv.\venv\Scripts\Activate.ps1 -
Install the dependencies:
Terminal window pip install -r requirements.txt -
Run the app:
Terminal window uvicorn main:app --reload -
Browse to the sample application at
http://localhost:8000in a web browser.
Adding OpenTelemetry
Section titled “Adding OpenTelemetry”To use the Aspire dashboard with your Python app, you need to install the OpenTelemetry SDK and exporter. The OpenTelemetry SDK provides the API for instrumenting your application, and the exporter sends telemetry data to the Aspire dashboard.
-
Install the OpenTelemetry SDK and exporter:
Terminal window pip install opentelemetry-api opentelemetry-sdk opentelemetry-exporter-otlp-proto-grpc -
Add a new file to your application called
otlp_tracing.pyand add OpenTelemetry configuration code.import loggingfrom opentelemetry import metrics, tracefrom opentelemetry._logs import set_logger_providerfrom opentelemetry.exporter.otlp.proto.grpc._log_exporter import (OTLPLogExporter,)from opentelemetry.exporter.otlp.proto.grpc.metric_exporter import OTLPMetricExporterfrom opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporterfrom opentelemetry.sdk._logs import LoggerProvider, LoggingHandlerfrom opentelemetry.sdk._logs.export import BatchLogRecordProcessorfrom opentelemetry.sdk.metrics import MeterProviderfrom opentelemetry.sdk.metrics.export import PeriodicExportingMetricReaderfrom opentelemetry.sdk.trace import TracerProviderfrom opentelemetry.sdk.trace.export import BatchSpanProcessordef configure_oltp_grpc_tracing(endpoint: str = None) -> trace.Tracer:# Configure TracingtraceProvider = TracerProvider()processor = BatchSpanProcessor(OTLPSpanExporter(endpoint=endpoint))traceProvider.add_span_processor(processor)trace.set_tracer_provider(traceProvider)# Configure Metricsreader = PeriodicExportingMetricReader(OTLPMetricExporter(endpoint=endpoint))meterProvider = MeterProvider(metric_readers=[reader])metrics.set_meter_provider(meterProvider)# Configure Logginglogger_provider = LoggerProvider()set_logger_provider(logger_provider)exporter = OTLPLogExporter(endpoint=endpoint)logger_provider.add_log_record_processor(BatchLogRecordProcessor(exporter))handler = LoggingHandler(level=logging.NOTSET, logger_provider=logger_provider)handler.setFormatter(logging.Formatter("Python: %(message)s"))# Attach OTLP handler to root loggerlogging.getLogger().addHandler(handler)tracer = trace.get_tracer(__name__)return tracer -
Update your application (
app.pyfor Flask,main.pyfor FastAPI) to include the imports and call theconfigure_oltp_grpc_tracingfunction:import loggingfrom otlp_tracing import configure_otel_otlplogging.basicConfig(level=logging.INFO)tracer = configure_oltp_grpc_tracing()logger = logging.getLogger(__name__) -
Replace the
printcalls withlogger.infocalls in your application. -
Restart your application.
Framework specific instrumentation
Section titled “Framework specific instrumentation”This instrumentation has only focused on adding OpenTelemetry to our code. For more detailed instrumentation, you can use the OpenTelemetry Instrumentation packages for the specific frameworks that you are using.
-
Install the Flask instrumentation package:
Terminal window pip install opentelemetry-instrumentation-flask -
Add the Flask instrumentation to your application code.
from opentelemetry.instrumentation.flask import FlaskInstrumentor# add this line after configure_otel_otlp() callFlaskInstrumentor().instrument()
-
Install the FastAPI instrumentation package:
Terminal window pip install opentelemetry-instrumentation-fastapi -
Add the FastAPI instrumentation to your application code.
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor# add this line after configure_otel_otlp() callFastAPIInstrumentor.instrument_app(app)
Start the Aspire dashboard
Section titled “Start the Aspire dashboard”To start the Aspire dashboard in standalone mode, run the following Docker command:
docker run --rm -it -p 18888:18888 -p 4317:18889 --name aspire-dashboard \ mcr.microsoft.com/dotnet/aspire-dashboard:latestIn the Docker logs, the endpoint and key for the dashboard are displayed. Copy the key and navigate to http://localhost:18888 in a web browser. Enter the key to log in to the dashboard.
View structured logs
Section titled “View structured logs”Navigate around the Python application, and you’ll see structured logs in the Aspire dashboard. The structured logs page displays logs from your application, and you can filter and search the logs.

Next steps
Section titled “Next steps”You have successfully used the Aspire dashboard with a Python application. To learn more about the Aspire dashboard, see the Aspire dashboard overview and how to orchestrate a Python application with the Aspire AppHost.