-
Notifications
You must be signed in to change notification settings - Fork 307
Add ability to control metric output level through spin_telemetry cargo feature #3619
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
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 |
|---|---|---|
|
|
@@ -88,20 +88,66 @@ pub(crate) fn otel_metrics_layer<S: Subscriber + for<'span> LookupSpan<'span>>( | |
| Ok(MetricsLayer::new(meter_provider)) | ||
| } | ||
|
|
||
| // The two mutually exclusive features cannot both select a default level. | ||
| #[cfg(all( | ||
| feature = "metrics-default-level-debug", | ||
| feature = "metrics-default-level-info" | ||
| ))] | ||
| compile_error!( | ||
| "features `metrics-default-level-debug` and `metrics-default-level-info` are mutually exclusive" | ||
| ); | ||
|
Comment on lines
+92
to
+98
Collaborator
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. Given that these are sort of "logically additive" it would be nice to treat this the same as just enabling
Collaborator
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. ...though annoyingly that already appears to be broken by |
||
|
|
||
| /// The [`tracing::Level`] at which the metrics macros (`counter!`, `histogram!`, | ||
| /// `monotonic_counter!`, `gauge!`) emit their events when no explicit `level:` is given. | ||
| /// | ||
| /// Defaults to [`tracing::Level::TRACE`]. Enable the `metrics-default-level-debug` or | ||
| /// `metrics-default-level-info` feature on `spin-telemetry` to raise it to `DEBUG` or `INFO`. | ||
| #[cfg(all( | ||
| not(feature = "metrics-default-level-debug"), | ||
| not(feature = "metrics-default-level-info") | ||
| ))] | ||
| pub const DEFAULT_METRICS_LEVEL: tracing::Level = tracing::Level::TRACE; | ||
|
|
||
| /// The [`tracing::Level`] at which the metrics macros (`counter!`, `histogram!`, | ||
| /// `monotonic_counter!`, `gauge!`) emit their events when no explicit `level:` is given. | ||
| /// | ||
| /// Defaults to [`tracing::Level::DEBUG`]. Enable the `metrics-default-level-info` or | ||
| /// remove the `metrics-default-level-debug` feature on `spin-telemetry` to raise it to `INFO` | ||
| /// or lower it to `TRACE` respectively. | ||
| #[cfg(all( | ||
| feature = "metrics-default-level-debug", | ||
| not(feature = "metrics-default-level-info") | ||
| ))] | ||
| pub const DEFAULT_METRICS_LEVEL: tracing::Level = tracing::Level::DEBUG; | ||
|
|
||
| /// The [`tracing::Level`] at which the metrics macros (`counter!`, `histogram!`, | ||
| /// `monotonic_counter!`, `gauge!`) emit their events when no explicit `level:` is given. | ||
| /// | ||
| /// Defaults to [`tracing::Level::INFO`]. Enable the `metrics-default-level-debug` or | ||
| /// remove the `metrics-default-level-info` feature on `spin-telemetry` to lower it to `DEBUG` | ||
| /// or `TRACE` respectively. | ||
| #[cfg(all( | ||
| feature = "metrics-default-level-info", | ||
| not(feature = "metrics-default-level-debug") | ||
| ))] | ||
| pub const DEFAULT_METRICS_LEVEL: tracing::Level = tracing::Level::INFO; | ||
|
|
||
| #[macro_export] | ||
| /// Records an increment to the named counter with the given attributes. | ||
| /// | ||
| /// The increment may only be an i64 or f64. You must not mix types for the same metric. | ||
| /// | ||
| /// Takes advantage of counter support in [tracing-opentelemetry](https://docs.rs/tracing-opentelemetry/0.32.0/tracing_opentelemetry/struct.MetricsLayer.html). | ||
| /// | ||
| /// The metric event is emitted at [`DEFAULT_METRICS_LEVEL`] (`TRACE` unless raised by a crate feature). | ||
| /// | ||
| /// ```no_run | ||
| /// # use spin_telemetry::metrics::counter; | ||
| /// counter!(spin.metric_name = 1, metric_attribute = "value"); | ||
| /// ``` | ||
| macro_rules! counter { | ||
| ($metric:ident $(. $suffixes:ident)* = $metric_value:expr $(, $attrs:ident=$values:expr)*) => { | ||
| tracing::trace!(counter.$metric $(. $suffixes)* = $metric_value $(, $attrs=$values)*); | ||
| tracing::event!($crate::metrics::DEFAULT_METRICS_LEVEL, counter.$metric $(. $suffixes)* = $metric_value $(, $attrs=$values)*); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -112,13 +158,15 @@ macro_rules! counter { | |
| /// | ||
| /// Takes advantage of histogram support in [tracing-opentelemetry](https://docs.rs/tracing-opentelemetry/0.32.0/tracing_opentelemetry/struct.MetricsLayer.html). | ||
| /// | ||
| /// The metric event is emitted at [`DEFAULT_METRICS_LEVEL`] (`TRACE` unless raised by a crate feature). | ||
| /// | ||
| /// ```no_run | ||
| /// # use spin_telemetry::metrics::histogram; | ||
| /// histogram!(spin.metric_name = 1.5, metric_attribute = "value"); | ||
| /// ``` | ||
| macro_rules! histogram { | ||
| ($metric:ident $(. $suffixes:ident)* = $metric_value:expr $(, $attrs:ident=$values:expr)*) => { | ||
| tracing::trace!(histogram.$metric $(. $suffixes)* = $metric_value $(, $attrs=$values)*); | ||
| tracing::event!($crate::metrics::DEFAULT_METRICS_LEVEL, histogram.$metric $(. $suffixes)* = $metric_value $(, $attrs=$values)*); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -129,30 +177,34 @@ macro_rules! histogram { | |
| /// | ||
| /// Takes advantage of monotonic counter support in [tracing-opentelemetry](https://docs.rs/tracing-opentelemetry/0.32.0/tracing_opentelemetry/struct.MetricsLayer.html). | ||
| /// | ||
| /// The metric event is emitted at [`DEFAULT_METRICS_LEVEL`] (`TRACE` unless raised by a crate feature). | ||
| /// | ||
| /// ```no_run | ||
| /// # use spin_telemetry::metrics::monotonic_counter; | ||
| /// monotonic_counter!(spin.metric_name = 1, metric_attribute = "value"); | ||
| /// ``` | ||
| macro_rules! monotonic_counter { | ||
| ($metric:ident $(. $suffixes:ident)* = $metric_value:expr $(, $attrs:ident=$values:expr)*) => { | ||
| tracing::trace!(monotonic_counter.$metric $(. $suffixes)* = $metric_value $(, $attrs=$values)*); | ||
| tracing::event!($crate::metrics::DEFAULT_METRICS_LEVEL, monotonic_counter.$metric $(. $suffixes)* = $metric_value $(, $attrs=$values)*); | ||
| } | ||
| } | ||
|
|
||
| #[macro_export] | ||
| /// Records an increment to the named monotonic counter with the given attributes. | ||
| /// Records the current value of the named gauge with the given attributes. | ||
| /// | ||
| /// The increment may only be a positive i64 or f64. You must not mix types for the same metric. | ||
| /// The value may only be a positive i64 or f64. You must not mix types for the same metric. | ||
| /// | ||
| /// Takes advantage of gauge support in [tracing-opentelemetry](https://docs.rs/tracing-opentelemetry/0.32.0/tracing_opentelemetry/struct.MetricsLayer.html). | ||
| /// | ||
| /// The metric event is emitted at [`DEFAULT_METRICS_LEVEL`] (`TRACE` unless raised by a crate feature). | ||
| /// | ||
| /// ```no_run | ||
| /// # use spin_telemetry::metrics::gauge; | ||
| /// gauge!(spin.metric_name = 1, metric_attribute = "value"); | ||
| /// ``` | ||
| macro_rules! gauge { | ||
| ($metric:ident $(. $suffixes:ident)* = $metric_value:expr $(, $attrs:ident=$values:expr)*) => { | ||
| tracing::trace!(gauge.$metric $(. $suffixes)* = $metric_value $(, $attrs=$values)*); | ||
| tracing::event!($crate::metrics::DEFAULT_METRICS_LEVEL, gauge.$metric $(. $suffixes)* = $metric_value $(, $attrs=$values)*); | ||
| } | ||
| } | ||
|
|
||
|
|
||
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.
FWIW we do already have
cfg-ifin the dep tree...Looking forward to bumping the MSRV too.