APM using Datadog.

Jose López
December 6th, 2019 · 2 min read

This is the second article of a series of articles about monitoring.

-> First article: Monitoring CloudWatch key metrics using Slack and manage them using Terraform

-> Third article: Monitoring CodePipeline deployments

-> Fourth article: Monitoring external services status using RSS and Slack

Aplication performance management (APM) is the monitoring and management of performance and availability of software applications. APM strives to detect and diagnose complex application performance problems to maintain an expected level of service. APM is “the translation of IT metrics into business meaning”. The ultimate goal of performance monitoring is to supply end users with a top quality end-user experience.

Datadog APM setup

Datadog APM is a Datadog suite tool to monitor, troubleshoot, and optimize end-to-end application performance.

Datadog APM offers a 14-day free trial where you can monitor as many host as you want. After that period, you are billed for the number of hosts and analyzed spans. Check the pricing page for more details.

Datadog uses ddtrace client to gather application performance metrics. There are official ddtrace libraries for .NET, Go, Java, Node.js, PHP, Python and Ruby, and community libraries for other languages. In this post, we are going to set up ddtrace-py, the official ddtrace client for Python.

The other part of the system is Datadog Agent. Datadog Agent is a software that runs on your hosts. It collects events and metrics from hosts (in this case, ddtrace gathers application info and send it to the Agent) and sends them to Datadog.

Deploying and configuring Datadog APM on a Django application running on ECS Fargate

Deploying Datadog Agent

The first part we need is Datadog Agent. In this example, we will use ECS Fargate, so we will deploy it as a sidecar container of the main application. We need to add a new container in the container definitions section of our application task definition.

1{
2 "name": "datadog-agent",
3 "image": "datadog/agent:6.15.1", # Use always a fixed tag to avoid undesired updates.
4 "environment": [
5 {
6 "name": "ECS_FARGATE",
7 "value": "true"
8 },
9 {
10 "name": "DD_APM_ENABLED",
11 "value": "true"
12 },
13 {
14 "name": "DD_ANALYTICS_ENABLED",
15 "value": "true"
16 }
17 ],
18 "secrets": [{
19 "name": "DD_API_KEY",
20 "valueFrom": "arn:aws:secretsmanager:region:aws_account_id:secret:secret_name-AbCdEf"
21 }]
22 "portMappings": [
23 {
24 "containerPort": 8126,
25 "hostPort": 8126,
26 "protocol": "tcp"
27 }
28 ]
29 }

DD_APM_ENABLED enables the Trace Agent and DD_ANALYTICS_ENABLED enables App Analytics.

Installing and configuring ddtrace-py

Once Datadog Agent is up and running it’s time to install and configure ddtrace-py on our Django application. First, we need to install the package. This is as easy as running pip install ddtrace. Then, prefix your Python entry-point command with ddtrace-run. For example, if you’re using uwsgi, do something like: ddtrace-run uwsgi --ini ./uwsgi.ini

ddtrace-py supports a big number of libraries out of the box. There are also community packages to trace other libraries.

On our Django settings.py file, we are going to add the following options.

1DATADOG_TRACE = {
2 'DEFAULT_SERVICE': '<SERVICE_NAME>',
3 'AGENT_HOSTNAME': '127.0.0.1',
4 'TAGS': {
5 'env': os.environ.get('ENV'),
6 },
7 'ENABLED': 'True'
8}

After this, ddtrace will start sending traces to Datadog Agent, and we will be able to see them on Datadog UI.

Manual instrumentation

You can trace functions manually by using ddtrace.Tracer.

1from ddtrace import tracer
2@tracer.wrap()
3def execute():
4 return 'executed'

More info about how to use the Tracer class is available here.

Visualize APM information on Datadog UI

Datadog offers a handy UI to visualize APM information gathered by ddtrace and Datadog Agent.

First of all, under APM/Services label, you’ll see a list of all your services that are sending traces to Datadog.

For every service, you can see differents graphs: latency, errors, number of requests, time spent…

Then you can filter traces by endpoint.

And for every trace, you can see detailed info. What step took most of the time on a request, errors, error messages…

Thanks for reading!

In this article, we covered how to get application monitoring data from our Django application. This will be very useful to find the slowest points of our applications, optimize queries… and improve the user experience and web performance. In the next articles, we will learn how to monitor deployments and external services used on our stack. Stay tuned!

More articles from Obytes

Monitoring CloudWatch key metrics using Slack and manage them using Terraform.

Key metrics to monitor using AWS CloudWatch, track them using Slack and manage them using Terraform.

November 18th, 2019 · 2 min read

UX/UI design: How to get started

A step by step guide on how to get started in UX/UI design.

November 5th, 2019 · 6 min read

ABOUT US

Our mission and ambition is to challenge the status quo, by doing things differently we nurture our love for craft and technology allowing us to create the unexpected.