Search code examples
mysqlmongodbtraceopen-telemetryotel

Does the traceparent header actually reaches the database servers (mongodb, mysql, postgres)?


If the application is instrumented with opentelemetry auto instrumentation (specifically @opentelemetry/auto-instrumentations-node) and the application makes outgoing database requests (mongodb, mysql, postgres), does the traceparent header actually reaches the database servers or just traces the operations at the application layer?

My application written in nodeJS is instrumented with @opentelemetry/auto-instrumentations-node. I tried looking up the traceparent header in the database packets using Wireshark, tcpdump, and database logs, but, I couldn't find it. Although, in the case of RabbitMQ or HTTP calls, I can clearly see the traceparent header inside the AMQP protocol headers and HTTP headers. So, for databases, does the traceparent actually propagate to the database servers or only limit working for the operations at the application layer?


Solution

  • Context propagation for databases is handled by injecting specifically formatted comments with the trace details. The format comes from sqlcommentor, which Google to the OpenTelemetry project.

    Here is an example from the sqlcommentor docs:

    2019-05-28 11:54:50.780 PDT [64128] LOG:  statement: INSERT INTO "polls_question"
    ("question_text", "pub_date") VALUES
    ('What is this?', '2019-05-28T18:54:50.767481+00:00'::timestamptz) RETURNING
    "polls_question"."id" /*controller='index',db_driver='django.db.backends.postgresql',
    framework='django%3A2.2.1',route='%5Epolls/%24',
    traceparent='00-5bd66ef5095369c7b0d1f8f4bd33716a-c532cb4098ac3dd2-01',
    tracestate='congo%3Dt61rcWkgMzE%2Crojo%3D00f067aa0ba902b7'*/
    

    It looks like there are libraries for Knex.js and Sequelize.js.

    The package @opentelemetry/sql-common exports a method named addSqlCommenterComment that would be a good starting point for a custom implementation that is specification compliant.