Skip to content

Commit 0b02cd3

Browse files
Merge branch 'master' into dotnet10/UpdateToPreview
2 parents 1aa305b + aabf016 commit 0b02cd3

File tree

63 files changed

+826
-406
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+826
-406
lines changed

tracer/src/Datadog.Trace/Ci/CiEnvironment/CIEnvironmentValues.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ public string? GitSearchFolder
9191

9292
public string? PrBaseCommit { get; protected set; }
9393

94+
public string? PrBaseHeadCommit { get; protected set; }
95+
9496
public string? PrBaseBranch { get; protected set; }
9597

9698
public string? PrNumber { get; protected set; }
@@ -283,6 +285,7 @@ public void DecorateSpan(Span span)
283285
SetTagIfNotNullOrEmpty(span, CommonTags.CINodeLabels, Datadog.Trace.Vendors.Newtonsoft.Json.JsonConvert.SerializeObject(nodeLabels));
284286
}
285287

288+
SetTagIfNotNullOrEmpty(span, CommonTags.GitPrBaseHeadCommit, PrBaseHeadCommit);
286289
SetTagIfNotNullOrEmpty(span, CommonTags.GitPrBaseCommit, PrBaseCommit);
287290
SetTagIfNotNullOrEmpty(span, CommonTags.GitPrBaseBranch, PrBaseBranch);
288291
SetTagIfNotNullOrEmpty(span, CommonTags.PrNumber, PrNumber);
@@ -529,6 +532,7 @@ internal sealed class Constants
529532
public const string GitlabRunnerTags = "CI_RUNNER_TAGS";
530533
public const string GitlabMergeRequestSourceBranchSha = "CI_MERGE_REQUEST_SOURCE_BRANCH_SHA";
531534
public const string GitlabMergeRequestTargetBranchSha = "CI_MERGE_REQUEST_TARGET_BRANCH_SHA";
535+
public const string GitlabMergeRequestDiffBaseSha = "CI_MERGE_REQUEST_DIFF_BASE_SHA";
532536
public const string GitlabMergeRequestTargetBranchName = "CI_MERGE_REQUEST_TARGET_BRANCH_NAME";
533537
public const string GitlabMergeRequestId = "CI_MERGE_REQUEST_IID";
534538

tracer/src/Datadog.Trace/Ci/CiEnvironment/GithubActionsEnvironmentValues.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ private void LoadGithubEventJson()
121121
var prBaseSha = pullRequestObject["base"]?["sha"]?.Value<string>();
122122
if (!string.IsNullOrWhiteSpace(prBaseSha))
123123
{
124-
PrBaseCommit = prBaseSha;
124+
PrBaseHeadCommit = prBaseSha;
125125
}
126126

127127
var prBaseRef = pullRequestObject["base"]?["ref"]?.Value<string>();

tracer/src/Datadog.Trace/Ci/CiEnvironment/GitlabEnvironmentValues.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ protected override void OnInitialize(IGitInfo gitInfo)
7777
Constants.GitlabJobId);
7878

7979
HeadCommit = ValueProvider.GetValue(Constants.GitlabMergeRequestSourceBranchSha);
80-
PrBaseCommit = ValueProvider.GetValue(Constants.GitlabMergeRequestTargetBranchSha);
80+
PrBaseHeadCommit = ValueProvider.GetValue(Constants.GitlabMergeRequestTargetBranchSha);
81+
PrBaseCommit = ValueProvider.GetValue(Constants.GitlabMergeRequestDiffBaseSha);
8182
PrBaseBranch = ValueProvider.GetValue(Constants.GitlabMergeRequestTargetBranchName);
8283
PrNumber = ValueProvider.GetValue(Constants.GitlabMergeRequestId);
8384
}

tracer/src/Datadog.Trace/Ci/Tagging/TestSessionSpanTags.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ public TestSessionSpanTags()
123123
[Metric(CommonTags.LogicalCpuCount)]
124124
public double? LogicalCpuCount { get; }
125125

126+
[Tag(CommonTags.GitPrBaseHeadCommit)]
127+
public string? GitPrBaseHeadCommit { get; set; }
128+
126129
[Tag(CommonTags.GitPrBaseCommit)]
127130
public string? GitPrBaseCommit { get; set; }
128131

@@ -182,6 +185,7 @@ public void SetCIEnvironmentValues(CIEnvironmentValues environmentValues)
182185
GitCommitCommitterEmail = environmentValues.CommitterEmail;
183186
GitCommitMessage = environmentValues.Message;
184187
BuildSourceRoot = environmentValues.SourceRoot;
188+
GitPrBaseHeadCommit = environmentValues.PrBaseHeadCommit;
185189
GitPrBaseCommit = environmentValues.PrBaseCommit;
186190
GitPrBaseBranch = environmentValues.PrBaseBranch;
187191
PrNumber = environmentValues.PrNumber;

tracer/src/Datadog.Trace/Ci/Tagging/TestSpanTags.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,12 @@ public TestSpanTags(TestSuiteSpanTags suiteTags, string testName)
6464
[Tag(TestTags.Parameters)]
6565
public string? Parameters { get; set; }
6666

67-
[Tag(TestTags.SourceFile)]
68-
public string? SourceFile { get; set; }
69-
7067
[Metric(TestTags.SourceStart)]
7168
public double? SourceStart { get; set; }
7269

7370
[Metric(TestTags.SourceEnd)]
7471
public double? SourceEnd { get; set; }
7572

76-
[Tag(TestTags.CodeOwners)]
77-
public string? CodeOwners { get; set; }
78-
7973
[Tag(TestTags.Traits)]
8074
public string? Traits { get; set; }
8175

tracer/src/Datadog.Trace/Ci/Tagging/TestSuiteSpanTags.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,10 @@ public TestSuiteSpanTags(TestModuleSpanTags moduleTags, string suiteName)
6060

6161
[Tag(TestTags.Suite)]
6262
public string? Suite { get; set; }
63+
64+
[Tag(TestTags.SourceFile)]
65+
public string? SourceFile { get; set; }
66+
67+
[Tag(TestTags.CodeOwners)]
68+
public string? CodeOwners { get; set; }
6369
}

tracer/src/Datadog.Trace/Ci/Tags/CommonTags.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,11 @@ internal static class CommonTags
206206
/// </summary>
207207
public const string GitPrBaseCommit = "git.pull_request.base_branch_sha";
208208

209+
/// <summary>
210+
/// GIT PR Base head commit hash
211+
/// </summary>
212+
public const string GitPrBaseHeadCommit = "git.pull_request.base_branch_head_sha";
213+
209214
/// <summary>
210215
/// GIT PR Base branch name
211216
/// </summary>

tracer/src/Datadog.Trace/Ci/Test.cs

Lines changed: 89 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -211,20 +211,104 @@ public void SetTestMethodInfo(MethodInfo methodInfo)
211211
tags.SourceFile = ciValues.MakeRelativePathFromSourceRoot(methodSymbol.File, false);
212212
tags.SourceStart = startLine;
213213
tags.SourceEnd = methodSymbol.EndLine;
214-
215214
_testOptimization.ImpactedTestsDetectionFeature?.ImpactedTestsAnalyzer.Analyze(this);
216215

217-
if (ciValues.CodeOwners is { } codeOwners)
216+
SetStringOrArray(
217+
tags,
218+
Suite.Tags,
219+
static testTags => testTags.SourceFile,
220+
static suiteTags => suiteTags.SourceFile,
221+
static (suiteTags, value) => suiteTags.SourceFile = value);
222+
223+
if (ciValues.CodeOwners is { } codeOwners &&
224+
codeOwners.Match("/" + tags.SourceFile) is { } match)
225+
{
226+
SetCodeOwnersOnTags(tags, Suite.Tags, match);
227+
}
228+
}
229+
}
230+
231+
internal static void SetStringOrArray(TestSpanTags testTags, TestSuiteSpanTags suiteTags, Func<TestSpanTags, string?> getTestTag, Func<TestSuiteSpanTags, string?> getSuiteTag, Action<TestSuiteSpanTags, string?> setSuiteTag)
232+
{
233+
// If the value is not set, we set it to the current test tag
234+
// If it is set, we check if it is an array and add the current test tag to it
235+
// If it is not an array, we create a new array with both values
236+
// This is to support multiple values in a single tag
237+
var suiteTagValue = getSuiteTag(suiteTags);
238+
var testTagValue = getTestTag(testTags);
239+
if (StringUtil.IsNullOrEmpty(testTagValue))
240+
{
241+
return;
242+
}
243+
244+
if (StringUtil.IsNullOrEmpty(suiteTagValue))
245+
{
246+
setSuiteTag(suiteTags, testTagValue);
247+
}
248+
else if (!string.Equals(suiteTagValue, testTagValue, StringComparison.Ordinal))
249+
{
250+
if (suiteTagValue.StartsWith("[", StringComparison.Ordinal) &&
251+
suiteTagValue.EndsWith("]", StringComparison.Ordinal))
252+
{
253+
// If the source file is an array, we add the new source file to it
254+
List<string>? files;
255+
try
256+
{
257+
files = Vendors.Newtonsoft.Json.JsonConvert.DeserializeObject<List<string>>(suiteTagValue);
258+
}
259+
catch (Exception ex)
260+
{
261+
TestOptimization.Instance.Log.Warning(ex, "Error deserializing '{SuiteTagValue}' environment variable.", suiteTagValue);
262+
files = [];
263+
}
264+
265+
if (files is not null && !files.Contains(testTagValue, StringComparer.Ordinal))
266+
{
267+
files.Add(testTagValue);
268+
setSuiteTag(suiteTags, Vendors.Newtonsoft.Json.JsonConvert.SerializeObject(files));
269+
}
270+
}
271+
else
218272
{
219-
var match = codeOwners.Match("/" + ciValues.MakeRelativePathFromSourceRoot(methodSymbol.File, false));
220-
if (match is not null)
273+
// If the source file is not an array, we create a new one with both values
274+
try
221275
{
222-
tags.CodeOwners = "[\"" + string.Join("\",\"", match) + "\"]";
276+
setSuiteTag(suiteTags, Vendors.Newtonsoft.Json.JsonConvert.SerializeObject(new List<string> { suiteTagValue, testTagValue }));
277+
}
278+
catch (Exception ex)
279+
{
280+
TestOptimization.Instance.Log.Warning(ex, "Error serializing '{SuiteTagValue}' environment variable.", suiteTagValue);
281+
setSuiteTag(suiteTags, $"[\"{suiteTagValue}\",\"{testTagValue}\"]");
223282
}
224283
}
225284
}
226285
}
227286

287+
internal static void SetCodeOwnersOnTags(TestSpanTags testTags, TestSuiteSpanTags suiteTags, IEnumerable<string> codeOwners)
288+
{
289+
testTags.CodeOwners = "[\"" + string.Join("\",\"", codeOwners) + "\"]";
290+
if (StringUtil.IsNullOrEmpty(suiteTags.CodeOwners))
291+
{
292+
suiteTags.CodeOwners = testTags.CodeOwners;
293+
}
294+
else if (!string.Equals(suiteTags.CodeOwners, testTags.CodeOwners, StringComparison.Ordinal))
295+
{
296+
List<string> suiteCodeOwners;
297+
try
298+
{
299+
suiteCodeOwners = Vendors.Newtonsoft.Json.JsonConvert.DeserializeObject<List<string>>(suiteTags.CodeOwners) ?? [];
300+
}
301+
catch (Exception ex)
302+
{
303+
TestOptimization.Instance.Log.Warning(ex, "Error deserializing '{SuiteCodeOwners}' environment variable.", suiteTags.CodeOwners);
304+
suiteCodeOwners = [];
305+
}
306+
307+
suiteCodeOwners.AddRange(codeOwners);
308+
suiteTags.CodeOwners = "[\"" + string.Join("\",\"", suiteCodeOwners.Distinct(StringComparer.Ordinal)) + "\"]";
309+
}
310+
}
311+
228312
/// <summary>
229313
/// Set Test traits
230314
/// </summary>

tracer/src/Datadog.Trace/Generated/net461/Datadog.Trace.SourceGenerators/TagListGenerator/TestSessionSpanTags.g.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ partial class TestSessionSpanTags
8080
private static ReadOnlySpan<byte> EarlyFlakeDetectionTestEnabledBytes => new byte[] { 184, 116, 101, 115, 116, 46, 101, 97, 114, 108, 121, 95, 102, 108, 97, 107, 101, 46, 101, 110, 97, 98, 108, 101, 100 };
8181
// EarlyFlakeDetectionTestAbortReasonBytes = MessagePack.Serialize("test.early_flake.abort_reason");
8282
private static ReadOnlySpan<byte> EarlyFlakeDetectionTestAbortReasonBytes => new byte[] { 189, 116, 101, 115, 116, 46, 101, 97, 114, 108, 121, 95, 102, 108, 97, 107, 101, 46, 97, 98, 111, 114, 116, 95, 114, 101, 97, 115, 111, 110 };
83+
// GitPrBaseHeadCommitBytes = MessagePack.Serialize("git.pull_request.base_branch_head_sha");
84+
private static ReadOnlySpan<byte> GitPrBaseHeadCommitBytes => new byte[] { 217, 37, 103, 105, 116, 46, 112, 117, 108, 108, 95, 114, 101, 113, 117, 101, 115, 116, 46, 98, 97, 115, 101, 95, 98, 114, 97, 110, 99, 104, 95, 104, 101, 97, 100, 95, 115, 104, 97 };
8385
// GitPrBaseCommitBytes = MessagePack.Serialize("git.pull_request.base_branch_sha");
8486
private static ReadOnlySpan<byte> GitPrBaseCommitBytes => new byte[] { 217, 32, 103, 105, 116, 46, 112, 117, 108, 108, 95, 114, 101, 113, 117, 101, 115, 116, 46, 98, 97, 115, 101, 95, 98, 114, 97, 110, 99, 104, 95, 115, 104, 97 };
8587
// GitPrBaseBranchBytes = MessagePack.Serialize("git.pull_request.base_branch");
@@ -139,6 +141,7 @@ partial class TestSessionSpanTags
139141
"test.itr.tests_skipping.type" => IntelligentTestRunnerSkippingType,
140142
"test.early_flake.enabled" => EarlyFlakeDetectionTestEnabled,
141143
"test.early_flake.abort_reason" => EarlyFlakeDetectionTestAbortReason,
144+
"git.pull_request.base_branch_head_sha" => GitPrBaseHeadCommit,
142145
"git.pull_request.base_branch_sha" => GitPrBaseCommit,
143146
"git.pull_request.base_branch" => GitPrBaseBranch,
144147
"pr.number" => PrNumber,
@@ -251,6 +254,9 @@ public override void SetTag(string key, string value)
251254
case "test.early_flake.abort_reason":
252255
EarlyFlakeDetectionTestAbortReason = value;
253256
break;
257+
case "git.pull_request.base_branch_head_sha":
258+
GitPrBaseHeadCommit = value;
259+
break;
254260
case "git.pull_request.base_branch_sha":
255261
GitPrBaseCommit = value;
256262
break;
@@ -455,6 +461,11 @@ public override void EnumerateTags<TProcessor>(ref TProcessor processor)
455461
processor.Process(new TagItem<string>("test.early_flake.abort_reason", EarlyFlakeDetectionTestAbortReason, EarlyFlakeDetectionTestAbortReasonBytes));
456462
}
457463

464+
if (GitPrBaseHeadCommit is not null)
465+
{
466+
processor.Process(new TagItem<string>("git.pull_request.base_branch_head_sha", GitPrBaseHeadCommit, GitPrBaseHeadCommitBytes));
467+
}
468+
458469
if (GitPrBaseCommit is not null)
459470
{
460471
processor.Process(new TagItem<string>("git.pull_request.base_branch_sha", GitPrBaseCommit, GitPrBaseCommitBytes));
@@ -739,6 +750,13 @@ protected override void WriteAdditionalTags(System.Text.StringBuilder sb)
739750
.Append(',');
740751
}
741752

753+
if (GitPrBaseHeadCommit is not null)
754+
{
755+
sb.Append("git.pull_request.base_branch_head_sha (tag):")
756+
.Append(GitPrBaseHeadCommit)
757+
.Append(',');
758+
}
759+
742760
if (GitPrBaseCommit is not null)
743761
{
744762
sb.Append("git.pull_request.base_branch_sha (tag):")

tracer/src/Datadog.Trace/Generated/net461/Datadog.Trace.SourceGenerators/TagListGenerator/TestSpanTags.g.cs

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ partial class TestSpanTags
2222
private static ReadOnlySpan<byte> NameBytes => new byte[] { 169, 116, 101, 115, 116, 46, 110, 97, 109, 101 };
2323
// ParametersBytes = MessagePack.Serialize("test.parameters");
2424
private static ReadOnlySpan<byte> ParametersBytes => new byte[] { 175, 116, 101, 115, 116, 46, 112, 97, 114, 97, 109, 101, 116, 101, 114, 115 };
25-
// SourceFileBytes = MessagePack.Serialize("test.source.file");
26-
private static ReadOnlySpan<byte> SourceFileBytes => new byte[] { 176, 116, 101, 115, 116, 46, 115, 111, 117, 114, 99, 101, 46, 102, 105, 108, 101 };
27-
// CodeOwnersBytes = MessagePack.Serialize("test.codeowners");
28-
private static ReadOnlySpan<byte> CodeOwnersBytes => new byte[] { 175, 116, 101, 115, 116, 46, 99, 111, 100, 101, 111, 119, 110, 101, 114, 115 };
2925
// TraitsBytes = MessagePack.Serialize("test.traits");
3026
private static ReadOnlySpan<byte> TraitsBytes => new byte[] { 171, 116, 101, 115, 116, 46, 116, 114, 97, 105, 116, 115 };
3127
// SkipReasonBytes = MessagePack.Serialize("test.skip_reason");
@@ -83,8 +79,6 @@ partial class TestSpanTags
8379
{
8480
"test.name" => Name,
8581
"test.parameters" => Parameters,
86-
"test.source.file" => SourceFile,
87-
"test.codeowners" => CodeOwners,
8882
"test.traits" => Traits,
8983
"test.skip_reason" => SkipReason,
9084
"test.skipped_by_itr" => SkippedByIntelligentTestRunner,
@@ -124,12 +118,6 @@ public override void SetTag(string key, string value)
124118
case "test.parameters":
125119
Parameters = value;
126120
break;
127-
case "test.source.file":
128-
SourceFile = value;
129-
break;
130-
case "test.codeowners":
131-
CodeOwners = value;
132-
break;
133121
case "test.traits":
134122
Traits = value;
135123
break;
@@ -223,16 +211,6 @@ public override void EnumerateTags<TProcessor>(ref TProcessor processor)
223211
processor.Process(new TagItem<string>("test.parameters", Parameters, ParametersBytes));
224212
}
225213

226-
if (SourceFile is not null)
227-
{
228-
processor.Process(new TagItem<string>("test.source.file", SourceFile, SourceFileBytes));
229-
}
230-
231-
if (CodeOwners is not null)
232-
{
233-
processor.Process(new TagItem<string>("test.codeowners", CodeOwners, CodeOwnersBytes));
234-
}
235-
236214
if (Traits is not null)
237215
{
238216
processor.Process(new TagItem<string>("test.traits", Traits, TraitsBytes));
@@ -377,20 +355,6 @@ protected override void WriteAdditionalTags(System.Text.StringBuilder sb)
377355
.Append(',');
378356
}
379357

380-
if (SourceFile is not null)
381-
{
382-
sb.Append("test.source.file (tag):")
383-
.Append(SourceFile)
384-
.Append(',');
385-
}
386-
387-
if (CodeOwners is not null)
388-
{
389-
sb.Append("test.codeowners (tag):")
390-
.Append(CodeOwners)
391-
.Append(',');
392-
}
393-
394358
if (Traits is not null)
395359
{
396360
sb.Append("test.traits (tag):")

0 commit comments

Comments
 (0)