forked from open-telemetry/opentelemetry-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessageReceiver.cs
More file actions
103 lines (84 loc) · 3.41 KB
/
MessageReceiver.cs
File metadata and controls
103 lines (84 loc) · 3.41 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Text;
using Microsoft.Extensions.Logging;
using OpenTelemetry;
using OpenTelemetry.Context.Propagation;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
namespace Utils.Messaging;
public sealed class MessageReceiver : IDisposable
{
private static readonly ActivitySource ActivitySource = new(nameof(MessageReceiver));
private static readonly TextMapPropagator Propagator = Propagators.DefaultTextMapPropagator;
private readonly ILogger<MessageReceiver> logger;
private IConnection? connection;
private IChannel? channel;
public MessageReceiver(ILogger<MessageReceiver> logger)
{
this.logger = logger;
}
public void Dispose()
{
this.channel?.Dispose();
this.connection?.Dispose();
}
public async Task StartConsumerAsync()
{
this.connection = await RabbitMqHelper.CreateConnectionAsync().ConfigureAwait(false);
this.channel = await RabbitMqHelper.CreateModelAndDeclareTestQueueAsync(this.connection).ConfigureAwait(false);
await RabbitMqHelper.StartConsumerAsync(this.channel, this.ReceiveMessageAsync).ConfigureAwait(false);
}
public async Task ReceiveMessageAsync(BasicDeliverEventArgs ea)
{
this.EnsureStarted();
ArgumentNullException.ThrowIfNull(ea);
// Extract the PropagationContext of the upstream parent from the message headers.
var parentContext = Propagator.Extract(default, ea.BasicProperties, this.ExtractTraceContextFromBasicProperties);
Baggage.Current = parentContext.Baggage;
// Start an activity with a name following the semantic convention of the OpenTelemetry messaging specification.
// https://github.com/open-telemetry/semantic-conventions/blob/main/docs/messaging/messaging-spans.md#span-name
var activityName = $"{ea.RoutingKey} receive";
using var activity = ActivitySource.StartActivity(activityName, ActivityKind.Consumer, parentContext.ActivityContext);
try
{
var message = Encoding.UTF8.GetString(ea.Body.Span.ToArray());
this.logger.MessageReceived(message);
activity?.SetTag("message", message);
// The OpenTelemetry messaging specification defines a number of attributes. These attributes are added here.
RabbitMqHelper.AddMessagingTags(activity);
// Simulate some work
await Task.Delay(1_000).ConfigureAwait(false);
}
catch (Exception ex)
{
this.logger.MessageProcessingFailed(ex);
}
}
private IEnumerable<string> ExtractTraceContextFromBasicProperties(IReadOnlyBasicProperties props, string key)
{
try
{
if (props.Headers?.TryGetValue(key, out var value) is true)
{
var bytes = (byte[])value!;
return [Encoding.UTF8.GetString(bytes)];
}
}
catch (Exception ex)
{
this.logger.FailedToExtractTraceContext(ex);
}
return [];
}
[MemberNotNull(nameof(channel), nameof(connection))]
private void EnsureStarted()
{
if (this.channel == null || this.connection == null)
{
throw new InvalidOperationException("The message receiver has not been started.");
}
}
}