Struct sentry::metrics::Metric

source ·
pub struct Metric { /* private fields */ }
Expand description

A metric value that contains a numeric value and metadata to be sent to Sentry.

§Units

To make the most out of metrics in Sentry, consider assigning a unit during construction. This can be achieved using the with_unit builder method. See the documentation for more examples on units.

use sentry::metrics::{Metric, InformationUnit};

Metric::distribution("request.size", 47.2)
    .with_unit(InformationUnit::Byte)
    .send();

§Sending Metrics

Metrics can be sent to Sentry directly using the send method on the constructor. This will send the metric to the Client on the current Hub. If there is no client on the current hub, the metric is dropped.

use sentry::metrics::Metric;

Metric::count("requests")
    .with_tag("method", "GET")
    .send();

§Sending to a Custom Client

Metrics can also be sent to a custom client. This is useful if you want to send metrics to a different Sentry project or with different configuration. To do so, finish building the metric and then call add_metric to the client:

use sentry::Hub;
use sentry::metrics::Metric;

let metric = Metric::count("requests")
   .with_tag("method", "GET")
   .finish();

// Obtain a client from somewhere
if let Some(client) = Hub::current().client() {
    client.add_metric(metric);
}

Implementations§

source§

impl Metric

source

pub fn build( name: impl Into<Cow<'static, str>>, value: MetricValue ) -> MetricBuilder

Creates a new metric with the stated name and value.

The provided name identifies the metric in Sentry. It should consist of alphanumeric characters and _, -, and .. While a single forward slash (/) is also allowed in metric names, it has a special meaning and should not be used in regular metric names. All characters that do not match this criteria are sanitized.

The value of the metric determines its type. See the struct-level docs and constructor methods for examples on how to build metrics.

source

pub fn parse_statsd(string: &str) -> Result<Metric, ParseMetricError>

Parses a metric from a StatsD string.

This supports regular StatsD payloads with an extension for tags. In the below example, tags are optional:

<metricname>:<value>|<type>|#<tag1>:<value1>,<tag2>:<value2>

Units are encoded into the metric name, separated by an @:

<metricname>@<unit>:<value>|<type>|#<tag1>:<value1>,<tag2>:<value2>
source

pub fn incr(name: impl Into<Cow<'static, str>>, value: f64) -> MetricBuilder

Builds a metric that increments a counter by the given value.

§Example
use sentry::metrics::{Metric};

Metric::incr("operation.total_values", 7.0).send();
source

pub fn count(name: impl Into<Cow<'static, str>>) -> MetricBuilder

Builds a metric that counts the single occurrence of an event.

§Example
use sentry::metrics::{Metric};

Metric::count("requests").send();
source

pub fn timing( name: impl Into<Cow<'static, str>>, timing: Duration ) -> MetricBuilder

Builds a metric that tracks the duration of an operation.

This is a distribution metric that is tracked in seconds.

§Example
use std::time::Duration;
use sentry::metrics::{Metric};

Metric::timing("operation", Duration::from_secs(1)).send();
source

pub fn distribution( name: impl Into<Cow<'static, str>>, value: f64 ) -> MetricBuilder

Builds a metric that tracks the distribution of values.

§Example
use sentry::metrics::{Metric};

Metric::distribution("operation.batch_size", 42.0).send();
source

pub fn set(name: impl Into<Cow<'static, str>>, string: &str) -> MetricBuilder

Builds a metric that tracks the unique number of values provided.

See MetricValue for more ways to construct sets.

§Example
use sentry::metrics::{Metric};

Metric::set("users", "user1").send();
source

pub fn gauge(name: impl Into<Cow<'static, str>>, value: f64) -> MetricBuilder

Builds a metric that tracks the snapshot of provided values.

§Example
use sentry::metrics::{Metric};

Metric::gauge("cache.size", 42.0).send();
source

pub fn send(self)

Sends the metric to the current client.

When building a metric, you can use MetricBuilder::send to send the metric directly. If there is no client on the current Hub, the metric is dropped.

Trait Implementations§

source§

impl Debug for Metric

source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Metric

§

impl RefUnwindSafe for Metric

§

impl Send for Metric

§

impl Sync for Metric

§

impl Unpin for Metric

§

impl UnwindSafe for Metric

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> 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> Same for T

§

type Output = T

Should always be Self
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> 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> SendSyncUnwindSafe for T
where T: Send + Sync + UnwindSafe + ?Sized,