How to start/continue an activity across boundaries in .NET 8

I have some micro-services running a bunch of workloads, and I’m trying to trace the “route” of a given workload. I’m using .NET Aspire, and have the 3 background worker projects added and a Web API project.

The first background worker starts the activity when a workload arrives. When it’s done, it pushes it into a RabbitMQ queue. I take the Id of the current activity and push that along in the headers of the message. Worker 2 uses the same method when it pushes a message to worker 3.

basicProperties.Headers = new Dictionary<String, Object>();

if (Activity.Current?.Id !=  null) 
{
    basicProperties.Headers.Add(DiagnosticsHandlerLoggingStrings.TraceParentHeaderName, Activity.Current?.Id);
}

In worker 2 and 3 I start an activity in this way:

if (basicProperties.Headers?.TryGetValue(DiagnosticsHandlerLoggingStrings.TraceParentHeaderName, out var parentActivityIdRaw) == true &&
    parentActivityIdRaw is Byte[] traceParentBytes) {
    parentActivityId = Encoding.UTF8.GetString(traceParentBytes);
}

return activitySource.StartActivity("Incoming message from queue", kind: ActivityKind.Consumer, parentId: parentActivityId);

Using HttpClient, the 3rd and last worker calls the Web API.

The trace page in Aspire shows worker 1, worker 3 and the Web API. Not worker 2. And moreover no of the events I add in worker 3 is shown in the details view, only the events from HttpClient.

What is the right way to continue an activity across boundaries? It seems HttpClient and also the IHttpActivityFeature can handle it, and is actually continuing the activity, but when I use Activity.Current in my own code, nothing get put in the events of the activity, at least it’s not shown in the trace details.

Looking at the trace, it seems like I’m getting the activity setup properly, but somehow I can’t add events to it myself, even though HttpClient can.

4

After looking at the link @martindotnet provided, and tinkering a bit, I finally got it working.
This is a solution for RabbitMQ (pre v7), so pushing and popping the trace across the boundaries are done for that, but!

The thing that is important, is what you need to start an activity on the receiving end.

First the consumer/receiver:

var context = Propagators.DefaultTextMapPropagator.Extract(new PropagationContext(
        new ActivityContext(),
         Baggage.Current),
     ea.BasicProperties,
     (properties, key) => {
        if (properties.Headers?.TryGetValue(key, out var propertyValue) == true && propertyValue is Byte[] propertyValueBytes) {
            return new List<String> { Encoding.UTF8.GetString(propertyValueBytes) };
        }
        return new List<String> { };
});

using var activity = this.transparencyObservability.ActivitySource.StartActivity(
    "Consume Message",
    ActivityKind.Consumer,
    context.ActivityContext);
Activity.Current?.AddEvent(new ActivityEvent(
    "Got message from queue, unpacked, ready to handle"));

And the publisher:

var basicProperties = channel.CreateBasicProperties();

Propagators.DefaultTextMapPropagator.Inject(
    new PropagationContext(Activity.Current?.Context ?? default, Baggage.Current),
    basicProperties, (msg, key, value) => {
        msg.Headers.TryAdd(key, value);
});

That’s it. The Propagators are part of OpenTelemetry, so no magic!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật