Skip to content

[tracing] add MassTransit integration#8055

Draft
chojomok wants to merge 144 commits intomasterfrom
mohammad/mass_transit_intg
Draft

[tracing] add MassTransit integration#8055
chojomok wants to merge 144 commits intomasterfrom
mohammad/mass_transit_intg

Conversation

@chojomok
Copy link
Copy Markdown
Collaborator

@chojomok chojomok commented Jan 14, 2026

Summary of changes

Adds automatic instrumentation for MassTransit 7.x and 8.x, a popular distributed application framework for .NET. The integration captures distributed traces across message
producers and consumers, propagates trace context across transport boundaries (RabbitMQ, Amazon SQS, In-Memory), and supports both DiagnosticSource (MT7) and
OpenTelemetry Activity (MT8) instrumentation patterns.

Reason for change

MassTransit is a widely-used message bus framework in the .NET ecosystem. This integration provides customers with end-to-end visibility into distributed message-driven
architectures, allowing them to track message flow across services, identify performance bottlenecks, and correlate traces across synchronous HTTP and asynchronous messaging
operations.

Implementation details

MassTransit 7.x:

  • Instruments via DiagnosticSource listeners (MassTransit.ReceivePipe, MassTransit.ConsumeContext)
  • Hooks into ConsumeContext, SendContext, and PublishContext for context propagation
  • Creates RECEIVE and PROCESS spans for incoming messages, SEND spans for outgoing messages

MassTransit 8.x:

  • Leverages MassTransit's built-in OpenTelemetry ActivitySource instrumentation
  • Enhances Activity tags with Datadog-specific metadata
  • Injects/extracts trace context via message headers (x-datadog-*, _datadog)

Key technical decisions:

  • Uses duck typing over reflection for ~30% performance improvement in header access
  • Transport-agnostic design handles RabbitMQ, SQS, and In-Memory
  • For MT8, enriches existing Activities rather than creating new spans to avoid duplication
  • Optimized Activity.Tags iteration (single foreach with switch vs multiple LINQ calls)

Test coverage

  • MassTransit7Tests.cs - DiagnosticSource instrumentation across multiple transports
  • MassTransit8Tests.cs - OpenTelemetry Activity enhancement across multiple transports
  • Package version testing: MT7 (7.0.0 - 7.3.1), MT8 (8.0.3 - 8.5.8)
  • Sample applications for both versions with multi-transport scenarios, saga state machines, and error handling

Other details

Compatibility:

  • MassTransit 7.x: .NET Core 2.1+ (.NET 5, 6, 7, 8, 9, 10)
  • MassTransit 8.x: .NET Framework 4.8 / .NET Core 2.1+ (.NET 5, 6, 7, 8, 9, 10)
  • Tested with RabbitMQ 3.x, LocalStack (SQS),

NEED TO TEST WITH Azure Service Bus !!!

@chojomok chojomok changed the title Mohammad/mass transit intg [tracing] add MassTransit integration Jan 14, 2026
Comment thread tracer/test/Datadog.Trace.ClrProfiler.IntegrationTests/MassTransit7Tests.cs Outdated
@pr-commenter
Copy link
Copy Markdown

pr-commenter Bot commented Jan 14, 2026

Benchmarks

Benchmark execution time: 2026-04-28 19:31:19

Comparing candidate commit 3073dab in PR branch mohammad/mass_transit_intg with baseline commit c2ddea2 in branch master.

Found 0 performance improvements and 0 performance regressions! Performance is the same for 27 metrics, 0 unstable metrics, 56 known flaky benchmarks, 31 flaky benchmarks without significant changes.

Explanation

This is an A/B test comparing a candidate commit's performance against that of a baseline commit. Performance changes are noted in the tables below as:

  • 🟩 = significantly better candidate vs. baseline
  • 🟥 = significantly worse candidate vs. baseline

We compute a confidence interval (CI) over the relative difference of means between metrics from the candidate and baseline commits, considering the baseline as the reference.

If the CI is entirely outside the configured SIGNIFICANT_IMPACT_THRESHOLD (or the deprecated UNCONFIDENCE_THRESHOLD), the change is considered significant.

Feel free to reach out to #apm-benchmarking-platform on Slack if you have any questions.

More details about the CI and significant changes

You can imagine this CI as a range of values that is likely to contain the true difference of means between the candidate and baseline commits.

CIs of the difference of means are often centered around 0%, because often changes are not that big:

---------------------------------(------|---^--------)-------------------------------->
                              -0.6%    0%  0.3%     +1.2%
                                 |          |        |
         lower bound of the CI --'          |        |
sample mean (center of the CI) -------------'        |
         upper bound of the CI ----------------------'

As described above, a change is considered significant if the CI is entirely outside the configured SIGNIFICANT_IMPACT_THRESHOLD (or the deprecated UNCONFIDENCE_THRESHOLD).

For instance, for an execution time metric, this confidence interval indicates a significantly worse performance:

----------------------------------------|---------|---(---------^---------)---------->
                                       0%        1%  1.3%      2.2%      3.1%
                                                  |   |         |         |
       significant impact threshold --------------'   |         |         |
                      lower bound of CI --------------'         |         |
       sample mean (center of the CI) --------------------------'         |
                      upper bound of CI ----------------------------------'

Known flaky benchmarks

These benchmarks are marked as flaky and will not trigger a failure. Modify FLAKY_BENCHMARKS_REGEX to control which benchmarks are marked as flaky.

scenario:Benchmarks.Trace.ActivityBenchmark.StartStopWithChild net6.0

  • 🟩 throughput [+8927.838op/s; +11121.558op/s] or [+7.504%; +9.348%]

scenario:Benchmarks.Trace.AgentWriterBenchmark.WriteAndFlushEnrichedTraces net472

  • 🟥 execution_time [+308.026ms; +313.452ms] or [+152.854%; +155.546%]
  • 🟥 throughput [-45.499op/s; -41.698op/s] or [-8.186%; -7.502%]

scenario:Benchmarks.Trace.AgentWriterBenchmark.WriteAndFlushEnrichedTraces net6.0

  • 🟥 execution_time [+381.264ms; +384.197ms] or [+301.222%; +303.539%]
  • 🟩 throughput [+81.345op/s; +85.496op/s] or [+10.725%; +11.272%]

scenario:Benchmarks.Trace.AgentWriterBenchmark.WriteAndFlushEnrichedTraces netcoreapp3.1

  • 🟥 execution_time [+394.079ms; +396.428ms] or [+348.745%; +350.824%]

scenario:Benchmarks.Trace.Asm.AppSecBodyBenchmark.AllCycleMoreComplexBody net472

  • 🟥 allocated_mem [+1.308KB; +1.308KB] or [+27.529%; +27.541%]

scenario:Benchmarks.Trace.Asm.AppSecBodyBenchmark.AllCycleMoreComplexBody net6.0

  • 🟥 allocated_mem [+471 bytes; +472 bytes] or [+9.977%; +9.987%]
  • 🟩 execution_time [-16.706ms; -12.523ms] or [-7.802%; -5.849%]

scenario:Benchmarks.Trace.Asm.AppSecBodyBenchmark.AllCycleMoreComplexBody netcoreapp3.1

  • 🟥 allocated_mem [+1.272KB; +1.272KB] or [+27.502%; +27.510%]

scenario:Benchmarks.Trace.Asm.AppSecBodyBenchmark.AllCycleSimpleBody net472

  • 🟥 allocated_mem [+1.307KB; +1.307KB] or [+105.746%; +105.759%]
  • 🟥 throughput [-260119.281op/s; -256707.758op/s] or [-26.559%; -26.211%]

scenario:Benchmarks.Trace.Asm.AppSecBodyBenchmark.AllCycleSimpleBody net6.0

  • 🟥 allocated_mem [+471 bytes; +472 bytes] or [+38.558%; +38.566%]
  • 🟩 execution_time [-26.580ms; -21.653ms] or [-11.854%; -9.656%]

scenario:Benchmarks.Trace.Asm.AppSecBodyBenchmark.AllCycleSimpleBody netcoreapp3.1

  • 🟥 allocated_mem [+1.272KB; +1.272KB] or [+105.292%; +105.304%]
  • 🟥 throughput [-134411.815op/s; -118110.907op/s] or [-19.312%; -16.970%]

scenario:Benchmarks.Trace.Asm.AppSecBodyBenchmark.ObjectExtractorMoreComplexBody net6.0

  • 🟩 throughput [+8168.380op/s; +11091.870op/s] or [+5.197%; +7.058%]

scenario:Benchmarks.Trace.Asm.AppSecBodyBenchmark.ObjectExtractorMoreComplexBody netcoreapp3.1

  • 🟩 throughput [+6759.870op/s; +9412.097op/s] or [+5.385%; +7.498%]

scenario:Benchmarks.Trace.Asm.AppSecBodyBenchmark.ObjectExtractorSimpleBody net6.0

  • 🟩 throughput [+362427.401op/s; +379487.086op/s] or [+12.085%; +12.654%]

scenario:Benchmarks.Trace.Asm.AppSecBodyBenchmark.ObjectExtractorSimpleBody netcoreapp3.1

  • 🟩 execution_time [-18.865ms; -14.478ms] or [-8.696%; -6.674%]
  • 🟩 throughput [+182194.582op/s; +237332.628op/s] or [+7.232%; +9.420%]

scenario:Benchmarks.Trace.Asm.AppSecEncoderBenchmark.EncodeArgs net472

  • 🟥 execution_time [+299.996ms; +300.675ms] or [+149.898%; +150.237%]

scenario:Benchmarks.Trace.Asm.AppSecEncoderBenchmark.EncodeArgs net6.0

  • 🟥 execution_time [+299.677ms; +302.950ms] or [+151.128%; +152.778%]

scenario:Benchmarks.Trace.Asm.AppSecEncoderBenchmark.EncodeArgs netcoreapp3.1

  • 🟥 execution_time [+299.905ms; +302.334ms] or [+151.069%; +152.292%]

scenario:Benchmarks.Trace.Asm.AppSecEncoderBenchmark.EncodeLegacyArgs net472

  • 🟥 execution_time [+296.625ms; +297.222ms] or [+145.690%; +145.984%]

scenario:Benchmarks.Trace.Asm.AppSecEncoderBenchmark.EncodeLegacyArgs net6.0

  • 🟥 execution_time [+293.238ms; +295.216ms] or [+143.353%; +144.320%]

scenario:Benchmarks.Trace.Asm.AppSecEncoderBenchmark.EncodeLegacyArgs netcoreapp3.1

  • 🟥 execution_time [+300.480ms; +301.672ms] or [+150.179%; +150.775%]

scenario:Benchmarks.Trace.Asm.AppSecWafBenchmark.RunWafRealisticBenchmarkWithAttack net6.0

  • 🟥 execution_time [+21.270µs; +44.852µs] or [+6.790%; +14.319%]
  • 🟥 throughput [-418.942op/s; -220.488op/s] or [-13.060%; -6.873%]

scenario:Benchmarks.Trace.AspNetCoreBenchmark.SendRequest net472

  • 🟥 execution_time [+300.117ms; +300.764ms] or [+149.789%; +150.112%]

scenario:Benchmarks.Trace.AspNetCoreBenchmark.SendRequest net6.0

  • 🟥 execution_time [+420.743ms; +426.520ms] or [+457.155%; +463.432%]
  • 🟩 throughput [+917.713op/s; +1049.295op/s] or [+7.541%; +8.622%]

scenario:Benchmarks.Trace.AspNetCoreBenchmark.SendRequest netcoreapp3.1

  • unstable execution_time [+285.553ms; +325.036ms] or [+216.818%; +246.796%]
  • 🟩 throughput [+608.602op/s; +809.565op/s] or [+5.892%; +7.837%]

scenario:Benchmarks.Trace.CIVisibilityProtocolWriterBenchmark.WriteAndFlushEnrichedTraces net472

  • unstable execution_time [+317.779ms; +347.385ms] or [+146.112%; +159.724%]
  • 🟥 throughput [-510.999op/s; -479.713op/s] or [-46.301%; -43.467%]

scenario:Benchmarks.Trace.CIVisibilityProtocolWriterBenchmark.WriteAndFlushEnrichedTraces net6.0

  • unstable execution_time [+174.956ms; +311.744ms] or [+74.559%; +132.852%]
  • 🟥 throughput [-668.103op/s; -584.652op/s] or [-44.563%; -38.996%]

scenario:Benchmarks.Trace.CIVisibilityProtocolWriterBenchmark.WriteAndFlushEnrichedTraces netcoreapp3.1

  • 🟥 execution_time [+336.526ms; +346.157ms] or [+201.282%; +207.042%]
  • 🟥 throughput [-405.215op/s; -369.161op/s] or [-28.215%; -25.704%]

scenario:Benchmarks.Trace.CharSliceBenchmark.OriginalCharSlice net6.0

  • 🟩 execution_time [-145.118µs; -105.021µs] or [-7.351%; -5.320%]
  • 🟩 throughput [+30.387op/s; +41.122op/s] or [+5.999%; +8.118%]

scenario:Benchmarks.Trace.ElasticsearchBenchmark.CallElasticsearch net472

  • 🟥 execution_time [+303.148ms; +304.372ms] or [+152.659%; +153.276%]

scenario:Benchmarks.Trace.ElasticsearchBenchmark.CallElasticsearch net6.0

  • 🟥 execution_time [+301.575ms; +311.059ms] or [+151.120%; +155.872%]

scenario:Benchmarks.Trace.ElasticsearchBenchmark.CallElasticsearch netcoreapp3.1

  • 🟥 execution_time [+300.828ms; +303.983ms] or [+151.123%; +152.708%]

scenario:Benchmarks.Trace.ElasticsearchBenchmark.CallElasticsearchAsync net472

  • 🟥 execution_time [+303.268ms; +304.441ms] or [+152.291%; +152.880%]
  • 🟩 throughput [+17218.011op/s; +18715.878op/s] or [+5.768%; +6.270%]

scenario:Benchmarks.Trace.ElasticsearchBenchmark.CallElasticsearchAsync net6.0

  • 🟥 execution_time [+298.698ms; +300.528ms] or [+147.693%; +148.598%]

scenario:Benchmarks.Trace.ElasticsearchBenchmark.CallElasticsearchAsync netcoreapp3.1

  • 🟥 execution_time [+304.124ms; +307.621ms] or [+154.144%; +155.916%]

scenario:Benchmarks.Trace.GraphQLBenchmark.ExecuteAsync net472

  • 🟥 execution_time [+302.007ms; +303.617ms] or [+151.580%; +152.388%]

scenario:Benchmarks.Trace.GraphQLBenchmark.ExecuteAsync net6.0

  • 🟥 execution_time [+301.601ms; +303.593ms] or [+150.320%; +151.313%]
  • 🟩 throughput [+34554.809op/s; +41029.347op/s] or [+6.861%; +8.147%]

scenario:Benchmarks.Trace.GraphQLBenchmark.ExecuteAsync netcoreapp3.1

  • 🟥 execution_time [+299.866ms; +302.426ms] or [+149.181%; +150.454%]

scenario:Benchmarks.Trace.ILoggerBenchmark.EnrichedLog net6.0

  • 🟩 execution_time [-15.896ms; -12.242ms] or [-7.392%; -5.692%]

scenario:Benchmarks.Trace.Iast.StringAspectsBenchmark.StringConcatAspectBenchmark net6.0

  • 🟩 allocated_mem [-20.258KB; -20.237KB] or [-7.390%; -7.382%]
  • unstable execution_time [-48.619µs; +3.740µs] or [-9.609%; +0.739%]

scenario:Benchmarks.Trace.Iast.StringAspectsBenchmark.StringConcatBenchmark net6.0

  • 🟥 execution_time [+5.352µs; +8.906µs] or [+12.651%; +21.052%]
  • 🟥 throughput [-4306.694op/s; -2630.123op/s] or [-18.130%; -11.072%]

scenario:Benchmarks.Trace.Iast.StringAspectsBenchmark.StringConcatBenchmark netcoreapp3.1

  • unstable execution_time [-14.034µs; -7.042µs] or [-21.774%; -10.925%]
  • 🟩 throughput [+1746.032op/s; +3216.490op/s] or [+10.712%; +19.734%]

scenario:Benchmarks.Trace.Log4netBenchmark.EnrichedLog net472

  • 🟥 execution_time [+301.919ms; +303.006ms] or [+152.607%; +153.156%]

scenario:Benchmarks.Trace.Log4netBenchmark.EnrichedLog net6.0

  • 🟥 execution_time [+303.170ms; +305.502ms] or [+154.313%; +155.500%]

scenario:Benchmarks.Trace.Log4netBenchmark.EnrichedLog netcoreapp3.1

  • 🟥 execution_time [+299.763ms; +302.122ms] or [+150.069%; +151.249%]

scenario:Benchmarks.Trace.SerilogBenchmark.EnrichedLog net472

  • 🟥 execution_time [+298.120ms; +299.864ms] or [+148.586%; +149.455%]

scenario:Benchmarks.Trace.SerilogBenchmark.EnrichedLog net6.0

  • 🟥 execution_time [+301.146ms; +302.076ms] or [+151.221%; +151.688%]

scenario:Benchmarks.Trace.SerilogBenchmark.EnrichedLog netcoreapp3.1

  • 🟥 execution_time [+303.987ms; +306.516ms] or [+154.162%; +155.445%]

scenario:Benchmarks.Trace.SingleSpanAspNetCoreBenchmark.SingleSpanAspNetCore net472

  • 🟥 execution_time [+300.597ms; +301.174ms] or [+149.939%; +150.227%]
  • 🟩 throughput [+61112604.685op/s; +61354610.947op/s] or [+44.506%; +44.682%]

scenario:Benchmarks.Trace.SingleSpanAspNetCoreBenchmark.SingleSpanAspNetCore net6.0

  • unstable execution_time [+316.800ms; +367.550ms] or [+393.997%; +457.114%]
  • 🟩 throughput [+1102.068op/s; +1287.824op/s] or [+8.520%; +9.956%]

scenario:Benchmarks.Trace.SingleSpanAspNetCoreBenchmark.SingleSpanAspNetCore netcoreapp3.1

  • 🟥 execution_time [+299.039ms; +299.943ms] or [+149.154%; +149.604%]

scenario:Benchmarks.Trace.SpanBenchmark.StartFinishScope net6.0

  • 🟩 throughput [+75812.286op/s; +88572.229op/s] or [+7.078%; +8.270%]

scenario:Benchmarks.Trace.SpanBenchmark.StartFinishScope netcoreapp3.1

  • 🟩 throughput [+49688.472op/s; +68736.965op/s] or [+5.751%; +7.956%]

scenario:Benchmarks.Trace.SpanBenchmark.StartFinishSpan netcoreapp3.1

  • 🟩 throughput [+69109.346op/s; +78007.657op/s] or [+6.864%; +7.747%]

scenario:Benchmarks.Trace.SpanBenchmark.StartFinishTwoScopes net6.0

  • 🟩 throughput [+58707.530op/s; +63027.382op/s] or [+10.660%; +11.445%]

scenario:Benchmarks.Trace.SpanBenchmark.StartFinishTwoScopes netcoreapp3.1

  • 🟩 throughput [+22901.344op/s; +32468.568op/s] or [+5.126%; +7.268%]

scenario:Benchmarks.Trace.TraceAnnotationsBenchmark.RunOnMethodBegin net6.0

  • 🟩 throughput [+82900.084op/s; +100660.356op/s] or [+9.262%; +11.246%]

Known flaky benchmarks without significant changes:

  • scenario:Benchmarks.Trace.ActivityBenchmark.StartStopWithChild net472
  • scenario:Benchmarks.Trace.ActivityBenchmark.StartStopWithChild netcoreapp3.1
  • scenario:Benchmarks.Trace.Asm.AppSecBodyBenchmark.ObjectExtractorMoreComplexBody net472
  • scenario:Benchmarks.Trace.Asm.AppSecBodyBenchmark.ObjectExtractorSimpleBody net472
  • scenario:Benchmarks.Trace.Asm.AppSecWafBenchmark.RunWafRealisticBenchmark net472
  • scenario:Benchmarks.Trace.Asm.AppSecWafBenchmark.RunWafRealisticBenchmark net6.0
  • scenario:Benchmarks.Trace.Asm.AppSecWafBenchmark.RunWafRealisticBenchmark netcoreapp3.1
  • scenario:Benchmarks.Trace.Asm.AppSecWafBenchmark.RunWafRealisticBenchmarkWithAttack net472
  • scenario:Benchmarks.Trace.Asm.AppSecWafBenchmark.RunWafRealisticBenchmarkWithAttack netcoreapp3.1
  • scenario:Benchmarks.Trace.CharSliceBenchmark.OptimizedCharSlice net472
  • scenario:Benchmarks.Trace.CharSliceBenchmark.OptimizedCharSlice net6.0
  • scenario:Benchmarks.Trace.CharSliceBenchmark.OptimizedCharSlice netcoreapp3.1
  • scenario:Benchmarks.Trace.CharSliceBenchmark.OptimizedCharSliceWithPool net472
  • scenario:Benchmarks.Trace.CharSliceBenchmark.OptimizedCharSliceWithPool net6.0
  • scenario:Benchmarks.Trace.CharSliceBenchmark.OptimizedCharSliceWithPool netcoreapp3.1
  • scenario:Benchmarks.Trace.CharSliceBenchmark.OriginalCharSlice net472
  • scenario:Benchmarks.Trace.CharSliceBenchmark.OriginalCharSlice netcoreapp3.1
  • scenario:Benchmarks.Trace.ILoggerBenchmark.EnrichedLog net472
  • scenario:Benchmarks.Trace.ILoggerBenchmark.EnrichedLog netcoreapp3.1
  • scenario:Benchmarks.Trace.Iast.StringAspectsBenchmark.StringConcatAspectBenchmark net472
  • scenario:Benchmarks.Trace.Iast.StringAspectsBenchmark.StringConcatAspectBenchmark netcoreapp3.1
  • scenario:Benchmarks.Trace.Iast.StringAspectsBenchmark.StringConcatBenchmark net472
  • scenario:Benchmarks.Trace.RedisBenchmark.SendReceive net472
  • scenario:Benchmarks.Trace.RedisBenchmark.SendReceive net6.0
  • scenario:Benchmarks.Trace.RedisBenchmark.SendReceive netcoreapp3.1
  • scenario:Benchmarks.Trace.SpanBenchmark.StartFinishScope net472
  • scenario:Benchmarks.Trace.SpanBenchmark.StartFinishSpan net472
  • scenario:Benchmarks.Trace.SpanBenchmark.StartFinishSpan net6.0
  • scenario:Benchmarks.Trace.SpanBenchmark.StartFinishTwoScopes net472
  • scenario:Benchmarks.Trace.TraceAnnotationsBenchmark.RunOnMethodBegin net472
  • scenario:Benchmarks.Trace.TraceAnnotationsBenchmark.RunOnMethodBegin netcoreapp3.1

}
}

/// <summary>
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When MassTransit uses AWS as a transport layer, we need a way to propagate context from MassTransit to AWS/RabbitMQv.

Not tested yet for AzureServiceBus


var tracer = Tracer.Instance;
var scope = AwsSnsCommon.CreateScope(tracer, sendType.OperationName, SpanKinds.Producer, out var tags);

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For RabbitMQ when I didn't put the current active scope as a priority before checking the headers, it caused some tests to fail.
Should I be doing the same here?

@@ -0,0 +1,1117 @@
[
Copy link
Copy Markdown
Collaborator Author

@chojomok chojomok Apr 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

8.0.4 is the latest version that works before there are issues on how the service name is created from the Activity 8.0.3 releases.
Ideally - I just want to test support for 8.2.1 + just because there are less snapshots to keep track off and MassTransit v9 is coming out. V8 is about to be OLD (but probably still used)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant