-
Notifications
You must be signed in to change notification settings - Fork 34
Add exemplar support #100
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
Merged
Merged
Add exemplar support #100
Changes from 3 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
2db4f32
Add exemplar support
emschwartz eac1f27
Simplify feature flag code
emschwartz 71d51fb
Clarify that the inner function in the example also has the trace_id
emschwartz f70bd7e
Enable customization of which fields will be extracted for the exempl…
emschwartz 70f11e9
Don't loop in tracing example
emschwartz 946cf17
Fix docs
emschwartz 68cbe87
Move exemplars to separate module
emschwartz 131c1a5
Rename example to exemplars-tracing
emschwartz 336ff73
Add exemplars to feature list
emschwartz 3b201a6
Remove from_field method
emschwartz 24a3d20
Add tests
emschwartz b870d51
Update changelog description
emschwartz ab602bd
Run tests with correct features
emschwartz e6f7ee7
Fix example name
emschwartz 6a213c0
Scrape metrics with Prometheus in exemplars example
emschwartz fa9203e
Create example showing how to expose metrics in the OpenMetrics format
emschwartz 6148f43
Remove trace_id as default field name
emschwartz 1c38a9c
Fix test
emschwartz 03e4b6a
Fix example import
emschwartz 05071e9
Fix run_prometheus signature
emschwartz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| #[cfg(feature = "prometheus-client")] | ||
| pub mod prometheus_client { | ||
| pub use crate::tracker::prometheus_client::REGISTRY; | ||
| } | ||
|
|
||
| #[cfg(feature = "exemplars-tracing")] | ||
| pub mod tracing; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| //! Tracing integration for autometrics | ||
| //! | ||
| //! This module enables autometrics to use the `trace_id` field from the current span as an exemplar. | ||
| //! Exemplars are a newer Prometheus / OpenMetrics / OpenTelemetry feature that allows you to associate | ||
| //! specific traces with a given metric. This enables you to dig into the specifics that produced | ||
| //! a certain metric by looking at a detailed example. | ||
| //! | ||
| //! # Example | ||
| //! ```rust | ||
| //! use autometrics::{autometrics, integrations::tracing::AutometricsLayer}; | ||
| //! use tracing::{instrument, trace}; | ||
| //! use tracing_subscriber::prelude::*; | ||
| //! use uuid::Uuid; | ||
| //! | ||
| //! #[autometrics] | ||
| //! #[instrument(fields(trace_id = %Uuid::new_v4())] | ||
| //! fn my_function() { | ||
| //! trace!("Hello world!"); | ||
| //! } | ||
| //! | ||
| //! fn main() { | ||
| //! tracing_subscriber::fmt::fmt() | ||
| //! .finish() | ||
| //! .with(AutometricsLayer::default()) | ||
| //! .init(); | ||
| //! } | ||
| //! ``` | ||
|
|
||
| use tracing::field::{Field, Visit}; | ||
| use tracing::{span::Attributes, Id, Subscriber}; | ||
| use tracing_subscriber::layer::{Context, Layer}; | ||
| use tracing_subscriber::registry::{LookupSpan, Registry}; | ||
|
|
||
| #[derive(Clone, Hash, PartialEq, Eq, Debug, Default)] | ||
| #[cfg_attr( | ||
| feature = "prometheus-client", | ||
| derive(prometheus_client::encoding::EncodeLabelSet) | ||
| )] | ||
| pub(crate) struct TraceLabel { | ||
| trace_id: String, | ||
| } | ||
|
|
||
| impl Visit for TraceLabel { | ||
| fn record_debug(&mut self, field: &Field, value: &dyn std::fmt::Debug) { | ||
| if field.name() == "trace_id" { | ||
| self.trace_id = format!("{:?}", value); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Get the exemplar from the current tracing span | ||
| pub(crate) fn get_exemplar() -> Option<TraceLabel> { | ||
| let span = tracing::span::Span::current(); | ||
|
|
||
| span.with_subscriber(|(id, sub)| { | ||
| sub.downcast_ref::<Registry>() | ||
| .and_then(|reg| reg.span(id)) | ||
| .and_then(|span| { | ||
| span.scope() | ||
| .find_map(|span| span.extensions().get::<TraceLabel>().cloned()) | ||
| }) | ||
| }) | ||
| .flatten() | ||
| } | ||
|
|
||
| /// A tracing [`Layer`] that enables autometrics to use the `trace_id` field from the current span | ||
| /// as an exemplar for Prometheus metrics. | ||
| /// | ||
| /// # Example | ||
| /// ```rust | ||
| /// use autometrics::integrations::tracing::AutometricsLayer; | ||
| /// | ||
| /// fn main() { | ||
| /// tracing_subscriber::fmt::fmt() | ||
| /// .finish() | ||
| /// .with(AutometricsLayer::default()) | ||
| /// .init(); | ||
| /// } | ||
| /// ``` | ||
| #[derive(Clone, Default)] | ||
| pub struct AutometricsLayer(); | ||
|
|
||
| impl<S: Subscriber + for<'lookup> LookupSpan<'lookup>> Layer<S> for AutometricsLayer { | ||
| fn on_new_span(&self, attrs: &Attributes<'_>, id: &Id, ctx: Context<'_, S>) { | ||
| let mut trace_label = TraceLabel::default(); | ||
| attrs.values().record(&mut trace_label); | ||
|
|
||
| if !trace_label.trace_id.is_empty() { | ||
| if let Some(span) = ctx.span(id) { | ||
| let mut ext = span.extensions_mut(); | ||
| ext.insert(trace_label); | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| [package] | ||
| name = "example-tracing" | ||
| version = "0.0.0" | ||
| publish = false | ||
| edition = "2021" | ||
|
|
||
| [dependencies] | ||
| autometrics = { path = "../../autometrics", features = ["prometheus-client", "prometheus-exporter", "exemplars-tracing"] } | ||
| tracing = "0.1" | ||
| tracing-subscriber = { version = "0.3", features = ["env-filter"] } | ||
| uuid = { version = "1.3", features = ["v4"] } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| # Tracing Exemplars Example | ||
|
|
||
| This example demonstrates how Autometrics can pick up the `trace_id` from [`tracing::Span`](https://docs.rs/tracing/latest/tracing/struct.Span.html)s and attach them to the metrics as [exemplars](https://grafana.com/docs/grafana/latest/fundamentals/exemplars/). | ||
|
|
||
| > **Note** | ||
| > | ||
| > Prometheus must be [specifically configured](https://prometheus.io/docs/prometheus/latest/feature_flags/#exemplars-storage) to enable the experimental exemplars feature. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| use autometrics::{autometrics, encode_global_metrics, integrations::tracing::AutometricsLayer}; | ||
| use tracing::{instrument, trace}; | ||
| use tracing_subscriber::{prelude::*, EnvFilter}; | ||
| use uuid::Uuid; | ||
|
|
||
| // Autometrics looks for a field called `trace_id` and attaches | ||
| // that as an exemplar for the metrics it generates. | ||
| #[autometrics] | ||
| #[instrument(fields(trace_id = %Uuid::new_v4()))] | ||
| fn outer_function() { | ||
| trace!("Outer function called"); | ||
| inner_function("hello") | ||
| } | ||
|
|
||
| // This function will also have the `trace_id` attached as an exemplar | ||
| // because it is called within the same span as `outer_function`. | ||
| #[autometrics] | ||
| #[instrument] | ||
| fn inner_function(param: &str) { | ||
| trace!("Inner function called"); | ||
| } | ||
|
|
||
| fn main() { | ||
| tracing_subscriber::fmt::fmt() | ||
| .finish() | ||
| .with(EnvFilter::from_default_env()) | ||
| .with(AutometricsLayer::default()) | ||
| .init(); | ||
|
|
||
| for _i in 0..10 { | ||
| outer_function(); | ||
| } | ||
emschwartz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| println!("{}", encode_global_metrics().unwrap()); | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.