< Summary

Information
Class: OrderConsumer
Assembly: PaymentService
File(s): /home/runner/work/MicroservicesApp/MicroservicesApp/PaymentService/OrderConsumer.cs
Tag: 23_15739319309
Line coverage
80%
Covered lines: 32
Uncovered lines: 8
Coverable lines: 40
Total lines: 63
Line coverage: 80%
Branch coverage
31%
Covered branches: 5
Total branches: 16
Branch coverage: 31.2%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
.cctor()100%1100%
.ctor(...)100%1100%
Consume()50%10100%

File(s)

/home/runner/work/MicroservicesApp/MicroservicesApp/PaymentService/OrderConsumer.cs

#LineLine coverage
 1using MassTransit;
 2using Contracts;
 3using OpenTelemetry;
 4using OpenTelemetry.Context.Propagation;
 5using System.Diagnostics;
 6using System.Text;
 7
 8public class OrderConsumer : IConsumer<Order>
 9{
 10    private readonly PaymentDbContext _dbContext;
 111    private static readonly ActivitySource ActivitySource = new("PaymentService");
 12
 113    public OrderConsumer(PaymentDbContext dbContext)
 14    {
 115        _dbContext = dbContext;
 116    }
 17
 18    public async Task Consume(ConsumeContext<Order> context)
 19    {
 20        // Extrai o contexto de rastreamento dos headers MassTransit
 121        var parentContext = Propagators.DefaultTextMapPropagator.Extract(
 122            default,
 123            context.Headers,
 124            (headers, key) =>
 125            {
 026                if (headers.TryGetHeader(key, out var value))
 127                {
 028                    return value switch
 029                    {
 030                        string s => new[] { s },
 031                        byte[] b => new[] { Encoding.UTF8.GetString(b) },
 032                        _ => Array.Empty<string>()
 033                    };
 134                }
 035                return Array.Empty<string>();
 136            });
 37
 138        Baggage.Current = parentContext.Baggage;
 139        Activity.Current = null; // 🔥 limpa contexto anterior
 40
 141        using var activity = ActivitySource.StartActivity(
 142            "Consume Order",
 143            ActivityKind.Consumer,
 144            parentContext.ActivityContext);
 45
 146        activity?.SetTag("message.id", context.MessageId);
 147        activity?.SetTag("order.id", context.Message.Id);
 148        activity?.SetTag("order.productName", context.Message.ProductName);
 149        activity?.SetTag("messaging.system", "rabbitmq");
 150        activity?.SetTag("messaging.destination", "payment-service-queue");
 51
 152        var payment = new Payment
 153        {
 154            Id = Guid.NewGuid(),
 155            OrderId = context.Message.Id
 156        };
 57
 158        _dbContext.Payments.Add(payment);
 159        await _dbContext.SaveChangesAsync();
 60
 161        Console.WriteLine($"💰 Pagamento registrado para pedido: {context.Message.ProductName}");
 162    }
 63}