Skip to content

Add fields to JSON logging #2496

Description

What is needed?

Due the issue we have in correlating APM logs and traces to have a global view on each request, we need to add to each request 5 new fields to correlate.

The fields needed are:

dd.service
dd.env
dd.version
dd.trace_id
dd.span_id

These fields have to be a printout of some env vars that will be passed to the deployment of the service itself.

In datadog they indicate how to do it:

Include the dd.env, dd.service, dd.version, dd.trace_id and dd.span_id attributes for your log record in the format string.

Here is an example using logging.basicConfig to configure the log injection:


import logging
from ddtrace import tracer

FORMAT = ('%(asctime)s %(levelname)s [%(name)s] [%(filename)s:%(lineno)d] '
          '[dd.service=%(dd.service)s dd.env=%(dd.env)s dd.version=%(dd.version)s dd.trace_id=%(dd.trace_id)s dd.span_id=%(dd.span_id)s] '
          '- %(message)s')
logging.basicConfig(format=FORMAT)
log = logging.getLogger(__name__)
log.level = logging.INFO

@tracer.wrap()
def hello():

a My main idea is in the logger you have to enable these parameters or disable them depending on an environment variable (e.g. DD_TRACE_LOGS_ENABLE or something like this)

References:

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions