-
Notifications
You must be signed in to change notification settings - Fork 1
Integration MassTransit Extension
The TestTrackingDiagrams.Extensions.MassTransit package adds MassTransit message tracking to your test diagrams. Send, Publish, and Consume operations appear as classified events in your sequence diagrams — showing the complete message flow across your services.
Because MassTransit is transport-agnostic, this single extension covers RabbitMQ, Azure Service Bus, Amazon SQS, and all other MassTransit-supported transports.
Using a custom messaging abstraction on top of MassTransit? If your code doesn't call
ISendEndpointProvider,IPublishEndpoint, or consume via MassTransit consumers directly — e.g. it goes through a shared messaging library or custom abstraction that wraps MassTransit — this extension's observers should still fire automatically since they hook into the MassTransit pipeline. If they don't (e.g. your abstraction bypasses MassTransit entirely), see Tracking Custom Dependencies for alternative approaches includingMessageTrackerandTrackingProxy<T>.
MassTransit provides observer interfaces (ISendObserver, IPublishObserver, IConsumeObserver) that fire on every message operation. The extension implements these observers to capture and classify each operation, then logs it to RequestResponseLogger with MetaType.Event.
A central MassTransitTracker (implementing ITrackingComponent) handles all logging. The three observer classes are thin adapters that extract context properties and delegate to the tracker.
dotnet add package TestTrackingDiagrams.Extensions.MassTransit| Level | Method shown | URI shown | Headers | Message body |
|---|---|---|---|---|
| Raw | Send OrderCreated → rabbitmq://localhost/orders |
Transport URI | N/A | Full JSON |
| Detailed | Send OrderCreated |
masstransit:///orders-queue |
N/A | Full JSON |
| Summarised |
→ OrderCreated (send/publish) or ← OrderCreated (consume) |
masstransit:///OrderCreated |
N/A | None |
The default is Detailed.
| Operation | Raw | Detailed | Summarised |
|---|---|---|---|
| Send message | Send OrderCreated → rabbitmq://localhost/orders |
Send OrderCreated |
→ OrderCreated |
| Publish event | Publish UserRegistered → rabbitmq://localhost/events |
Publish UserRegistered |
→ UserRegistered |
| Consume message | Consume OrderCreated → rabbitmq://localhost/orders |
Consume OrderCreated |
← OrderCreated |
| Send fault | SendFault OrderCreated → rabbitmq://localhost/orders |
Send Fault OrderCreated |
SendFault |
| Operation | Trigger |
|---|---|
Send |
ISendObserver.PostSend |
Publish |
IPublishObserver.PostPublish |
Consume |
IConsumeObserver.PostConsume |
SendFault |
ISendObserver.SendFault |
PublishFault |
IPublishObserver.PublishFault |
ConsumeFault |
IConsumeObserver.ConsumeFault |
services.AddMassTransit(cfg =>
{
cfg.UsingRabbitMq((ctx, rabbit) =>
{
rabbit.WithTestTracking(new MassTransitTrackingOptions
{
ServiceName = "RabbitMQ",
CallingServiceName = "My API",
Verbosity = MassTransitTrackingVerbosity.Detailed,
CurrentTestInfoFetcher = CurrentTestInfo.Fetcher
});
rabbit.ConfigureEndpoints(ctx);
});
});WithTestTracking connects all three observers (send, publish, consume) to the bus and returns a MassTransitTracker instance.
services.AddMassTransit(cfg =>
{
cfg.UsingAzureServiceBus((ctx, sb) =>
{
sb.WithTestTracking(new MassTransitTrackingOptions
{
ServiceName = "ServiceBus",
CallingServiceName = "My API",
CurrentTestInfoFetcher = () => /* ... */
});
sb.ConfigureEndpoints(ctx);
});
});services.AddMassTransit(cfg =>
{
cfg.UsingInMemory((ctx, mem) =>
{
mem.WithTestTracking(new MassTransitTrackingOptions
{
ServiceName = "MessageBus",
CallingServiceName = "My API",
CurrentTestInfoFetcher = () => /* ... */
});
mem.ConfigureEndpoints(ctx);
});
});If you need more control:
var tracker = new MassTransitTracker(new MassTransitTrackingOptions
{
ServiceName = "RabbitMQ",
CallingServiceName = "My API",
CurrentTestInfoFetcher = () => /* ... */
});
services.AddMassTransit(cfg =>
{
cfg.UsingRabbitMq((ctx, rabbit) =>
{
rabbit.ConnectSendObserver(new TrackingSendObserver(tracker));
rabbit.ConnectPublishObserver(new TrackingPublishObserver(tracker));
rabbit.ConnectConsumeObserver(new TrackingConsumeObserver(tracker));
rabbit.ConfigureEndpoints(ctx);
});
});| Property | Type | Default | Description |
|---|---|---|---|
ServiceName |
string |
"MassTransit" |
The participant name shown in the diagram for the message bus |
CallingServiceName |
string |
"Caller" |
The participant name shown for the service making/receiving messages |
Verbosity |
MassTransitTrackingVerbosity |
Detailed |
Controls how much detail appears in the diagram (Raw, Detailed, Summarised) |
CurrentTestInfoFetcher |
Func<(string Name, string Id)>? |
null |
Returns the current test's name and ID. Required — if null, messages are not logged |
CurrentStepTypeFetcher |
Func<string?>? |
null |
Optional — returns the current BDD step type (Given/When/Then) |
HttpContextAccessor |
IHttpContextAccessor? |
null |
Optional — enables dual-resolution of test identity from HTTP headers. Auto-resolved by DI extensions (v2.26.3+). See HTTP Tracking Setup#Dual-Resolution Test Identity (v2.23.0+) |
v2.23.0+ Dual-Resolution:
MassTransitTrackeraccepts an optionalIHttpContextAccessor? httpContextAccessorconstructor parameter for resolving test identity from HTTP request headers when running inside the SUT's request pipeline. v2.26.3+: SetHttpContextAccessoronMassTransitTrackingOptionsinstead — the tracker reads it automatically. See HTTP Tracking Setup#Dual-Resolution Test Identity (v2.23.0+) for details. |TrackSend|bool|true| Whether to trackISendObserverevents | |TrackPublish|bool|true| Whether to trackIPublishObserverevents | |TrackConsume|bool|true| Whether to trackIConsumeObserverevents | |LogMessageBody|bool|true| Whether to serialize and include message bodies in diagrams | |LogFaults|bool|true| Whether to log fault (exception) events | |SetupVerbosity|MassTransitTrackingVerbosity?|null| Verbosity override for the Setup phase. See Phase-Aware Tracking | |ActionVerbosity|MassTransitTrackingVerbosity?|null| Verbosity override for the Action phase. See Phase-Aware Tracking | |TrackDuringSetup|bool|true| Whenfalse, tracking is suppressed during Setup. See Phase-Aware Tracking | |TrackDuringAction|bool|true| Whenfalse, tracking is suppressed during Action. See Phase-Aware Tracking |
You can enable/disable individual observer types:
new MassTransitTrackingOptions
{
TrackSend = true,
TrackPublish = true,
TrackConsume = false, // Don't track consumer side
LogFaults = false // Don't log fault events
}The extension correctly models message direction in diagrams:
-
Send/Publish — Shown as outgoing from
CallingServiceNametoServiceName - Consume — Shown as incoming: caller and service names are swapped so the arrow points from the bus to the consuming service
This means a publish followed by a consume will show the correct bidirectional flow in the sequence diagram.
MassTransitTracker implements ITrackingComponent and auto-registers with TrackingComponentRegistry on construction. At report generation time, unused components are automatically detected and surfaced as console warnings and in the diagnostic report (when DiagnosticMode=true). This never throws or fails tests.
See Diagnostics and Debugging for full details on the TrackingComponentRegistry API.
Getting Started
Common Tasks
Integration Guides
- Integration xUnit3
- Integration xUnit2
- Integration NUnit
- Integration MSTest
- Integration TUnit
- Integration BDDfy xUnit3
- Integration LightBDD xUnit2
- Integration LightBDD xUnit3
- Integration LightBDD TUnit
- Integration ReqNRoll xUnit2
- Integration ReqNRoll xUnit3
- Integration ReqNRoll TUnit
Extensions
- Integration AtlasDataApi Extension
- Integration BigQuery Extension
- Integration Bigtable Extension
- Integration BlobStorage Extension
- Integration ClickHouse Extension
- Integration CloudStorage Extension
- Integration CosmosDB Extension
- Integration Dapper Extension
- Integration DynamoDB Extension
- Integration EF Core Relational Extension
- Integration Elasticsearch Extension
- Integration EventBridge Extension
- Integration EventHubs Extension
- Integration Grpc Extension
- Integration Kafka Extension
- Integration MassTransit Extension
- Integration MongoDB Extension
- Integration MySqlConnector Extension
- Integration Npgsql Extension
- Integration Oracle Extension
- Integration PubSub Extension
- Integration Redis Extension
- Integration S3 Extension
- Integration ServiceBus Extension
- Integration SNS Extension
- Integration Spanner Extension
- Integration SqlClient Extension
- Integration Sqlite Extension
- Integration SQS Extension
- Integration StorageQueues Extension
- Integration OpenTelemetry Extension
- Integration DispatchProxy Extension
- Integration MediatR Extension
- Integration PlantUML IKVM
Configuration
- Tracking Dependencies
- Tracking Custom Dependencies
- HTTP Tracking Setup
- Report Configuration
- Diagram Customisation
- Phase-Aware Tracking
- Content Formatting
- PlantUML Server Configuration
Features
- Generated Reports
- Search Syntax
- Component Diagrams
- PlantUML Browser Rendering
- Inline SVG Rendering
- Internal Flow Tracking
- Tags and Attributes
- Excluding Requests
- Excluded Headers
- Multi-Host Test Architectures
- Event-Driven Architecture Testing
- Service Bus Tracking Patterns
- Background Thread Correlation
- Parallel-Safe Background Correlation
- Event & Message Tracking
- Assertion Tracking
- Step Tracking
- Tabular Attributes
- Large Response and Diagram Handling
- Diagnostics and Debugging
- CI Summary Integration
- CI Artifact Upload
- Merging Parallel Reports
Reference