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: track lines rejected in prometheus metrics #25722

Merged
merged 2 commits into from
Dec 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
70 changes: 54 additions & 16 deletions influxdb3_write/src/write_buffer/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,51 @@ use metric::{Metric, Registry, U64Counter};

#[derive(Debug)]
pub(super) struct WriteMetrics {
write_lines_total: Metric<U64Counter>,
write_bytes_total: Metric<U64Counter>,
write_lines_count: Metric<U64Counter>,
Copy link
Member

Choose a reason for hiding this comment

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

write_lines_total is the more standard naming convention. Metric names should end in units, e.g. _seconds, _bytes, or _total for unit-less metrics. See https://prometheus.io/docs/practices/naming/

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed this in cd51bc2

write_lines_rejected_count: Metric<U64Counter>,
write_bytes_count: Metric<U64Counter>,
}

pub(super) const WRITE_LINES_TOTAL_NAME: &str = "influxdb3_write_lines_total";
pub(super) const WRITE_BYTES_TOTAL_NAME: &str = "influxdb3_write_bytes_total";
pub(super) const WRITE_LINES_METRIC_NAME: &str = "influxdb3_write_lines";
pub(super) const WRITE_LINES_REJECTED_METRIC_NAME: &str = "influxdb3_write_lines_rejected";
Copy link
Member

Choose a reason for hiding this comment

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

Should end in _total

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I mentioned this in the PR description, but our Prometheus exporter adds a "_total" to the end of metric names that use a counter on export, which is why I removed it from the names here.

Copy link
Member

Choose a reason for hiding this comment

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

Oh sorry, I should have read that closer!

pub(super) const WRITE_BYTES_METRIC_NAME: &str = "influxdb3_write_bytes";

impl WriteMetrics {
pub(super) fn new(metric_registry: &Registry) -> Self {
let write_lines_total = metric_registry.register_metric::<U64Counter>(
WRITE_LINES_TOTAL_NAME,
let write_lines_count = metric_registry.register_metric::<U64Counter>(
WRITE_LINES_METRIC_NAME,
"track total number of lines written to the database",
);
let write_bytes_total = metric_registry.register_metric::<U64Counter>(
WRITE_BYTES_TOTAL_NAME,
let write_lines_rejected_count = metric_registry.register_metric::<U64Counter>(
WRITE_LINES_REJECTED_METRIC_NAME,
"track total number of lines written to the database that were rejected",
);
let write_bytes_count = metric_registry.register_metric::<U64Counter>(
WRITE_BYTES_METRIC_NAME,
"track total number of bytes written to the database",
);
Self {
write_lines_total,
write_bytes_total,
write_lines_count,
write_lines_rejected_count,
write_bytes_count,
}
}

pub(super) fn record_lines<D: Into<String>>(&self, db: D, lines: u64) {
let db: Cow<'static, str> = Cow::from(db.into());
self.write_lines_total.recorder([("db", db)]).inc(lines);
self.write_lines_count.recorder([("db", db)]).inc(lines);
}

pub(super) fn record_lines_rejected<D: Into<String>>(&self, db: D, lines: u64) {
let db: Cow<'static, str> = Cow::from(db.into());
self.write_lines_rejected_count
.recorder([("db", db)])
.inc(lines);
}

pub(super) fn record_bytes<D: Into<String>>(&self, db: D, bytes: u64) {
let db: Cow<'static, str> = Cow::from(db.into());
self.write_bytes_total.recorder([("db", db)]).inc(bytes);
self.write_bytes_count.recorder([("db", db)]).inc(bytes);
}
}

Expand All @@ -53,15 +67,39 @@ mod tests {
assert_eq!(
64,
metrics
.write_lines_total
.write_lines_count
.get_observer(&Attributes::from(&[("db", "foo")]))
.unwrap()
.fetch()
);
assert_eq!(
256,
metrics
.write_lines_count
.get_observer(&Attributes::from(&[("db", "bar")]))
.unwrap()
.fetch()
);
}

#[test]
fn record_lines_rejected() {
let metric_registry = Registry::new();
let metrics = WriteMetrics::new(&metric_registry);
metrics.record_lines_rejected("foo", 64);
metrics.record_lines_rejected(String::from("bar"), 256);
assert_eq!(
64,
metrics
.write_lines_rejected_count
.get_observer(&Attributes::from(&[("db", "foo")]))
.unwrap()
.fetch()
);
assert_eq!(
256,
metrics
.write_lines_total
.write_lines_rejected_count
.get_observer(&Attributes::from(&[("db", "bar")]))
.unwrap()
.fetch()
Expand All @@ -77,15 +115,15 @@ mod tests {
assert_eq!(
64,
metrics
.write_bytes_total
.write_bytes_count
.get_observer(&Attributes::from(&[("db", "foo")]))
.unwrap()
.fetch()
);
assert_eq!(
256,
metrics
.write_bytes_total
.write_bytes_count
.get_observer(&Attributes::from(&[("db", "bar")]))
.unwrap()
.fetch()
Expand Down
38 changes: 33 additions & 5 deletions influxdb3_write/src/write_buffer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,9 +302,11 @@ impl WriteBufferImpl {
// Thus, after this returns, the data is both durable and queryable.
self.wal.write_ops(ops).await?;

// record metrics for lines written and bytes written, for valid lines
// record metrics for lines written, rejected, and bytes written
self.metrics
.record_lines(&db_name, result.line_count as u64);
self.metrics
.record_lines_rejected(&db_name, result.errors.len() as u64);
self.metrics
.record_bytes(&db_name, result.valid_bytes_count);

Expand Down Expand Up @@ -1002,7 +1004,9 @@ mod tests {
use iox_query::exec::IOxSessionContext;
use iox_time::{MockProvider, Time};
use metric::{Attributes, Metric, U64Counter};
use metrics::{WRITE_BYTES_TOTAL_NAME, WRITE_LINES_TOTAL_NAME};
use metrics::{
WRITE_BYTES_METRIC_NAME, WRITE_LINES_METRIC_NAME, WRITE_LINES_REJECTED_METRIC_NAME,
};
use object_store::local::LocalFileSystem;
use object_store::memory::InMemory;
use object_store::{ObjectStore, PutPayload};
Expand Down Expand Up @@ -2387,7 +2391,7 @@ mod tests {
assert!(result.is_ok());
}

#[tokio::test]
#[test_log::test(tokio::test)]
async fn write_metrics() {
let object_store = Arc::new(InMemory::new());
let (buf, metrics) = setup_with_metrics(
Expand All @@ -2397,10 +2401,13 @@ mod tests {
)
.await;
let lines_observer = metrics
.get_instrument::<Metric<U64Counter>>(WRITE_LINES_TOTAL_NAME)
.get_instrument::<Metric<U64Counter>>(WRITE_LINES_METRIC_NAME)
.unwrap();
let lines_rejected_observer = metrics
.get_instrument::<Metric<U64Counter>>(WRITE_LINES_REJECTED_METRIC_NAME)
.unwrap();
let bytes_observer = metrics
.get_instrument::<Metric<U64Counter>>(WRITE_BYTES_TOTAL_NAME)
.get_instrument::<Metric<U64Counter>>(WRITE_BYTES_METRIC_NAME)
.unwrap();

let db_1 = "foo";
Expand Down Expand Up @@ -2428,6 +2435,13 @@ mod tests {
.unwrap()
.fetch()
);
assert_eq!(
0,
lines_rejected_observer
.get_observer(&Attributes::from(&[("db", db_1)]))
.unwrap()
.fetch()
);
let mut bytes: usize = lp.lines().map(|l| l.len()).sum();
assert_eq!(
bytes as u64,
Expand Down Expand Up @@ -2459,6 +2473,13 @@ mod tests {
.unwrap()
.fetch()
);
assert_eq!(
0,
lines_rejected_observer
.get_observer(&Attributes::from(&[("db", db_1)]))
.unwrap()
.fetch()
);
bytes += lp.lines().map(|l| l.len()).sum::<usize>();
assert_eq!(
bytes as u64,
Expand Down Expand Up @@ -2495,6 +2516,13 @@ mod tests {
.unwrap()
.fetch()
);
assert_eq!(
1,
lines_rejected_observer
.get_observer(&Attributes::from(&[("db", db_2)]))
.unwrap()
.fetch()
);
// only take first three (valid) lines to get expected bytes:
let bytes: usize = lp.lines().take(3).map(|l| l.len()).sum();
assert_eq!(
Expand Down
Loading