pub struct PrometheusBuilder { /* private fields */ }
Expand description

Builder for creating and installing a Prometheus recorder/exporter.

Implementations§

source§

impl PrometheusBuilder

source

pub fn new() -> Self

Creates a new PrometheusBuilder.

source

pub fn with_http_listener(self, addr: impl Into<SocketAddr>) -> Self

Available on crate feature http-listener only.

Configures the exporter to expose an HTTP listener that functions as a scrape endpoint.

The HTTP listener that is spawned will respond to GET requests on any request path.

Running in HTTP listener mode is mutually exclusive with the push gateway i.e. enabling the HTTP listener will disable the push gateway, and vise versa.

Defaults to enabled, listening at 0.0.0.0:9000.

source

pub fn with_push_gateway<T>( self, endpoint: T, interval: Duration, username: Option<String>, password: Option<String> ) -> Result<Self, BuildError>
where T: AsRef<str>,

Available on crate feature push-gateway only.

Configures the exporter to push periodic requests to a Prometheus push gateway.

Running in push gateway mode is mutually exclusive with the HTTP listener i.e. enabling the push gateway will disable the HTTP listener, and vise versa.

Defaults to disabled.

§Errors

If the given endpoint cannot be parsed into a valid URI, an error variant will be returned describing the error.

source

pub fn add_allowed_address<A>(self, address: A) -> Result<Self, BuildError>
where A: AsRef<str>,

Available on crate feature http-listener only.

Adds an IP address or subnet to the allowlist for the scrape endpoint.

If a client makes a request to the scrape endpoint and their IP is not present in the allowlist, either directly or within any of the allowed subnets, they will receive a 403 Forbidden response.

Defaults to allowing all IPs.

§Security Considerations

On its own, an IP allowlist is insufficient for access control, if the exporter is running in an environment alongside applications (such as web browsers) that are susceptible to DNS rebinding attacks.

§Errors

If the given address cannot be parsed into an IP address or subnet, an error variant will be returned describing the error.

source

pub fn set_quantiles(self, quantiles: &[f64]) -> Result<Self, BuildError>

Sets the quantiles to use when rendering histograms.

Quantiles represent a scale of 0 to 1, where percentiles represent a scale of 1 to 100, so a quantile of 0.99 is the 99th percentile, and a quantile of 0.99 is the 99.9th percentile.

Defaults to a hard-coded set of quantiles: 0.0, 0.5, 0.9, 0.95, 0.99, 0.999, and 1.0. This means that all histograms will be exposed as Prometheus summaries.

If buckets are set (via set_buckets or set_buckets_for_metric) then all histograms will be exposed as summaries instead.

§Errors

If quantiles is empty, an error variant will be thrown.

source

pub fn set_bucket_duration(self, value: Duration) -> Result<Self, BuildError>

Sets the bucket width when using summaries.

Summaries are rolling, which means that they are divided into buckets of a fixed duration (width), and older buckets are dropped as they age out. This means data from a period as large as the width will be dropped at a time.

The total amount of data kept for a summary is the number of buckets times the bucket width. For example, a bucket count of 3 and a bucket width of 20 seconds would mean that 60 seconds of data is kept at most, with the oldest 20 second chunk of data being dropped as the summary rolls forward.

Use more buckets with a smaller width to roll off smaller amounts of data at a time, or fewer buckets with a larger width to roll it off in larger chunks.

Defaults to 20 seconds.

§Errors

If the duration given is zero, an error variant will be thrown.

source

pub fn set_bucket_count(self, count: NonZeroU32) -> Self

Sets the bucket count when using summaries.

Summaries are rolling, which means that they are divided into buckets of a fixed duration (width), and older buckets are dropped as they age out. This means data from a period as large as the width will be dropped at a time.

The total amount of data kept for a summary is the number of buckets times the bucket width. For example, a bucket count of 3 and a bucket width of 20 seconds would mean that 60 seconds of data is kept at most, with the oldest 20 second chunk of data being dropped as the summary rolls forward.

Use more buckets with a smaller width to roll off smaller amounts of data at a time, or fewer buckets with a larger width to roll it off in larger chunks.

Defaults to 3.

source

pub fn set_buckets(self, values: &[f64]) -> Result<Self, BuildError>

Sets the buckets to use when rendering histograms.

Buckets values represent the higher bound of each buckets. If buckets are set, then all histograms will be rendered as true Prometheus histograms, instead of summaries.

§Errors

If values is empty, an error variant will be thrown.

source

pub fn set_buckets_for_metric( self, matcher: Matcher, values: &[f64] ) -> Result<Self, BuildError>

Sets the bucket for a specific pattern.

The match pattern can be a full match (equality), prefix match, or suffix match. The matchers are applied in that order if two or more matchers would apply to a single metric. That is to say, if a full match and a prefix match applied to a metric, the full match would win, and if a prefix match and a suffix match applied to a metric, the prefix match would win.

Buckets values represent the higher bound of each buckets. If buckets are set, then any histograms that match will be rendered as true Prometheus histograms, instead of summaries.

This option changes the observer’s output of histogram-type metric into summaries. It only affects matching metrics if set_buckets was not used.

§Errors

If values is empty, an error variant will be thrown.

source

pub fn idle_timeout( self, mask: MetricKindMask, timeout: Option<Duration> ) -> Self

Sets the idle timeout for metrics.

If a metric hasn’t been updated within this timeout, it will be removed from the registry and in turn removed from the normal scrape output until the metric is emitted again. This behavior is driven by requests to generate rendered output, and so metrics will not be removed unless a request has been made recently enough to prune the idle metrics.

Further, the metric kind “mask” configures which metrics will be considered by the idle timeout. If the kind of a metric being considered for idle timeout is not of a kind represented by the mask, it will not be affected, even if it would have othered been removed for exceeding the idle timeout.

Refer to the documentation for MetricKindMask for more information on defining a metric kind mask.

source

pub fn upkeep_timeout(self, timeout: Duration) -> Self

Sets the upkeep interval.

The upkeep task handles periodic maintenance operations, such as draining histogram data, to ensure that all recorded data is up-to-date and prevent unbounded memory growth.

source

pub fn add_global_label<K, V>(self, key: K, value: V) -> Self
where K: Into<String>, V: Into<String>,

Adds a global label to this exporter.

Global labels are applied to all metrics. Labels defined on the metric key itself have precedence over any global labels. If this method is called multiple times, the latest value for a given label key will be used.

source

pub fn install(self) -> Result<(), BuildError>

Available on crate features http-listener or push-gateway only.

Builds the recorder and exporter and installs them globally.

When called from within a Tokio runtime, the exporter future is spawned directly into the runtime. Otherwise, a new single-threaded Tokio runtime is created on a background thread, and the exporter is spawned there.

§Errors

If there is an error while either building the recorder and exporter, or installing the recorder and exporter, an error variant will be returned describing the error.

source

pub fn install_recorder(self) -> Result<PrometheusHandle, BuildError>

Builds the recorder and installs it globally, returning a handle to it.

The handle can be used to generate valid Prometheus scrape endpoint payloads directly.

§Errors

If there is an error while building the recorder, or installing the recorder, an error variant will be returned describing the error.

source

pub fn build(self) -> Result<(PrometheusRecorder, ExporterFuture), BuildError>

Available on crate features http-listener or push-gateway only.

Builds the recorder and exporter and returns them both.

In most cases, users should prefer to use install to create and install the recorder and exporter automatically for them. If a caller is combining recorders, or needs to schedule the exporter to run in a particular way, this method, or build_recorder, provide the flexibility to do so.

§Panics

This method must be called from within an existing Tokio runtime or it will panic.

§Errors

If there is an error while building the recorder and exporter, an error variant will be returned describing the error.

source

pub fn build_recorder(self) -> PrometheusRecorder

Builds the recorder and returns it.

Trait Implementations§

source§

impl Default for PrometheusBuilder

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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

source§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. 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<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