Add CCKR adaptive log reservoir sampler to Microsoft.Extensions.Telem… - #7676
Draft
evgenyfedorov2 wants to merge 1 commit into
Draft
evgenyfedorov2 wants to merge 1 commit into
evgenyfedorov2 wants to merge 1 commit into
Conversation
…etry Implements the CCKR (Chao-Cohen-Kaplan-Reservoir) adaptive log sampler as implementations of the existing LoggingSampler and LogBuffer seams, so no changes to the logging pipeline are required: - Cckr<TCallsite,TPayload>: bottom-(K+1) weighted reservoir with EXP ranks, cross-period inverse-frequency feedback, Chao1 unseen-weight, bounded novelty preserve, and Horvitz-Thompson sampling weights (faithful port of the reference algorithm). - CckrLoggingSampler (LoggingSampler): the admit/drop decision. CckrLogBuffer (LogBuffer): holds admitted records per category and emits the kept records carrying the sampling.count weight at each flush, reusing SerializedLogRecord/DeserializedLogRecord/IBufferedLogger. - AddCckrLogSampling registers one reservoir instance as both seams. Public API: AddCckrLogSampling, ReservoirSamplingConfig, UnseenWeightMode. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Yun-Ting
reviewed
Sep 16, 2026
| state.HeapCount++; | ||
| HeapPush(new HeapEntry(key, callsite, payload)); | ||
|
|
||
| if (_heap.Count > _reservoirCapacity) |
There was a problem hiding this comment.
Suggested change
| if (_heap.Count > _reservoirCapacity) | |
| // Retain T+1 entries: the root is the (K+1)-th smallest rank, the first item excluded from the bottom-K sample (tau) | |
| if (_heap.Count > _reservoirCapacity + 1) |
In Bottom (K + 1) algorithm, tau is the rank of the first excluded item.
That's what keeps tau independent of every retained item's own rank; the condition the HT weights to be correct.
Yun-Ting
reviewed
Sep 16, 2026
| { | ||
| _ = Throw.IfNull(output); | ||
|
|
||
| double finalTau = Tau; |
There was a problem hiding this comment.
Suggested change
| double finalTau = Tau; | |
| // tau is the (K+1)-th smallest rank (the first excluded item), so take it from the heap before draining. i.e.: leaving exactly K records to emit. | |
| double finalTau = double.PostiveInfinity; | |
| if (_heap.Count > _reservoirCapcity) | |
| { | |
| finalTau = HeapPopMax().Key; | |
| } |
Yun-Ting
reviewed
Sep 17, 2026
| } | ||
| } | ||
|
|
||
| public void Dispose() => _pending.Dispose(); |
There was a problem hiding this comment.
question - do we need to call Flush() here before disposing, so we don't lose whatever's still in the reservoirs up to a full FlushInterval?
I don't think the providers are gone by this point. And ExtendedLoggerFactory.cs:76 registers DI-supplied providers with dispose: false, so the factory skips them and the container disposes them itself (in reverse creation order.)
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
…etry
Implements the CCKR (Chao-Cohen-Kaplan-Reservoir) adaptive log sampler as implementations of the existing LoggingSampler and LogBuffer seams, so no changes to the logging pipeline are required:
Cckr<TCallsite,TPayload>: bottom-(K+1) weighted reservoir with EXP ranks, cross-period inverse-frequency feedback, Chao1 unseen-weight, bounded novelty preserve, and Horvitz-Thompson sampling weights (faithful port of the reference algorithm).
CckrLoggingSampler (LoggingSampler): the admit/drop decision. CckrLogBuffer (LogBuffer): holds admitted records per category and emits the kept records carrying the sampling.count weight at each flush, reusing SerializedLogRecord/DeserializedLogRecord/IBufferedLogger.
AddCckrLogSampling registers one reservoir instance as both seams. Public API: AddCckrLogSampling, ReservoirSamplingConfig, UnseenWeightMode.
Microsoft Reviewers: Open in CodeFlow