[Config Registry] 5/7 Replace manual ConfigurationKeys by generated ones in the whole solution by anna-git · Pull Request #7931 · DataDog/dd-trace-dotnet
This was referenced
Dec 9, 2025
marked this pull request as ready for review
This was referenced
Dec 9, 2025anna-git added a commit that referenced this pull request
Dec 9, 2025…any string key (#7689) ## Context Part of **Configuration Inversion (Step 4)** - Stack progress: 1. [#7548](#7548) - Add GitLab step and JSON configuration file 2. [#7688](#7688) - Cleanup configuration / platform keys + analyzers 3. [#7698](#7698) - Source generator for ConfigurationKeys 4. **→ [#7689](#7689) - Aliases handling and analyzers (this PR)** 5. [#7931](#7931) - Replace manual ConfigurationKeys with generated version 6. #[7932](#7932 Forbid use of System.Environment methods and adapt everywhere ## Summary Adds source generator for configuration key aliases, integrates alias resolution into `ConfigurationBuilder`, and adds Roslyn analyzers to enforce proper configuration key usage. ## Changes **Alias Source Generator:** - `ConfigKeyAliasesSwitcherGenerator` reads aliases from `supported-configurations.json` - Auto-generates switch statements to resolve primary keys from aliases - Generated for all target frameworks (net461, netstandard2.0, netcoreapp3.1, net6.0) **ConfigurationBuilder Improvements:** - Integrated alias resolution directly into `ConfigurationBuilder.WithKeys()` - Removed manual fallback overloads throughout codebase - Added `GetKeyWithAlias()` method to `IConfigurationSource` interface **IntegrationSettings Special Handling:** - Updated to use pattern-based key construction (e.g., `DD_TRACE_{INTEGRATION}_ENABLED`) - Simplified configuration reading by leveraging alias system **Roslyn Analyzers:** - **DD0007**: `PlatformKeysAnalyzer` - Enforces use of `PlatformKeys` for external platform environment variables - **DD0008**: `ConfigurationBuilderWithKeysAnalyzer` - Enforces use of [ConfigurationKeys](cci:2://file:///Users/anna.yafi/go/src/github.com/DataDog/dd-trace-dotnet3/tracer/src/Datadog.Trace/Configuration/ConfigurationKeys.Logging.cs:9:0-58:1)/`PlatformKeys` constants in `ConfigurationBuilder.WithKeys()` calls **Special Cases:** - Fixed `DatadogLoggingFactory` to handle deprecated `DD_TRACE_LOG_PATH` with proper analyzer suppression - Updated configuration tests to work with alias resolution ## Motivation Completes configuration inversion by: - Auto-generating alias resolution from `supported-configurations.json` - Eliminating manual fallback chains - Enforcing compile-time validation of configuration keys - Preventing hardcoded strings and typos ## Test Coverage - Added `ConfigKeyAliasesSwitcherGeneratorTests` with comprehensive test coverage - Added `PlatformKeysAnalyzerTests` with 146 lines of tests - Added `ConfigurationBuilderWithKeysAnalyzerTests` with 538 lines of tests - Updated existing configuration tests for alias support - All tests pass with new alias system ## Related Work Builds on #7548 (configuration registry), #7688 (PlatformKeys separation), and #7698 (source generator for ConfigurationKeys).
Base automatically changed from anna/config-inversion-configuration-analyzers-4 to master
December 9, 2025 22:23
anna-git
changed the title
[Config Registry] 5/6 Replace manual ConfigurationKeys by generated ones in the whole solution
[Config Registry] 5/7 Replace manual ConfigurationKeys by generated ones in the whole solution
anna-git
deleted the
anna/config-inversion-use-generate-config-keys-5
branch
anna-git added a commit that referenced this pull request
Dec 10, 2025…ersion (#7944) ## Summary of changes Due to this [Rider](https://youtrack.jetbrains.com/projects/RIDER/issues/RIDER-132634/Source-Generated-files-missing-when-using-System.Text.Json) issue , the IDE doesn't index source generated files using the last version of the package properly which considerably degrades the dev experience. And following the merge [of this PR](#7931), we now have the issue: <img width="2148" height="980" alt="image" src="https://github.com/user-attachments/assets/5c113a18-9126-4a57-9cda-eb1168a41876" /> It still builds but Rider doesn't see the generated sources and generates errors and doesn't autocompletes or index anything. ## Reason for change Downgrade the package version to fix the dev experience. ## Implementation details ## Test coverage ## Other details <!-- Fixes #{issue} --> <!-- ⚠️ Note: Where possible, please obtain 2 approvals prior to merging. Unless CODEOWNERS specifies otherwise, for external teams it is typically best to have one review from a team member, and one review from apm-dotnet. Trivial changes do not require 2 reviews. MergeQueue is NOT enabled in this repository. If you have write access to the repo, the PR has 1-2 approvals (see above), and all of the required checks have passed, you can use the Squash and Merge button to merge the PR. If you don't have write access, or you need help, reach out in the #apm-dotnet channel in Slack. -->
anna-git added a commit that referenced this pull request
Jan 20, 2026…apt everywhere (#7932) ## Context Part of **Configuration Inversion (Step 6)** - Stack progress: 1. [#7548](#7548) - Add GitLab step and JSON configuration file 2. [#7688](#7688) - Cleanup configuration / platform keys + source generator 3. [#7698](#7698) - Aliases handling via source generator 4. [#7689](#7689) - Analyzers for platform and ConfigurationBuilder 5. [#7931](#7931) - Replace manual ConfigurationKeys by generated ones in the whole solution 6. **→ [#7932](#7932) - Forbid use of System.Environment methods and adapt everywhere (this PR)** 7. [#7937](#7937) - Integration names to generated keys I'll update the PR summary to mention the YAML documentation file: ## Summary of changes Banned direct `System.Environment.GetEnvironmentVariable()` usage and migrated all environment variable access to use `EnvironmentHelpers` with strongly-typed [ConfigurationKeys](cci:2://file:///Users/anna.yafi/go/src/github.com/DataDog/dd-trace-dotnet3/tracer/src/Datadog.Trace/Generated/net461/Datadog.Trace.SourceGenerators/ConfigurationKeysGenerator/ConfigurationKeys.g.cs:15:0-737:1) and `PlatformKeys` constants. ### Key changes: - **Banned `System.Environment.GetEnvironmentVariable()`** via `BannedApiAnalyzers` - **Added `EnvironmentGetEnvironmentVariableAnalyzer` (DD0009)** to enforce [ConfigurationKeys](cci:2://file:///Users/anna.yafi/go/src/github.com/DataDog/dd-trace-dotnet3/tracer/src/Datadog.Trace/Generated/net461/Datadog.Trace.SourceGenerators/ConfigurationKeysGenerator/ConfigurationKeys.g.cs:15:0-737:1)/`PlatformKeys` usage only - **Nested `PlatformKeys` by category** (Ci, Aws, AzureAppService, ServiceFabric, DotNet) - **Migrated ~50+ files** across CI Visibility, AWS Lambda, AppSec, Telemetry, Profiler, and Agent components - **Added missing keys** to [supported-configurations.json](cci:7://file:///Users/anna.yafi/go/src/github.com/DataDog/dd-trace-dotnet3/tracer/src/Datadog.Trace/Configuration/supported-configurations.json:0:0-0:0) and their documentation to [supported-configurations-docs.yaml](cci:7://file:///Users/anna.yafi/go/src/github.com/DataDog/dd-trace-dotnet3/tracer/src/Datadog.Trace/Configuration/supported-configurations-docs.yaml:0:0-0:0) ## Reason for change Direct `System.Environment.GetEnvironmentVariable()` calls with string literals are error-prone. Centralizing through `EnvironmentHelpers` with strongly-typed constants provides compile-time validation, discoverability, and refactoring safety. ## Implementation details 1. **Banned API enforcement** - Added `BannedSymbols.txt` and configured `.editorconfig` to treat RS0030 as error (vendored code excluded) 2. **Custom analyzer** - DD0009 validates all `EnvironmentHelpers` calls accept only [ConfigurationKeys](cci:2://file:///Users/anna.yafi/go/src/github.com/DataDog/dd-trace-dotnet3/tracer/src/Datadog.Trace/Generated/net461/Datadog.Trace.SourceGenerators/ConfigurationKeysGenerator/ConfigurationKeys.g.cs:15:0-737:1)/`PlatformKeys` constants, rejecting hardcoded strings 3. **PlatformKeys organization** - Nested by category for better discoverability 4. **EnvironmentHelpers refactoring** - Added overloads for nested class constants, maintained backward compatibility 5. **Configuration documentation** - Added missing configuration keys to [supported-configurations.json](cci:7://file:///Users/anna.yafi/go/src/github.com/DataDog/dd-trace-dotnet3/tracer/src/Datadog.Trace/Configuration/supported-configurations.json:0:0-0:0) and comprehensive XML documentation to [supported-configurations-docs.yaml](cci:7://file:///Users/anna.yafi/go/src/github.com/DataDog/dd-trace-dotnet3/tracer/src/Datadog.Trace/Configuration/supported-configurations-docs.yaml:0:0-0:0), which the source generator uses to generate XML doc comments in [ConfigurationKeys](cci:2://file:///Users/anna.yafi/go/src/github.com/DataDog/dd-trace-dotnet3/tracer/src/Datadog.Trace/Generated/net461/Datadog.Trace.SourceGenerators/ConfigurationKeysGenerator/ConfigurationKeys.g.cs:15:0-737:1) classes ## Test coverage - ✅ Comprehensive analyzer tests covering valid/invalid scenarios - ✅ All existing unit tests pass - ✅ Banned API analyzer correctly flags direct `System.Environment` usage ## Other details +2,600/-940 lines. No breaking changes, negligible performance impact.
anna-git added a commit that referenced this pull request
Jan 27, 2026## Context Part of **Configuration Inversion (Step 6)** - Stack progress: 1. [#7548](#7548) - Add GitLab step and JSON configuration file 2. [#7688](#7688) - Cleanup configuration / platform keys + source generator 3. [#7698](#7698) - Aliases handling via source generator 4. [#7689](#7689) - Analyzers for platform and ConfigurationBuilder 5. [#7931](#7931) - Replace manual ConfigurationKeys by generated ones in the whole solution 6. [#7932](#7932) - Forbid use of System.Environment methods and adapt everywhere 7. **→ [#7937](#7937) - Integration names to generated keys (this PR)** ## Summary of changes Extends the `EnumExtensionsGenerator` to generate [IntegrationNameToKeys](cci:2://file:///Users/anna.yafi/go/src/github.com/DataDog/dd-trace-dotnet3/tracer/src/Datadog.Trace/Generated/net6.0/Datadog.Trace.SourceGenerators/EnumExtensionsGenerator/IntegrationNameToKeys.g.cs:14:4-392:5) class for the `IntegrationId` enum, providing centralized configuration key mapping for integrations including enabled, analytics enabled, and analytics sample rate keys. Also fixes AdoNet instrumentation compilation error. ## Reason for change - Eliminates code duplication in configuration key generation across the codebase - Provides compile-time safety for integration configuration key access - Centralizes key pattern definitions with proper documentation - Ensures consistency across all integration key formats (canonical, mixed case, short aliases) ## Implementation details - **Source Generator**: Extended `EnumExtensionsGenerator` with [GenerateIntegrationNameToKeys](cci:1://file:///Users/anna.yafi/go/src/github.com/DataDog/dd-trace-dotnet3/tracer/src/Datadog.Trace.SourceGenerators/EnumExtensions/Sources.cs:82:4-178:5) method that generates: - `AllIntegrationEnabledKeys` array containing all enabled configuration keys - [GetIntegrationEnabledKeys(string)](cci:1://file:///Users/anna.yafi/go/src/github.com/DataDog/dd-trace-dotnet3/tracer/src/Datadog.Trace/Generated/net6.0/Datadog.Trace.SourceGenerators/EnumExtensionsGenerator/IntegrationNameToKeys.g.cs:102:8-197:9) - returns enabled keys for an integration - [GetIntegrationAnalyticsEnabledKeys(string)](cci:1://file:///Users/anna.yafi/go/src/github.com/DataDog/dd-trace-dotnet3/tracer/src/Datadog.Trace/Generated/net6.0/Datadog.Trace.SourceGenerators/EnumExtensionsGenerator/IntegrationNameToKeys.g.cs:198:8-294:9) - returns analytics enabled keys (marked obsolete) - [GetIntegrationAnalyticsSampleRateKeys(string)](cci:1://file:///Users/anna.yafi/go/src/github.com/DataDog/dd-trace-dotnet3/tracer/src/Datadog.Trace/Generated/net6.0/Datadog.Trace.SourceGenerators/EnumExtensionsGenerator/IntegrationNameToKeys.g.cs:295:8-391:9) - returns analytics sample rate keys (marked obsolete) - Key pattern constants (`IntegrationEnabledKeyPattern`, `AnalyticsEnabledKeyPattern`, `AnalyticsSampleRateKeyPattern`) - **Configuration**: Updated [ConfigurationBuilder.WithIntegrationAnalyticsKey](cci:1://file:///Users/anna.yafi/go/src/github.com/DataDog/dd-trace-dotnet3/tracer/src/Datadog.Trace/Configuration/ConfigurationSources/Telemetry/ConfigurationBuilder.cs:32:4-42:5) and [WithIntegrationAnalyticsSampleRateKey](cci:1://file:///Users/anna.yafi/go/src/github.com/DataDog/dd-trace-dotnet3/tracer/src/Datadog.Trace/Configuration/ConfigurationSources/Telemetry/ConfigurationBuilder.cs:44:4-54:5) to use generated methods ## Test coverage - Added [CanGenerateIntegrationNameToKeysForIntegrationId](cci:1://file:///Users/anna.yafi/go/src/github.com/DataDog/dd-trace-dotnet3/tracer/test/Datadog.Trace.SourceGenerators.Tests/EnumExtensionsGeneratorTests.cs:411:4-527:5) - verifies complete generated output with all three methods - Added [DoesNotGenerateIntegrationNameToKeysForNonIntegrationIdEnum](cci:1://file:///Users/anna.yafi/go/src/github.com/DataDog/dd-trace-dotnet3/tracer/test/Datadog.Trace.SourceGenerators.Tests/EnumExtensionsGeneratorTests.cs:529:4-552:5) - ensures generation only for `IntegrationId` enum - Added [IntegrationNameToKeysGeneratesCorrectKeyFormats](cci:1://file:///Users/anna.yafi/go/src/github.com/DataDog/dd-trace-dotnet3/tracer/test/Datadog.Trace.SourceGenerators.Tests/EnumExtensionsGeneratorTests.cs:554:4-670:5) - validates key formats for various integration names - All tests verify obsolete attributes, key patterns, and switch expression logic
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters