Struct goodmetrics::Metrics

source ·
pub struct Metrics<TBuildHasher = RandomState> { /* private fields */ }
Expand description

A Metrics encapsulates a unit of work.

It is a record of the interesting things that happened during that unit, and the things that describe it.

A web request handler, grpc service rpc implementation or periodic job’s execution is a unit of work you should have a Metrics for.

Metrics does not deal in things like “gauges” or “counters.” It is more like a single-level trace; it works with concrete observations rather than exposing your code to aggregators.

Metrics objects are emitted through a reporter chain when they are Dropped. It is at that point that aggregation, if you’ve configured any, is performed.

Your code is responsible for putting the details of interest into the Metrics object as it encounters interesting details. You just record what you want to.

Generally prefer to record dimensions as early as possible, in case you early-out somewhere; in this way you’ll have more information in those cases.

Implementations§

source§

impl<TBuildHasher> Metrics<TBuildHasher>
where TBuildHasher: BuildHasher,

source

pub fn dimension(&mut self, name: impl Into<Name>, value: impl Into<Dimension>)

Record a dimension name and value pair - last write per metrics object wins!

source

pub fn measurement( &mut self, name: impl Into<Name>, value: impl Into<Observation>, )

Record a measurement name and value pair - last write per metrics object wins!

source

pub fn distribution( &mut self, name: impl Into<Name>, value: impl Into<Distribution>, )

Record a distribution name and value pair - last write per metrics object wins! Check out t-digests if you’re using a goodmetrics + timescale downstream.

source

pub fn sum(&mut self, name: impl Into<Name>, value: impl Into<i64>)

Record a sum. Repeated reports add together in this object.

Aggregation: Locally summed per report period.

source

pub fn time(&mut self, timer_name: impl Into<Name>) -> Timer

Record a time distribution in nanoseconds. Check out t-digests if you’re using a goodmetrics + timescale downstream.

The returned Timer is a scope guard.

source

pub fn guarded_dimension( &mut self, name: impl Into<Name>, default: impl Into<Dimension>, ) -> DimensionGuard

A dimension that you set a default for in case you drop early or something.

source

pub fn name(&self) -> &Name

Name of the metrics you passed in when you created it.

source

pub fn restart(&mut self)

Clear the structure in preparation for reuse without allocation. You still need to set the right behaviors and start times.

source

pub fn suppress(&mut self)

Do not report this metrics instance.

source

pub fn has_behavior(&self, behavior: MetricsBehavior) -> bool

Check if this metrics has that behavior.

source

pub unsafe fn add_behavior(&mut self, behavior: MetricsBehavior)

§Safety

This function is intended to be used by MetricsFactories while creating new instances. It is not intended for use outside of infrastructure code. It is exposed in case you have something special you need to do with your allocator. You shouldn’t call this unless you know you need to and provide your own guarantees about when the behavior is added and whether it’s legal & valid

source

pub unsafe fn set_raw_behavior(&mut self, behavior: u32)

§Safety

This function is intended to be used by MetricsFactories while creating new instances. It is not intended for use outside of infrastructure code. It is exposed in case you have something special you need to do with your allocator. You shouldn’t call this unless you know you need to and provide your own guarantees about when the behavior is added and whether it’s legal & valid

source

pub fn drain( &mut self, ) -> (&mut HashMap<Name, Dimension, TBuildHasher>, &mut HashMap<Name, Measurement, TBuildHasher>)

A sibling of restart(), this is a way to destructively move dimensions and measurements out of the metrics for aggregation or downstream message building.

Trait Implementations§

source§

impl<H> AsMut<Metrics<H>> for Metrics<H>

source§

fn as_mut(&mut self) -> &mut Metrics<H>

Converts this type into a mutable reference of the (usually inferred) input type.
source§

impl<TBuildHasher: BuildHasher + Default + Send> AsMut<Metrics<TBuildHasher>> for CachedMetrics<TBuildHasher>

source§

fn as_mut(&mut self) -> &mut Metrics<TBuildHasher>

Converts this type into a mutable reference of the (usually inferred) input type.
source§

impl<H> AsRef<Metrics<H>> for Metrics<H>

source§

fn as_ref(&self) -> &Metrics<H>

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl<TBuildHasher: BuildHasher + Default + Send> AsRef<Metrics<TBuildHasher>> for CachedMetrics<TBuildHasher>

source§

fn as_ref(&self) -> &Metrics<TBuildHasher>

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl<TBuildHasher: Debug> Debug for Metrics<TBuildHasher>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T> Display for Metrics<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<TBuildHasher> Freeze for Metrics<TBuildHasher>
where TBuildHasher: Freeze,

§

impl<TBuildHasher> RefUnwindSafe for Metrics<TBuildHasher>
where TBuildHasher: RefUnwindSafe,

§

impl<TBuildHasher> Send for Metrics<TBuildHasher>
where TBuildHasher: Send,

§

impl<TBuildHasher> Sync for Metrics<TBuildHasher>
where TBuildHasher: Sync,

§

impl<TBuildHasher> Unpin for Metrics<TBuildHasher>
where TBuildHasher: Unpin,

§

impl<TBuildHasher> UnwindSafe for Metrics<TBuildHasher>
where TBuildHasher: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> IntoRequest<T> for T

source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

impl<T, TBuildHasher> MetricsRef<TBuildHasher> for T
where T: AsRef<Metrics<TBuildHasher>> + AsMut<Metrics<TBuildHasher>> + 'static,