Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for RabbitMQ v7 #6479

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open

Add support for RabbitMQ v7 #6479

wants to merge 10 commits into from

Conversation

andrewlock
Copy link
Member

@andrewlock andrewlock commented Dec 20, 2024

Summary of changes

Adds support for RabbitMQ v7

Reason for change

We want to support the latest versions of packages. 7.x.x was released ~6 weeks ago, with significant changes to both the public API and the internal API (which is probably why it hasn't been explicitly requested yet).

Implementation details

This one took a lot of work. In summary:

  • Basically all the integrations had to be rewritten to work with the new async APIs
    • Where possible, simply called into the original sync integration (other than where the work is trivial)
  • Some challenges, in that the duck types had to change.
    • Most problematic was the BasicProperties because now IReadOnlyBasicProperties is null, which means we can't easily add the headers if they're not provided (to propagate context).
    • Worked around it with an additional instrumentation, but I'm not super happy about it 😅
  • Had to completely rewrite the sample app to use the new async APIs
    • Originally I tried to #if RABBITMQ_7_0 my way in the existing methods, but was impossible to follow, so split into -pre and -post v7 methods instead.
    • Tweaked some of the pre-V7 methods in the sample to be async to improve reuse

Test coverage

We get the same overall snapshots for v7 as we do for v6, so 🤞 Did an "all TFMs, all versions" run here (and a previous one for DSM)

Other details

Stacked on:

Note

This PR actually highlighted a current limitation in our CallTarget instrumentation. If an API uses ValueTask, which comes from a library rather than being built-in (i.e. it's netfx or < .NET Core 3.1) then we don't instrument it correctly. Originally I worked around it, but there's a "proper" fix in #6480. Assuming that actually works and is merged, then we don't need the workaround, otherwise I can re-enable it.

@andrewlock andrewlock added area:tracer The core tracer library (Datadog.Trace, does not include OpenTracing, native code, or integrations) area:automatic-instrumentation Automatic instrumentation managed C# code (Datadog.Trace.ClrProfiler.Managed) labels Dec 20, 2024
@andrewlock andrewlock requested review from a team as code owners December 20, 2024 17:04
@andrewlock
Copy link
Member Author

andrewlock commented Dec 20, 2024

Execution-Time Benchmarks Report ⏱️

Execution-time results for samples comparing the following branches/commits:

Execution-time benchmarks measure the whole time it takes to execute a program. And are intended to measure the one-off costs. Cases where the execution time results for the PR are worse than latest master results are shown in red. The following thresholds were used for comparing the execution times:

  • Welch test with statistical test for significance of 5%
  • Only results indicating a difference greater than 5% and 5 ms are considered.

Note that these results are based on a single point-in-time result for each branch. For full results, see the dashboard.

Graphs show the p99 interval based on the mean and StdDev of the test run, as well as the mean value of the run (shown as a diamond below the graph).

gantt
    title Execution time (ms) FakeDbCommand (.NET Framework 4.6.2) 
    dateFormat  X
    axisFormat %s
    todayMarker off
    section Baseline
    This PR (6479) - mean (69ms)  : 66, 72
     .   : milestone, 69,
    master - mean (69ms)  : 66, 72
     .   : milestone, 69,

    section CallTarget+Inlining+NGEN
    This PR (6479) - mean (981ms)  : 959, 1003
     .   : milestone, 981,
    master - mean (975ms)  : 950, 1001
     .   : milestone, 975,

Loading
gantt
    title Execution time (ms) FakeDbCommand (.NET Core 3.1) 
    dateFormat  X
    axisFormat %s
    todayMarker off
    section Baseline
    This PR (6479) - mean (107ms)  : 105, 109
     .   : milestone, 107,
    master - mean (108ms)  : 105, 110
     .   : milestone, 108,

    section CallTarget+Inlining+NGEN
    This PR (6479) - mean (681ms)  : 669, 694
     .   : milestone, 681,
    master - mean (679ms)  : 664, 694
     .   : milestone, 679,

Loading
gantt
    title Execution time (ms) FakeDbCommand (.NET 6) 
    dateFormat  X
    axisFormat %s
    todayMarker off
    section Baseline
    This PR (6479) - mean (91ms)  : 89, 93
     .   : milestone, 91,
    master - mean (91ms)  : 90, 93
     .   : milestone, 91,

    section CallTarget+Inlining+NGEN
    This PR (6479) - mean (636ms)  : 620, 652
     .   : milestone, 636,
    master - mean (635ms)  : 619, 651
     .   : milestone, 635,

Loading
gantt
    title Execution time (ms) HttpMessageHandler (.NET Framework 4.6.2) 
    dateFormat  X
    axisFormat %s
    todayMarker off
    section Baseline
    This PR (6479) - mean (190ms)  : 186, 193
     .   : milestone, 190,
    master - mean (194ms)  : 189, 198
     .   : milestone, 194,

    section CallTarget+Inlining+NGEN
    This PR (6479) - mean (1,092ms)  : 1064, 1119
     .   : milestone, 1092,
    master - mean (1,094ms)  : 1067, 1122
     .   : milestone, 1094,

Loading
gantt
    title Execution time (ms) HttpMessageHandler (.NET Core 3.1) 
    dateFormat  X
    axisFormat %s
    todayMarker off
    section Baseline
    This PR (6479) - mean (275ms)  : 271, 280
     .   : milestone, 275,
    master - mean (277ms)  : 273, 281
     .   : milestone, 277,

    section CallTarget+Inlining+NGEN
    This PR (6479) - mean (868ms)  : 843, 893
     .   : milestone, 868,
    master - mean (871ms)  : 849, 892
     .   : milestone, 871,

Loading
gantt
    title Execution time (ms) HttpMessageHandler (.NET 6) 
    dateFormat  X
    axisFormat %s
    todayMarker off
    section Baseline
    This PR (6479) - mean (264ms)  : 260, 269
     .   : milestone, 264,
    master - mean (267ms)  : 263, 271
     .   : milestone, 267,

    section CallTarget+Inlining+NGEN
    This PR (6479) - mean (849ms)  : 819, 879
     .   : milestone, 849,
    master - mean (855ms)  : 820, 889
     .   : milestone, 855,

Loading

@datadog-ddstaging
Copy link

datadog-ddstaging bot commented Dec 20, 2024

Datadog Report

Branch report: andrew/update-rabbitmq
Commit report: 09e7eb4
Test service: dd-trace-dotnet

❌ 356 Failed (1 Known Flaky), 587296 Passed, 5483 Skipped, 48h 23m 19.31s Total Time

❌ Failed Tests (356)

This report shows up to 5 failed tests.

  • GraphQL2SchemaV0Tests.SubmitsTraces - Datadog.Trace.ClrProfiler.IntegrationTests - Details

    Expand for error
     Unable to determine port application is listening on
    
  • GraphQL2SchemaV0Tests.SubmitsTraces - Datadog.Trace.ClrProfiler.IntegrationTests - Details

    Expand for error
     Unable to determine port application is listening on
    
  • GraphQL2SchemaV0Tests.SubmitsTraces - Datadog.Trace.ClrProfiler.IntegrationTests - Details

    Expand for error
     Unable to determine port application is listening on
    
  • GraphQL2SchemaV0Tests.SubmitsTraces - Datadog.Trace.ClrProfiler.IntegrationTests - Details

    Expand for error
     Unable to determine port application is listening on
    
  • GraphQL3SchemaV0Tests.SubmitsTraces - Datadog.Trace.ClrProfiler.IntegrationTests - Details

    Expand for error
     Unable to determine port application is listening on
    

@andrewlock
Copy link
Member Author

andrewlock commented Dec 21, 2024

Benchmarks Report for tracer 🐌

Benchmarks for #6479 compared to master:

  • 1 benchmarks are slower, with geometric mean 1.193
  • All benchmarks have the same allocations

The following thresholds were used for comparing the benchmark speeds:

  • Mann–Whitney U test with statistical test for significance of 5%
  • Only results indicating a difference greater than 10% and 0.3 ns are considered.

Allocation changes below 0.5% are ignored.

Benchmark details

Benchmarks.Trace.ActivityBenchmark - Same speed ✔️ Same allocations ✔️

Raw results

Branch Method Toolchain Mean StdError StdDev Gen 0 Gen 1 Gen 2 Allocated
master StartStopWithChild net6.0 8.32μs 44.7ns 253ns 0.0165 0.00824 0 5.62 KB
master StartStopWithChild netcoreapp3.1 10.2μs 50.7ns 287ns 0.0199 0.00993 0 5.8 KB
master StartStopWithChild net472 16.3μs 35.3ns 137ns 1.06 0.307 0.102 6.21 KB
#6479 StartStopWithChild net6.0 7.86μs 44.3ns 313ns 0.016 0.00799 0 5.61 KB
#6479 StartStopWithChild netcoreapp3.1 10.1μs 56.9ns 403ns 0.0156 0.00519 0 5.8 KB
#6479 StartStopWithChild net472 16.2μs 49.3ns 191ns 1.06 0.312 0.104 6.21 KB
Benchmarks.Trace.AgentWriterBenchmark - Same speed ✔️ Same allocations ✔️

Raw results

Branch Method Toolchain Mean StdError StdDev Gen 0 Gen 1 Gen 2 Allocated
master WriteAndFlushEnrichedTraces net6.0 470μs 385ns 1.49μs 0 0 0 2.7 KB
master WriteAndFlushEnrichedTraces netcoreapp3.1 662μs 269ns 1.04μs 0 0 0 2.7 KB
master WriteAndFlushEnrichedTraces net472 859μs 722ns 2.79μs 0.428 0 0 3.3 KB
#6479 WriteAndFlushEnrichedTraces net6.0 477μs 321ns 1.2μs 0 0 0 2.7 KB
#6479 WriteAndFlushEnrichedTraces netcoreapp3.1 654μs 405ns 1.57μs 0 0 0 2.7 KB
#6479 WriteAndFlushEnrichedTraces net472 860μs 461ns 1.72μs 0.428 0 0 3.3 KB
Benchmarks.Trace.AspNetCoreBenchmark - Same speed ✔️ Same allocations ✔️

Raw results

Branch Method Toolchain Mean StdError StdDev Gen 0 Gen 1 Gen 2 Allocated
master SendRequest net6.0 157μs 923ns 8.71μs 0.147 0 0 14.47 KB
master SendRequest netcoreapp3.1 175μs 1.12μs 11.1μs 0.191 0 0 17.27 KB
master SendRequest net472 0.00137ns 0.000761ns 0.00285ns 0 0 0 0 b
#6479 SendRequest net6.0 154μs 956ns 9.42μs 0.162 0 0 14.47 KB
#6479 SendRequest netcoreapp3.1 166μs 965ns 8.24μs 0.162 0 0 17.27 KB
#6479 SendRequest net472 0.000144ns 0.000144ns 0.000518ns 0 0 0 0 b
Benchmarks.Trace.CIVisibilityProtocolWriterBenchmark - Same speed ✔️ Same allocations ✔️

Raw results

Branch Method Toolchain Mean StdError StdDev Gen 0 Gen 1 Gen 2 Allocated
master WriteAndFlushEnrichedTraces net6.0 571μs 2.67μs 10μs 0.584 0 0 41.6 KB
master WriteAndFlushEnrichedTraces netcoreapp3.1 695μs 3.86μs 23.5μs 0.353 0 0 41.76 KB
master WriteAndFlushEnrichedTraces net472 825μs 2.97μs 10.7μs 8.5 2.55 0.425 53.27 KB
#6479 WriteAndFlushEnrichedTraces net6.0 577μs 2.99μs 14μs 0.566 0 0 41.8 KB
#6479 WriteAndFlushEnrichedTraces netcoreapp3.1 672μs 3.67μs 22.3μs 0.338 0 0 41.73 KB
#6479 WriteAndFlushEnrichedTraces net472 863μs 3.78μs 14.6μs 8.62 2.59 0.431 53.25 KB
Benchmarks.Trace.DbCommandBenchmark - Same speed ✔️ Same allocations ✔️

Raw results

Branch Method Toolchain Mean StdError StdDev Gen 0 Gen 1 Gen 2 Allocated
master ExecuteNonQuery net6.0 1.32μs 1ns 3.87ns 0.0147 0 0 1.02 KB
master ExecuteNonQuery netcoreapp3.1 1.77μs 1.85ns 6.92ns 0.0133 0 0 1.02 KB
master ExecuteNonQuery net472 2.06μs 3.08ns 11.9ns 0.156 0.00102 0 987 B
#6479 ExecuteNonQuery net6.0 1.24μs 1.35ns 5.03ns 0.0141 0 0 1.02 KB
#6479 ExecuteNonQuery netcoreapp3.1 1.86μs 1.99ns 7.7ns 0.0139 0 0 1.02 KB
#6479 ExecuteNonQuery net472 2.09μs 1.84ns 7.11ns 0.156 0.00105 0 987 B
Benchmarks.Trace.ElasticsearchBenchmark - Same speed ✔️ Same allocations ✔️

Raw results

Branch Method Toolchain Mean StdError StdDev Gen 0 Gen 1 Gen 2 Allocated
master CallElasticsearch net6.0 1.26μs 1.05ns 3.94ns 0.0133 0 0 976 B
master CallElasticsearch netcoreapp3.1 1.57μs 1.18ns 4.58ns 0.0132 0 0 976 B
master CallElasticsearch net472 2.64μs 1.83ns 7.08ns 0.157 0 0 995 B
master CallElasticsearchAsync net6.0 1.28μs 0.793ns 2.97ns 0.0133 0 0 952 B
master CallElasticsearchAsync netcoreapp3.1 1.59μs 0.636ns 2.38ns 0.0135 0 0 1.02 KB
master CallElasticsearchAsync net472 2.65μs 2.23ns 8.62ns 0.166 0 0 1.05 KB
#6479 CallElasticsearch net6.0 1.3μs 0.71ns 2.75ns 0.0138 0 0 976 B
#6479 CallElasticsearch netcoreapp3.1 1.57μs 0.609ns 2.36ns 0.0134 0 0 976 B
#6479 CallElasticsearch net472 2.65μs 1.93ns 7.48ns 0.157 0 0 995 B
#6479 CallElasticsearchAsync net6.0 1.25μs 0.681ns 2.55ns 0.0132 0 0 952 B
#6479 CallElasticsearchAsync netcoreapp3.1 1.67μs 0.526ns 2.04ns 0.0133 0 0 1.02 KB
#6479 CallElasticsearchAsync net472 2.58μs 1.36ns 5.26ns 0.166 0 0 1.05 KB
Benchmarks.Trace.GraphQLBenchmark - Same speed ✔️ Same allocations ✔️

Raw results

Branch Method Toolchain Mean StdError StdDev Gen 0 Gen 1 Gen 2 Allocated
master ExecuteAsync net6.0 1.23μs 0.583ns 2.18ns 0.0132 0 0 952 B
master ExecuteAsync netcoreapp3.1 1.57μs 2.12ns 7.92ns 0.0126 0 0 952 B
master ExecuteAsync net472 1.83μs 0.494ns 1.91ns 0.145 0 0 915 B
#6479 ExecuteAsync net6.0 1.21μs 0.422ns 1.58ns 0.0135 0 0 952 B
#6479 ExecuteAsync netcoreapp3.1 1.69μs 0.865ns 3.24ns 0.0126 0 0 952 B
#6479 ExecuteAsync net472 1.77μs 0.455ns 1.76ns 0.145 0 0 915 B
Benchmarks.Trace.HttpClientBenchmark - Same speed ✔️ Same allocations ✔️

Raw results

Branch Method Toolchain Mean StdError StdDev Gen 0 Gen 1 Gen 2 Allocated
master SendAsync net6.0 4.4μs 2.07ns 7.76ns 0.0325 0 0 2.31 KB
master SendAsync netcoreapp3.1 5.21μs 1.27ns 4.76ns 0.039 0 0 2.85 KB
master SendAsync net472 7.35μs 2.08ns 8.06ns 0.493 0 0 3.12 KB
#6479 SendAsync net6.0 4.32μs 2.67ns 9.99ns 0.0319 0 0 2.31 KB
#6479 SendAsync netcoreapp3.1 5.34μs 4.93ns 19.1ns 0.0398 0 0 2.85 KB
#6479 SendAsync net472 7.38μs 2.2ns 8.54ns 0.493 0 0 3.12 KB
Benchmarks.Trace.ILoggerBenchmark - Same speed ✔️ Same allocations ✔️

Raw results

Branch Method Toolchain Mean StdError StdDev Gen 0 Gen 1 Gen 2 Allocated
master EnrichedLog net6.0 1.5μs 0.724ns 2.8ns 0.0233 0 0 1.64 KB
master EnrichedLog netcoreapp3.1 2.24μs 5.12ns 19.8ns 0.022 0 0 1.64 KB
master EnrichedLog net472 2.63μs 1.34ns 5.19ns 0.25 0 0 1.57 KB
#6479 EnrichedLog net6.0 1.5μs 1.07ns 4.01ns 0.0233 0 0 1.64 KB
#6479 EnrichedLog netcoreapp3.1 2.25μs 1.33ns 5.16ns 0.0225 0 0 1.64 KB
#6479 EnrichedLog net472 2.74μs 5.66ns 21.9ns 0.25 0 0 1.57 KB
Benchmarks.Trace.Log4netBenchmark - Same speed ✔️ Same allocations ✔️

Raw results

Branch Method Toolchain Mean StdError StdDev Gen 0 Gen 1 Gen 2 Allocated
master EnrichedLog net6.0 117μs 178ns 689ns 0.0579 0 0 4.28 KB
master EnrichedLog netcoreapp3.1 123μs 186ns 722ns 0 0 0 4.28 KB
master EnrichedLog net472 150μs 117ns 452ns 0.684 0.228 0 4.46 KB
#6479 EnrichedLog net6.0 117μs 224ns 866ns 0.0586 0 0 4.28 KB
#6479 EnrichedLog netcoreapp3.1 122μs 348ns 1.35μs 0.0603 0 0 4.28 KB
#6479 EnrichedLog net472 154μs 81.6ns 316ns 0.689 0.23 0 4.46 KB
Benchmarks.Trace.NLogBenchmark - Same speed ✔️ Same allocations ✔️

Raw results

Branch Method Toolchain Mean StdError StdDev Gen 0 Gen 1 Gen 2 Allocated
master EnrichedLog net6.0 2.98μs 0.65ns 2.52ns 0.0299 0 0 2.2 KB
master EnrichedLog netcoreapp3.1 4.33μs 2.1ns 8.15ns 0.0281 0 0 2.2 KB
master EnrichedLog net472 4.88μs 1.59ns 6.17ns 0.319 0 0 2.02 KB
#6479 EnrichedLog net6.0 2.93μs 2.17ns 8.41ns 0.0311 0 0 2.2 KB
#6479 EnrichedLog netcoreapp3.1 4.15μs 1.87ns 7.24ns 0.029 0 0 2.2 KB
#6479 EnrichedLog net472 4.97μs 1.06ns 3.98ns 0.32 0 0 2.02 KB
Benchmarks.Trace.RedisBenchmark - Same speed ✔️ Same allocations ✔️

Raw results

Branch Method Toolchain Mean StdError StdDev Gen 0 Gen 1 Gen 2 Allocated
master SendReceive net6.0 1.3μs 0.604ns 2.34ns 0.0162 0 0 1.14 KB
master SendReceive netcoreapp3.1 1.78μs 1.7ns 6.38ns 0.0154 0 0 1.14 KB
master SendReceive net472 2.11μs 1.65ns 6.4ns 0.183 0 0 1.16 KB
#6479 SendReceive net6.0 1.39μs 0.88ns 3.29ns 0.016 0 0 1.14 KB
#6479 SendReceive netcoreapp3.1 1.8μs 1.43ns 5.52ns 0.0153 0 0 1.14 KB
#6479 SendReceive net472 2.07μs 1.05ns 4.08ns 0.183 0 0 1.16 KB
Benchmarks.Trace.SerilogBenchmark - Same speed ✔️ Same allocations ✔️

Raw results

Branch Method Toolchain Mean StdError StdDev Gen 0 Gen 1 Gen 2 Allocated
master EnrichedLog net6.0 2.82μs 1.48ns 5.72ns 0.0225 0 0 1.6 KB
master EnrichedLog netcoreapp3.1 3.91μs 2.99ns 11.6ns 0.0214 0 0 1.65 KB
master EnrichedLog net472 4.35μs 2.51ns 9.73ns 0.322 0 0 2.04 KB
#6479 EnrichedLog net6.0 2.71μs 0.829ns 3.21ns 0.0217 0 0 1.6 KB
#6479 EnrichedLog netcoreapp3.1 3.88μs 1.4ns 5.22ns 0.0213 0 0 1.65 KB
#6479 EnrichedLog net472 4.39μs 2.24ns 8.66ns 0.323 0 0 2.04 KB
Benchmarks.Trace.SpanBenchmark - Slower ⚠️ Same allocations ✔️

Slower ⚠️ in #6479

Benchmark diff/base Base Median (ns) Diff Median (ns) Modality
Benchmarks.Trace.SpanBenchmark.StartFinishSpan‑net6.0 1.193 397.43 474.03

Raw results

Branch Method Toolchain Mean StdError StdDev Gen 0 Gen 1 Gen 2 Allocated
master StartFinishSpan net6.0 398ns 0.155ns 0.602ns 0.00819 0 0 576 B
master StartFinishSpan netcoreapp3.1 615ns 0.451ns 1.75ns 0.00767 0 0 576 B
master StartFinishSpan net472 700ns 0.374ns 1.4ns 0.0917 0 0 578 B
master StartFinishScope net6.0 542ns 0.325ns 1.22ns 0.00975 0 0 696 B
master StartFinishScope netcoreapp3.1 718ns 0.412ns 1.49ns 0.00946 0 0 696 B
master StartFinishScope net472 884ns 0.661ns 2.56ns 0.104 0 0 658 B
#6479 StartFinishSpan net6.0 474ns 0.28ns 1.08ns 0.00808 0 0 576 B
#6479 StartFinishSpan netcoreapp3.1 572ns 0.484ns 1.87ns 0.00768 0 0 576 B
#6479 StartFinishSpan net472 722ns 0.79ns 2.95ns 0.0916 0 0 578 B
#6479 StartFinishScope net6.0 516ns 0.297ns 1.07ns 0.00967 0 0 696 B
#6479 StartFinishScope netcoreapp3.1 713ns 0.502ns 1.95ns 0.00931 0 0 696 B
#6479 StartFinishScope net472 929ns 0.819ns 3.17ns 0.104 0 0 658 B
Benchmarks.Trace.TraceAnnotationsBenchmark - Same speed ✔️ Same allocations ✔️

Raw results

Branch Method Toolchain Mean StdError StdDev Gen 0 Gen 1 Gen 2 Allocated
master RunOnMethodBegin net6.0 667ns 0.353ns 1.32ns 0.00969 0 0 696 B
master RunOnMethodBegin netcoreapp3.1 876ns 0.706ns 2.74ns 0.00922 0 0 696 B
master RunOnMethodBegin net472 1.07μs 0.659ns 2.55ns 0.104 0 0 658 B
#6479 RunOnMethodBegin net6.0 665ns 0.363ns 1.41ns 0.00965 0 0 696 B
#6479 RunOnMethodBegin netcoreapp3.1 884ns 0.34ns 1.32ns 0.00926 0 0 696 B
#6479 RunOnMethodBegin net472 1.04μs 0.709ns 2.75ns 0.105 0 0 658 B

@andrewlock andrewlock changed the base branch from master to andrew/value-task-support December 23, 2024 15:20
@andrewlock andrewlock force-pushed the andrew/update-rabbitmq branch from c47a319 to c2b1675 Compare December 23, 2024 15:20
@andrewlock andrewlock force-pushed the andrew/value-task-support branch from ffc922e to 457a93f Compare December 23, 2024 17:06
@andrewlock andrewlock force-pushed the andrew/update-rabbitmq branch from c2b1675 to 288fb51 Compare December 23, 2024 17:06
@andrewlock andrewlock force-pushed the andrew/value-task-support branch from 457a93f to 6c8dbe5 Compare December 30, 2024 09:47
@andrewlock andrewlock force-pushed the andrew/update-rabbitmq branch from 288fb51 to 882ea95 Compare December 30, 2024 09:48
@andrewlock andrewlock force-pushed the andrew/value-task-support branch from 6c8dbe5 to a73c2c7 Compare January 8, 2025 17:33
@andrewlock andrewlock force-pushed the andrew/update-rabbitmq branch from 882ea95 to 8443b2e Compare January 8, 2025 17:34
andrewlock added a commit that referenced this pull request Jan 9, 2025
…#6480)

## Summary of changes

Add support for correct `CallTarget` instrumentation of methods that
return `ValueTask` in < .NET Core 3.1 or .NET Framework

## Reason for change

We already support instrumenting methods that returns `ValueTask` in
.NET Core 3.1. However, in .NET Framework or .NET Standard 2.0, this
support is [provided by a
package](https://www.nuget.org/packages/System.Threading.Tasks.Extensions),
and we currently _don't_ support instrumenting these methods. Or rather,
we just ignore the `OnAsyncMethodEnd` in integrations in these cases.

## Implementation details

We already support `ValueTask` in more recent frameworks, and the
support is very similar to our `Task` support. Unfortunately, in .NET FX
we can't reference the `ValueTask` type itself.

To work around this, we do the following:
- Detect that the return type is `ValueTask` or `ValueTask<T>` either by
loading the type directly (.NET Core) or checking the type name (.NET
Framework)
- Duck-type the `ValueTask` to read the `IsCompletedSuccessfully` value.
- If this is true, the task is already completed synchronously, and we
can simply call the target method.
  - For `ValueTask<T>` we duck type `Result` and read that directly too.
  - If it _hasn't_ completed, we need to extract the `Task` from it.
- Duck typing is used again to extract the `Task()` for uncompleted
`ValueTask`
- At this point, it's mostly a copy-paste of the existing `Task`
integrations
- Once the integration returns, we need to create a "new" `ValueTask`
instead from the previous one
- The semantics of `ValueTask` _require_ that we create a "fresh" one,
we can't just "reuse" the one we got originally, because we've already
retrieved the result/ awaited the inner task
  - Have to use `Activator`/`DynamicMethod` for this

> [!WARNING]
> The existing `ValueTask`/`ValueTask<>` and `Task`/`Task<>`
integrations are written quite differently, and I'm not entirely sure
why 🤔 Given the `ContinuationAction()` methods for both these cases
operate on `Task`, I based on the new `ValueTask` integrations on the
`Task` integrations, but if anyone has reasons why it shouldn't be, I'm
all ears!

## Test coverage

- Added `Task` and `ValueTask` tests to the `CallTargetNativeTests`
integration tests. Previously we were only testing a single `Task`
example, and that was somewhat insufficient
- Update the `CallTargetNativeTests` to explicitly assert that the
`OnAsyncMethodEnd` methods are called for `Task` / `ValueTask` method
integrations. As we provide _both_ methods in our target integration, we
were silently calling the wrong one for .NET FX
- Prior to the fix in this PR, these updated tests would fail on < .NET
Core 3.0 and .NET FX
- Run the `ValueTaskAsyncContinutationGenerator` unit tests on all TFMs,
not just .NET Core 3.1
- Unit tests for the `ValueTaskHelper` for checking if a type is a
`ValueTask`
- Unit tests for the `ValueTaskActivator` for creating a `ValueTask`
from a `Task` or `Task<T>`
- Verified it fixes the issues I was seeing in the RabbitMQ integration
  - #6479

## Other details

Required for
- #6479
Base automatically changed from andrew/value-task-support to master January 9, 2025 15:16
@andrewlock andrewlock force-pushed the andrew/update-rabbitmq branch 3 times, most recently from a3bd662 to 3059d67 Compare January 14, 2025 15:04
Copy link
Contributor

@bouwkast bouwkast left a comment

Choose a reason for hiding this comment

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

Took a bit to review but thanks LGTM

Just one comment on the ValueTask addition/change

@andrewlock andrewlock force-pushed the andrew/update-rabbitmq branch from 127cf20 to 09e7eb4 Compare January 15, 2025 10:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:automatic-instrumentation Automatic instrumentation managed C# code (Datadog.Trace.ClrProfiler.Managed) area:tracer The core tracer library (Datadog.Trace, does not include OpenTracing, native code, or integrations)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants