Skip to content

[OpenTelemetry] Handle duplicate sampler attributes - #7558

Merged
martincostello merged 2 commits into
open-telemetry:mainfrom
xiang17:xiang17/duplicate-sampler-attribute
Jul 29, 2026
Merged

martincostello merged 2 commits into
open-telemetry:mainfrom
xiang17:xiang17/duplicate-sampler-attribute

Conversation

@xiang17

@xiang17 xiang17 commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Fixes Azure/azure-sdk-for-net#58948
Design discussion issue #

Changes

Replaced ActivityTagsCollection.Add with indexer assignment when applying sampler attributes during Activity creation.

This prevents Activity creation from throwing when multiple matching TracerProvider instances return an attribute with the same key. The behavior now matches the existing legacy Activity path, which applies sampler attributes using Activity.SetTag replacement semantics.

When multiple samplers return different values for the same key, the value from the last invoked listener is retained.

Added a regression test with two providers that return the same attribute key.

Motivation

OpenTelemetry supports multiple TracerProvider instances, and .NET ActivitySource invokes every matching ActivityListener while creating one Activity. These listeners contribute to the Activity's shared sampling-tag collection.

Previously, the second provider failed while adding a duplicate key:

System.InvalidOperationException: The collection already contains item with same key 'duplicate-sampler-attribute'

This exception prevents Activity creation and, in ASP.NET Core TestHost, may present as a request timeout.

Validation

Before the fix, the new regression test failed in TracerProviderSdk.ComputeActivitySamplingResult.

After the fix:

  • TracerProviderSdkDuplicateSamplerAttributesUseLastValue passed.
  • Full TracerProviderSdkTests class passed: 67 tests.
  • Production source formatting verification passed.

Merge requirement checklist

  • CONTRIBUTING guidelines followed (license requirements, nullable enabled, static analysis, etc.)
  • Unit tests added/updated
  • Appropriate CHANGELOG.md files updated for non-trivial changes
  • [ ] Changes in public API reviewed (if applicable)

@xiang17
xiang17 requested a review from a team as a code owner July 28, 2026 22:46
@github-actions github-actions Bot added the pkg:OpenTelemetry Issues related to OpenTelemetry NuGet package label Jul 28, 2026
@opentelemetry-pr-dashboard

opentelemetry-pr-dashboard Bot commented Jul 28, 2026

Copy link
Copy Markdown

Pull request dashboard status

Merged · refreshed 2026-07-29 06:33 UTC

Status above doesn't look right?
  • Anything look wrong? Report it with what you expected; it helps us improve the dashboard.

@codecov

codecov Bot commented Jul 28, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.39%. Comparing base (9440662) to head (f081445).
✅ All tests successful. No failed tests found.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #7558      +/-   ##
==========================================
- Coverage   90.43%   90.39%   -0.04%     
==========================================
  Files         290      290              
  Lines       15989    15989              
==========================================
- Hits        14459    14453       -6     
- Misses       1530     1536       +6     
Flag Coverage Δ
unittests-Project-Experimental 90.35% <100.00%> (-0.17%) ⬇️
unittests-Project-Stable 90.43% <100.00%> (-0.02%) ⬇️

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

Files with missing lines Coverage Δ
src/OpenTelemetry/Trace/TracerProviderSdk.cs 98.72% <100.00%> (ø)

... and 3 files with indirect coverage changes

@cijothomas cijothomas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM.

@martincostello martincostello changed the title Handle duplicate sampler attributes from multiple tracer providers [OpenTelemetry] Handle duplicate sampler attributes Jul 29, 2026
@martincostello
martincostello added this pull request to the merge queue Jul 29, 2026
Merged via the queue into open-telemetry:main with commit 2e44c2a Jul 29, 2026
78 checks passed
@xiang17
xiang17 deleted the xiang17/duplicate-sampler-attribute branch August 5, 2026 21:35
This was referenced Sep 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pkg:OpenTelemetry Issues related to OpenTelemetry NuGet package

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Azure.Monitor.OpenTelemetry.AspNetCore 1.5.0 causes dotnet test to hang indefinitely

3 participants