DataDog / dd-trace-py

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

asyncpg and Database Monitoring #7966

Closed Giaco9 closed 10 months ago

Giaco9 commented 11 months ago

Summary of problem

asyncpg is not supported for Database Monitoring. As reported in the table here, it seems that for python only psycopg2 is supported.

Which version of dd-trace-py are you using?

ddtrace==2.3.2

Which version of pip are you using?

pip --version
pip 23.3.1 from /.venv/lib/python3.8/site-packages/pip (python 3.8)
python --version
Python 3.8.18

Which libraries and their versions are you using?

pip freeze alembic==1.6.5 asyncpg==0.22.0 ddtrace==2.3.2 fastapi==0.73.0

How can we reproduce your problem?

Our application runs in docker, and the command to start the application is the following:

exec ddtrace-run python ./app/main.py

These are the environment variable set in the container:

DD_DBM_PROPAGATION_MODE=full
DD_ENV=production
DD_VERSION=v1.121.0
DD_REQUESTS_SERVICE=cronus
DD_TAGS=ContainerName:webapp
DD_SERVICE=cronus

What is the result that you get?

The APM works as expected, but we can't configure the tracer to correlate traces between the application and the database. The dd agent, which monitors the database, differs from the agent that monitors the application. The agent dedicated to the database is configured using docker labels. These are the labels:

"com.datadoghq.ad.check_names": "[\"postgres\"]",
"com.datadoghq.ad.init_configs": "[{}]",
"com.datadoghq.ad.instances": "[[{\"collect_schemas\":{\"enabled\":true},\"database_autodiscovery\":{\"enabled\":true,\"exclude\":[\"^rdsadmin$\"]},\"dbm\":true,\"host\":\"<database-host>\",\"password\":\"%%env_POSTGRES_0_PASSWORD%%\",\"port\":5432,\"relations\":[{\"relation_regex\":\".*\"}],\"tags\":[\"dbinstanceidentifier:production-platform-psql-version-13\",\"env:production\"],\"username\":\"datadog\"}]]"

For simplicity, I put here the JSON added as com.datadoghq.ad.instances label:

[
    [
        {
            "collect_schemas": {
                "enabled": true
            },
            "database_autodiscovery": {
                "enabled": true,
                "exclude": [
                    "^rdsadmin$"
                ]
            },
            "dbm": true,
            "host": "<database-host>",
            "password": "%%env_POSTGRES_0_PASSWORD%%",
            "port": 5432,
            "relations": [
                {
                    "relation_regex": ".*"
                }
            ],
            "tags": [
                "dbinstanceidentifier:production-platform-psql-version-13",
                "env:production"
            ],
            "username": "datadog"
        }
    ]
]

Please note that we have multiple databases in the same instance, and we successfully configured other applications whose traces correlate correctly to database metrics. Those applications are based on different languages and frameworks like python (with psycopg2), golang and kotlin. For those applications, the Database Monitoring feature is working correctly.

What is the result that you expected?

I would like to understand if there is any way to correctly configure an application based on fastapi and asyncpg to correlate APM data with Database monitoring data.

emmettbutler commented 10 months ago

@Giaco9 thanks for opening this ticket. There is currently not a standard way to correlate APM and DBM for unsupported databases. To implement it, one would need to do something similar to what the psycopg integration does, instantiating a _DBM_Propagator object and implementing a sql injector function for asyncpg. A pull request doing these things would be a welcome contribution for anyone who's up for it.

emmettbutler commented 10 months ago

I've added this as a feature request to our internal backlog, which means it will be considered during future prioritization rounds. Please comment or reopen if you disagree with my understanding that this is a feature request.

Giaco9 commented 10 months ago

Hi, Thank you for your reply :) I'm going to talk with the development team if they can have a look and open a pull request to implement this

josumoreno-BP commented 4 months ago

Hi @emmettbutler, we are also insterested on this feature. Can you share the priority you are managing for this task on your internal backlog?

Thanks

AmitArie commented 1 week ago

Any news?