Datadog APM tracing and logging

Recently, I have set up my micro services application onto datadog APM with tracing and logging.

something like,

so that we can follow through, for example, any http request or user_id from HTTP request, controller, service, authentication, database, and etc, with the same tracing ID and logs detailed.

Here are the steps I have followed:

  1. Set up datadog account and access, so that we will get the datadog API key, which is to be used to upload the data to datadog server
  2. install the datadog agent onto the server, we are using kubernetes on AWS (EKS) here, and have installed it as a daemonset (so that we have one for each node) with terraform.

set up the necessary environment variable in the daemonset

and expose the necessary ports

3. from the python services, leverage on libraries, for example,, to log the messages into JSON format

format = "%(asctime)s %(levelname)s [%(name)s]-- '[dd.trace_id=%(dd.trace_id)s dd.span_id=%(dd.span_id)s] ' -- %(message)s"
json = jsonlogger.JsonFormatter(format)

handler = logging.StreamHandler()

so that these logs would be in JSON format, and datadog would be able to parse the logs into tags & attributes.

4. then configure the datadog tracing

from ddtrace import config, patch_all, tracer

def setup_datadog():
        hostname=os.environ["DD_AGENT_HOST"],#set up in the deployment.yaml, the env attribute
    tracer.set_tags({"env": "production"})
    config.flask["service_name"] = "checkout-service" #this could also set up in the deployment.yaml, the env attribute
    config.flask["analytics_enabled"] = True
    config.flask["extra_error_codes"] = [401, 403]

5. finally, then annotate the entry point function, with


sample configuration in the deployment.yaml

kind: Deployment
apiVersion: apps/v1
  annotations: '[{"source":"python", "service": "checkout-service"}]'
  replicas: 1
      -  name: checkout
          - name: DD_AGENT_HOST
                fieldPath: status.hostIP
          - name: DD_SERVICE
            value: checkout-service
            value: "true"
      restartPolicy: Always
status: {}

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s