Skip to content

Commit 402b518

Browse files
committed
Source generator for ConfigurationsKeys2 (temporary) to replace ConfigurationsKeys.cs later
1 parent 05622b1 commit 402b518

File tree

9 files changed

+1132
-8
lines changed

9 files changed

+1132
-8
lines changed

tracer/src/Datadog.Trace.SourceGenerators/Configuration/ConfigurationKeysGenerator.cs

Lines changed: 493 additions & 0 deletions
Large diffs are not rendered by default.

tracer/src/Datadog.Trace.SourceGenerators/Constants.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// <copyright file="Constants.cs" company="Datadog">
1+
// <copyright file="Constants.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>
@@ -20,5 +20,13 @@ internal static class Constants
2020
#nullable enable
2121
2222
23+
""";
24+
25+
public const string ConfigurationGeneratorComment =
26+
"""
27+
// This file is auto-generated from supported-configurations.json and supported-configurations-docs.yaml
28+
// Do not edit this file directly. The source generator will regenerate it on build.
29+
// NOTE: If you remove keys/products from the JSON, run 'dotnet clean' and remove old generated files.
30+
2331
""";
2432
}

tracer/src/Datadog.Trace.SourceGenerators/Datadog.Trace.SourceGenerators.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFramework>netstandard2.0</TargetFramework>
44
<Nullable>enable</Nullable>
@@ -12,6 +12,7 @@
1212
<ItemGroup>
1313
<PackageReference Include="System.Reflection.Emit" Version="4.7.0" />
1414
<PackageReference Include="System.Reflection.Emit.Lightweight" Version="4.7.0" />
15+
<PackageReference Include="System.Text.Json" Version="8.0.5" PrivateAssets="all" />
1516
</ItemGroup>
1617
<ItemGroup>
1718
<Compile Include="..\Datadog.Trace\ClrProfiler\InstrumentationCategory.cs" Link="InstrumentationDefinitions\InstrumentationCategory.cs" />

tracer/src/Datadog.Trace.SourceGenerators/Helpers/TrackingNames.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,17 @@ internal class TrackingNames
3131
public const string AssemblyCallTargetDefinitionSource = nameof(AssemblyCallTargetDefinitionSource);
3232
public const string AdoNetCallTargetDefinitionSource = nameof(AdoNetCallTargetDefinitionSource);
3333
public const string AdoNetSignatures = nameof(AdoNetSignatures);
34+
35+
// Configuration key matcher
36+
public const string ConfigurationKeysParseConfiguration = nameof(ConfigurationKeysParseConfiguration);
37+
public const string ConfigurationKeyMatcherDiagnostics = nameof(ConfigurationKeyMatcherDiagnostics);
38+
public const string ConfigurationKeyMatcherValidData = nameof(ConfigurationKeyMatcherValidData);
39+
public const string ConfigurationKeysAdditionalText = nameof(ConfigurationKeysAdditionalText);
40+
41+
// Configuration key generator
42+
public const string ConfigurationKeysGenParseConfiguration = nameof(ConfigurationKeysGenParseConfiguration);
43+
public const string ConfigurationKeysGenContentExtracted = nameof(ConfigurationKeysGenContentExtracted);
44+
public const string ConfigurationKeysGenMatcherValidData = nameof(ConfigurationKeysGenMatcherValidData);
45+
public const string ConfigurationKeysGenAdditionalText = nameof(ConfigurationKeysGenAdditionalText);
46+
public const string ConfigurationKeysGenYamlAdditionalText = nameof(ConfigurationKeysGenYamlAdditionalText);
3447
}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
// <copyright file="YamlReader.cs" company="Datadog">
2+
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License.
3+
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
4+
// </copyright>
5+
6+
using System.Collections.Generic;
7+
using System.Text;
8+
9+
namespace Datadog.Trace.SourceGenerators.Helpers
10+
{
11+
/// <summary>
12+
/// Simple YAML parser for reading documentation strings from YAML files.
13+
/// Supports basic YAML features needed for configuration documentation.
14+
/// </summary>
15+
internal static class YamlReader
16+
{
17+
/// <summary>
18+
/// Parses a YAML file containing configuration key documentation.
19+
/// Expects format: KEY: | followed by multi-line documentation
20+
/// </summary>
21+
public static Dictionary<string, string> ParseDocumentation(string yamlContent)
22+
{
23+
var result = new Dictionary<string, string>();
24+
var lines = yamlContent.Replace("\r\n", "\n").Split('\n');
25+
26+
string? currentKey = null;
27+
var currentDoc = new StringBuilder();
28+
var inMultiLine = false;
29+
var baseIndent = 0;
30+
31+
for (int i = 0; i < lines.Length; i++)
32+
{
33+
var line = lines[i];
34+
35+
// Skip empty lines and comments when not in multi-line
36+
if (!inMultiLine && (string.IsNullOrWhiteSpace(line) || line.TrimStart().StartsWith("#")))
37+
{
38+
continue;
39+
}
40+
41+
// Check for new key (starts at column 0, contains colon)
42+
if (!inMultiLine && line.Length > 0 && line[0] != ' ' && line.Contains(":"))
43+
{
44+
// Save previous key if exists
45+
if (currentKey != null)
46+
{
47+
result[currentKey] = currentDoc.ToString().TrimEnd();
48+
currentDoc.Clear();
49+
}
50+
51+
var colonIndex = line.IndexOf(':');
52+
currentKey = line.Substring(0, colonIndex).Trim();
53+
54+
// Check if it's a multi-line string (|)
55+
var afterColon = line.Substring(colonIndex + 1).Trim();
56+
if (afterColon == "|" || afterColon == ">")
57+
{
58+
inMultiLine = true;
59+
baseIndent = -1; // Will be set on first content line
60+
}
61+
else if (!string.IsNullOrEmpty(afterColon))
62+
{
63+
// Single line value
64+
currentDoc.Append(afterColon);
65+
result[currentKey] = currentDoc.ToString();
66+
currentDoc.Clear();
67+
currentKey = null;
68+
}
69+
70+
continue;
71+
}
72+
73+
// Handle multi-line content
74+
if (inMultiLine && currentKey != null)
75+
{
76+
// Check if we've reached the next key (no indentation)
77+
if (line.Length > 0 && line[0] != ' ' && line.Contains(":"))
78+
{
79+
// Save current key and process this line as new key
80+
result[currentKey] = currentDoc.ToString().TrimEnd();
81+
currentDoc.Clear();
82+
inMultiLine = false;
83+
84+
var colonIndex = line.IndexOf(':');
85+
currentKey = line.Substring(0, colonIndex).Trim();
86+
var afterColon = line.Substring(colonIndex + 1).Trim();
87+
if (afterColon == "|" || afterColon == ">")
88+
{
89+
inMultiLine = true;
90+
baseIndent = -1;
91+
}
92+
93+
continue;
94+
}
95+
96+
// Determine base indentation from first content line
97+
if (baseIndent == -1 && line.Length > 0 && line[0] == ' ')
98+
{
99+
baseIndent = 0;
100+
while (baseIndent < line.Length && line[baseIndent] == ' ')
101+
{
102+
baseIndent++;
103+
}
104+
}
105+
106+
// Add content line (remove base indentation)
107+
if (line.Length > 0)
108+
{
109+
var contentStart = 0;
110+
while (contentStart < line.Length && contentStart < baseIndent && line[contentStart] == ' ')
111+
{
112+
contentStart++;
113+
}
114+
115+
if (currentDoc.Length > 0)
116+
{
117+
currentDoc.AppendLine();
118+
}
119+
120+
currentDoc.Append(line.Substring(contentStart));
121+
}
122+
else
123+
{
124+
// Empty line in multi-line content
125+
if (currentDoc.Length > 0)
126+
{
127+
currentDoc.AppendLine();
128+
}
129+
}
130+
}
131+
}
132+
133+
// Save last key
134+
if (currentKey != null)
135+
{
136+
result[currentKey] = currentDoc.ToString().TrimEnd();
137+
}
138+
139+
return result;
140+
}
141+
}
142+
}

tracer/src/Datadog.Trace/Datadog.Trace.csproj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,4 +156,13 @@
156156
<DefineConstants>$(DefineConstants);ENABLE_IL2CPP</DefineConstants>
157157
</PropertyGroup>
158158

159+
<ItemGroup>
160+
<AdditionalFiles Include="Configuration\supported-configurations.json">
161+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
162+
</AdditionalFiles>
163+
<AdditionalFiles Include="Configuration\supported-configurations-docs.yaml">
164+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
165+
</AdditionalFiles>
166+
</ItemGroup>
167+
159168
</Project>

0 commit comments

Comments
 (0)