Repository navigation
Add a metric handle API for telemetry - #404
cataphract wants to merge 2 commits into
Conversation
BenchmarksBenchmark execution time: 2026-11-24 14:40:00 Comparing candidate commit 22ee92f in PR branch Found 0 performance improvements and 0 performance regressions! Performance is the same for 8 metrics, 0 unstable metrics.
|
572f18f to
be4aea3
Compare
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
A metric is registered once with typed tags, so recording a point builds, copies and hashes no tag strings. Nothing uses the handles yet.
be4aea3 to
1d99d30
Compare
There was a problem hiding this comment.
An optional enum containing an unnamed -1 value wraps to the missing-tag encoding, causing its metric point to be counted in the wrong series instead of being dropped.
🤖 Bits Code Review · Commit 1d99d30
There was a problem hiding this comment.
More details
The PR adds typed metric handles with schema-defined tags and per-handle storage, with telemetry sending deferred to follow-up work. The new commit fixes the optional-enum -1 wraparound and adds regression coverage; no reportable findings remain open, though runtime tests were not run.
🤖 Bits Code Review · Commit 22ee92f
xlamorlette-datadog
left a comment
There was a problem hiding this comment.
Impressive!
| enum class MetricType : char { counter, rate, distribution }; | ||
| } | ||
|
|
||
| /// A DDSketch protobuf message, as bytes. |
There was a problem hiding this comment.
opt, nit, tiny: rather use double slash // not triple ///. I do see you so this to conform to surrounding code, but I would prefer to fix this whole (little) file.
Actually, I don't know where this comes from. Quick internet search seems to show that it could come from old Visual Studio or Doxygen conventions, or even from C#.
| #include <type_traits> | ||
|
|
||
| namespace datadog { | ||
| namespace telemetry { |
There was a problem hiding this comment.
opt, nit, tiny: by the way, use compound namespaces
| } | ||
|
|
||
| /// A DDSketch protobuf message, as bytes. | ||
| using EncodedSketch = std::string; |
There was a problem hiding this comment.
nit: more explicit EncodedDDSketch
| struct Metric final { | ||
| /// The type of the metric. | ||
| static constexpr details::MetricType type = T; | ||
| /// The value of one series: a count or a rate, or the sketch of a |
There was a problem hiding this comment.
opt, nit: suggestion for slightly improved comment: Value type of a series: encoded sketch for a distribution, integer for a count or a rate
| #include <datadog/string_view.h> | ||
| #include <datadog/telemetry/metrics.h> | ||
|
|
||
| #include <algorithm> |
There was a problem hiding this comment.
nit: remove include lines already indirectly included: this shrinks lines 52-63 to:
#include <unordered_map>
#include <vector>
| } | ||
|
|
||
| std::string StringTable::value(std::uint16_t id) { | ||
| if (id == overflow_id) return "other"; |
There was a problem hiding this comment.
nit: Maybe we should use a more explicit string stating that we overflown the table. (If changed, change also the comment line 69 of metric_handle.h.)
| } | ||
|
|
||
| std::string render(std::uint64_t value) const override { | ||
| return name_ + ":" + interned_string(value); |
There was a problem hiding this comment.
An empty TagString will render as name:. Is this intended?
| class MetricHandle final : public MetricBase<Metric> { | ||
| static_assert( | ||
| fits_in_series_id(static_cast<decltype(Tags::tag_fields())*>(nullptr)), | ||
| "Too many combinations of tag values for a 64-bit SeriesId"); |
There was a problem hiding this comment.
nit: Given the checks in fits_in_series_id(), we get this Too many combinations error message for an enum with no values.
| // | DistributionHandle) | fixed_tags_ | ||
| // | | | ||
| // | record(Tags, value) v | ||
| // | take_series() vector<unique_ptr<TagCodec>> -|> NamedCodec |
There was a problem hiding this comment.
nit: NamedCode, OptionalCodec and StringCodecderive fromTagCodec`: the arrow is in the wrong direction.
| namespace { | ||
|
|
||
| // Id of every new string once the table is full. | ||
| constexpr std::uint16_t overflow_id = 0xFFFF; |
There was a problem hiding this comment.
nit: rather TagString::id_count - 1?
Overview
Adds metric handles. A metric is registered once, with its fixed tags and a struct that describes its other tags. Recording a point then takes that struct and a number. Nothing uses the handles yet.
Stacked on #401.
Motivation
The current API makes every point pay for the full tag strings. Each call to
counter::increment(counter, tags)does this:std::vector<std::string>with one"name:value"string per tag.Telemetrytakes a mutex that every counter in the process shares. Rates and distributions each have one too.counters_[{counter, tags}]builds a temporaryMetricContextkey. That copies the metric's name and namespace and every tag string. The key is then hashed in full, and compared in full with the stored key.The cost grows with the number of tags and with traffic. nginx-datadog will send AppSec metrics such as
appsec.waf.requests, which has up to 13 tags and is recorded once per request. With the current API, each request would build those strings, then copy, hash and compare about 250 bytes under a lock that every thread takes.Tags are also free-form strings. Nothing checks the tag names, or limits the values. A typo at one call site, or an unbounded value such as a raw status code, silently creates new series.
With a handle, the work moves to creation time:
name:valuestring of every enum and bool value, are built once.record()reads each field as a number, and combines the numbers into a 64-bit series id. It then updates the handle's own store, under the handle's own lock. It builds, copies and hashes no strings.TagStringis for values not known in advance; it is interned, and after 65535 strings new ones are sent asother. Astatic_assertchecks that every combination of tag values fits in the series id.Additional Notes
LogBinnedDistr(Add a DDSketch-compatible distribution for telemetry #401) per series, so its memory does not grow with traffic.details::MetricRegistrycreates the handles and takes their series. It holds only weak references: a handle that its owner drops is forgotten, so the metric can be created again with other fixed tags.Telemetrysend the handles' series, then remove the string-tagged functions and move their callers to handles../build/test/tests '[metric_handle]'runs the new tests. The full suite passes.References
Jira ticket: APPSEC-70954
Contributor Checklist