Skip to content

[AI generated] Implemented metric sampling#34

Closed
DrDet wants to merge 1 commit into
VKCOM:masterfrom
DrDet:master
Closed

[AI generated] Implemented metric sampling#34
DrDet wants to merge 1 commit into
VKCOM:masterfrom
DrDet:master

Conversation

@DrDet
Copy link
Copy Markdown

@DrDet DrDet commented May 14, 2026

Add Per-Metric Sampling to C++ Client

Adds sample_factor(n) method for configurable client-side sampling of high-volume metrics.

Usage

// Send ~1% of events with counters automatically multiplied by 100
statshouse.metric("hot_metric").sample_factor(100).write_count(1);

How It Works

  • Events written with probability 1/sample_factor
  • Counter multiplied by factor to preserve expected values
  • Zero overhead when sample_factor=1 (default)
  • Thread-local wyhash PRNG - no locks

API

MetricBuilder& sample_factor(uint32_t factor);  // factor >= 1

Works with all write methods:

  • write_count()
  • write_value() / write_values()
  • write_unique()

Example

statshouse::TransportUDP statshouse;

// Normal metric
statshouse.metric("requests").write_count(1);

// Hot metric - 100x less overhead
statshouse.metric("api_calls")
    .tag("endpoint", "/v1/users")
    .sample_factor(100)
    .write_count(1);

Performance

Case Overhead
No sampling (factor=1) Zero - single LIKELY branch
With sampling One wyhash call + modulo

Uses thread-local wyhash PRNG (same as the library's hashing) - no locks, no allocations, no syscalls.

// Hot path: single branch, no function calls
if (STATSHOUSE_LIKELY(sample_factor_ <= 1)) { return true; }

// Sampling: fast wyhash
auto r = wyhash::wyrand(&seed);
return wyhash::wy2u0k(r, sample_factor_) == 0;

Implementation Notes

  • TransportUDP: Sampling at write time
  • Registry: Sampling during aggregation, unsampled flush
  • Fully backward compatible (default factor=1)

@DrDet DrDet requested a review from a team as a code owner May 14, 2026 14:08
@DrDet DrDet changed the title [AI genereated] Implemented metric sampling [AI generated] Implemented metric sampling May 14, 2026
@Rustamchuk Rustamchuk closed this May 14, 2026
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