You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/development/Configuration/AddingConfigurationKeys.md
+43-23Lines changed: 43 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,8 +22,10 @@ This guide explains how to add new configuration keys to the .NET Tracer. Config
22
22
23
23
Configuration keys in the .NET Tracer are defined in two source files:
24
24
25
-
-**`tracer/src/Datadog.Trace/Configuration/supported-configurations.json`** - Defines the configuration keys, their environment variable names, and fallback chains
26
-
-**`tracer/src/Datadog.Trace/Configuration/supported-configurations-docs.yaml`** - Contains XML documentation for each key
25
+
-**`tracer/src/Datadog.Trace/Configuration/supported-configurations.json`** - Defines the configuration keys, their
26
+
environment variable names, and optional fallbacks.
27
+
-**`tracer/src/Datadog.Trace/Configuration/supported-configurations-docs.yaml`** - Contains XML documentation for
28
+
each key. We're using yaml here as it makes it easier for some of the long documentation summaries and formatting.
27
29
28
30
Two source generators read these files at build time:
29
31
@@ -38,7 +40,10 @@ Two source generators read these files at build time:
38
40
39
41
### 1. Add the Configuration Key Definition
40
42
41
-
Add your new configuration key to `tracer/src/Datadog.Trace/Configuration/supported-configurations.json`.
43
+
Add your new configuration key to `tracer/src/Datadog.Trace/Configuration/supported-configurations.json`, specifying
44
+
an arbitrary version string (e.g. `"A"`, as shown below). and specifying the product if required. Any product name
45
+
is allowed, but try to reuse the existing ones (see [Common products](#common-products)) if it makes sense, as they will create another partial class, ie
46
+
ConfigurationKeys.ProductName.cs. Without a product name, the keys will go in the main class, ConfigurationKeys.cs.
- **Do NOT include `<summary>` tags** - the source generator automatically wraps your documentation in `<summary>` tags
78
83
- You can include `<seealso>` and `<see>` tags directly in the content - the source generator will extract `<seealso>` tags and place them outside the `<summary>` section as needed
79
84
80
-
### 3. (Optional) Add Fallback Keys (Aliases)
85
+
### 3. (Optional) Add Aliases
81
86
82
-
Configuration keys can have **fallback keys** (also called aliases) that are checked in order of appearance when the primary key is not found. Add them to the `fallbacks` section in `supported-configurations.json`:
87
+
Configuration keys can have **aliases** that are checked in order of appearance when the primary key is not found. Add them to the `aliases` section in `supported-configurations.json`:
83
88
84
89
```json
85
90
{
86
-
"fallbacks": {
91
+
"aliases": {
87
92
"OTEL_EXPORTER_OTLP_LOGS_TIMEOUT": [
88
93
"OTEL_EXPORTER_OTLP_TIMEOUT"
89
94
]
@@ -93,9 +98,11 @@ Configuration keys can have **fallback keys** (also called aliases) that are che
93
98
94
99
**How it works:**
95
100
1. The configuration system first looks for `OTEL_EXPORTER_OTLP_LOGS_TIMEOUT`
96
-
2. If not found, it checks `OTEL_EXPORTER_OTLP_TIMEOUT` (the fallback)
101
+
2. If not found, it automatically checks `OTEL_EXPORTER_OTLP_TIMEOUT` (the alias)
97
102
3. If still not found, it uses the default value
98
103
104
+
The `ConfigKeyAliasesSwitcherGenerator` source generator automatically generates the alias resolution logic from this section. No additional code is needed - just use the primary configuration key constant and the aliases are handled transparently.
105
+
99
106
**Use cases:**
100
107
- **Specific → General fallback:** A specific key (e.g., logs timeout) falls back to a general key (e.g., overall timeout)
101
108
- **Backward compatibility:** Renamed keys can fall back to their old names to maintain compatibility
@@ -119,7 +126,7 @@ If you need to explicitly control the constant name (e.g., for backward compatib
119
126
120
127
### 5. Build to Generate Code
121
128
122
-
Build the `Datadog.Trace` project to run the source generator:
129
+
Build the `Datadog.Trace` project to run the source generator, either using Nuke or by building the project directly from the command line or your IDE:
123
130
124
131
```bash
125
132
# From repository root
@@ -147,6 +154,18 @@ var timeout = source.GetInt32(ConfigurationKeys.OpenTelemetry.ExporterOtlpLogsTi
147
154
148
155
**Note:** The generated constants are in the `Datadog.Trace.Configuration` namespace.
149
156
157
+
#### Syntax Analyzers
158
+
159
+
The codebase includes Roslyn analyzers that enforce the use of configuration keys from the `ConfigurationKeys` classes:
160
+
161
+
- **`ConfigurationBuilderWithKeysAnalyzer`** - Enforces that `ConfigurationBuilder.WithKeys()` method calls only accept string constants from `ConfigurationKeys` or `PlatformKeys` classes, not hardcoded strings or variables.
162
+
163
+
**Diagnostic rules:**
164
+
- **DD0007**: Triggers when hardcoded string literals are used instead of configuration key constants
165
+
- **DD0008**: Triggers when variables or expressions are used instead of configuration key constants
166
+
167
+
These analyzers help prevent typos and ensure consistency across the codebase by enforcing compile-time validation of configuration keys.
168
+
150
169
### 7. Add to Telemetry Normalization Rules
151
170
152
171
Configuration keys are reported in telemetry with normalized names. Add your key to the normalization rules:
@@ -190,7 +209,8 @@ Use the `product` field to organize related keys into nested classes:
Comment("/// Generated configuration key matcher that handles main keys and aliases."),
210
-
Comment("/// This file is auto-generated from supported-configurations.json and supported-configurations-docs.yaml. Do not edit this file directly. The source generator will regenerate it on build."),
sb.AppendLine("/// Generated configuration key matcher that handles main keys and aliases.");
210
+
sb.AppendLine("/// Do not edit this file directly as it is auto-generated from supported-configurations.json and supported-configurations-docs.yaml.");
211
+
sb.AppendLine("/// For more info, see docs/development/Configuration/AddingConfigurationKeys.md");
212
+
sb.AppendLine("/// </summary>");
213
+
214
+
// Class declaration
215
+
sb.AppendLine("internal static partial class ConfigKeyAliasesSwitcher");
216
+
sb.AppendLine("{");
217
+
218
+
// Method XML documentation
219
+
sb.AppendLine(" /// <summary>");
220
+
sb.AppendLine(" /// Gets all aliases for the given configuration key.");
0 commit comments