DataDog / dd-trace-py

Datadog Python APM Client
https://ddtrace.readthedocs.io/
Other
546 stars 411 forks source link

Can't get logs injection to work #11133

Closed tsoos99dev closed 5 hours ago

tsoos99dev commented 5 hours ago

Hi. I'd like to send logs to the web portal, but not matter what I try I'm only getting traces to show.

Here's the example code I'm running:

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 main():
    log.info("Hello World")

if __name__ == '__main__':
    main()

I'm running it like this: DD_SERVICE="webapp" DD_ENV="dev" DD_VERSION="1.0.0" DD_LOGS_INJECTION=true ddtrace-run python main.py

On the console output I get the following: 2024-10-22 22:30:15,162 INFO [__main__] [standalone.py:14] [dd.service=webapp dd.env=dev dd.version=1.0.0 dd.trace_id=67180b570000000015d3de3d2700c4d1 dd.span_id=8815720975494057739] - Hello World

I have the agent configured with logs_enabled: true. Otherwise it's a fresh install on my macbook. Should ddtrace-run handle passing on the logs to the agent based on my setup? It passes on the traces just fine. What could be going wrong?

mabdinur commented 5 hours ago

Hey @tsoos99dev,

The ddtrace library injects the active span data (span_id, trace_id, env, service, etc.) into your logs but it does not submit logs to Datadog.

To send your logs to Datadog you will need to enable and configure log collection on your Datadog Agent. For more detailed steps you can refer to this page: https://docs.datadoghq.com/getting_started/logs/.

Feel free to reopen this issue if you experience correlating traces and logs.