Skip to content

Avoid copying Activity tags and links when sampling - #135277

Merged
tarekgh merged 3 commits into
dotnet:mainfrom
martincostello:gh-135007
Oct 7, 2026
Merged

tarekgh merged 3 commits into
dotnet:mainfrom
martincostello:gh-135007

Conversation

@martincostello

Copy link
Copy Markdown
Member

Do not copy links and tags when creating a new Activity when ActivitySamplingResult.PropagationData is specified.

Fixes #135007.

Benchmarks

BenchmarkDotNet, default job, --inProcess --affinity 4095, creating, starting and stopping an activity with the listener returning the given sampling result. main vs. PR; the tag set is the 9 tags ASP.NET Core-style server instrumentation passes at creation, plus one link. Ratios are PR / main (lower is better).

Sampling Creation data Method main Mean main Allocated PR Mean PR Allocated Time Ratio Alloc Ratio
PropagationData None NoTags 120.8 ns 416 B 122.0 ns 416 B 1.01 1.00
PropagationData 9 tags NineTags 211.2 ns 848 B 116.0 ns 416 B 0.55 0.49
PropagationData 9 tags + 1 link NineTagsAndLink 234.7 ns 976 B 116.0 ns 416 B 0.49 0.43
AllDataAndRecorded None NoTags 126.7 ns 416 B 116.4 ns 416 B 0.92 1.00
AllDataAndRecorded 9 tags NineTags 202.0 ns 848 B 195.0 ns 848 B 0.97 1.00
AllDataAndRecorded 9 tags + 1 link NineTagsAndLink 242.9 ns 976 B 225.4 ns 976 B 0.93 1.00
Benchmark Code
using System.Diagnostics;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;

BenchmarkSwitcher.FromAssembly(typeof(PropagationDataBenchmarks).Assembly).Run(args);

[MemoryDiagnoser]
public class PropagationDataBenchmarks
{
    private static readonly KeyValuePair<string, object?>[] s_tags =
    [
        new("client.address", "192.0.2.1"), new("network.peer.address", "192.0.2.1"), new("network.peer.port", 50000),
        new("server.address", "localhost"), new("server.port", 5000), new("http.request.method", "GET"),
        new("user_agent.original", "Mozilla/5.0"), new("url.scheme", "https"), new("url.path", "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/api/items"),
    ];

    private static readonly ActivityLink[] s_links = [new(new ActivityContext(ActivityTraceId.CreateRandom(), ActivitySpanId.CreateRandom(), ActivityTraceFlags.None))];

    private ActivitySource _source = null!;
    private ActivityListener _listener = null!;
    private ActivitySamplingResult _result;

    [Params(ActivitySamplingResult.PropagationData, ActivitySamplingResult.AllDataAndRecorded)]
    public ActivitySamplingResult Sampling { get; set; }

    [GlobalSetup]
    public void Setup()
    {
        Console.WriteLine("Assembly: " + typeof(Activity).Assembly.Location);
        _result = Sampling;
        _source = new ActivitySource("Bench");
        _listener = new ActivityListener
        {
            ShouldListenTo = s => ReferenceEquals(s, _source),
            Sample = (ref _) => _result,
        };
        ActivitySource.AddActivityListener(_listener);
    }

    [GlobalCleanup]
    public void Cleanup() { _listener.Dispose(); _source.Dispose(); }

    [Benchmark(Baseline = true)]
    public void NoTags() => Run(null, null);

    [Benchmark]
    public void NineTags() => Run(s_tags, null);

    [Benchmark]
    public void NineTagsAndLink() => Run(s_tags, s_links);

    private void Run(KeyValuePair<string, object?>[]? tags, ActivityLink[]? links)
    {
        var a = _source.CreateActivity("Request", ActivityKind.Server, default(ActivityContext), tags, links);
        a!.Start();
        a.Stop();
    }
}

Do not copy links and tags when creating a new `Activity` when `ActivitySamplingResult.PropagationData` is specified.

Fixes dotnet#135007.
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Oct 6, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @steveisok, @dotnet/area-system-diagnostics-tracing
See info in area-owners.md if you want to be subscribed.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-diagnostics-activity
See info in area-owners.md if you want to be subscribed.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟡 Changes recommended

Sampler tags violate their documented contract, and the behavioral change needs compatibility documentation.

Review effort: Balanced
Findings: 1 Low severity

Open (1)
What changed in this PR

Optimizes Activity creation for PropagationData sampling.

Changes:

  • Skips copying creation tags, links, and sampler tags.
  • Adds coverage for parent formats and multiple listeners.
File Description
Activity.cs Conditionally skips data copying.
ActivitySourceTests.cs Tests the revised sampling behavior.

@tarekgh tarekgh added this to the 12.0.0 milestone Oct 6, 2026
- Harden check for skipping tags.
- Don't skip sampler tags.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟡 Changes recommended

Public API documentation remains inaccurate for the new sampling semantics.

Review effort: Balanced
Findings: 2 Low severity

Open (2)

Update `ActivitySource,CreateActivity()` parameter documentation for `tags` and `links` for new behaviour.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟡 Changes recommended

The existing unresolved compatibility-documentation requirement blocks approval despite the implementation and tests appearing sound.

Review effort: Balanced
Findings: 2 Low severity

Open (2)

@tarekgh tarekgh added the breaking-change Issue or PR that represents a breaking API or functional change over a previous release. label Oct 7, 2026
@dotnet-policy-service dotnet-policy-service Bot added the needs-breaking-change-doc-created Breaking changes need an issue opened with https://github.com/dotnet/docs/issues/new?template=dotnet label Oct 7, 2026
@dotnet-policy-service

dotnet-policy-service Bot commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Added needs-breaking-change-doc-created label because this PR has the breaking-change label.

When you commit this breaking change:

  1. Create and link to this PR and the issue a matching issue in the dotnet/docs repo using the breaking change documentation template, then remove this needs-breaking-change-doc-created label.
  2. Ask a committer to mail the .NET Breaking Change Notification DL.

Tagging @dotnet/compat for awareness of the breaking change.

@tarekgh tarekgh 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.

@tarekgh
tarekgh merged commit 496f6f8 into dotnet:main Oct 7, 2026
101 checks passed
@tarekgh
tarekgh deployed to copilot-pat-pool October 7, 2026 16:42 — with GitHub Actions Active
@tarekgh
tarekgh deployed to copilot-pat-pool October 7, 2026 16:42 — with GitHub Actions Active
@tarekgh
tarekgh deployed to copilot-pat-pool October 7, 2026 16:43 — with GitHub Actions Active
@martincostello
martincostello deleted the gh-135007 branch October 7, 2026 16:44
@tarekgh
tarekgh deployed to copilot-pat-pool October 7, 2026 16:46 — with GitHub Actions Active
@tarekgh
tarekgh deployed to copilot-pat-pool October 7, 2026 16:48 — with GitHub Actions Active
@tarekgh
tarekgh deployed to copilot-pat-pool October 7, 2026 16:48 — with GitHub Actions Active
@github-actions

github-actions Bot commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

Breaking Change Documentation

A breaking change draft has been prepared for this PR.

👉 Click here to create the issue in dotnet/docs

After creating the issue, please email a link to it to the
.NET Breaking Change Notifications alias (dotnetbcn@microsoft.com).

Note

This documentation was generated with AI assistance from Copilot.

Generated by Breaking Change Documentation · gpt56 · 44 AIC · ⊞ 27.7K · ◷

@tarekgh

tarekgh commented Oct 7, 2026

Copy link
Copy Markdown
Member

Breaking change issue dotnet/docs#56340 is filed.

@tarekgh tarekgh removed the needs-breaking-change-doc-created Breaking changes need an issue opened with https://github.com/dotnet/docs/issues/new?template=dotnet label Oct 7, 2026
tarekgh added a commit that referenced this pull request Oct 14, 2026
## Summary

Fix the breaking-change documentation workflow completing successfully
without publishing its generated comment.

This occurred after #135277 merged: [workflow run
37654155119](https://github.com/dotnet/runtime/actions/runs/37654155119)
generated draft artifacts but published no comment. Required shell
commands were denied, and the incomplete outcome did not fail the
workflow.

## Changes

- Configure one default shallow checkout so the required helper scripts
are available, retaining the trusted base context for
`pull_request_target` without checking out the same repository twice.
- Allow the required `jq` and `mkdir` commands without granting
unrestricted shell access.
- Require the existing version and comment-building helpers, and submit
the complete comment through a single-command safe-output invocation.
- Expose the effective dry-run mode in the rendered prompt, with a
`false` fallback for automatic PR events.
- Validate the outcome, PR destination, complete draft pair, and raw
comment body against `pr-comment.md`. Preserve valid early no-ops and
dry runs, while rejecting incomplete generation and publication
failures.
- Prevent publication unless the generation job succeeds.
- Regenerate the lock file with gh-aw v0.86.2.

## Validation

- Strict compilation and schema validation passed for the submitted
workflow:

  ```powershell
gh aw compile breaking-change-doc --schedule-seed dotnet/runtime
--no-check-update --strict --validate --no-emit
  ```

- All 58 session-local regression checks passed, covering the original
failed-run output, numeric and string PR identifiers, raw comment-body
binding, dry runs, no-op handling, failed-generation publication gating,
and single trusted checkout behavior. Seven cases use the exact pinned
workflow renderer to verify the effective mode for manual and automatic
events. The test snapshot matches both workflow files. These checks are
not included in this PR or wired into repository CI.
- Targeted Markdownlint passed for
`.github/workflows/breaking-change-doc.md`.
- `git diff --check` passed.

## Live validation

An [initial dry
run](https://github.com/dotnet/runtime/actions/runs/37688137969) exposed
that the selected dry-run mode was not supplied to the rendered prompt.
The validator rejected a comment intent and the publication gate blocked
posting. This led to the explicit mode-propagation correction.

A [corrected dry
run](https://github.com/dotnet/runtime/actions/runs/37690925971) passed
on commit `73ea0d33b02` against #135277 with
`suppress_output=true`. The prompt explicitly contained `true`, both
complete documentation artifacts were generated, and the outcome was
exactly one `noop` with no publication intent. All jobs passed and the
source PR's four comment IDs remained unchanged.

Commit `4db4d8311d9` addresses the checkout review comment by using the
default checkout configuration. The generated agent job now has one
repository checkout with `persist-credentials: false` and `fetch-depth:
1`. The review thread is resolved.

A [final-commit dry
run](https://github.com/dotnet/runtime/actions/runs/37694130479) passed
against #135277 with `suppress_output=true`. The single
checkout succeeded, both complete draft artifacts were generated, and
the raw and ingested outcomes were exactly one `noop`. All seven jobs
passed, and the source PR's four comment IDs remained unchanged.

A [publication-enabled
run](https://github.com/dotnet/runtime/actions/runs/37695040898) passed
on the same final commit with `suppress_output=false`. All seven jobs,
outcome validation, and the publication gate passed. Exactly one raw
`add_comment` intent targeted #135277 and matched the
complete helper-generated comment before sanitization.

The workflow actually [posted the documentation
comment](#135277 (comment))
as `github-actions[bot]`. The source PR's comment count increased from
four to five. The published comment was checked to contain the ingested
safe-output body and the complete issue draft in its prefilled issue
link. No dotnet/docs issue was created.

## Scope and limitations

This PR remains a draft. Manual dry-run generation and actual comment
publication are verified on the final commit; the automatic merge/label
trigger was not exercised by these manual dispatches.

The version helper is invoked, but the sandbox's unauthenticated `gh`
dependency requires its documented metadata fallback. An independent
authenticated local execution confirmed `.NET 12 Preview 1` with
`Tentative=false` and no backports, matching the generated
documentation. This PR does not grant additional GitHub credentials.

PR CI passed Markdownlint, but it does not exercise this workflow's
generation/publication path or the session-local regression checks. The
main runtime build/test pipelines exclude these `.github`-only changes.

---------

Co-authored-by: Tarek Mahmoud Sayed <tarekms@ntdev.microsoft.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>

This branch was successfully deployed

1 active deployment
copilot-pat-pool — abc422c1 Deployed Oct 7, 2026 by tarekgh via conclusion #9803
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Diagnostics.Activity breaking-change Issue or PR that represents a breaking API or functional change over a previous release. community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ActivitySource copies creation tags and links into activities sampled as PropagationData

3 participants