-
Notifications
You must be signed in to change notification settings - Fork 423
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
metrics: implement batch observer #429
Merged
Merged
Changes from 2 commits
Commits
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 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 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 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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
use crate::sdk::InstrumentationLibrary; | ||
use crate::{ | ||
metrics::{ | ||
sdk_api, AsyncRunner, BatchObserver, BatchObserverCallback, CounterBuilder, Descriptor, | ||
sdk_api, AsyncRunner, BatchObserver, BatchObserverResult, CounterBuilder, Descriptor, | ||
Measurement, NumberKind, ObserverResult, Result, SumObserverBuilder, UpDownCounterBuilder, | ||
UpDownSumObserverBuilder, ValueObserverBuilder, ValueRecorderBuilder, | ||
}, | ||
|
@@ -93,7 +93,7 @@ impl Meter { | |
SumObserverBuilder::new( | ||
self, | ||
name.into(), | ||
AsyncRunner::F64(Box::new(callback)), | ||
Some(AsyncRunner::F64(Box::new(callback))), | ||
NumberKind::F64, | ||
) | ||
} | ||
|
@@ -111,7 +111,7 @@ impl Meter { | |
UpDownSumObserverBuilder::new( | ||
self, | ||
name.into(), | ||
AsyncRunner::F64(Box::new(callback)), | ||
Some(AsyncRunner::F64(Box::new(callback))), | ||
NumberKind::F64, | ||
) | ||
} | ||
|
@@ -125,7 +125,7 @@ impl Meter { | |
ValueObserverBuilder::new( | ||
self, | ||
name.into(), | ||
AsyncRunner::F64(Box::new(callback)), | ||
Some(AsyncRunner::F64(Box::new(callback))), | ||
NumberKind::F64, | ||
) | ||
} | ||
|
@@ -163,7 +163,7 @@ impl Meter { | |
SumObserverBuilder::new( | ||
self, | ||
name.into(), | ||
AsyncRunner::I64(Box::new(callback)), | ||
Some(AsyncRunner::I64(Box::new(callback))), | ||
NumberKind::I64, | ||
) | ||
} | ||
|
@@ -181,7 +181,7 @@ impl Meter { | |
UpDownSumObserverBuilder::new( | ||
self, | ||
name.into(), | ||
AsyncRunner::I64(Box::new(callback)), | ||
Some(AsyncRunner::I64(Box::new(callback))), | ||
NumberKind::I64, | ||
) | ||
} | ||
|
@@ -195,7 +195,7 @@ impl Meter { | |
ValueObserverBuilder::new( | ||
self, | ||
name.into(), | ||
AsyncRunner::I64(Box::new(callback)), | ||
Some(AsyncRunner::I64(Box::new(callback))), | ||
NumberKind::I64, | ||
) | ||
} | ||
|
@@ -233,7 +233,7 @@ impl Meter { | |
SumObserverBuilder::new( | ||
self, | ||
name.into(), | ||
AsyncRunner::U64(Box::new(callback)), | ||
Some(AsyncRunner::U64(Box::new(callback))), | ||
NumberKind::U64, | ||
) | ||
} | ||
|
@@ -251,7 +251,7 @@ impl Meter { | |
UpDownSumObserverBuilder::new( | ||
self, | ||
name.into(), | ||
AsyncRunner::U64(Box::new(callback)), | ||
Some(AsyncRunner::U64(Box::new(callback))), | ||
NumberKind::U64, | ||
) | ||
} | ||
|
@@ -265,15 +265,75 @@ impl Meter { | |
ValueObserverBuilder::new( | ||
self, | ||
name.into(), | ||
AsyncRunner::U64(Box::new(callback)), | ||
Some(AsyncRunner::U64(Box::new(callback))), | ||
NumberKind::U64, | ||
) | ||
} | ||
|
||
/// Creates a new `BatchObserver` that supports making batches of observations for | ||
/// multiple instruments. | ||
pub fn batch_observer(&self, callback: BatchObserverCallback) -> BatchObserver<'_> { | ||
BatchObserver::new(self, AsyncRunner::Batch(callback)) | ||
/// Creates a new `BatchObserver` that supports making batches of observations | ||
/// for multiple instruments or returns an error if instrument initialization | ||
/// fails. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// use opentelemetry::{global, metrics::BatchObserverResult, KeyValue}; | ||
/// | ||
/// # fn init_observer() -> opentelemetry::metrics::Result<()> { | ||
/// let meter = global::meter("test"); | ||
/// | ||
/// meter.build_batch_observer(|batch| { | ||
/// let instrument = batch.u64_value_observer("test_instrument").try_init()?; | ||
/// | ||
/// Ok(move |result: BatchObserverResult| { | ||
/// result.observe(&[KeyValue::new("my-key", "my-value")], &[instrument.observation(1)]); | ||
/// }) | ||
/// })?; | ||
/// # Ok(()) | ||
/// # } | ||
/// ``` | ||
pub fn build_batch_observer<B, F>(&self, builder: B) -> Result<()> | ||
where | ||
B: Fn(BatchObserver<'_>) -> Result<F>, | ||
F: Fn(BatchObserverResult) + Send + Sync + 'static, | ||
{ | ||
let observer = builder(BatchObserver::new(self))?; | ||
self.core | ||
.new_batch_observer(AsyncRunner::Batch(Box::new(observer))) | ||
} | ||
|
||
/// Creates a new `BatchObserver` that supports making batches of observations | ||
/// for multiple instruments. | ||
/// | ||
/// # Panics | ||
/// | ||
/// Panics if instrument initialization or observer registration returns an | ||
/// error. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// use opentelemetry::{global, metrics::BatchObserverResult, KeyValue}; | ||
/// | ||
/// let meter = global::meter("test"); | ||
/// | ||
/// meter.batch_observer(|batch| { | ||
/// let instrument = batch.u64_value_observer("test_instrument").init(); | ||
/// | ||
/// move |result: BatchObserverResult| { | ||
/// result.observe(&[KeyValue::new("my-key", "my-value")], &[instrument.observation(1)]); | ||
/// } | ||
/// }); | ||
/// ``` | ||
pub fn batch_observer<B, F>(&self, builder: B) | ||
where | ||
B: Fn(BatchObserver<'_>) -> F, | ||
F: Fn(BatchObserverResult) + Send + Sync + 'static, | ||
{ | ||
let observer = builder(BatchObserver::new(self)); | ||
self.core | ||
.new_batch_observer(AsyncRunner::Batch(Box::new(observer))) | ||
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. Same here. |
||
.unwrap() | ||
} | ||
|
||
/// Atomically record a batch of measurements. | ||
|
@@ -306,7 +366,7 @@ impl Meter { | |
pub(crate) fn new_async_instrument( | ||
&self, | ||
descriptor: Descriptor, | ||
runner: AsyncRunner, | ||
runner: Option<AsyncRunner>, | ||
) -> Result<Arc<dyn sdk_api::AsyncInstrumentCore>> { | ||
self.core.new_async_instrument(descriptor, runner) | ||
} | ||
|
This file contains 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 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
Oops, something went wrong.
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.
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.
I'd probably just inline the call to
builder()
here, so you don't have to name the intermediate.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.
Are you suggesting this specifically?
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.
Yeah. Maybe the way rustfmt treats it's not actually an improvement in this case...