Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(metrics): Another statsd metric to measure bucket duplication [INGEST-421] #1128

Merged
merged 5 commits into from
Nov 18, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

**Internal**:

- Another statsd metric to measure bucket duplication. ([#1128](https://github.com/getsentry/relay/pull/1128))

## 21.11.0

**Features**:
Expand Down
7 changes: 4 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions relay-metrics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ relay-statsd = { path = "../relay-statsd" }
serde = { version = "1.0.114", features = ["derive"] }
serde_json = "1.0.55"
failure = "0.1.8"
crc32fast = "1.2.1"

[dev-dependencies]
criterion = "0.3"
Expand Down
26 changes: 25 additions & 1 deletion relay-metrics/src/aggregation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use serde::{Deserialize, Serialize};

use relay_common::{MonotonicResult, ProjectKey, UnixTimestamp};

use crate::statsd::{MetricCounters, MetricGauges, MetricHistograms, MetricTimers};
use crate::statsd::{MetricCounters, MetricGauges, MetricHistograms, MetricSets, MetricTimers};
use crate::{Metric, MetricType, MetricUnit, MetricValue};

/// A snapshot of values within a [`Bucket`].
Expand Down Expand Up @@ -706,6 +706,24 @@ struct BucketKey {
tags: BTreeMap<String, String>,
}

impl BucketKey {
/// An extremely hamfisted way to hash a bucket key into an integer.
///
/// This is necessary for (and probably only useful for) reporting unique bucket keys in a
/// cadence set metric, as cadence set metrics can only be constructed from values that
/// implement [`cadence::ext::ToSetValue`]. This trait is only implemented for [`i64`], and
/// while we could implement it directly for [`BucketKey`] the documentation advises us not to
/// interact with this trait.
///
fn as_integer_lossy(&self) -> i64 {
// XXX: The way this hasher is used may be platform-dependent. If we want to produce the
// same hash across platforms, the `deterministic_hash` crate may be useful.
let mut hasher = crc32fast::Hasher::new();
std::hash::Hash::hash(self, &mut hasher);
hasher.finalize() as i64
}
}

/// Parameters used by the [`Aggregator`].
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(default)]
Expand Down Expand Up @@ -996,6 +1014,12 @@ impl Aggregator {
metric_type = entry.key().metric_type.as_str(),
metric_name = &entry.key().metric_name
);
relay_statsd::metric!(
set(MetricSets::UniqueBucketsCreated) = entry.key().as_integer_lossy(),
metric_type = entry.key().metric_type.as_str(),
metric_name = &entry.key().metric_name
);

let flush_at = self.config.get_flush_time(timestamp);
entry.insert(QueuedBucket::new(flush_at, value.into()));
}
Expand Down
22 changes: 21 additions & 1 deletion relay-metrics/src/statsd.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
use relay_statsd::{CounterMetric, GaugeMetric, HistogramMetric, TimerMetric};
use relay_statsd::{CounterMetric, GaugeMetric, HistogramMetric, SetMetric, TimerMetric};

pub enum MetricSets {
/// Count the number of unique buckets created.
///
/// This is a set of bucketing keys. The metric is basically equivalent to
/// `metrics.buckets.merge.miss` for a single Relay, but could be useful to determine how much
/// duplicate buckets there are when multiple instances are running.
///
/// The hashing is platform-dependent at the moment, so all your relays that send this metric
/// should run on the same CPU architecture, otherwise this metric is not reliable.
UniqueBucketsCreated,
}

impl SetMetric for MetricSets {
fn name(&self) -> &'static str {
match *self {
Self::UniqueBucketsCreated => "metrics.buckets.created.unique",
}
}
}

/// Counter metrics for Relay Metrics.
pub enum MetricCounters {
Expand Down