Skip to content

Commit 1b97c72

Browse files
Kieleklachmatt
andauthored
[Continuous Profiler] prepare test infrastructure for .NET Fx (#4631)
Co-authored-by: Mateusz Łach <[email protected]>
1 parent 841e022 commit 1b97c72

File tree

10 files changed

+46
-24
lines changed

10 files changed

+46
-24
lines changed
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4-
#if NET
54
using System.Text.Json;
65

76
namespace IntegrationTests;
@@ -11,8 +10,8 @@ internal static class ConsoleProfileExporterHelpers
1110
public static List<ConsoleThreadSample> ExtractSamples(string output)
1211
{
1312
var batchSeparator = $"{Environment.NewLine}{Environment.NewLine}";
14-
var lines = output.Split(batchSeparator);
15-
var deserializedSampleBatches = lines[..^1].Select(sample => JsonSerializer.Deserialize<List<ConsoleThreadSample>>(sample)).ToList();
13+
var lines = output.Split([batchSeparator], StringSplitOptions.None);
14+
var deserializedSampleBatches = lines.Take(lines.Length - 1).Select(sample => JsonSerializer.Deserialize<List<ConsoleThreadSample>>(sample)).ToList();
1615

1716
var threadSamples = new List<ConsoleThreadSample>();
1817
foreach (var batch in deserializedSampleBatches)
@@ -23,5 +22,3 @@ public static List<ConsoleThreadSample> ExtractSamples(string output)
2322
return threadSamples;
2423
}
2524
}
26-
27-
#endif
Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4-
#if NET
54
namespace IntegrationTests;
65

76
internal class ConsoleThreadSample
@@ -16,11 +15,5 @@ internal class ConsoleThreadSample
1615

1716
public string? ThreadName { get; set; }
1817

19-
public uint ThreadIndex { get; set; }
20-
2118
public bool SelectedForFrequentSampling { get; set; }
22-
23-
public IList<string> Frames { get; set; } = new List<string>();
2419
}
25-
26-
#endif

test/IntegrationTests/Helpers/MockProfilesCollector.cs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4-
#if NET
5-
64
using System.Collections.Concurrent;
75
using System.Text;
8-
using Microsoft.AspNetCore.Http;
96
using OpenTelemetry.Proto.Collector.Profiles.V1Development;
107
using OpenTelemetry.Proto.Profiles.V1Development;
118
using Xunit.Abstractions;
129

10+
#if NETFRAMEWORK
11+
using System.Net;
12+
#else
13+
using Microsoft.AspNetCore.Http;
14+
#endif
15+
1316
namespace IntegrationTests.Helpers;
1417

1518
public class MockProfilesCollector : IDisposable
@@ -21,10 +24,14 @@ public class MockProfilesCollector : IDisposable
2124
private readonly BlockingCollection<Collected> _profilesSnapshots = new(10); // bounded to avoid memory leak; contains protobuf type
2225
private CollectedExpectation? _collectedExpectation;
2326

24-
public MockProfilesCollector(ITestOutputHelper output)
27+
public MockProfilesCollector(ITestOutputHelper output, string host = "localhost")
2528
{
2629
_output = output;
30+
#if NETFRAMEWORK
31+
_listener = new(output, HandleHttpRequests, host, "/v1/metrics/");
32+
#else
2733
_listener = new(output, nameof(MockProfilesCollector), new PathHandler(HandleHttpRequests, "/v1development/profiles"));
34+
#endif
2835
}
2936

3037
/// <summary>
@@ -167,6 +174,15 @@ private static void FailCollectedExpectation(string? collectedExpectationDescrip
167174
Assert.Fail(message.ToString());
168175
}
169176

177+
#if NETFRAMEWORK
178+
private void HandleHttpRequests(HttpListenerContext ctx)
179+
{
180+
var profilesMessage = ExportProfilesServiceRequest.Parser.ParseFrom(ctx.Request.InputStream);
181+
HandleProfilesMessage(profilesMessage);
182+
183+
ctx.GenerateEmptyProtobufResponse<ExportProfilesServiceResponse>();
184+
}
185+
#else
170186
private async Task HandleHttpRequests(HttpContext ctx)
171187
{
172188
using var bodyStream = await ctx.ReadBodyToMemoryAsync();
@@ -175,6 +191,7 @@ private async Task HandleHttpRequests(HttpContext ctx)
175191

176192
await ctx.GenerateEmptyProtobufResponseAsync<ExportProfilesServiceResponse>();
177193
}
194+
#endif
178195

179196
private void HandleProfilesMessage(ExportProfilesServiceRequest profileMessage)
180197
{
@@ -233,5 +250,3 @@ public CollectedExpectation(Func<ICollection<ExportProfilesServiceRequest>, bool
233250
public string? Description { get; }
234251
}
235252
}
236-
237-
#endif

test/IntegrationTests/Helpers/TestHelper.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,10 @@ public void SetFileBasedExporter(MockLogsCollector collector)
8686
SetEnvironmentVariable("OTEL_EXPORTER_OTLP_LOGS_ENDPOINT", $"http://localhost:{collector.Port}/v1/logs");
8787
}
8888

89-
#if NET
9089
public void SetExporter(MockProfilesCollector collector)
9190
{
9291
SetEnvironmentVariable("OTEL_EXPORTER_OTLP_ENDPOINT", $"http://localhost:{collector.Port}");
9392
}
94-
#endif
9593

9694
public void EnableBytecodeInstrumentation()
9795
{

test/test-applications/integrations/TestApplication.ContinuousProfiler.ContextTracking/TestApplication.ContinuousProfiler.ContextTracking.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFrameworks>net10.0;net9.0;net8.0</TargetFrameworks>
65
<!-- SA1518 needed for files generated by Grpc.Tools -->
76
<NoWarn>SA1518;$(NoWarn)</NoWarn>
87
</PropertyGroup>

test/test-applications/integrations/TestApplication.ContinuousProfiler/ClassA.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static void MethodA()
3030

3131
for (var i = 0; i < numberOfItems; i++)
3232
{
33-
TextWriter.Null.Write(items[i][^2]);
33+
TextWriter.Null.Write(items[i][items[i].Length - 2]);
3434
}
3535
}
3636

@@ -48,8 +48,13 @@ public static void MethodABytes(
4848
int.MaxValue,
4949
ulong.MaxValue,
5050
long.MaxValue,
51+
#if NET
5152
nint.MaxValue,
5253
nuint.MaxValue);
54+
#else
55+
0x7fffffff,
56+
0xffffffff);
57+
#endif
5358
}
5459

5560
[MethodImpl(MethodImplOptions.NoInlining)]
@@ -105,7 +110,11 @@ public static void MethodAOthers<T>(
105110
static void Action(int s) => InternalClassB<string, int>.DoubleInternalClassB.TripleInternalClassB<int>.MethodB(s, [3], TimeSpan.Zero, 0, ["a"], []);
106111
}
107112

113+
#if NET
108114
[DllImport("TestApplication.ContinuousProfiler.NativeDep")]
115+
#else
116+
[DllImport("TestApplication.ContinuousProfiler.NativeDep.dll")]
117+
#endif
109118
private static extern int OTelAutoCallbackTest(Callback fp, int n);
110119

111120
internal static class InternalClassB<TA, TD>

test/test-applications/integrations/TestApplication.ContinuousProfiler/Exporter/OtlpOverHttpExporter.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ public class OtlpOverHttpExporter
2727
public OtlpOverHttpExporter(TimeSpan cpuPeriod, SampleNativeFormatParser parser)
2828
{
2929
_parser = parser;
30+
#if NET
3031
this.cpuPeriod = (long)cpuPeriod.TotalNanoseconds;
32+
#else
33+
this.cpuPeriod = cpuPeriod.Ticks * 100L; // convert to nanoseconds
34+
#endif
3135
}
3236

3337
public void ExportThreadSamples(byte[] buffer, int read, CancellationToken cancellationToken)
@@ -148,7 +152,7 @@ private static SampleBuilder CreateSampleBuilder(ThreadSample threadSample, Exte
148152

149153
if (!string.IsNullOrEmpty(threadSample.ThreadName))
150154
{
151-
extendedPprofBuilder.AddAttribute(sampleBuilder, "thread.name", threadSample.ThreadName);
155+
extendedPprofBuilder.AddAttribute(sampleBuilder, "thread.name", threadSample.ThreadName!);
152156
}
153157

154158
return sampleBuilder;
@@ -255,7 +259,11 @@ private static ScopeProfiles CreateScopeProfiles()
255259

256260
private HttpResponseMessage SendHttpRequest(HttpRequestMessage request, CancellationToken cancellationToken)
257261
{
262+
#if NET
258263
return _httpClient.Send(request, cancellationToken);
264+
#else
265+
return _httpClient.SendAsync(request, cancellationToken).GetAwaiter().GetResult();
266+
#endif
259267
}
260268

261269
private HttpContent CreateHttpContent(ExportProfilesServiceRequest exportRequest)
@@ -290,10 +298,12 @@ public ExportRequestContent(ExportProfilesServiceRequest exportRequest)
290298
Headers.ContentType = ProtobufMediaTypeHeader;
291299
}
292300

301+
#if NET
293302
protected override void SerializeToStream(Stream stream, TransportContext? context, CancellationToken cancellationToken)
294303
{
295304
SerializeToStreamInternal(stream);
296305
}
306+
#endif
297307

298308
protected override Task SerializeToStreamAsync(Stream stream, TransportContext? context)
299309
{

test/test-applications/integrations/TestApplication.ContinuousProfiler/TestApplication.ContinuousProfiler.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFrameworks>net10.0;net9.0;net8.0</TargetFrameworks>
65
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
76
<!-- SA1518 needed for files generated by Grpc.Tools -->
87
<NoWarn>SA1518;$(NoWarn)</NoWarn>

test/test-applications/integrations/dependency-libs/TestApplication.ContinuousProfiler.Fs/TestApplication.ContinuousProfiler.Fs.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFrameworks>net10.0;net9.0;net8.0</TargetFrameworks>
5+
<TargetFrameworks Condition="'$(OS)' == 'Windows_NT'">$(TargetFrameworks);netstandard2.0</TargetFrameworks>
56
<GenerateDocumentationFile>true</GenerateDocumentationFile>
67
<LangVersion>latest</LangVersion>
78
</PropertyGroup>

test/test-applications/integrations/dependency-libs/TestApplication.ContinuousProfiler.Vb/TestApplication.ContinuousProfiler.Vb.vbproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<PropertyGroup>
44
<RootNamespace>TestApplication.ContinuousProfiler.Vb</RootNamespace>
55
<TargetFrameworks>net10.0;net9.0;net8.0</TargetFrameworks>
6+
<TargetFrameworks Condition="'$(OS)' == 'Windows_NT'">$(TargetFrameworks);net462</TargetFrameworks>
67
<LangVersion>latest</LangVersion>
78
</PropertyGroup>
89

0 commit comments

Comments
 (0)