![]() |
VOOZH | about |
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:
To complete this guide, you need the following:
To create an application to observe in 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
from flask import Flask
import random
app = 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('/')
def index():
quote = random.choice(quotes)+"\n"
return quote
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5050)
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.
Run the installation command:
DD_API_KEY=<YOUR_DD_API_KEY> DD_SITE="<YOUR_DD_SITE>" DD_APM_INSTRUMENTATION_ENABLED=host DD_APM_INSTRUMENTATION_LIBRARIES=python:4 DD_ENV=<AGENT_ENV> bash -c "$(curl -L https://install.datadoghq.com/scripts/install_script_agent7.sh)"
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.
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:
python3 -m venv ./venv
Activate the venv virtual environment:
source ./venv/bin/activate
Install pip and flask:
sudo apt-get install python3-pip
pip install flask
Set the service name and run hello.py:
export DD_SERVICE=hello
python3 hello.py
Test the application to send traces to Datadog:
In a new command prompt, run the following:
curl http://0.0.0.0:5050/
Confirm that a random quote is returned.
Believe you can and you're halfway there. - Theodore Roosevelt
Each time you run the curl command, a new trace is sent to 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.
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 SDK into hello.py and create a custom span and span tag.
To add custom instrumentation:
Install the Datadog SDK:
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:
from flask import Flask
import random
from ddtrace import tracer
app = 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('/')
def index():
with tracer.trace("get_quote") as span:
quote = random.choice(quotes)+"\n"
span.set_tag("quote", quote)
return quote
if __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:
curl http://0.0.0.0:5050/
In Datadog, go to APM > Traces.
Select the hello trace.
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.
After you set up tracing and your application is sending data to Datadog, explore additional APM features:
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.
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 SDK level, while retention filters determine which spans are indexed for search and analytics.
Additional helpful documentation, links, and articles:
| |