forked from vectordotdev/vector
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(observability): ensure
sent_event
and received_event
metric…
…s are estimated json size (vectordotdev#17465) This PR creates a newtype - [`JsonSize`](https://github.com/vectordotdev/vector/blob/stephen/event_json_size/lib/vector-common/src/json_size.rs) that is returned by the `EstimatedJsonEncodedSizeOf::estimated_json_encoded_size_of` trait function. The events that emit a `component_received_event_bytes_total` or `component_sent_event_bytes_total` event accept `JsonSize`. This allows us to use the compiler to ensure we are emitting the correct measurement. A number of components needed changing to ensure this worked. --------- Signed-off-by: Stephen Wakely <fungus.humungus@gmail.com>
- Loading branch information
1 parent
dbd7151
commit 3b2a2be
Showing
87 changed files
with
807 additions
and
449 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,105 @@ | ||
use std::{ | ||
fmt, | ||
iter::Sum, | ||
ops::{Add, AddAssign, Sub}, | ||
}; | ||
|
||
/// A newtype for the JSON size of an event. | ||
/// Used to emit the `component_received_event_bytes_total` and | ||
/// `component_sent_event_bytes_total` metrics. | ||
#[derive(Clone, Copy, Default, Debug, PartialEq, Eq, PartialOrd, Ord)] | ||
pub struct JsonSize(usize); | ||
|
||
impl fmt::Display for JsonSize { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!(f, "{}", self.0) | ||
} | ||
} | ||
|
||
impl Sub for JsonSize { | ||
type Output = JsonSize; | ||
|
||
#[inline] | ||
fn sub(mut self, rhs: Self) -> Self::Output { | ||
self.0 -= rhs.0; | ||
self | ||
} | ||
} | ||
|
||
impl Add for JsonSize { | ||
type Output = JsonSize; | ||
|
||
#[inline] | ||
fn add(mut self, rhs: Self) -> Self::Output { | ||
self.0 += rhs.0; | ||
self | ||
} | ||
} | ||
|
||
impl AddAssign for JsonSize { | ||
#[inline] | ||
fn add_assign(&mut self, rhs: Self) { | ||
self.0 += rhs.0; | ||
} | ||
} | ||
|
||
impl Sum for JsonSize { | ||
#[inline] | ||
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self { | ||
let mut accum = 0; | ||
for val in iter { | ||
accum += val.get(); | ||
} | ||
|
||
JsonSize::new(accum) | ||
} | ||
} | ||
|
||
impl From<usize> for JsonSize { | ||
#[inline] | ||
fn from(value: usize) -> Self { | ||
Self(value) | ||
} | ||
} | ||
|
||
impl JsonSize { | ||
/// Create a new instance with the specified size. | ||
#[must_use] | ||
#[inline] | ||
pub const fn new(size: usize) -> Self { | ||
Self(size) | ||
} | ||
|
||
/// Create a new instance with size 0. | ||
#[must_use] | ||
#[inline] | ||
pub const fn zero() -> Self { | ||
Self(0) | ||
} | ||
|
||
/// Returns the contained size. | ||
#[must_use] | ||
#[inline] | ||
pub fn get(&self) -> usize { | ||
self.0 | ||
} | ||
} | ||
|
||
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] | ||
#[allow(clippy::module_name_repetitions)] | ||
pub struct NonZeroJsonSize(JsonSize); | ||
|
||
impl NonZeroJsonSize { | ||
#[must_use] | ||
#[inline] | ||
pub fn new(size: JsonSize) -> Option<Self> { | ||
(size.0 > 0).then_some(NonZeroJsonSize(size)) | ||
} | ||
} | ||
|
||
impl From<NonZeroJsonSize> for JsonSize { | ||
#[inline] | ||
fn from(value: NonZeroJsonSize) -> Self { | ||
value.0 | ||
} | ||
} |
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
Oops, something went wrong.