Skip to content

add tracing configuration#129380

Open
rosebyte wants to merge 3 commits into
dotnet:mainfrom
rosebyte:trcing-configuration
Open

add tracing configuration#129380
rosebyte wants to merge 3 commits into
dotnet:mainfrom
rosebyte:trcing-configuration

Conversation

@rosebyte

@rosebyte rosebyte commented Jun 14, 2026

Copy link
Copy Markdown
Member

Summary

Implements tracing configuration support with a builder-style API, aligned with the approved design in #100317.

Key changes in this PR:

  • Adds AddTracing and tracing builder APIs in Microsoft.Extensions.Diagnostics.
  • Adds tracing rule configuration (EnableTracing / DisableTracing) with source, operation, listener, and scope matching.
  • Adds configuration-based tracing enable/disable support.
  • Introduces ActivitySourceFactory and related ActivitySource / ActivityListener enhancements needed for factory-driven and refreshable listener behaviour.
  • Adds tests covering rule evaluation, configuration loading, listener refresh, and factory caching/disposal scenarios.

Issue

Fixes #100317

API Approval

This implementation follows the API proposal approved in #100317 (api-approved) except two details we're signing off currently, nonetheless, it shouldn't be a blocker for PR review.

Testing

Added/updated unit tests in:

  • System.Diagnostics.DiagnosticSource
  • Microsoft.Extensions.Diagnostics

Note

This PR description was generated with GitHub Copilot assistance.

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

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.

Pull request overview

This PR expands the tracing stack across System.Diagnostics.DiagnosticSource and Microsoft.Extensions.Diagnostics to support configurable tracing with factory-created ActivitySource instances, including listener refresh semantics and additional concurrency hardening around dispose races.

Changes:

  • Adds new public tracing APIs in System.Diagnostics (ActivitySourceFactory, ActivitySourceOptions.Scope, ActivitySource.Scope, ActivityListener.RefreshSources, and unseals ActivitySource to support derived factory-owned instances).
  • Implements Microsoft.Extensions.Diagnostics tracing services/configuration (AddTracing, rule parsing, listener registration, and a caching DefaultActivitySourceFactory).
  • Adds extensive tests for listener refresh/dispose races and rule/configuration behavior.

Reviewed changes

Copilot reviewed 31 out of 31 changed files in this pull request and generated 9 comments.

Show a summary per file
File Description
src/libraries/System.Diagnostics.DiagnosticSource/tests/ActivitySourceTests.cs Adds new RemoteExecutor tests for RefreshSources, dispose races, predicate-throw behavior, and factory creation semantics.
src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/ActivitySourceOptions.cs Adds Scope option for attaching an opaque scope to ActivitySource.
src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/ActivitySourceFactory.cs Introduces ActivitySourceFactory base class that validates/binds scope and creates sources from options.
src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/ActivitySource.cs Unseals ActivitySource, adds Scope, adds dispose sentinel handling, and implements ResetSourceFilters for refresh semantics + predicate exception aggregation.
src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/ActivityListener.cs Adds RefreshSources() and a disposal flag to make refresh/dispose races no-op / self-cleaning.
src/libraries/System.Diagnostics.DiagnosticSource/src/System.Diagnostics.DiagnosticSource.csproj Includes the new ActivitySourceFactory.cs in the build.
src/libraries/System.Diagnostics.DiagnosticSource/src/Resources/Strings.resx Adds resource strings for invalid scope and refresh predicate failures.
src/libraries/System.Diagnostics.DiagnosticSource/ref/System.Diagnostics.DiagnosticSourceActivity.cs Updates ref surface for the new/changed public APIs (ActivitySource unsealed, Scope, Dispose(bool), ActivitySourceFactory, ActivityListener.RefreshSources).
src/libraries/Microsoft.Extensions.Diagnostics/tests/TracingConfigureOptionsTests.cs Adds unit tests for configuration-to-rule parsing.
src/libraries/Microsoft.Extensions.Diagnostics/tests/Microsoft.Extensions.Diagnostics.Tests.csproj Compiles tracing configuration implementation into the test project for direct testing.
src/libraries/Microsoft.Extensions.Diagnostics/tests/DefaultActivitySourceFactoryTests.cs Adds tests for caching, rule matching, config reload behavior, and factory disposal behavior.
src/libraries/Microsoft.Extensions.Diagnostics/src/Tracing/TracingServiceExtensions.cs Adds IServiceCollection.AddTracing() registrations (factory, listener config factory, startup activator).
src/libraries/Microsoft.Extensions.Diagnostics/src/Tracing/DefaultActivitySourceFactory.cs Implements caching ActivitySourceFactory + listener registrations that refresh based on TracingOptions changes.
src/libraries/Microsoft.Extensions.Diagnostics/src/Tracing/Configuration/TracingConfigureOptions.cs Parses IConfiguration into TracingRule instances for tracing enablement.
src/libraries/Microsoft.Extensions.Diagnostics/src/Tracing/Configuration/TracingConfiguration.cs Wraps an IConfiguration instance for later per-listener merging.
src/libraries/Microsoft.Extensions.Diagnostics/src/Tracing/Configuration/TracingBuilderConfigurationExtensions.cs Adds ITracingBuilder.AddConfiguration(...) to wire options + change tokens.
src/libraries/Microsoft.Extensions.Diagnostics/src/Tracing/Configuration/DefaultActivityListenerConfigurationFactory.cs Merges multiple configuration sources into a per-listener view.
src/libraries/Microsoft.Extensions.Diagnostics/src/Tracing/Configuration/ActivityListenerConfigurationFactory.cs New public abstraction for resolving per-listener configuration views.
src/libraries/Microsoft.Extensions.Diagnostics/ref/Microsoft.Extensions.Diagnostics.cs Adds ref entries for TracingServiceExtensions + tracing configuration factory surface.
src/libraries/Microsoft.Extensions.Diagnostics/README.md Documents the new tracing extension entry points.
src/libraries/Microsoft.Extensions.Diagnostics.Abstractions/tests/TracingBuilderExtensionsRulesTests.cs Adds tests validating the rule-registration extension methods.
src/libraries/Microsoft.Extensions.Diagnostics.Abstractions/src/Tracing/TracingRule.cs Introduces a public rule type with wildcard validation and scope validation.
src/libraries/Microsoft.Extensions.Diagnostics.Abstractions/src/Tracing/TracingOptions.cs Introduces tracing options holding the rule list.
src/libraries/Microsoft.Extensions.Diagnostics.Abstractions/src/Tracing/TracingBuilderExtensions.Rules.cs Adds enable/disable rule extension methods for builder/options.
src/libraries/Microsoft.Extensions.Diagnostics.Abstractions/src/Tracing/TracingBuilderExtensions.Listeners.cs Adds listener registration/clearing APIs for ITracingBuilder.
src/libraries/Microsoft.Extensions.Diagnostics.Abstractions/src/Tracing/ITracingBuilder.cs Adds the tracing builder abstraction (DI surface).
src/libraries/Microsoft.Extensions.Diagnostics.Abstractions/src/Tracing/ActivitySourceScopes.cs Adds the Global/Local scope enum for rule matching.
src/libraries/Microsoft.Extensions.Diagnostics.Abstractions/src/Tracing/ActivityListenerBuilder.cs Adds the listener builder type used by AddListener callbacks.
src/libraries/Microsoft.Extensions.Diagnostics.Abstractions/ref/Microsoft.Extensions.Diagnostics.Abstractions.csproj Adds needed ref project references for the new tracing types.
src/libraries/Microsoft.Extensions.Diagnostics.Abstractions/ref/Microsoft.Extensions.Diagnostics.Abstractions.cs Adds ref entries for the new tracing abstractions and extensions.
src/libraries/Common/src/System/ExceptionPolyfills.cs Adds a downlevel polyfill for ArgumentException.ThrowIfNullOrEmpty.

Comment thread src/libraries/System.Diagnostics.DiagnosticSource/tests/ActivitySourceTests.cs Outdated
Comment thread src/libraries/System.Diagnostics.DiagnosticSource/tests/ActivitySourceTests.cs Outdated
Comment thread src/libraries/System.Diagnostics.DiagnosticSource/tests/ActivitySourceTests.cs Outdated
Comment thread src/libraries/System.Diagnostics.DiagnosticSource/tests/ActivitySourceTests.cs Outdated
Comment thread src/libraries/System.Diagnostics.DiagnosticSource/tests/ActivitySourceTests.cs Outdated
Comment thread src/libraries/System.Diagnostics.DiagnosticSource/tests/ActivitySourceTests.cs Outdated
@rosebyte rosebyte force-pushed the trcing-configuration branch from aecf70c to ff45fb8 Compare June 14, 2026 05:57
@rosebyte

Copy link
Copy Markdown
Member Author

@copilot , there are comments demanding timeout for waiting, add the timeouts as suggested.

Copilot AI review requested due to automatic review settings June 14, 2026 14:46

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.

Pull request overview

Copilot reviewed 31 out of 31 changed files in this pull request and generated 5 comments.

/// </summary>
/// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
/// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
public static IServiceCollection AddTracing(this IServiceCollection services)
/// <param name="version">The version of the <see cref="ActivitySource"/>.</param>
/// <param name="tags">The tags to associate with the <see cref="ActivitySource"/>.</param>
/// <returns>An <see cref="ActivitySource"/> with the specified <paramref name="name"/>, <paramref name="version"/>, and <paramref name="tags"/>.</returns>
public ActivitySource Create(string name, string? version = "", IEnumerable<KeyValuePair<string, object?>>? tags = null)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tracing configuration similar to MetricsBuilder

2 participants