| | 1 | | using System.Diagnostics; |
| | 2 | | using System.Text; |
| | 3 | | using System.Text.Json; |
| | 4 | | using Microsoft.AspNetCore.Http.Json; |
| | 5 | | using Microsoft.Extensions.Options; |
| | 6 | | using OpenTelemetry; |
| | 7 | | using OpenTelemetry.Context.Propagation; |
| | 8 | | using RabbitMQ.Client; |
| | 9 | | using RabbitMQ.Client.Events; |
| | 10 | |
|
| | 11 | | namespace Gateway.Apis.Amqp.RegistrationExtensions |
| | 12 | | { |
| | 13 | | public static class QueueConsumerFactory |
| | 14 | | { |
| 0 | 15 | | private static readonly TextMapPropagator Propagator = Propagators.DefaultTextMapPropagator; |
| 0 | 16 | | private static readonly ActivitySource ActivitySource = new(nameof(QueueConsumerFactory)); |
| | 17 | |
|
| | 18 | | public static void CreateQueueConsumer<TMessage>( |
| | 19 | | IServiceProvider service, |
| | 20 | | IModel channel, |
| | 21 | | string queueName |
| | 22 | | ) |
| | 23 | | { |
| 0 | 24 | | channel.QueueDeclare( |
| 0 | 25 | | queue: queueName, |
| 0 | 26 | | durable: false, |
| 0 | 27 | | exclusive: false, |
| 0 | 28 | | autoDelete: false, |
| 0 | 29 | | arguments: null |
| 0 | 30 | | ); |
| 0 | 31 | | var consumer = new AsyncEventingBasicConsumer(channel); |
| 0 | 32 | | consumer.Received += async (model, ea) => |
| 0 | 33 | | { |
| 0 | 34 | | using (Activity? activity = CreateActivity(queueName, ea)) |
| 0 | 35 | | { |
| 0 | 36 | | using var serviceScope = service.CreateAsyncScope(); |
| 0 | 37 | | var jsonOptions = serviceScope |
| 0 | 38 | | .ServiceProvider |
| 0 | 39 | | .GetRequiredService<IOptions<JsonOptions>>(); |
| 0 | 40 | | var messageHandler = serviceScope |
| 0 | 41 | | .ServiceProvider |
| 0 | 42 | | .GetRequiredService<IConsumer<TMessage>>(); |
| 0 | 43 | | var body = ea.Body.ToArray(); |
| 0 | 44 | | var message = JsonSerializer.Deserialize<TMessage>( |
| 0 | 45 | | ea.Body.ToArray(), |
| 0 | 46 | | jsonOptions.Value.SerializerOptions |
| 0 | 47 | | ); |
| 0 | 48 | | await messageHandler.RunAsync(message!, CancellationToken.None); |
| 0 | 49 | | } |
| 0 | 50 | | }; |
| | 51 | |
|
| 0 | 52 | | channel.BasicConsume(queue: "hello", autoAck: true, consumer: consumer); |
| 0 | 53 | | } |
| | 54 | |
|
| | 55 | | private static Activity? CreateActivity(string queueName, BasicDeliverEventArgs ea) |
| | 56 | | { |
| 0 | 57 | | var parentContext = Propagator.Extract( |
| 0 | 58 | | default, |
| 0 | 59 | | ea.BasicProperties, |
| 0 | 60 | | ExtractTraceContextFromBasicProperties |
| 0 | 61 | | ); |
| 0 | 62 | | Baggage.Current = parentContext.Baggage; |
| 0 | 63 | | var activityName = $"{ea.RoutingKey} receive"; |
| 0 | 64 | | var activity = ActivitySource.StartActivity( |
| 0 | 65 | | activityName, |
| 0 | 66 | | ActivityKind.Consumer, |
| 0 | 67 | | parentContext.ActivityContext |
| 0 | 68 | | ); |
| 0 | 69 | | AddMessagingTags(activity, queueName, ea.Exchange); |
| 0 | 70 | | return activity; |
| | 71 | | } |
| | 72 | |
|
| | 73 | | private static IEnumerable<string> ExtractTraceContextFromBasicProperties( |
| | 74 | | IBasicProperties props, |
| | 75 | | string key |
| | 76 | | ) |
| | 77 | | { |
| 0 | 78 | | if (props.Headers.TryGetValue(key, out var value)) |
| | 79 | | { |
| 0 | 80 | | var bytes = value as byte[]; |
| 0 | 81 | | return [Encoding.UTF8.GetString(bytes!)]; |
| | 82 | | } |
| 0 | 83 | | return []; |
| | 84 | | } |
| | 85 | |
|
| | 86 | | private static void AddMessagingTags( |
| | 87 | | Activity? activity, |
| | 88 | | string queueName, |
| | 89 | | string exchangeName |
| | 90 | | ) |
| | 91 | | { |
| 0 | 92 | | activity?.SetTag("messaging.system", "rabbitmq"); |
| 0 | 93 | | activity?.SetTag("messaging.destination_kind", "queue"); |
| 0 | 94 | | activity?.SetTag("messaging.destination", queueName); |
| 0 | 95 | | activity?.SetTag("messaging.rabbitmq.routing_key", exchangeName); |
| 0 | 96 | | } |
| | 97 | | } |
| | 98 | | } |