Skip to content

Commit 7a98247

Browse files
Merge pull request #7576 from Particular/samples-open-telemetry-customizing-net10
Upgrade Open-Telemetry Customizing Sample to .NET 10
2 parents bd169e8 + 2a8ffde commit 7a98247

13 files changed

+210
-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}") = "Sample", "Sample\Sample.csproj", "{D6DAC310-11C3-4B79-B1E4-AB6078A2E214}"
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+
{D6DAC310-11C3-4B79-B1E4-AB6078A2E214}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{D6DAC310-11C3-4B79-B1E4-AB6078A2E214}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{D6DAC310-11C3-4B79-B1E4-AB6078A2E214}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{D6DAC310-11C3-4B79-B1E4-AB6078A2E214}.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 = {42F58BDA-0C79-41BB-AA96-F03CAF76FB6F}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using NServiceBus.Pipeline;
2+
using System.Diagnostics;
3+
4+
#region add-tags-from-handler-behavior
5+
6+
class TraceCustomExceptionInHandlerBehavior : Behavior<IInvokeHandlerContext>
7+
{
8+
public override async Task Invoke(IInvokeHandlerContext context, Func<Task> next)
9+
{
10+
try
11+
{
12+
await next();
13+
}
14+
catch (MyBusinessException e)
15+
{
16+
Activity.Current?.AddTag("sample.business-exception.reason", e.ReasonCode);
17+
throw;
18+
}
19+
}
20+
}
21+
22+
public class MyBusinessException : Exception
23+
{
24+
public int ReasonCode { get; init; }
25+
}
26+
27+
#endregion
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using NServiceBus.Pipeline;
2+
using System.Diagnostics;
3+
4+
#region add-tags-from-outgoing-behavior
5+
6+
class TraceOutgoingMessageSizeBehavior : Behavior<IOutgoingPhysicalMessageContext>
7+
{
8+
public override Task Invoke(IOutgoingPhysicalMessageContext context, Func<Task> next)
9+
{
10+
Activity.Current?.AddTag("sample.messaging.body.size", context.Body.Length);
11+
return next();
12+
}
13+
}
14+
15+
#endregion
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System.Diagnostics;
2+
3+
#region custom-activity-source
4+
static class CustomActivitySources
5+
{
6+
public const string Name = "Sample.ActivitySource";
7+
public static readonly ActivitySource Main = new(Name);
8+
}
9+
#endregion
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class BillOrderHandler : IHandleMessages<BillOrder>
2+
{
3+
public Task Handle(BillOrder message, IMessageHandlerContext context)
4+
{
5+
Console.WriteLine($"Order billed {message.OrderId}");
6+
return Task.CompletedTask;
7+
}
8+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class CreateOrderHandler : IHandleMessages<CreateOrder>
2+
{
3+
#region custom-activity-in-handler
4+
public async Task Handle(CreateOrder message, IMessageHandlerContext context)
5+
{
6+
using var activity = CustomActivitySources.Main.StartActivity("Billing Order");
7+
8+
if (message.SimulateFailure)
9+
{
10+
throw new MyBusinessException{ ReasonCode = 1};
11+
}
12+
13+
Console.WriteLine($"Billing order {message.OrderId}");
14+
activity?.AddTag("sample.billing.system", "paypal");
15+
// Calculate order cost
16+
await context.SendLocal(new BillOrder { OrderId = message.OrderId });
17+
18+
Console.WriteLine($"Shipping order {message.OrderId}");
19+
await context.SendLocal(new ShipOrder { OrderId = message.OrderId });
20+
}
21+
#endregion
22+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.Diagnostics;
2+
3+
#region add-tags-from-handler
4+
class ShipOrderHandler : IHandleMessages<ShipOrder>
5+
{
6+
public Task Handle(ShipOrder message, IMessageHandlerContext context)
7+
{
8+
Console.WriteLine($"Order shipped {message.OrderId}");
9+
// Figure out what state we are shipping to
10+
Activity.Current?.AddTag("sample.shipping.state", "STATE");
11+
return Task.CompletedTask;
12+
}
13+
}
14+
#endregion
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class BillOrder : IMessage
2+
{
3+
public Guid OrderId { get; set;}
4+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class CreateOrder : IMessage
2+
{
3+
public Guid OrderId { get; set; }
4+
public bool SimulateFailure { get; set; }
5+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class ShipOrder : IMessage
2+
{
3+
public Guid OrderId { get; set;}
4+
}

0 commit comments

Comments
 (0)