Skip to content

Conversation

@sincejune
Copy link
Contributor

…on to propagate trace info to SQL Server

Changes

This PR introduces a new flag, ContextPropagationLevel, in SqlClientInstrumentation, which enables the library to propagate traceparent information to the SQL Server database.

The ContextPropagationLevel can be set to either trace or disabled (the default value). When set to trace, the instrumentation library uses the SET CONTEXT_INFO command to pass a traceparent ID of the current Activity to the SQL Server.

Next steps:

I plan to introduce a "service" level that will pass service information via SQL commands via sqlcommenter.

Merge requirement checklist

  • CONTRIBUTING guidelines followed (license requirements, nullable enabled, static analysis, etc.)
  • Unit tests updated: Enable this flag by default in integration tests
  • Appropriate CHANGELOG.md files updated for non-trivial changes
  • Changes in public API reviewed (if applicable)

@sincejune sincejune requested a review from a team as a code owner April 22, 2025 11:59
@github-actions github-actions bot added comp:exporter.geneva Things related to OpenTelemetry.Exporter.Geneva comp:instrumentation.sqlclient Things related to OpenTelemetry.Instrumentation.SqlClient labels Apr 22, 2025
@codecov
Copy link

codecov bot commented Apr 22, 2025

Codecov Report

Attention: Patch coverage is 91.42857% with 3 lines in your changes missing coverage. Please review.

Project coverage is 88.48%. Comparing base (8e317c1) to head (4ca9eab).
Report is 1 commits behind head on main.

✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
...lementation/SqlClientInstrumentationEventSource.cs 50.00% 2 Missing ⚠️
...ient/Implementation/SqlClientDiagnosticListener.cs 96.00% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##             main    #2709       +/-   ##
===========================================
+ Coverage   69.74%   88.48%   +18.73%     
===========================================
  Files         394        9      -385     
  Lines       15980      495    -15485     
===========================================
- Hits        11146      438    -10708     
+ Misses       4834       57     -4777     
Flag Coverage Δ
unittests-Contrib.Shared.Tests ?
unittests-Exporter.Geneva ?
unittests-Exporter.InfluxDB ?
unittests-Exporter.Instana ?
unittests-Exporter.OneCollector ?
unittests-Exporter.Stackdriver ?
unittests-Extensions ?
unittests-Extensions.Enrichment ?
unittests-Instrumentation.AWS ?
unittests-Instrumentation.AspNet ?
unittests-Instrumentation.AspNetCore ?
unittests-Instrumentation.ConfluentKafka ?
unittests-Instrumentation.ElasticsearchClient ?
unittests-Instrumentation.EntityFrameworkCore ?
unittests-Instrumentation.EventCounters ?
unittests-Instrumentation.GrpcCore ?
unittests-Instrumentation.GrpcNetClient ?
unittests-Instrumentation.Hangfire ?
unittests-Instrumentation.Http ?
unittests-Instrumentation.Owin ?
unittests-Instrumentation.Process ?
unittests-Instrumentation.Quartz ?
unittests-Instrumentation.Runtime ?
unittests-Instrumentation.ServiceFabricRemoting ?
unittests-Instrumentation.SqlClient 88.48% <91.42%> (+0.22%) ⬆️
unittests-Instrumentation.StackExchangeRedis ?
unittests-Instrumentation.Wcf ?
unittests-PersistentStorage ?
unittests-Resources.AWS ?
unittests-Resources.Azure ?
unittests-Resources.Container ?
unittests-Resources.Gcp ?
unittests-Resources.Host ?
unittests-Resources.OperatingSystem ?
unittests-Resources.Process ?
unittests-Resources.ProcessRuntime ?
unittests-Sampler.AWS ?

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
....SqlClient/SqlClientTraceInstrumentationOptions.cs 100.00% <100.00%> (ø)
...ient/Implementation/SqlClientDiagnosticListener.cs 88.60% <96.00%> (+1.38%) ⬆️
...lementation/SqlClientInstrumentationEventSource.cs 67.85% <50.00%> (-2.98%) ⬇️

... and 385 files with indirect coverage changes

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Kielek
Copy link
Member

Kielek commented Apr 22, 2025

@sincejune, could you please rise issue under semantic convention repository? It is not something we should accept without common standard for all supported languages (including other db clients).

@sincejune
Copy link
Contributor Author

@sincejune, could you please rise issue under semantic convention repository? It is not something we should accept without common standard for all supported languages (including other db clients).

@Kielek Sure, filed open-telemetry/semantic-conventions#2162

