-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathSingleOutputService.cs
61 lines (52 loc) · 2.09 KB
/
SingleOutputService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Motor.Extensions.Diagnostics.Telemetry;
using OpenTelemetryExample.Model;
using Motor.Extensions.Hosting.Abstractions;
using Motor.Extensions.Hosting.CloudEvents;
namespace OpenTelemetryExample;
public class SingleOutputService : ISingleOutputService<InputMessage, OutputMessage>
{
private static readonly ActivitySource ActivitySource = new(nameof(OpenTelemetryExample));
// Handle incoming messages
public Task<MotorCloudEvent<OutputMessage>> ConvertMessageAsync(
MotorCloudEvent<InputMessage> inputEvent,
CancellationToken token = default)
{
// Get the input message from the cloud event
var input = inputEvent.TypedData;
if (string.IsNullOrEmpty(input.FancyText))
{
// Reject message in RabbitMQ queue (Any ArgumentException can be used to reject to messages.).
throw new ArgumentNullException("FancyText is empty");
}
// Extract ActivityContext from the incoming CloudEvent
var parentContext = inputEvent.GetActivityContext();
// Create new Activity with extracted ActivityContext as parent
using var activity =
ActivitySource.StartActivity(nameof(MagicFunc), ActivityKind.Consumer, parentContext);
activity?.SetTag("fancyText", input.FancyText);
// Add Trace for MagicFunc with a tag
OutputMessage output;
using (activity?.Start())
{
// Do your magic here .....
output = MagicFunc(input);
}
// Create a new cloud event from your output message which is automatically published and return a new task.
var outputEvent = inputEvent.CreateNew(output);
return Task.FromResult(outputEvent);
}
private static OutputMessage MagicFunc(InputMessage input)
{
var output = new OutputMessage
{
NotSoFancyText = input.FancyText.Reverse().ToString(),
NotSoFancyNumber = input.FancyNumber * -1,
};
return output;
}
}