Skip to content

Add a DDSketch-compatible distribution for telemetry - #401

Merged
cataphract merged 7 commits into
mainfrom
glopes/telemetry-ddsketch
Nov 24, 2026
Merged

cataphract merged 7 commits into
mainfrom
glopes/telemetry-ddsketch

Conversation

@cataphract

@cataphract cataphract commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Overview

Adds LogBinnedDistr, a DDSketch-compatible distribution, and its protobuf encoding. Nothing uses it yet.

Motivation

The goal is to let nginx-datadog send some AppSec distribution metrics, such as appsec.waf.duration and appsec.instrum.body_size. The AppSec telemetry spec asks for these only through sketches. A raw distributions payload carries every point, so its size grows with traffic.

libdatadog made the same change in DataDog/libdatadog#268: it removed the distributions payload in favor of sketches ("Distributions should be replaced by sketches"). This class is meant to replace distributions here too. A follow-up PR will make telemetry::distribution::add record into a sketch and send a sketches payload.

Additional Notes

  • Same mapping as libdatadog, which matches the backend's sketches: γ = 65/64 (relative error 1/129), index offset 1338.5, at most 2048 bins, lowest bins collapsed first.
  • Bins are encoded in the sparse binCounts form. libdatadog uses contiguousBinCounts. Both are valid DDSketch protobuf.
  • The tests check the bins against hand-computed results. ./build/test/tests '[log_binned_distr]': all 13 test cases pass; so does the full suite.
  • Spec this follows (Datadog internal): telemetry sketch distributions.

References

Jira ticket: APPSEC-70954

Contributor Checklist

  • I am the author of this pull request and I own it.
  • I reviewed every line of this pull request.
  • This pull request follows the Contributing Guidelines.

@pr-commenter

pr-commenter Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Benchmarks

Benchmark execution time: 2026-11-24 11:16:04

Comparing candidate commit 14bdff6 in PR branch glopes/telemetry-ddsketch with baseline commit f20b05f in branch main.

📊 Benchmarking dashboard

Found 0 performance improvements and 0 performance regressions! Performance is the same for 8 metrics, 0 unstable metrics.

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 ----------------------------------'

@cataphract
cataphract force-pushed the glopes/telemetry-ddsketch branch from 4073956 to 0e9a6be Compare November 13, 2026 15:33
Raw distribution payloads grow with traffic. Like libdatadog, telemetry
will send DDSketches instead. This adds the sketch; it is not wired in.
@cataphract
cataphract force-pushed the glopes/telemetry-ddsketch branch from 0e9a6be to 38c49d5 Compare November 13, 2026 22:27
@cataphract
cataphract marked this pull request as ready for review November 13, 2026 23:32
@cataphract
cataphract requested review from a team as code owners November 13, 2026 23:32
@cataphract
cataphract requested review from Anilm3 and removed request for a team November 13, 2026 23:32
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-13T23:33:44.356749Z 38c49d5 Draft marked ready
🔒 Security Review ✅ Completed 2026-11-13T23:35:21.237351Z 38c49d5 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@datadog-prod-us1-6 datadog-prod-us1-6 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bits Code Review: PASS

More details

The static review identified no actionable defects in bin mapping, range collapsing, or DDSketch protobuf encoding. Syntax checks passed, but runtime behavior remains unverified by test execution.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit 38c49d5

@xlamorlette-datadog xlamorlette-datadog left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Impressive work!

Comment thread src/datadog/telemetry/log_bin_index.h Resolved
Comment thread src/datadog/telemetry/log_bin_index.cpp Resolved
Comment thread src/datadog/telemetry/log_bin_index.h Resolved
Comment thread src/datadog/telemetry/log_bin_index.cpp Resolved
Review showed three parts that cost something and gave nothing back.
The sketch's own mutex was redundant: its only user, a distribution
handle's store, already locks around every call. The clamp for values
below a full range only helps when one series spans more than a factor
of 6e13, which no metric does, and it slowed the usual case on x86.
The empty negative store was copied from libdatadog, but neither the
DDSketch protobuf definition nor the intake needs it.

The other changes are for readability: full names, a single copy of the
bit-cast helpers, named protobuf wire types and a const count_at(). The
tests now cover a negative bin, and the exactness checks on random
values run ten times fewer values, since a separate test already checks
every bin edge.

@datadog-prod-us1-6 datadog-prod-us1-6 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bits Code Review: FAIL

Reusing a moved-from sketch can access an empty bin array. If a higher sample triggers both bin collapse and window growth, an allocation failure can silently discard samples that were already recorded.

Open Bits AI session

🤖 Bits Code Review · Commit 0ee78e0

Comment thread src/datadog/telemetry/log_bin_index.cpp Resolved
The class was renamed to LogBinnedDistribution in review, to use full
words. The files kept the short log_binned_distr name, so they no
longer matched the class they hold. They now do.
The implicit move took the counts vector but left the source's range
behind. Recording into the source or encoding it then read past the end
of an empty vector.

The move now takes every member and resets the source to an empty
sketch, as review suggested. Making a move fall back to a copy would
also have kept the source valid, but every move would then allocate and
copy up to 32 KiB of counts, and could throw. The move constructor goes
through the move assignment, so the members are listed only once.
When a high value merged the lowest bins and also needed a larger
window, the merged bins were zeroed before the new window was
allocated. If that allocation threw and the caller caught it, the
counts were gone, and later encodes silently left them out.

Now the merged counts are summed first, the window grows, and only then
are the merged bins cleared. The window growth is the only step that
can throw, and it changes nothing until its new vector exists, so a
failed record() leaves the sketch as it was. The test replaces the
global operator new to fail on demand, only while a flag is set.
@cataphract

cataphract commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor Author

@xlamorlette-datadog the only open thread is now #401 (comment) PTAL

@datadog-prod-us1-6 datadog-prod-us1-6 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bits Code Review: FAIL

The global delete overrides in the test allocator are mismatched with how Catch2 allocates (e.g., new(std::nothrow)), causing both an ASan alloc-dealloc-mismatch at runtime and a -Wmismatched-new-delete build failure under GCC at -O2 with -Werror; matching nothrow overrides or scoping the injection are needed to fix both.

Open Bits AI session

🤖 Bits Code Review · Commit dcf0478

@cataphract
cataphract merged commit f54a882 into main Nov 24, 2026
39 checks passed
@cataphract
cataphract deleted the glopes/telemetry-ddsketch branch November 24, 2026 12:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants