-
Notifications
You must be signed in to change notification settings - Fork 150
Support for Delegate instrumentation #4613
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
Conversation
andrewlock
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is excellent, thanks!
tracer/src/Datadog.Trace/Util/Delegates/DelegateInstrumentation.cs
Outdated
Show resolved
Hide resolved
| func2 = DelegateInstrumentation.Wrap(func2, new DelegateFunc0Callbacks( | ||
| target => | ||
| { | ||
| Interlocked.Increment(ref value).Should().Be(1); | ||
| return null; | ||
| }, | ||
| (target, returnValue, exception, state) => | ||
| { | ||
| Interlocked.Increment(ref value).Should().Be(3); | ||
| return ((int)returnValue) + 1; | ||
| })); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we should actually include these DelegateFunc0Callbacks etc, or maybe just move them into the test project 🤔 Seems like we'd never want to use them in practice if they add extra allocations?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find this useful for prototyping or when we actually wants to instrument a delegate that runs only once, where the allocations are not so important.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough, I was mostly thinking that we have enough complexity with this stuff already, having 2 ways to do things muddies the water, especially as it's not as good perf wise 🤷♂️ But I'm not passionate about it
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:
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 (4613) - mean (71ms) : 64, 79
. : milestone, 71,
master - mean (69ms) : 63, 74
. : milestone, 69,
section CallTarget+Inlining+NGEN
This PR (4613) - mean (1,029ms) : 1005, 1054
. : milestone, 1029,
master - mean (1,028ms) : 1011, 1045
. : milestone, 1028,
gantt
title Execution time (ms) FakeDbCommand (.NET Core 3.1)
dateFormat X
axisFormat %s
todayMarker off
section Baseline
This PR (4613) - mean (106ms) : 97, 115
. : milestone, 106,
master - mean (107ms) : 94, 120
. : milestone, 107,
section CallTarget+Inlining+NGEN
This PR (4613) - mean (749ms) : 731, 766
. : milestone, 749,
master - mean (746ms) : 732, 760
. : milestone, 746,
gantt
title Execution time (ms) FakeDbCommand (.NET 6)
dateFormat X
axisFormat %s
todayMarker off
section Baseline
This PR (4613) - mean (90ms) : 83, 96
. : milestone, 90,
master - mean (90ms) : 83, 96
. : milestone, 90,
section CallTarget+Inlining+NGEN
This PR (4613) - mean (708ms) : 691, 724
. : milestone, 708,
master - mean (710ms) : 694, 727
. : milestone, 710,
gantt
title Execution time (ms) HttpMessageHandler (.NET Framework 4.6.2)
dateFormat X
axisFormat %s
todayMarker off
section Baseline
This PR (4613) - mean (189ms) : 183, 196
. : milestone, 189,
master - mean (187ms) : 179, 196
. : milestone, 187,
section CallTarget+Inlining+NGEN
This PR (4613) - mean (1,140ms) : 1111, 1168
. : milestone, 1140,
master - mean (1,124ms) : 1098, 1149
. : milestone, 1124,
gantt
title Execution time (ms) HttpMessageHandler (.NET Core 3.1)
dateFormat X
axisFormat %s
todayMarker off
section Baseline
This PR (4613) - mean (273ms) : 264, 283
. : milestone, 273,
master - mean (270ms) : 261, 280
. : milestone, 270,
section CallTarget+Inlining+NGEN
This PR (4613) - mean (1,108ms) : 1077, 1140
. : milestone, 1108,
master - mean (1,104ms) : 1084, 1125
. : milestone, 1104,
gantt
title Execution time (ms) HttpMessageHandler (.NET 6)
dateFormat X
axisFormat %s
todayMarker off
section Baseline
This PR (4613) - mean (260ms) : 247, 274
. : milestone, 260,
master - mean (260ms) : 247, 273
. : milestone, 260,
section CallTarget+Inlining+NGEN
This PR (4613) - mean (1,058ms) : 1028, 1089
. : milestone, 1058,
master - mean (1,053ms) : 1024, 1081
. : milestone, 1053,
|
Datadog ReportBranch report: ✅ |
Benchmarks Report 🐌Benchmarks for #4613 compared to master:
The following thresholds were used for comparing the benchmark speeds:
Allocation changes below 0.5% are ignored. Benchmark detailsBenchmarks.Trace.ActivityBenchmark - Same speed ✔️ Same allocations ✔️Raw results
Benchmarks.Trace.AgentWriterBenchmark - Same speed ✔️ Same allocations ✔️Raw results
Benchmarks.Trace.Asm.AppSecBodyBenchmark - Same speed ✔️ Same allocations ✔️Raw results
Benchmarks.Trace.Asm.AppSecWafBenchmark - Same speed ✔️ Same allocations ✔️Raw results
Benchmarks.Trace.AspNetCoreBenchmark - Same speed ✔️ Same allocations ✔️Raw results
Benchmarks.Trace.CIVisibilityProtocolWriterBenchmark - Same speed ✔️ Same allocations ✔️Raw results
Benchmarks.Trace.DbCommandBenchmark - Faster 🎉 Same allocations ✔️
|
| Benchmark | base/diff | Base Median (ns) | Diff Median (ns) | Modality |
|---|---|---|---|---|
| Benchmarks.Trace.DbCommandBenchmark.ExecuteNonQuery‑net6.0 | 1.149 | 1,174.96 | 1,022.15 |
Raw results
| Branch | Method | Toolchain | Mean | StdError | StdDev | Gen 0 | Gen 1 | Gen 2 | Allocated |
|---|---|---|---|---|---|---|---|---|---|
| master | ExecuteNonQuery |
net6.0 | 1.18μs | 0.715ns | 2.77ns | 0.0106 | 0 | 0 | 768 B |
| master | ExecuteNonQuery |
netcoreapp3.1 | 1.35μs | 0.616ns | 2.39ns | 0.0101 | 0 | 0 | 768 B |
| master | ExecuteNonQuery |
net472 | 1.63μs | 0.372ns | 1.44ns | 0.116 | 0 | 0 | 730 B |
| #4613 | ExecuteNonQuery |
net6.0 | 1.02μs | 0.323ns | 1.25ns | 0.0107 | 0 | 0 | 768 B |
| #4613 | ExecuteNonQuery |
netcoreapp3.1 | 1.34μs | 0.462ns | 1.73ns | 0.0107 | 0 | 0 | 768 B |
| #4613 | ExecuteNonQuery |
net472 | 1.61μs | 2.66ns | 10.3ns | 0.116 | 0 | 0 | 730 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.2μs | 1.37ns | 5.32ns | 0.0138 | 0 | 0 | 992 B |
| master | CallElasticsearch |
netcoreapp3.1 | 1.46μs | 0.463ns | 1.79ns | 0.0131 | 0 | 0 | 992 B |
| master | CallElasticsearch |
net472 | 2.23μs | 0.631ns | 2.36ns | 0.159 | 0.00112 | 0 | 1 KB |
| master | CallElasticsearchAsync |
net6.0 | 1.31μs | 1.64ns | 6.12ns | 0.0137 | 0 | 0 | 968 B |
| master | CallElasticsearchAsync |
netcoreapp3.1 | 1.51μs | 0.815ns | 3.05ns | 0.0143 | 0 | 0 | 1.04 KB |
| master | CallElasticsearchAsync |
net472 | 2.49μs | 0.776ns | 3ns | 0.167 | 0 | 0 | 1.06 KB |
| #4613 | CallElasticsearch |
net6.0 | 1.32μs | 1.19ns | 4.61ns | 0.0139 | 0 | 0 | 992 B |
| #4613 | CallElasticsearch |
netcoreapp3.1 | 1.4μs | 0.559ns | 2.17ns | 0.0133 | 0 | 0 | 992 B |
| #4613 | CallElasticsearch |
net472 | 2.21μs | 0.757ns | 2.73ns | 0.158 | 0.00111 | 0 | 1 KB |
| #4613 | CallElasticsearchAsync |
net6.0 | 1.3μs | 1.2ns | 4.64ns | 0.0136 | 0 | 0 | 968 B |
| #4613 | CallElasticsearchAsync |
netcoreapp3.1 | 1.57μs | 0.888ns | 3.32ns | 0.0143 | 0 | 0 | 1.04 KB |
| #4613 | CallElasticsearchAsync |
net472 | 2.41μs | 0.579ns | 2.17ns | 0.168 | 0.0012 | 0 | 1.06 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.34μs | 0.695ns | 2.6ns | 0.0129 | 0 | 0 | 912 B |
| master | ExecuteAsync |
netcoreapp3.1 | 1.41μs | 0.595ns | 2.22ns | 0.0121 | 0 | 0 | 912 B |
| master | ExecuteAsync |
net472 | 1.73μs | 1.34ns | 5.19ns | 0.139 | 0.000862 | 0 | 875 B |
| #4613 | ExecuteAsync |
net6.0 | 1.25μs | 3.41ns | 13.2ns | 0.013 | 0 | 0 | 912 B |
| #4613 | ExecuteAsync |
netcoreapp3.1 | 1.47μs | 0.599ns | 2.24ns | 0.0123 | 0 | 0 | 912 B |
| #4613 | ExecuteAsync |
net472 | 1.81μs | 1.03ns | 3.98ns | 0.138 | 0 | 0 | 875 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 | 3.8μs | 1.7ns | 6.35ns | 0.0267 | 0 | 0 | 1.94 KB |
| master | SendAsync |
netcoreapp3.1 | 4.33μs | 2.17ns | 8.39ns | 0.0325 | 0 | 0 | 2.48 KB |
| master | SendAsync |
net472 | 7.23μs | 4.65ns | 18ns | 0.482 | 0 | 0 | 3.05 KB |
| #4613 | SendAsync |
net6.0 | 3.84μs | 1.35ns | 5.24ns | 0.0269 | 0 | 0 | 1.94 KB |
| #4613 | SendAsync |
netcoreapp3.1 | 4.45μs | 4.28ns | 16ns | 0.0333 | 0 | 0 | 2.48 KB |
| #4613 | SendAsync |
net472 | 7.13μs | 2.3ns | 8.61ns | 0.482 | 0 | 0 | 3.05 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.34μs | 3.19ns | 12.4ns | 0.0231 | 0 | 0 | 1.62 KB |
| master | EnrichedLog |
netcoreapp3.1 | 2.01μs | 0.971ns | 3.63ns | 0.0221 | 0 | 0 | 1.62 KB |
| master | EnrichedLog |
net472 | 2.38μs | 4.33ns | 16.8ns | 0.244 | 0 | 0 | 1.54 KB |
| #4613 | EnrichedLog |
net6.0 | 1.48μs | 0.574ns | 2.15ns | 0.0223 | 0 | 0 | 1.62 KB |
| #4613 | EnrichedLog |
netcoreapp3.1 | 1.98μs | 1.11ns | 4.16ns | 0.0217 | 0 | 0 | 1.62 KB |
| #4613 | EnrichedLog |
net472 | 2.32μs | 1.22ns | 4.57ns | 0.245 | 0 | 0 | 1.54 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 | 114μs | 192ns | 742ns | 0.0571 | 0 | 0 | 4.21 KB |
| master | EnrichedLog |
netcoreapp3.1 | 118μs | 179ns | 669ns | 0 | 0 | 0 | 4.21 KB |
| master | EnrichedLog |
net472 | 150μs | 159ns | 594ns | 0.671 | 0.224 | 0 | 4.38 KB |
| #4613 | EnrichedLog |
net6.0 | 112μs | 118ns | 457ns | 0 | 0 | 0 | 4.21 KB |
| #4613 | EnrichedLog |
netcoreapp3.1 | 118μs | 119ns | 462ns | 0 | 0 | 0 | 4.21 KB |
| #4613 | EnrichedLog |
net472 | 148μs | 202ns | 784ns | 0.67 | 0.223 | 0 | 4.38 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.96μs | 1ns | 3.75ns | 0.0298 | 0 | 0 | 2.18 KB |
| master | EnrichedLog |
netcoreapp3.1 | 3.77μs | 1.09ns | 4.07ns | 0.0282 | 0 | 0 | 2.18 KB |
| master | EnrichedLog |
net472 | 4.59μs | 2.28ns | 8.84ns | 0.314 | 0 | 0 | 1.99 KB |
| #4613 | EnrichedLog |
net6.0 | 2.85μs | 1.28ns | 4.79ns | 0.0296 | 0 | 0 | 2.18 KB |
| #4613 | EnrichedLog |
netcoreapp3.1 | 3.79μs | 1.7ns | 6.35ns | 0.0284 | 0 | 0 | 2.18 KB |
| #4613 | EnrichedLog |
net472 | 4.63μs | 1.89ns | 7.33ns | 0.316 | 0 | 0 | 1.99 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.24μs | 0.844ns | 3.16ns | 0.0162 | 0 | 0 | 1.16 KB |
| master | SendReceive |
netcoreapp3.1 | 1.62μs | 2.34ns | 8.1ns | 0.0155 | 0 | 0 | 1.16 KB |
| master | SendReceive |
net472 | 2.12μs | 2.2ns | 8.53ns | 0.185 | 0 | 0 | 1.16 KB |
| #4613 | SendReceive |
net6.0 | 1.34μs | 0.622ns | 2.41ns | 0.0164 | 0 | 0 | 1.16 KB |
| #4613 | SendReceive |
netcoreapp3.1 | 1.6μs | 0.85ns | 3.18ns | 0.0152 | 0 | 0 | 1.16 KB |
| #4613 | SendReceive |
net472 | 2.06μs | 1.43ns | 5.36ns | 0.185 | 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.62μs | 1.77ns | 6.86ns | 0.0209 | 0 | 0 | 1.53 KB |
| master | EnrichedLog |
netcoreapp3.1 | 3.66μs | 1.16ns | 4.33ns | 0.0201 | 0 | 0 | 1.58 KB |
| master | EnrichedLog |
net472 | 3.97μs | 3.62ns | 13.5ns | 0.309 | 0 | 0 | 1.96 KB |
| #4613 | EnrichedLog |
net6.0 | 2.48μs | 0.735ns | 2.65ns | 0.0212 | 0 | 0 | 1.53 KB |
| #4613 | EnrichedLog |
netcoreapp3.1 | 3.72μs | 1.82ns | 7.07ns | 0.0204 | 0 | 0 | 1.58 KB |
| #4613 | EnrichedLog |
net472 | 4.12μs | 3.35ns | 13ns | 0.309 | 0 | 0 | 1.96 KB |
Benchmarks.Trace.SpanBenchmark - Same speed ✔️ Same allocations ✔️
Raw results
| Branch | Method | Toolchain | Mean | StdError | StdDev | Gen 0 | Gen 1 | Gen 2 | Allocated |
|---|---|---|---|---|---|---|---|---|---|
| master | StartFinishSpan |
net6.0 | 441ns | 0.694ns | 2.69ns | 0.00751 | 0 | 0 | 536 B |
| master | StartFinishSpan |
netcoreapp3.1 | 575ns | 0.192ns | 0.693ns | 0.00721 | 0 | 0 | 536 B |
| master | StartFinishSpan |
net472 | 621ns | 0.285ns | 1.07ns | 0.0852 | 0 | 0 | 538 B |
| master | StartFinishScope |
net6.0 | 551ns | 0.176ns | 0.657ns | 0.00916 | 0 | 0 | 656 B |
| master | StartFinishScope |
netcoreapp3.1 | 773ns | 0.459ns | 1.78ns | 0.00888 | 0 | 0 | 656 B |
| master | StartFinishScope |
net472 | 847ns | 0.321ns | 1.24ns | 0.098 | 0 | 0 | 618 B |
| #4613 | StartFinishSpan |
net6.0 | 461ns | 0.225ns | 0.841ns | 0.00747 | 0 | 0 | 536 B |
| #4613 | StartFinishSpan |
netcoreapp3.1 | 558ns | 0.227ns | 0.879ns | 0.0073 | 0 | 0 | 536 B |
| #4613 | StartFinishSpan |
net472 | 656ns | 0.617ns | 2.39ns | 0.0854 | 0 | 0 | 538 B |
| #4613 | StartFinishScope |
net6.0 | 537ns | 0.209ns | 0.782ns | 0.0092 | 0 | 0 | 656 B |
| #4613 | StartFinishScope |
netcoreapp3.1 | 751ns | 0.635ns | 2.46ns | 0.00902 | 0 | 0 | 656 B |
| #4613 | StartFinishScope |
net472 | 775ns | 0.303ns | 1.17ns | 0.0981 | 0 | 0 | 618 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 | 563ns | 0.244ns | 0.946ns | 0.00937 | 0 | 0 | 656 B |
| master | RunOnMethodBegin |
netcoreapp3.1 | 811ns | 0.538ns | 2.01ns | 0.00888 | 0 | 0 | 656 B |
| master | RunOnMethodBegin |
net472 | 887ns | 0.283ns | 1.02ns | 0.098 | 0 | 0 | 618 B |
| #4613 | RunOnMethodBegin |
net6.0 | 513ns | 0.172ns | 0.665ns | 0.00918 | 0 | 0 | 656 B |
| #4613 | RunOnMethodBegin |
netcoreapp3.1 | 803ns | 4.04ns | 17.1ns | 0.00874 | 0 | 0 | 656 B |
| #4613 | RunOnMethodBegin |
net472 | 927ns | 0.212ns | 0.82ns | 0.0978 | 0 | 0 | 618 B |
kevingosse
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's always a bit scary to see that much copy/pasting, I wonder if at some point in the future we should try using source generators for those
That’s actually a nice idea for a future PR |
| public TReturn OnDelegateEnd<TReturn>(object sender, TReturn returnValue, Exception exception, object state) | ||
| { | ||
| Count.Value++; | ||
| return (TReturn)(object)((int)(object)returnValue + 1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The number of casts 😂
| #region Action 0 Argument | ||
|
|
||
| private class Action0Wrapper<TDelegate, TCallbacks> : Wrapper<TDelegate, TCallbacks> | ||
| where TCallbacks : struct, IBegin0Callbacks, IVoidReturnCallback |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These generic constraints :chefs-kiss:
zacharycmontoya
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a little late to reviewing this but this is amazing. Nice work!
Summary of changes
This PR adds support for instrumenting
delegates.Currently supports:
Action,Action<T>,Action<T, T2>,Action<T, T2, T3>,Action<T, T2, T3, T4>andAction<T, T2, T3, T4, T5>. Please note that it doesn't require to be exactly these delegates, but the signature needs to be compatible with those.Func<TReturn>,Func<T, TReturn>,Func<T, T2, TReturn>,Func<T, T2, T3, TReturn>,Func<T, T2, T3, T4, TReturn>andFunc<T, T2, T3, T4, T5, TReturn>. Please note that it doesn't require to be exactly these delegates, but the signature needs to be compatible with those.Not supported:
ref,inoroutkeywords.ValueTaskorValueTask<T>Implementation details
Normal
Func<string>instrumentation with no allocations:Delegate based
Func<string>instrumentation with boxing:Async support and setting continuations:
Test coverage
There's a new suite to test all the supported cases.