Skip to content

Commit bd169e8

Browse files
Merge pull request #7577 from Particular/samples-open-telemetry-appinsights-net10
Upgrade Open-Telemetry Application Insights Sample to .NET 10
2 parents a95c410 + b55f0f8 commit bd169e8

File tree

7 files changed

+205
-0
lines changed

7 files changed

+205
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.2.32616.157
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Endpoint", "Endpoint\Endpoint.csproj", "{E70FE4B3-B277-4483-B66F-4217C22C9D26}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{E70FE4B3-B277-4483-B66F-4217C22C9D26}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{E70FE4B3-B277-4483-B66F-4217C22C9D26}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{E70FE4B3-B277-4483-B66F-4217C22C9D26}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{E70FE4B3-B277-4483-B66F-4217C22C9D26}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {3E6F6C3D-DA9F-4A8B-B43A-FD9A6F678A67}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net10.0</TargetFramework>
6+
<LangVersion>preview</LangVersion>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.*" />
12+
<PackageReference Include="Azure.Monitor.OpenTelemetry.Exporter" Version="1.*" />
13+
<PackageReference Include="NServiceBus" Version="10.0.0-alpha.1" />
14+
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.*" />
15+
</ItemGroup>
16+
17+
</Project>
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Simulates busy (almost no delay) / quite time in a sine wave
2+
class LoadSimulator
3+
{
4+
public LoadSimulator(IEndpointInstance endpointInstance, TimeSpan minimumDelay, TimeSpan idleDuration)
5+
{
6+
this.endpointInstance = endpointInstance;
7+
this.minimumDelay = minimumDelay;
8+
this.idleDuration = TimeSpan.FromTicks(idleDuration.Ticks / 2);
9+
}
10+
11+
public void Start(CancellationToken cancellationToken)
12+
{
13+
_ = Task.Run(() => Loop(cancellationToken), cancellationToken);
14+
}
15+
16+
async Task Loop(CancellationToken cancellationToken)
17+
{
18+
try
19+
{
20+
while (!cancellationToken.IsCancellationRequested)
21+
{
22+
await Work(cancellationToken);
23+
var delay = NextDelay();
24+
await Task.Delay(delay, cancellationToken);
25+
}
26+
}
27+
catch (OperationCanceledException)
28+
{
29+
}
30+
}
31+
32+
TimeSpan NextDelay()
33+
{
34+
var angleInRadians = Math.PI / 180.0 * ++index;
35+
var delay = TimeSpan.FromMilliseconds(idleDuration.TotalMilliseconds * Math.Sin(angleInRadians));
36+
delay += idleDuration;
37+
delay += minimumDelay;
38+
return delay;
39+
}
40+
41+
Task Work(CancellationToken cancellationToken)
42+
{
43+
var sendOptions = new SendOptions();
44+
45+
sendOptions.RouteToThisEndpoint();
46+
47+
if (Random.Shared.Next(100) <= 10)
48+
{
49+
sendOptions.SetHeader("simulate-immediate-retry", bool.TrueString);
50+
}
51+
52+
if (Random.Shared.Next(100) <= 5)
53+
{
54+
sendOptions.SetHeader("simulate-failure", bool.TrueString);
55+
}
56+
57+
return endpointInstance.Send(new SomeMessage(), sendOptions, cancellationToken);
58+
}
59+
60+
public Task Stop(CancellationToken cancellationToken)
61+
{
62+
return Task.CompletedTask;
63+
}
64+
65+
IEndpointInstance endpointInstance;
66+
TimeSpan minimumDelay;
67+
TimeSpan idleDuration;
68+
int index;
69+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using Azure.Monitor.OpenTelemetry.Exporter;
2+
using OpenTelemetry;
3+
using OpenTelemetry.Resources;
4+
using OpenTelemetry.Trace;
5+
using OpenTelemetry.Metrics;
6+
7+
var endpointName = "Samples.OpenTelemetry.AppInsights";
8+
9+
Console.Title = endpointName;
10+
11+
var attributes = new Dictionary<string, object>
12+
{
13+
["service.name"] = endpointName,
14+
["service.instance.id"] = Guid.NewGuid().ToString(),
15+
};
16+
17+
var appInsightsConnectionString = "<YOUR CONNECTION STRING HERE>";
18+
19+
var resourceBuilder = ResourceBuilder.CreateDefault().AddAttributes(attributes);
20+
21+
#region enable-tracing
22+
23+
var traceProvider = Sdk.CreateTracerProviderBuilder()
24+
.SetResourceBuilder(resourceBuilder)
25+
.AddSource("NServiceBus.Core*")
26+
.AddAzureMonitorTraceExporter(o => o.ConnectionString = appInsightsConnectionString)
27+
.AddConsoleExporter()
28+
.Build();
29+
30+
#endregion
31+
32+
#region enable-meters
33+
34+
var meterProvider = Sdk.CreateMeterProviderBuilder()
35+
.SetResourceBuilder(resourceBuilder)
36+
.AddMeter("NServiceBus.Core*")
37+
.AddAzureMonitorMetricExporter(o => o.ConnectionString = appInsightsConnectionString)
38+
.AddConsoleExporter()
39+
.Build();
40+
41+
#endregion
42+
43+
#region enable-open-telemetry
44+
var endpointConfiguration = new EndpointConfiguration(endpointName);
45+
endpointConfiguration.EnableOpenTelemetry();
46+
#endregion
47+
48+
endpointConfiguration.UseSerialization<SystemJsonSerializer>();
49+
endpointConfiguration.UseTransport<LearningTransport>();
50+
var cancellation = new CancellationTokenSource();
51+
var endpointInstance = await Endpoint.Start(endpointConfiguration, cancellation.Token);
52+
53+
var simulator = new LoadSimulator(endpointInstance, TimeSpan.Zero, TimeSpan.FromSeconds(10));
54+
simulator.Start(cancellation.Token);
55+
56+
try
57+
{
58+
Console.WriteLine("Endpoint started. Press any key to send a message. Press ESC to stop");
59+
60+
while (Console.ReadKey(true).Key != ConsoleKey.Escape)
61+
{
62+
await endpointInstance.SendLocal(new SomeMessage(), cancellation.Token);
63+
}
64+
}
65+
finally
66+
{
67+
await simulator.Stop(cancellation.Token);
68+
await endpointInstance.Stop(cancellation.Token);
69+
traceProvider?.Dispose();
70+
meterProvider?.Dispose();
71+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class SomeMessage : IMessage
2+
{
3+
4+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class SomeMessageHandler : IHandleMessages<SomeMessage>
2+
{
3+
public async Task Handle(SomeMessage message, IMessageHandlerContext context)
4+
{
5+
await Task.Delay(Random.Shared.Next(50, 250), context.CancellationToken);
6+
7+
if (context.MessageHeaders.ContainsKey("simulate-failure"))
8+
{
9+
throw new Exception("Simulated failure");
10+
}
11+
12+
if (context.MessageHeaders.ContainsKey("simulate-immediate-retry") && !context.MessageHeaders.ContainsKey(Headers.ImmediateRetries))
13+
{
14+
throw new Exception("Simulated immediate retry");
15+
}
16+
17+
Console.WriteLine("Message processed");
18+
}
19+
}

samples/open-telemetry/application-insights/Core_10/prerelease.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)