Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions test/IntegrationTests/ConsoleProfileExporterHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#if NET
using System.Text.Json;

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

var threadSamples = new List<ConsoleThreadSample>();
foreach (var batch in deserializedSampleBatches)
Expand All @@ -23,5 +22,3 @@ public static List<ConsoleThreadSample> ExtractSamples(string output)
return threadSamples;
}
}

#endif
7 changes: 0 additions & 7 deletions test/IntegrationTests/ConsoleThreadSample.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#if NET
namespace IntegrationTests;

internal class ConsoleThreadSample
Expand All @@ -16,11 +15,5 @@ internal class ConsoleThreadSample

public string? ThreadName { get; set; }

public uint ThreadIndex { get; set; }

public bool SelectedForFrequentSampling { get; set; }

public IList<string> Frames { get; set; } = new List<string>();
}

#endif
27 changes: 21 additions & 6 deletions test/IntegrationTests/Helpers/MockProfilesCollector.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#if NET

using System.Collections.Concurrent;
using System.Text;
using Microsoft.AspNetCore.Http;
using OpenTelemetry.Proto.Collector.Profiles.V1Development;
using OpenTelemetry.Proto.Profiles.V1Development;
using Xunit.Abstractions;

#if NETFRAMEWORK
using System.Net;
#else
using Microsoft.AspNetCore.Http;
#endif

namespace IntegrationTests.Helpers;

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

public MockProfilesCollector(ITestOutputHelper output)
public MockProfilesCollector(ITestOutputHelper output, string host = "localhost")
{
_output = output;
#if NETFRAMEWORK
_listener = new(output, HandleHttpRequests, host, "/v1/metrics/");
#else
_listener = new(output, nameof(MockProfilesCollector), new PathHandler(HandleHttpRequests, "/v1development/profiles"));
#endif
}

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

#if NETFRAMEWORK
private void HandleHttpRequests(HttpListenerContext ctx)
{
var metricsMessage = ExportProfilesServiceRequest.Parser.ParseFrom(ctx.Request.InputStream);
HandleProfilesMessage(metricsMessage);

ctx.GenerateEmptyProtobufResponse<ExportProfilesServiceResponse>();
}
#else
private async Task HandleHttpRequests(HttpContext ctx)
{
using var bodyStream = await ctx.ReadBodyToMemoryAsync();
Expand All @@ -175,6 +191,7 @@ private async Task HandleHttpRequests(HttpContext ctx)

await ctx.GenerateEmptyProtobufResponseAsync<ExportProfilesServiceResponse>();
}
#endif

private void HandleProfilesMessage(ExportProfilesServiceRequest profileMessage)
{
Expand Down Expand Up @@ -233,5 +250,3 @@ public CollectedExpectation(Func<ICollection<ExportProfilesServiceRequest>, bool
public string? Description { get; }
}
}

#endif
2 changes: 0 additions & 2 deletions test/IntegrationTests/Helpers/TestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,10 @@ public void SetFileBasedExporter(MockLogsCollector collector)
SetEnvironmentVariable("OTEL_EXPORTER_OTLP_LOGS_ENDPOINT", $"http://localhost:{collector.Port}/v1/logs");
}

#if NET
public void SetExporter(MockProfilesCollector collector)
{
SetEnvironmentVariable("OTEL_EXPORTER_OTLP_ENDPOINT", $"http://localhost:{collector.Port}");
}
#endif

public void EnableBytecodeInstrumentation()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net10.0;net9.0;net8.0</TargetFrameworks>
<!-- SA1518 needed for files generated by Grpc.Tools -->
<NoWarn>SA1518;$(NoWarn)</NoWarn>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static void MethodA()

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

Expand All @@ -48,8 +48,13 @@ public static void MethodABytes(
int.MaxValue,
ulong.MaxValue,
long.MaxValue,
#if NET
nint.MaxValue,
nuint.MaxValue);
#else
0x7fffffff,
0xffffffff);
#endif
}

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

#if NET
[DllImport("TestApplication.ContinuousProfiler.NativeDep")]
#else
[DllImport("TestApplication.ContinuousProfiler.NativeDep.dll")]
#endif
private static extern int OTelAutoCallbackTest(Callback fp, int n);

internal static class InternalClassB<TA, TD>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ public class OtlpOverHttpExporter
public OtlpOverHttpExporter(TimeSpan cpuPeriod, SampleNativeFormatParser parser)
{
_parser = parser;
#if NET
this.cpuPeriod = (long)cpuPeriod.TotalNanoseconds;
#else
this.cpuPeriod = cpuPeriod.Ticks * 100L; // convert to nanoseconds
#endif
}

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

if (!string.IsNullOrEmpty(threadSample.ThreadName))
{
extendedPprofBuilder.AddAttribute(sampleBuilder, "thread.name", threadSample.ThreadName);
extendedPprofBuilder.AddAttribute(sampleBuilder, "thread.name", threadSample.ThreadName!);
}

return sampleBuilder;
Expand Down Expand Up @@ -255,7 +259,11 @@ private static ScopeProfiles CreateScopeProfiles()

private HttpResponseMessage SendHttpRequest(HttpRequestMessage request, CancellationToken cancellationToken)
{
#if NET
return _httpClient.Send(request, cancellationToken);
#else
return _httpClient.SendAsync(request, cancellationToken).GetAwaiter().GetResult();
#endif
}

private HttpContent CreateHttpContent(ExportProfilesServiceRequest exportRequest)
Expand Down Expand Up @@ -290,10 +298,12 @@ public ExportRequestContent(ExportProfilesServiceRequest exportRequest)
Headers.ContentType = ProtobufMediaTypeHeader;
}

#if NET
protected override void SerializeToStream(Stream stream, TransportContext? context, CancellationToken cancellationToken)
{
SerializeToStreamInternal(stream);
}
#endif

protected override Task SerializeToStreamAsync(Stream stream, TransportContext? context)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net10.0;net9.0;net8.0</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<!-- SA1518 needed for files generated by Grpc.Tools -->
<NoWarn>SA1518;$(NoWarn)</NoWarn>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<TargetFrameworks>net10.0;net9.0;net8.0</TargetFrameworks>
<TargetFrameworks Condition="'$(OS)' == 'Windows_NT'">$(TargetFrameworks);netstandard2.0</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<LangVersion>latest</LangVersion>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<RootNamespace>TestApplication.ContinuousProfiler.Vb</RootNamespace>
<TargetFrameworks>net10.0;net9.0;net8.0</TargetFrameworks>
<TargetFrameworks Condition="'$(OS)' == 'Windows_NT'">$(TargetFrameworks);net462</TargetFrameworks>
<LangVersion>latest</LangVersion>
</PropertyGroup>

Expand Down
Loading