Skip to content

Commit de9334f

Browse files
committed
Add IntegrationNameToKey source generator
1 parent be89b22 commit de9334f

File tree

11 files changed

+3250
-76
lines changed

11 files changed

+3250
-76
lines changed

tracer/src/Datadog.Trace.SourceGenerators/EnumExtensions/EnumExtensionsGenerator.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// <copyright file="EnumExtensionsGenerator.cs" company="Datadog">
1+
// <copyright file="EnumExtensionsGenerator.cs" company="Datadog">
22
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License.
33
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
44
// </copyright>
@@ -57,6 +57,13 @@ private static void Execute(in EnumToGenerate enumToGenerate, SourceProductionCo
5757
StringBuilder sb = new StringBuilder();
5858
var result = Sources.GenerateExtensionClass(sb, in enumToGenerate);
5959
context.AddSource(enumToGenerate.ExtensionsName + "_EnumExtensions.g.cs", SourceText.From(result, Encoding.UTF8));
60+
61+
// If this is the IntegrationId enum, also generate IntegrationNameToKeys
62+
if (enumToGenerate.FullyQualifiedName == "Datadog.Trace.Configuration.IntegrationId")
63+
{
64+
var integrationKeysResult = Sources.GenerateIntegrationNameToKeys(sb, in enumToGenerate);
65+
context.AddSource("IntegrationNameToKeys.g.cs", SourceText.From(integrationKeysResult, Encoding.UTF8));
66+
}
6067
}
6168

6269
private static Result<(EnumToGenerate Enum, bool IsValid)> GetTypeToGenerate(

tracer/src/Datadog.Trace.SourceGenerators/EnumExtensions/Sources.cs

Lines changed: 119 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// <copyright file="Sources.cs" company="Datadog">
1+
// <copyright file="Sources.cs" company="Datadog">
22
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License.
33
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
44
// </copyright>
@@ -80,6 +80,124 @@ public static string[] GetNames()
8080
""";
8181
}
8282

83+
public static string GenerateIntegrationNameToKeys(StringBuilder sb, in EnumToGenerate enumToGenerate)
84+
{
85+
var arrayKeys = new StringBuilder();
86+
var switchCasesEnabled = new StringBuilder();
87+
var switchCasesAnalyticsEnabled = new StringBuilder();
88+
var switchCasesAnalyticsSampleRate = new StringBuilder();
89+
90+
// Single loop to build array keys and all switch cases
91+
foreach (var member in enumToGenerate.Names)
92+
{
93+
var name = member.Property;
94+
var upperName = name.ToUpperInvariant();
95+
96+
// Configuration key pattern for enabling or disabling an integration
97+
var upperKey = $"DD_TRACE_{upperName}_ENABLED";
98+
var mixedKey = $"DD_TRACE_{name}_ENABLED";
99+
var shortKey = $"DD_{name}_ENABLED";
100+
arrayKeys.AppendLine($" \"{upperKey}\", \"{mixedKey}\", \"{shortKey}\",");
101+
switchCasesEnabled.AppendLine($" \"{name}\" => new[] {{ \"{upperKey}\", \"{mixedKey}\", \"{shortKey}\" }},");
102+
103+
// Configuration key pattern for enabling or disabling Analytics in an integration
104+
var analyticsUpperKey = $"DD_TRACE_{upperName}_ANALYTICS_ENABLED";
105+
var analyticsMixedKey = $"DD_TRACE_{name}_ANALYTICS_ENABLED";
106+
var analyticsShortKey = $"DD_{name}_ANALYTICS_ENABLED";
107+
switchCasesAnalyticsEnabled.AppendLine($" \"{name}\" => new[] {{ \"{analyticsUpperKey}\", \"{analyticsMixedKey}\", \"{analyticsShortKey}\" }},");
108+
arrayKeys.AppendLine($" \"{analyticsUpperKey}\", \"{analyticsMixedKey}\", \"{analyticsShortKey}\",");
109+
110+
// Configuration key pattern for setting Analytics sampling rate in an integration
111+
var sampleRateUpperKey = $"DD_TRACE_{upperName}_ANALYTICS_SAMPLE_RATE";
112+
var sampleRateMixedKey = $"DD_TRACE_{name}_ANALYTICS_SAMPLE_RATE";
113+
var sampleRateShortKey = $"DD_{name}_ANALYTICS_SAMPLE_RATE";
114+
switchCasesAnalyticsSampleRate.AppendLine($" \"{name}\" => new[] {{ \"{sampleRateUpperKey}\", \"{sampleRateMixedKey}\", \"{sampleRateShortKey}\" }},");
115+
arrayKeys.AppendLine($" \"{sampleRateUpperKey}\", \"{sampleRateMixedKey}\", \"{sampleRateShortKey}\", ");
116+
}
117+
118+
sb.Clear();
119+
sb.AppendLine(Constants.FileHeader);
120+
sb.AppendLine("namespace Datadog.Trace.Configuration");
121+
sb.AppendLine("{");
122+
sb.AppendLine(" /// <summary>");
123+
sb.AppendLine(" /// Generated mapping of integration names to their configuration keys.");
124+
sb.AppendLine(" /// </summary>");
125+
sb.AppendLine(" internal static partial class IntegrationNameToKeys");
126+
sb.AppendLine(" {");
127+
sb.AppendLine(" private const string ObsoleteMessage = DeprecationMessages.AppAnalytics;");
128+
sb.AppendLine();
129+
sb.AppendLine(" /// <summary>");
130+
sb.AppendLine(" /// All integration enabled keys (canonical + aliases).");
131+
sb.AppendLine(" /// </summary>");
132+
sb.AppendLine(" public static readonly string[] AllIntegrationEnabledKeys = new[]");
133+
sb.AppendLine(" {");
134+
sb.Append(arrayKeys);
135+
sb.AppendLine(" };");
136+
sb.AppendLine(" /// <summary>");
137+
sb.AppendLine(" /// Gets the configuration keys for the specified integration name.");
138+
sb.AppendLine(" /// Returns keys in order: [canonical key, alias1, alias2]");
139+
sb.AppendLine(" /// </summary>");
140+
sb.AppendLine(" /// <param name=\"integrationName\">The integration name</param>");
141+
sb.AppendLine(" /// <returns>Array of configuration keys to check in order</returns>");
142+
sb.AppendLine(" public static string[] GetIntegrationEnabledKeys(string integrationName)");
143+
sb.AppendLine(" {");
144+
sb.AppendLine(" return integrationName switch");
145+
sb.AppendLine(" {");
146+
sb.Append(switchCasesEnabled);
147+
sb.AppendLine(" _ => new[]");
148+
sb.AppendLine(" {");
149+
sb.AppendLine(" string.Format(\"DD_TRACE_{0}_ENABLED\", integrationName.ToUpperInvariant()),");
150+
sb.AppendLine(" string.Format(\"DD_TRACE_{0}_ENABLED\", integrationName),");
151+
sb.AppendLine(" $\"DD_{integrationName}_ENABLED\"");
152+
sb.AppendLine(" }");
153+
sb.AppendLine(" };");
154+
sb.AppendLine(" }");
155+
sb.AppendLine(" /// <summary>");
156+
sb.AppendLine(" /// Gets the analytics enabled configuration keys for the specified integration name.");
157+
sb.AppendLine(" /// Returns keys in order: [canonical key, alias1, alias2]");
158+
sb.AppendLine(" /// </summary>");
159+
sb.AppendLine(" /// <param name=\"integrationName\">The integration name</param>");
160+
sb.AppendLine(" /// <returns>Array of configuration keys to check in order</returns>");
161+
sb.AppendLine(" [System.Obsolete(ObsoleteMessage)]");
162+
sb.AppendLine(" public static string[] GetIntegrationAnalyticsEnabledKeys(string integrationName)");
163+
sb.AppendLine(" {");
164+
sb.AppendLine(" return integrationName switch");
165+
sb.AppendLine(" {");
166+
sb.Append(switchCasesAnalyticsEnabled);
167+
sb.AppendLine(" _ => new[]");
168+
sb.AppendLine(" {");
169+
sb.AppendLine(" string.Format(\"DD_TRACE_{0}_ANALYTICS_ENABLED\", integrationName.ToUpperInvariant()),");
170+
sb.AppendLine(" string.Format(\"DD_TRACE_{0}_ANALYTICS_ENABLED\", integrationName),");
171+
sb.AppendLine(" $\"DD_{integrationName}_ANALYTICS_ENABLED\"");
172+
sb.AppendLine(" }");
173+
sb.AppendLine(" };");
174+
sb.AppendLine(" }");
175+
sb.AppendLine(" /// <summary>");
176+
sb.AppendLine(" /// Gets the analytics sample rate configuration keys for the specified integration name.");
177+
sb.AppendLine(" /// Returns keys in order: [canonical key, alias1, alias2]");
178+
sb.AppendLine(" /// </summary>");
179+
sb.AppendLine(" /// <param name=\"integrationName\">The integration name</param>");
180+
sb.AppendLine(" /// <returns>Array of configuration keys to check in order</returns>");
181+
sb.AppendLine(" [System.Obsolete(ObsoleteMessage)]");
182+
sb.AppendLine(" public static string[] GetIntegrationAnalyticsSampleRateKeys(string integrationName)");
183+
sb.AppendLine(" {");
184+
sb.AppendLine(" return integrationName switch");
185+
sb.AppendLine(" {");
186+
sb.Append(switchCasesAnalyticsSampleRate);
187+
sb.AppendLine(" _ => new[]");
188+
sb.AppendLine(" {");
189+
sb.AppendLine(" string.Format(\"DD_TRACE_{0}_ANALYTICS_SAMPLE_RATE\", integrationName.ToUpperInvariant()),");
190+
sb.AppendLine(" string.Format(\"DD_TRACE_{0}_ANALYTICS_SAMPLE_RATE\", integrationName),");
191+
sb.AppendLine(" $\"DD_{integrationName}_ANALYTICS_SAMPLE_RATE\"");
192+
sb.AppendLine(" }");
193+
sb.AppendLine(" };");
194+
sb.AppendLine(" }");
195+
sb.AppendLine(" }");
196+
sb.AppendLine("}");
197+
198+
return sb.ToString();
199+
}
200+
83201
private static string GetToStringFast(StringBuilder sb, in EnumToGenerate enumToGenerate)
84202
{
85203
sb.Clear();

tracer/src/Datadog.Trace/Configuration/ConfigurationSources/Telemetry/ConfigurationBuilder.cs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,29 @@ internal readonly struct ConfigurationBuilder(IConfigurationSource source, IConf
2828
$"DD_{integrationName}_ENABLED"
2929
]);
3030

31-
public HasKeys WithIntegrationAnalyticsKey(string integrationName) => new(
32-
_source,
33-
_telemetry,
31+
public HasKeys WithIntegrationAnalyticsKey(string integrationName)
32+
{
3433
#pragma warning disable 618 // App analytics is deprecated, but still used
35-
string.Format(IntegrationSettings.AnalyticsEnabledKey, integrationName.ToUpperInvariant()),
36-
[
37-
string.Format(IntegrationSettings.AnalyticsEnabledKey, integrationName),
34+
var integrationAnalyticsEnabledKeys = IntegrationNameToKeys.GetIntegrationAnalyticsEnabledKeys(integrationName);
3835
#pragma warning restore 618
39-
$"DD_{integrationName}_ANALYTICS_ENABLED"
40-
]);
36+
return new(
37+
_source,
38+
_telemetry,
39+
integrationAnalyticsEnabledKeys[0],
40+
integrationAnalyticsEnabledKeys.Skip(1).ToArray());
41+
}
4142

42-
public HasKeys WithIntegrationAnalyticsSampleRateKey(string integrationName) => new(
43-
_source,
44-
_telemetry,
43+
public HasKeys WithIntegrationAnalyticsSampleRateKey(string integrationName)
44+
{
4545
#pragma warning disable 618 // App analytics is deprecated, but still used
46-
string.Format(IntegrationSettings.AnalyticsSampleRateKey, integrationName.ToUpperInvariant()),
47-
[
48-
string.Format(IntegrationSettings.AnalyticsSampleRateKey, integrationName),
46+
var integrationAnalyticsSampleRateKeys = IntegrationNameToKeys.GetIntegrationAnalyticsSampleRateKeys(integrationName);
4947
#pragma warning restore 618
50-
$"DD_{integrationName}_ANALYTICS_SAMPLE_RATE"
51-
]);
48+
return new(
49+
_source,
50+
_telemetry,
51+
integrationAnalyticsSampleRateKeys[0],
52+
integrationAnalyticsSampleRateKeys.Skip(1).ToArray());
53+
}
5254

5355
internal readonly struct HasKeys(IConfigurationSource source, IConfigurationTelemetry telemetry, string key, string[]? providedAliases = null)
5456
{

0 commit comments

Comments
 (0)