[OTLP] Cache resource bytes for protobuf serializer#7303
Conversation
|
Benchmarks Before
After
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #7303 +/- ##
==========================================
+ Coverage 89.82% 89.85% +0.02%
==========================================
Files 275 275
Lines 13834 13852 +18
==========================================
+ Hits 12427 12447 +20
+ Misses 1407 1405 -2
Flags with carried forward coverage won't be shown. Click here to find out more.
|
|
@martincostello , theoretically same optimization can be added within Metric, as it contains some metadata which is static anyways. Win there will be way smaller (only Name/Description/Unit are static). Would you mind attaching copilot to work on this, after this PR is closed? |
|
I'm sure I left a comment on my last review but I can't see it now. Could you re-run the new benchmarks with the latest changes and share the results for main and this PR? |
…lp-resource # Conflicts: # src/OpenTelemetry.Exporter.OpenTelemetryProtocol/CHANGELOG.md
|
After merging
Notes:
|
It sounds reasonable to make the same change for metrics. It's probably overall easiest if you do the PR (or ask Copilot to do it locally) as then I can review and merge the PR for you. |
I thought Microsoft had an infinite amount of tokens. That's why I asked whether a dedicated Copilot could be assigned to work on it, since it would already have an example of exactly what to change 🙂 I am also working on some improvements in the dotnet/runtime telemetry area. It really puzzles me that |
That's not really relevant here - this isn't a Microsoft project, and I'm not a Microsoft employee. I could assign it to use my personal Copilot access, but then it counts as written by me so I can't review/merge it and we're tight on review bandwidth here. If you submitted a PR I can take a look at then it would potentially move faster. If you're too busy with other things that's fine, I can just pick it up in the next day or so. |
|
Thank you for your contribution @unsafePtr! 🎉 We would like to hear from you about your experience contributing to OpenTelemetry by taking a few minutes to fill out this survey. |
| private const int ReserveSizeForLength = 4; | ||
| private const int InitialBufferSize = 2048; | ||
|
|
||
| private static readonly ConcurrentDictionary<Resource, byte[]> CachedResourceBytes = new(); |
There was a problem hiding this comment.
unsure if static is the right choice here. if we store it in MeterProvider or exporter itself, then we can avoid the Dictionary completely as there is single Resource associated with an exporter always.
There was a problem hiding this comment.
So you mean move the cache up one level to the caller?
That sounds similar to something I've done in #7279 to cache Prometheus metrics.
There was a problem hiding this comment.
@cijothomas , Resource is immutable, isn't it? static dictionary is the right choice here.
There was a problem hiding this comment.
Immutability of Resource isn't really the concern — the issue is lifetime. A static ConcurrentDictionary<Resource, byte[]> strongly references every Resource instance the process ever sees, including ones from disposed MeterProviders. For Resource it's usually bounded in practice so the leak is small, but caching on the exporter/provider (where there's a single Resource per exporter) would avoid the dictionary entirely and bound the lifetime correctly.
There was a problem hiding this comment.
I thought about it, but the changes surface was quite big, plus same bytes would be effectively duplicated across 3 exporters. While static dictionary is a simple change and I thought that Resource reference is typically living for a lifetime of the application. Several dangling references for a long-running processes, is usually acceptable. Still it's quite easy to swap ConcurrentDicitonary with ConditionalWeakTable here with loosing a bit on perf if dangling references are problem.
There was a problem hiding this comment.
Fair. This one has low risk, but it'd be still better to switch to ConditionalWeakTable here in a follow up PR. I am more worried about the Metric/scope change PR (#7307), as that has more real leak risks.
|
https://github.com/open-telemetry/opentelemetry-dotnet/pull/7303/changes#r3260565691 Already merged, but I'd suggest to reconsider. |
|
Have opened #7326 as a follow-up for discussion. |
Changes
Caches bytes for the protobuf serializer for the Resource, since Resource is immutable
Merge requirement checklist
CHANGELOG.mdfiles updated for non-trivial changes