Skip to content

Commit 7397248

Browse files
committed
Refactor LoadSimulator to use IMessageSession and update DataDogMetrics setup
1 parent 7fb481c commit 7397248

File tree

3 files changed

+27
-13
lines changed

3 files changed

+27
-13
lines changed

samples/logging/datadog/Core_10/Endpoint/DataDogMetrics.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ static string[] ComposeTags(string messageType)
8686
return tags.ToArray();
8787
}
8888

89-
static readonly Dictionary<string, string> nameMapping = new Dictionary<string, string>
90-
{
89+
static readonly Dictionary<string, string> nameMapping = new()
90+
{
9191
{"# of msgs successfully processed / sec", "nservicebus.processed"},
9292
{"# of msgs pulled from the input queue /sec", "nservicebus.fetched"},
9393
{"# of msgs failures / sec", "nservicebus.failed"},

samples/logging/datadog/Core_10/Endpoint/LoadSimulator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using NServiceBus;
55

66
// Simulates busy (almost no delay) / quiet time in a sine wave
7-
class LoadSimulator(IEndpointInstance endpointInstance, TimeSpan minimumDelay, TimeSpan idleDuration)
7+
class LoadSimulator(IMessageSession messageSession, TimeSpan minimumDelay, TimeSpan idleDuration)
88
{
99
CancellationTokenSource tokenSource = new();
1010
TimeSpan idleDuration = TimeSpan.FromTicks(idleDuration.Ticks / 2);
@@ -43,7 +43,7 @@ TimeSpan NextDelay()
4343
return delay;
4444
}
4545

46-
Task Work() => endpointInstance.SendLocal(new SomeCommand());
46+
Task Work() => messageSession.SendLocal(new SomeCommand());
4747

4848
public Task Stop()
4949
{
Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,44 @@
11
using System;
2+
using Microsoft.Extensions.DependencyInjection;
23
using NServiceBus;
4+
using Microsoft.Extensions.Hosting;
35

46
const string EndpointName = "Samples.Metrics.Tracing.Endpoint";
57

68
Console.Title = EndpointName;
7-
var endpointConfiguration = new EndpointConfiguration(EndpointName);
89

9-
endpointConfiguration.UseSerialization<SystemJsonSerializer>();
10-
endpointConfiguration.UseTransport<LearningTransport>();
10+
var host = Host.CreateDefaultBuilder(args)
11+
.UseConsoleLifetime()
12+
.UseNServiceBus(_ =>
13+
{
14+
var endpointConfiguration = new EndpointConfiguration(EndpointName);
15+
16+
endpointConfiguration.UseSerialization<SystemJsonSerializer>();
17+
endpointConfiguration.UseTransport<LearningTransport>();
18+
19+
DataDogMetrics.Setup(endpointConfiguration);
20+
21+
return endpointConfiguration;
22+
})
23+
.Build();
1124

12-
DataDogMetrics.Setup(endpointConfiguration);
25+
await host.StartAsync();
1326

14-
var endpointInstance = await Endpoint.Start(endpointConfiguration);
27+
var endpointInstance = host.Services.GetRequiredService<IMessageSession>();
1528

1629
var simulator = new LoadSimulator(endpointInstance, TimeSpan.Zero, TimeSpan.FromSeconds(10));
1730
await simulator.Start();
1831

1932
try
2033
{
21-
Console.WriteLine("Endpoint started. Press 'enter' to send a message");
22-
Console.WriteLine("Press ESC key to quit");
34+
Console.WriteLine("Endpoint started.");
35+
Console.WriteLine("Press [ENTER] to send additional messages.");
36+
Console.WriteLine("Press [Q] to quit.");
2337

2438
while (true)
2539
{
2640
var key = Console.ReadKey(true);
27-
if (key.Key == ConsoleKey.Escape)
41+
if (key.Key == ConsoleKey.Q)
2842
{
2943
break;
3044
}
@@ -35,5 +49,5 @@
3549
finally
3650
{
3751
await simulator.Stop();
38-
await endpointInstance.Stop();
52+
await host.StopAsync();
3953
}

0 commit comments

Comments
 (0)