Skip to content

Commit ae3b719

Browse files
Create Core_10 Sample
1 parent bebc7c3 commit ae3b719

File tree

10 files changed

+233
-0
lines changed

10 files changed

+233
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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="NServiceBus" Version="10.0.0-alpha.1" />
12+
<PackageReference Include="OpenTelemetry.Exporter.Prometheus.HttpListener" Version="1.*-*" />
13+
</ItemGroup>
14+
15+
</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: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using OpenTelemetry;
2+
using OpenTelemetry.Metrics;
3+
using OpenTelemetry.Resources;
4+
5+
public class Program
6+
{
7+
const string EndpointName = "Samples.OpenTelemetry.Metrics";
8+
public static async Task Main()
9+
{
10+
Console.Title = EndpointName;
11+
12+
var attributes = new Dictionary<string, object>
13+
{
14+
["service.name"] = EndpointName,
15+
["service.instance.id"] = Guid.NewGuid().ToString(),
16+
};
17+
18+
var resourceBuilder = ResourceBuilder.CreateDefault().AddAttributes(attributes);
19+
20+
#region enable-opentelemetry-metrics
21+
var meterProviderBuilder = Sdk.CreateMeterProviderBuilder()
22+
.SetResourceBuilder(resourceBuilder)
23+
.AddMeter("NServiceBus.Core*");
24+
#endregion
25+
26+
#region enable-prometheus-http-listener
27+
meterProviderBuilder.AddPrometheusHttpListener(options => options.UriPrefixes = new[] { "http://127.0.0.1:9464" });
28+
#endregion
29+
30+
var meterProvider = meterProviderBuilder.Build();
31+
32+
#region enable-opentelemetry
33+
var endpointConfiguration = new EndpointConfiguration(EndpointName);
34+
endpointConfiguration.EnableOpenTelemetry();
35+
#endregion
36+
37+
endpointConfiguration.UseSerialization<SystemJsonSerializer>();
38+
endpointConfiguration.UseTransport<LearningTransport>();
39+
40+
var cancellation = new CancellationTokenSource();
41+
var endpointInstance = await Endpoint.Start(endpointConfiguration, cancellation.Token);
42+
43+
#region prometheus-load-simulator
44+
45+
var simulator = new LoadSimulator(endpointInstance, TimeSpan.Zero, TimeSpan.FromSeconds(10));
46+
simulator.Start(cancellation.Token);
47+
48+
#endregion
49+
50+
try
51+
{
52+
Console.WriteLine("Endpoint started. Press any key to send a message. Press ESC to stop");
53+
54+
while (Console.ReadKey(true).Key != ConsoleKey.Escape)
55+
{
56+
57+
await endpointInstance.SendLocal(new SomeMessage(), cancellation.Token);
58+
}
59+
}
60+
finally
61+
{
62+
await simulator.Stop(cancellation.Token);
63+
await endpointInstance.Stop(cancellation.Token);
64+
meterProvider?.Dispose();
65+
}
66+
}
67+
}
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+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Endpoint", "Endpoint\Endpoint.csproj", "{339B8D67-CAB2-416C-8C15-50A757CB5073}"
4+
EndProject
5+
Global
6+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
7+
Debug|Any CPU = Debug|Any CPU
8+
Release|Any CPU = Release|Any CPU
9+
EndGlobalSection
10+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
11+
{339B8D67-CAB2-416C-8C15-50A757CB5073}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
12+
{339B8D67-CAB2-416C-8C15-50A757CB5073}.Debug|Any CPU.Build.0 = Debug|Any CPU
13+
{339B8D67-CAB2-416C-8C15-50A757CB5073}.Release|Any CPU.ActiveCfg = Release|Any CPU
14+
{339B8D67-CAB2-416C-8C15-50A757CB5073}.Release|Any CPU.Build.0 = Release|Any CPU
15+
EndGlobalSection
16+
EndGlobal
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
version: '3'
2+
3+
services:
4+
prometheus:
5+
image: prom/prometheus:v2.21.0
6+
ports:
7+
- 9000:9090
8+
extra_hosts:
9+
- "host.docker.internal:host-gateway"
10+
volumes:
11+
- ./prometheus:/etc/prometheus
12+
- ./data-prometheus:/prometheus
13+
command: --web.enable-lifecycle --config.file=/etc/prometheus/prometheus.yml
14+
15+
grafana:
16+
image: grafana/grafana-oss:latest
17+
ports:
18+
- 3000:3000
19+
restart: unless-stopped
20+
volumes:
21+
- ./grafana/provisioning/datasources:/etc/grafana/provisioning/datasources
22+
- ./data-grafana:/var/lib/grafana
23+
24+
volumes:
25+
prometheus-data:
26+
grafana-data:
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
datasources:
2+
- name: Prometheus
3+
access: proxy
4+
type: prometheus
5+
url: http://prometheus:9090
6+
isDefault: true

samples/open-telemetry/prometheus-grafana/Core_10/prerelease.txt

Whitespace-only changes.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
global:
2+
scrape_interval: 30s
3+
scrape_timeout: 10s
4+
5+
scrape_configs:
6+
- job_name: 'NServiceBus Telemetry'
7+
scrape_interval: 1s
8+
metrics_path: /metrics
9+
static_configs:
10+
- targets:
11+
- 'host.docker.internal:9464'

0 commit comments

Comments
 (0)