Datadog Application Performance Monitoring (APM) provides deep visibility into your applications, enabling you to identify performance bottlenecks, troubleshoot issues, and optimize your services.
This guide demonstrates how to get started with APM and send your first trace to Datadog:
On your Linux host or VM, create a new Python application named hello.py. For example, nano hello.py.
Add the following code to hello.py:
hello.py
fromflaskimportFlaskimportrandomapp=Flask(__name__)quotes=["Strive not to be a success, but rather to be of value. - Albert Einstein","Believe you can and you're halfway there. - Theodore Roosevelt","The future belongs to those who believe in the beauty of their dreams. - Eleanor Roosevelt"]@app.route('/')defindex():quote=random.choice(quotes)+"\n"returnquoteif__name__=='__main__':app.run(host='0.0.0.0',port=5050)
Set up Datadog APM
To set up Datadog APM without needing to modify your application’s code or the deployment process, use Single Step APM Instrumentation, or alternatively, you can set up APM using Datadog tracing libraries.
Replace <YOUR_DD_API_KEY> with your Datadog API key, <YOUR_DD_SITE> with your Datadog site, and <AGENT_ENV> with the environment your Agent is installed on (for example, development).
Restart the services on your host or VM.
Verify the Agent is running:
sudo datadog-agent status
This approach automatically installs the Datadog Agent, enables Datadog APM, and instruments your application at runtime.
Run the application
When you set up Datadog APM with Single Step Instrumentation, Datadog automatically instruments your application at runtime.
To run hello.py:
Create a Python virtual environment in the current directory:
Believe you can and you're halfway there. - Theodore Roosevelt
Each time you run the curl command, a new trace is sent to Datadog.
Explore traces in Datadog
In Datadog, go to APM > Services. You should see a Python service named hello:
Select the service to view its performance metrics, such as latency, throughput, and error rates.
Go to APM > Traces. You should see a trace for the hello service:
Select a trace to see its details, including the flame graph, which helps identify performance bottlenecks.
Advanced APM setup
Up until this point, you let Datadog automatically instrument the hello.py application using Single Step Instrumentation. This approach is recommended if you want to capture essential traces across common libraries and languages without touching code or manually installing libraries.
However, if you need to collect traces from custom code or require more fine-grained control, you can add custom instrumentation.
To illustrate this, you will import the Datadog Python tracing library into hello.py and create a custom span and span tag.
To add custom instrumentation:
Install the Datadog tracing library:
pip install ddtrace
Add the highlighted lines to the code in hello.py to create a custom span tag get_quote and a custom span tag quote:
fromflaskimportFlaskimportrandomfromddtraceimporttracerapp=Flask(__name__)quotes=["Strive not to be a success, but rather to be of value. - Albert Einstein","Believe you can and you're halfway there. - Theodore Roosevelt","The future belongs to those who believe in the beauty of their dreams. - Eleanor Roosevelt"]@app.route('/')defindex():withtracer.trace("get_quote")asspan:quote=random.choice(quotes)+"\n"span.set_tag("quote",quote)returnquoteif__name__=='__main__':app.run(host='0.0.0.0',port=5050)
Run hello.py in the virtual environment from earlier:
ddtrace-run python hello.py
Run a few curl commands in a separate command prompt:
Find the new custom get_quote span in the flame graph and hover over it:
Notice that the custom quote span tag displays on the Info tab.
What’s next?
After you set up tracing and your application is sending data to Datadog, explore additional APM features:
Software Catalog
Software Catalog provides a consolidated view of your services, combining ownership metadata, performance insights, security analysis, and cost allocation in one place. Configure service metadata using tags, annotations, or a service.datadog.yaml file to enrich your services with ownership information, runbooks, and documentation links.
Trace ingestion and retention
Control costs and manage data volume by configuring ingestion controls and retention filters. Ingestion controls let you customize sampling rates at the Datadog Agent or tracing library level, while retention filters determine which spans are indexed for search and analytics.
Further reading
Additional helpful documentation, links, and articles: