-
Notifications
You must be signed in to change notification settings - Fork 417
chore: Abstract metrics meters for execution #6384
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,14 @@ | ||
| use std::{ | ||
| borrow::Cow, | ||
| sync::atomic::{AtomicU64, Ordering}, | ||
| sync::atomic::{AtomicI64, AtomicU64, Ordering}, | ||
| }; | ||
|
|
||
| use atomic_float::AtomicF64; | ||
| use opentelemetry::{KeyValue, metrics::Meter}; | ||
| use opentelemetry::{InstrumentationScope, KeyValue, global}; | ||
|
|
||
| use crate::{ | ||
| ATTR_QUERY_ID, DURATION_KEY, QueryID, ROWS_IN_KEY, ROWS_OUT_KEY, UNIT_MICROSECONDS, UNIT_ROWS, | ||
| }; | ||
|
|
||
| pub fn normalize_name(name: impl Into<Cow<'static, str>>) -> String { | ||
| let name = name.into(); | ||
|
|
@@ -21,8 +25,8 @@ pub struct Counter { | |
| } | ||
|
|
||
| impl Counter { | ||
| pub fn new( | ||
| meter: &Meter, | ||
| fn new( | ||
| meter: &opentelemetry::metrics::Meter, | ||
| name: impl Into<Cow<'static, str>>, | ||
| description: Option<Cow<'static, str>>, | ||
| unit: Option<Cow<'static, str>>, | ||
|
|
@@ -62,8 +66,8 @@ pub struct Gauge { | |
| } | ||
|
|
||
| impl Gauge { | ||
| pub fn new( | ||
| meter: &Meter, | ||
| fn new( | ||
| meter: &opentelemetry::metrics::Meter, | ||
| name: impl Into<Cow<'static, str>>, | ||
| description: Option<Cow<'static, str>>, | ||
| ) -> Self { | ||
|
|
@@ -89,3 +93,97 @@ impl Gauge { | |
| self.value.load(ordering) | ||
| } | ||
| } | ||
|
|
||
| pub struct UpDownCounter { | ||
| value: AtomicI64, | ||
| otel: opentelemetry::metrics::UpDownCounter<i64>, | ||
| } | ||
|
|
||
| impl UpDownCounter { | ||
| fn new(meter: &opentelemetry::metrics::Meter, name: impl Into<Cow<'static, str>>) -> Self { | ||
| let normalized_name = normalize_name(name); | ||
| let builder = meter.i64_up_down_counter(normalized_name); | ||
| Self { | ||
| value: AtomicI64::new(0), | ||
| otel: builder.build(), | ||
| } | ||
| } | ||
|
|
||
| pub fn add(&self, value: i64, key_values: &[KeyValue]) { | ||
| self.value.fetch_add(value, Ordering::Relaxed); | ||
| self.otel.add(value, key_values); | ||
| } | ||
|
|
||
| pub fn load(&self, ordering: Ordering) -> i64 { | ||
| self.value.load(ordering) | ||
| } | ||
| } | ||
|
|
||
| #[derive(Clone)] | ||
| pub struct Meter { | ||
| otel: opentelemetry::metrics::Meter, | ||
| } | ||
|
|
||
| impl Meter { | ||
| pub fn query_scope(query_id: QueryID, name: impl Into<Cow<'static, str>>) -> Self { | ||
| let scope = InstrumentationScope::builder(name) | ||
| .with_attributes(vec![KeyValue::new(ATTR_QUERY_ID, query_id)]) | ||
| .build(); | ||
|
|
||
| let otel = global::meter_with_scope(scope); | ||
| Self { otel } | ||
| } | ||
|
|
||
| pub fn test_scope(name: &'static str) -> Self { | ||
| let otel = global::meter(name); | ||
| Self { otel } | ||
| } | ||
|
|
||
| pub fn u64_counter(&self, name: impl Into<Cow<'static, str>>) -> Counter { | ||
| Counter::new(&self.otel, name, None, None) | ||
| } | ||
|
|
||
| pub fn u64_counter_with_desc_and_unit( | ||
| &self, | ||
| name: impl Into<Cow<'static, str>>, | ||
| description: Option<Cow<'static, str>>, | ||
| unit: Option<Cow<'static, str>>, | ||
| ) -> Counter { | ||
| Counter::new(&self.otel, name, description, unit) | ||
| } | ||
|
Comment on lines
+142
to
+153
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Two similar
pub fn u64_counter(
&self,
name: impl Into<Cow<'static, str>>,
description: Option<Cow<'static, str>>,
unit: Option<Cow<'static, str>>,
) -> Counter {
Counter::new(&self.otel, name, description, unit)
}All call sites that currently pass no description/unit would use Rule Used: Prefer single parametrized functions over multiple... (source) Learnt From Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||
|
|
||
| pub fn f64_gauge(&self, name: impl Into<Cow<'static, str>>) -> Gauge { | ||
| Gauge::new(&self.otel, name, None) | ||
| } | ||
|
|
||
| pub fn i64_up_down_counter(&self, name: impl Into<Cow<'static, str>>) -> UpDownCounter { | ||
| UpDownCounter::new(&self.otel, name) | ||
| } | ||
|
|
||
| pub fn duration_us_metric(&self) -> Counter { | ||
| Counter::new( | ||
| &self.otel, | ||
| DURATION_KEY, | ||
| None, | ||
| Some(Cow::Borrowed(UNIT_MICROSECONDS)), | ||
| ) | ||
| } | ||
|
|
||
| pub fn rows_in_metric(&self) -> Counter { | ||
| Counter::new( | ||
| &self.otel, | ||
| ROWS_IN_KEY, | ||
| None, | ||
| Some(Cow::Borrowed(UNIT_ROWS)), | ||
| ) | ||
| } | ||
|
|
||
| pub fn rows_out_metric(&self) -> Counter { | ||
| Counter::new( | ||
| &self.otel, | ||
| ROWS_OUT_KEY, | ||
| None, | ||
| Some(Cow::Borrowed(UNIT_ROWS)), | ||
| ) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
test_scopeis public and not#[cfg(test)]-gatedMeter::test_scopeis intended only for test usage (as the name implies and as it's only used in#[cfg(test)]blocks), but it is declaredpub fnwithout a#[cfg(test)]guard. This means the method is compiled into production binaries and exposed as part of the public API of thecommon_metricscrate.Consider gating it with
#[cfg(test)]or, if it also needs to be available to integration tests in other crates,#[cfg(any(test, feature = "test-utils"))]: