OpsCanary
observabilitytracingPractitioner

Connecting Application Traces to Database Queries: The Power of Trace Linkage

5 min read Grafana BlogNov 3, 2026Reviewed for accuracy
Share
Practitioner — Hands-on experience recommended

In modern observability practices, connecting the dots between application traces and database queries is essential. Trace linkage allows you to correlate telemetry from OpenTelemetry-instrumented applications with database telemetry collected using Alloy. This connection helps you pinpoint performance bottlenecks and understand how database interactions impact your application’s responsiveness.

The mechanism behind trace linkage relies on trace context, specifically the trace ID and span ID. When your application executes a query, spans are created that represent this execution. On the Database Observability side, telemetry is collected regarding these query executions. As long as the telemetry includes the same trace ID and span ID, you can effectively link the application trace to the database query. The sqlcommenter plays a pivotal role here; it allows SQL statements to carry a piece of trace context as a comment, which can be extracted and included in query sample logs.

However, there are important caveats to keep in mind. Having sqlcommenter enabled does not guarantee that Alloy captured the specific SQL execution. Additionally, not every database engine is supported, and using sqlcommenter can have downsides for database caches. This means you might encounter traces for which you cannot find a corresponding query sample in Database Observability, especially if relying solely on sqlcommenter.

Key takeaways

  • →Leverage trace context by using trace ID and span ID to connect application traces with database telemetry.
  • →Utilize sqlcommenter to embed trace context directly into SQL statements for better correlation.
  • →Be aware that sqlcommenter does not guarantee capture of every SQL execution in Alloy.
  • →Consider database engine compatibility when implementing trace linkage.
  • →Understand that relying solely on sqlcommenter may lead to missing query samples in Database Observability.

Why it matters

In production, trace linkage can significantly enhance your ability to diagnose performance issues by providing a clear view of how database queries affect application behavior. This insight leads to faster troubleshooting and improved user experiences.

When NOT to use this

The official docs don't call out specific anti-patterns here. Use your judgment based on your scale and requirements.

Want the complete reference?

Read official docs

Test what you just learned

Quiz questions written from this article

Take the quiz →
DigitalOcean Serverless InferenceSponsor

OpenAI & Anthropic-compatible inference API — no GPU provisioning needed. 55+ models, pay-per-token with no minimums. VPC + zero data retention by default.

Try Serverless Inference →

Get the daily digest

One email. 5 articles. Every morning.

No spam. Unsubscribe anytime.