atoulme added a commit to open-telemetry/opentelemetry-collector-contrib that referenced this pull request Apr 24, 2025
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue.
Ex. Adding a feature - Explain what this achieves.-->
#### Description
Accept `context_info` with `traceparent` format in query sample
collection, setting log record with correct traceId and spanId.

Example use case:
open-telemetry/opentelemetry-dotnet-contrib#2709

<!-- Issue number (e.g. #1234) or full URL to issue, if applicable. -->
#### Link to tracking issue
n/a

<!--Describe what testing was performed and which tests were added.-->
#### Testing
Updated

<!--Describe the documentation added.-->
#### Documentation
Added

<!--Please delete paragraphs that you did not use before submitting.-->

---------

Co-authored-by: Antoine Toulme <[email protected]>
@github-actions
Copy link
Contributor

This PR was marked stale due to lack of activity. It will be closed in 7 days.

@github-actions github-actions bot added the Stale label Apr 30, 2025
@github-actions
Copy link
Contributor

github-actions bot commented May 7, 2025

Closed as inactive. Feel free to reopen if this PR is still being worked on.

@github-actions github-actions bot closed this May 7, 2025
@Kielek Kielek reopened this Jul 8, 2025
@Kielek
Copy link
Member

Kielek commented Jul 8, 2025

Reopening, per request in private channel. open-telemetry/semantic-conventions#2363 is now merged

@github-actions github-actions bot removed the comp:exporter.geneva Things related to OpenTelemetry.Exporter.Geneva label Jul 8, 2025
@sincejune sincejune force-pushed the sqlclient-propagation-trace-context branch from 2c304af to 215add0 Compare July 8, 2025 11:50
@github-actions github-actions bot removed the Stale label Jul 9, 2025
@Kielek Kielek changed the title [Instrumentation.SqlClient] Introducing ContextPropagationLevel opini… [Instrumentation.SqlClient] Introducing context propagationto SQL Server Jul 9, 2025
Copy link
Member

@Kielek Kielek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is some issues on .NET Framework 4.6.2 (locally executed) in integration tests
2 cases are failing on

        if (commandText == GetContextInfoQuery)
        {
            Assert.NotEqual(commandResult, DBNull.Value); // <---- failing here
            Assert.True(commandResult is byte[]);
            var contextInfo = Encoding.ASCII.GetString((byte[])commandResult).TrimEnd('\0');
            Assert.Equal(contextInfo, activity.Id);
        }

.NET 8 and 9 are passing.

@martincostello
Copy link
Member

There is some issues on .NET Framework 4.6.2 (locally executed) in integration tests

Do the .NET Framework tests not run in CI (I thought they did)? That might explain why #2826 (comment) happened.

@Kielek
Copy link
Member

Kielek commented Jul 11, 2025

There is some issues on .NET Framework 4.6.2 (locally executed) in integration tests

Do the .NET Framework tests not run in CI (I thought they did)? That might explain why #2826 (comment) happened.

It requires docker for linux. It is not available on the GH runners on Windows systems. So itnegration tests are not executed for such cases.

@cuichenli
Copy link
Member

There is some issues on .NET Framework 4.6.2 (locally executed) in integration tests

The current implementation does not yet support .NET Framework. We may need to explore how to integrate it later.

#if NETFRAMEWORK
this.sqlEventSourceListener = new SqlEventSourceListener();
#else
this.diagnosticSourceSubscriber = new DiagnosticSourceSubscriber(
name => new SqlClientDiagnosticListener(name),

The logic is currently implemented in the SqlClientDiagnosticListener.cs file, but it's only used when running on .NET.
Given that, the most straightforward solution for now is to disable the context info check in .NET Framework tests.

Would that be acceptable? If not, we can continue working on support in this PR. Otherwise, we can defer it to a follow-up PR. Thanks!

@Kielek Kielek requested a review from martincostello July 14, 2025 08:21
@YayBurritos
Copy link

@sincejune , @alanwest , @Kielek : Any concerns over using SET CONTEXT_INFO with connection pooling? Is there a risk of the TraceId that's propagated "contaminating" unrelated SQL queries? It's great that the instrumentation library provides support for TraceId propagation, but I'd like to have some confidence that if our applications use this, we can rely on the TraceIds associated with our SQL queries to be accurate. Any info you can provide would be appreciated. Thanks!

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

Labels

comp:instrumentation.sqlclient Things related to OpenTelemetry.Instrumentation.SqlClient

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants