-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathContinuousProfilerContextTrackingTests.cs
63 lines (48 loc) · 2.51 KB
/
ContinuousProfilerContextTrackingTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
#if NET
using IntegrationTests.Helpers;
using OpenTelemetry.Proto.Collector.Profiles.V1Development;
using Xunit.Abstractions;
namespace IntegrationTests;
public class ContinuousProfilerContextTrackingTests : TestHelper
{
public ContinuousProfilerContextTrackingTests(ITestOutputHelper output)
: base("ContinuousProfiler.ContextTracking", output)
{
}
[Fact]
[Trait("Category", "EndToEnd")]
public void TraceContextIsCorrectlyAssociatedWithThreadSamples()
{
EnableBytecodeInstrumentation();
using var collector = new MockProfilesCollector(Output);
SetExporter(collector);
SetEnvironmentVariable("OTEL_DOTNET_AUTO_PLUGINS", "TestApplication.ContinuousProfiler.ContextTracking.TestPlugin, TestApplication.ContinuousProfiler.ContextTracking, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");
SetEnvironmentVariable("OTEL_DOTNET_AUTO_TRACES_ADDITIONAL_SOURCES", "TestApplication.ContinuousProfiler.ContextTracking");
collector.ExpectCollected(AssertAllProfiles, $"{nameof(AssertAllProfiles)} failed");
RunTestApplication();
collector.AssertCollected();
}
private bool AssertAllProfiles(ICollection<ExportProfilesServiceRequest> profilesServiceRequests)
{
var totalSamplesWithTraceContextCount = 0;
var managedThreadsWithTraceContext = new HashSet<string>();
foreach (var batch in profilesServiceRequests)
{
var profile = batch.ResourceProfiles.Single().ScopeProfiles.Single().Profiles.Single();
var samplesInBatch = profile.Sample;
var samplesWithTraceContext = samplesInBatch.Where(s => s.HasLinkIndex).ToList();
Assert.True(samplesWithTraceContext.Count <= 1, "at most one sample in a batch should have trace context associated.");
totalSamplesWithTraceContextCount += samplesWithTraceContext.Count;
if (samplesWithTraceContext.FirstOrDefault() is { } sampleWithTraceContext)
{
managedThreadsWithTraceContext.Add(profile.AttributeTable[sampleWithTraceContext.AttributeIndices.Single()].Value.StringValue);
}
}
Assert.True(managedThreadsWithTraceContext.Count > 1, "at least 2 distinct threads should have trace context associated.");
Assert.True(totalSamplesWithTraceContextCount >= 3, "there should be sample with trace context in most of the batches.");
return true;
}
}
#endif