Skip to content

Commit 54aa553

Browse files
authored
prometheus: update docs to highlight requirements around upkeep (#537)
1 parent d82bbb8 commit 54aa553

File tree

3 files changed

+116
-103
lines changed

3 files changed

+116
-103
lines changed

metrics-exporter-prometheus/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88

99
## [Unreleased] - ReleaseDate
1010

11+
### Changed
12+
13+
- Updated the crate-level documentation, and the documentation for `PrometheusBuilder::build_recorder` and
14+
`PrometheusBuilder::install_recorder`, to call out the requirements around running upkeep periodically.
15+
([#537](https://github.com/metrics-rs/metrics/pull/537))
16+
1117
## [0.16.0] - 2024-10-12
1218

1319
### Added

metrics-exporter-prometheus/src/exporter/builder.rs

Lines changed: 78 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ impl PrometheusBuilder {
8787
///
8888
/// The HTTP listener that is spawned will respond to GET requests on any request path.
8989
///
90-
/// Running in HTTP listener mode is mutually exclusive with the push gateway i.e. enabling the
91-
/// HTTP listener will disable the push gateway, and vise versa.
90+
/// Running in HTTP listener mode is mutually exclusive with the push gateway i.e. enabling the HTTP listener will
91+
/// disable the push gateway, and vise versa.
9292
///
9393
/// Defaults to enabled, listening at `0.0.0.0:9000`.
9494
///
@@ -105,15 +105,14 @@ impl PrometheusBuilder {
105105

106106
/// Configures the exporter to push periodic requests to a Prometheus [push gateway].
107107
///
108-
/// Running in push gateway mode is mutually exclusive with the HTTP listener i.e. enabling the
109-
/// push gateway will disable the HTTP listener, and vise versa.
108+
/// Running in push gateway mode is mutually exclusive with the HTTP listener i.e. enabling the push gateway will
109+
/// disable the HTTP listener, and vise versa.
110110
///
111111
/// Defaults to disabled.
112112
///
113113
/// ## Errors
114114
///
115-
/// If the given endpoint cannot be parsed into a valid URI, an error variant will be
116-
/// returned describing the error.
115+
/// If the given endpoint cannot be parsed into a valid URI, an error variant will be returned describing the error.
117116
///
118117
/// [push gateway]: https://prometheus.io/docs/instrumenting/pushing/
119118
#[cfg(feature = "push-gateway")]
@@ -139,13 +138,13 @@ impl PrometheusBuilder {
139138
Ok(self)
140139
}
141140

142-
/// Configures the exporter to expose an HTTP listener that functions as a [scrape endpoint],
143-
/// listening on a Unix Domain socket at the given path
141+
/// Configures the exporter to expose an HTTP listener that functions as a [scrape endpoint], listening on a Unix
142+
/// Domain socket at the given path
144143
///
145144
/// The HTTP listener that is spawned will respond to GET requests on any request path.
146145
///
147-
/// Running in HTTP listener mode is mutually exclusive with the push gateway i.e. enabling the
148-
/// HTTP listener will disable the push gateway, and vise versa.
146+
/// Running in HTTP listener mode is mutually exclusive with the push gateway i.e. enabling the HTTP listener will
147+
/// disable the push gateway, and vise versa.
149148
///
150149
/// Defaults to disabled.
151150
///
@@ -162,22 +161,21 @@ impl PrometheusBuilder {
162161

163162
/// Adds an IP address or subnet to the allowlist for the scrape endpoint.
164163
///
165-
/// If a client makes a request to the scrape endpoint and their IP is not present in the
166-
/// allowlist, either directly or within any of the allowed subnets, they will receive a 403
167-
/// Forbidden response.
164+
/// If a client makes a request to the scrape endpoint and their IP is not present in the allowlist, either directly
165+
/// or within any of the allowed subnets, they will receive a 403 Forbidden response.
168166
///
169167
/// Defaults to allowing all IPs.
170168
///
171169
/// ## Security Considerations
172170
///
173-
/// On its own, an IP allowlist is insufficient for access control, if the exporter is running
174-
/// in an environment alongside applications (such as web browsers) that are susceptible to [DNS
171+
/// On its own, an IP allowlist is insufficient for access control, if the exporter is running in an environment
172+
/// alongside applications (such as web browsers) that are susceptible to [DNS
175173
/// rebinding](https://en.wikipedia.org/wiki/DNS_rebinding) attacks.
176174
///
177175
/// ## Errors
178176
///
179-
/// If the given address cannot be parsed into an IP address or subnet, an error variant will be
180-
/// returned describing the error.
177+
/// If the given address cannot be parsed into an IP address or subnet, an error variant will be returned describing
178+
/// the error.
181179
#[cfg(feature = "http-listener")]
182180
#[cfg_attr(docsrs, doc(cfg(feature = "http-listener")))]
183181
pub fn add_allowed_address<A>(mut self, address: A) -> Result<Self, BuildError>
@@ -195,15 +193,15 @@ impl PrometheusBuilder {
195193

196194
/// Sets the quantiles to use when rendering histograms.
197195
///
198-
/// Quantiles represent a scale of 0 to 1, where percentiles represent a scale of 1 to 100, so
199-
/// a quantile of 0.99 is the 99th percentile, and a quantile of 0.99 is the 99.9th percentile.
196+
/// Quantiles represent a scale of 0 to 1, where percentiles represent a scale of 1 to 100, so a quantile of 0.99 is
197+
/// the 99th percentile, and a quantile of 0.99 is the 99.9th percentile.
200198
///
201-
/// 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
202-
/// that all histograms will be exposed as Prometheus summaries.
199+
/// 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
200+
/// histograms will be exposed as Prometheus summaries.
203201
///
204202
/// If buckets are set (via [`set_buckets`][Self::set_buckets] or
205-
/// [`set_buckets_for_metric`][Self::set_buckets_for_metric]) then all histograms will be exposed
206-
/// as summaries instead.
203+
/// [`set_buckets_for_metric`][Self::set_buckets_for_metric]) then all histograms will be exposed as summaries
204+
/// instead.
207205
///
208206
/// ## Errors
209207
///
@@ -219,17 +217,16 @@ impl PrometheusBuilder {
219217

220218
/// Sets the bucket width when using summaries.
221219
///
222-
/// Summaries are rolling, which means that they are divided into buckets of a fixed duration
223-
/// (width), and older buckets are dropped as they age out. This means data from a period as
224-
/// large as the width will be dropped at a time.
220+
/// Summaries are rolling, which means that they are divided into buckets of a fixed duration (width), and older
221+
/// buckets are dropped as they age out. This means data from a period as large as the width will be dropped at a
222+
/// time.
225223
///
226-
/// The total amount of data kept for a summary is the number of buckets times the bucket width.
227-
/// For example, a bucket count of 3 and a bucket width of 20 seconds would mean that 60 seconds
228-
/// of data is kept at most, with the oldest 20 second chunk of data being dropped as the
229-
/// summary rolls forward.
224+
/// The total amount of data kept for a summary is the number of buckets times the bucket width. For example, a
225+
/// bucket count of 3 and a bucket width of 20 seconds would mean that 60 seconds of data is kept at most, with the
226+
/// oldest 20 second chunk of data being dropped as the summary rolls forward.
230227
///
231-
/// Use more buckets with a smaller width to roll off smaller amounts of data at a time, or
232-
/// fewer buckets with a larger width to roll it off in larger chunks.
228+
/// Use more buckets with a smaller width to roll off smaller amounts of data at a time, or fewer buckets with a
229+
/// larger width to roll it off in larger chunks.
233230
///
234231
/// Defaults to 20 seconds.
235232
///
@@ -247,17 +244,16 @@ impl PrometheusBuilder {
247244

248245
/// Sets the bucket count when using summaries.
249246
///
250-
/// Summaries are rolling, which means that they are divided into buckets of a fixed duration
251-
/// (width), and older buckets are dropped as they age out. This means data from a period as
252-
/// large as the width will be dropped at a time.
247+
/// Summaries are rolling, which means that they are divided into buckets of a fixed duration (width), and older
248+
/// buckets are dropped as they age out. This means data from a period as large as the width will be dropped at a
249+
/// time.
253250
///
254-
/// The total amount of data kept for a summary is the number of buckets times the bucket width.
255-
/// For example, a bucket count of 3 and a bucket width of 20 seconds would mean that 60 seconds
256-
/// of data is kept at most, with the oldest 20 second chunk of data being dropped as the
257-
/// summary rolls forward.
251+
/// The total amount of data kept for a summary is the number of buckets times the bucket width. For example, a
252+
/// bucket count of 3 and a bucket width of 20 seconds would mean that 60 seconds of data is kept at most, with the
253+
/// oldest 20 second chunk of data being dropped as the summary rolls forward.
258254
///
259-
/// Use more buckets with a smaller width to roll off smaller amounts of data at a time, or
260-
/// fewer buckets with a larger width to roll it off in larger chunks.
255+
/// Use more buckets with a smaller width to roll off smaller amounts of data at a time, or fewer buckets with a
256+
/// larger width to roll it off in larger chunks.
261257
///
262258
/// Defaults to 3.
263259
#[must_use]
@@ -268,8 +264,8 @@ impl PrometheusBuilder {
268264

269265
/// Sets the buckets to use when rendering histograms.
270266
///
271-
/// Buckets values represent the higher bound of each buckets. If buckets are set, then all
272-
/// histograms will be rendered as true Prometheus histograms, instead of summaries.
267+
/// Buckets values represent the higher bound of each buckets. If buckets are set, then all histograms will be
268+
/// rendered as true Prometheus histograms, instead of summaries.
273269
///
274270
/// ## Errors
275271
///
@@ -285,16 +281,16 @@ impl PrometheusBuilder {
285281

286282
/// Sets the bucket for a specific pattern.
287283
///
288-
/// The match pattern can be a full match (equality), prefix match, or suffix match. The
289-
/// matchers are applied in that order if two or more matchers would apply to a single metric.
290-
/// That is to say, if a full match and a prefix match applied to a metric, the full match would
291-
/// win, and if a prefix match and a suffix match applied to a metric, the prefix match would win.
284+
/// The match pattern can be a full match (equality), prefix match, or suffix match. The matchers are applied in
285+
/// that order if two or more matchers would apply to a single metric. That is to say, if a full match and a prefix
286+
/// match applied to a metric, the full match would win, and if a prefix match and a suffix match applied to a
287+
/// metric, the prefix match would win.
292288
///
293-
/// Buckets values represent the higher bound of each buckets. If buckets are set, then any
294-
/// histograms that match will be rendered as true Prometheus histograms, instead of summaries.
289+
/// Buckets values represent the higher bound of each buckets. If buckets are set, then any histograms that match
290+
/// will be rendered as true Prometheus histograms, instead of summaries.
295291
///
296-
/// This option changes the observer's output of histogram-type metric into summaries.
297-
/// It only affects matching metrics if [`set_buckets`][Self::set_buckets] was not used.
292+
/// This option changes the observer's output of histogram-type metric into summaries. It only affects matching
293+
/// metrics if [`set_buckets`][Self::set_buckets] was not used.
298294
///
299295
/// ## Errors
300296
///
@@ -315,18 +311,17 @@ impl PrometheusBuilder {
315311

316312
/// Sets the idle timeout for metrics.
317313
///
318-
/// If a metric hasn't been updated within this timeout, it will be removed from the registry
319-
/// and in turn removed from the normal scrape output until the metric is emitted again. This
320-
/// behavior is driven by requests to generate rendered output, and so metrics will not be
321-
/// removed unless a request has been made recently enough to prune the idle metrics.
314+
/// If a metric hasn't been updated within this timeout, it will be removed from the registry and in turn removed
315+
/// from the normal scrape output until the metric is emitted again. This behavior is driven by requests to
316+
/// generate rendered output, and so metrics will not be removed unless a request has been made recently enough to
317+
/// prune the idle metrics.
322318
///
323-
/// Further, the metric kind "mask" configures which metrics will be considered by the idle
324-
/// timeout. If the kind of a metric being considered for idle timeout is not of a kind
325-
/// represented by the mask, it will not be affected, even if it would have othered been removed
326-
/// for exceeding the idle timeout.
319+
/// Further, the metric kind "mask" configures which metrics will be considered by the idle timeout. If the kind of
320+
/// a metric being considered for idle timeout is not of a kind represented by the mask, it will not be affected,
321+
/// even if it would have otherwise been removed for exceeding the idle timeout.
327322
///
328-
/// Refer to the documentation for [`MetricKindMask`](metrics_util::MetricKindMask) for more
329-
/// information on defining a metric kind mask.
323+
/// Refer to the documentation for [`MetricKindMask`](metrics_util::MetricKindMask) for more information on defining
324+
/// a metric kind mask.
330325
#[must_use]
331326
pub fn idle_timeout(mut self, mask: MetricKindMask, timeout: Option<Duration>) -> Self {
332327
self.idle_timeout = timeout;
@@ -336,8 +331,8 @@ impl PrometheusBuilder {
336331

337332
/// Sets the upkeep interval.
338333
///
339-
/// The upkeep task handles periodic maintenance operations, such as draining histogram data,
340-
/// to ensure that all recorded data is up-to-date and prevent unbounded memory growth.
334+
/// The upkeep task handles periodic maintenance operations, such as draining histogram data, to ensure that all
335+
/// recorded data is up-to-date and prevent unbounded memory growth.
341336
#[must_use]
342337
pub fn upkeep_timeout(mut self, timeout: Duration) -> Self {
343338
self.upkeep_timeout = timeout;
@@ -346,9 +341,8 @@ impl PrometheusBuilder {
346341

347342
/// Adds a global label to this exporter.
348343
///
349-
/// Global labels are applied to all metrics. Labels defined on the metric key itself have precedence
350-
/// over any global labels. If this method is called multiple times, the latest value for a given label
351-
/// key will be used.
344+
/// Global labels are applied to all metrics. Labels defined on the metric key itself have precedence over any
345+
/// global labels. If this method is called multiple times, the latest value for a given label key will be used.
352346
#[must_use]
353347
pub fn add_global_label<K, V>(mut self, key: K, value: V) -> Self
354348
where
@@ -362,14 +356,13 @@ impl PrometheusBuilder {
362356

363357
/// Builds the recorder and exporter and installs them globally.
364358
///
365-
/// When called from within a Tokio runtime, the exporter future is spawned directly
366-
/// into the runtime. Otherwise, a new single-threaded Tokio runtime is created
367-
/// on a background thread, and the exporter is spawned there.
359+
/// When called from within a Tokio runtime, the exporter future is spawned directly into the runtime. Otherwise, a
360+
/// new single-threaded Tokio runtime is created on a background thread, and the exporter is spawned there.
368361
///
369362
/// ## Errors
370363
///
371-
/// If there is an error while either building the recorder and exporter, or installing the
372-
/// recorder and exporter, an error variant will be returned describing the error.
364+
/// If there is an error while either building the recorder and exporter, or installing the recorder and exporter,
365+
/// an error variant will be returned describing the error.
373366
#[cfg(any(feature = "http-listener", feature = "push-gateway"))]
374367
#[cfg_attr(docsrs, doc(cfg(any(feature = "http-listener", feature = "push-gateway"))))]
375368
pub fn install(self) -> Result<(), BuildError> {
@@ -415,10 +408,13 @@ impl PrometheusBuilder {
415408
///
416409
/// The handle can be used to generate valid Prometheus scrape endpoint payloads directly.
417410
///
411+
/// The caller is responsible for ensuring that upkeep is run periodically. See the **Upkeep and maintenance**
412+
/// section in the top-level crate documentation for more information.
413+
///
418414
/// ## Errors
419415
///
420-
/// If there is an error while building the recorder, or installing the recorder, an error
421-
/// variant will be returned describing the error.
416+
/// If there is an error while building the recorder, or installing the recorder, an error variant will be returned
417+
/// describing the error.
422418
pub fn install_recorder(self) -> Result<PrometheusHandle, BuildError> {
423419
let recorder = self.build_recorder();
424420
let handle = recorder.handle();
@@ -430,19 +426,19 @@ impl PrometheusBuilder {
430426

431427
/// Builds the recorder and exporter and returns them both.
432428
///
433-
/// In most cases, users should prefer to use [`install`][PrometheusBuilder::install] to create
434-
/// and install the recorder and exporter automatically for them. If a caller is combining
435-
/// recorders, or needs to schedule the exporter to run in a particular way, this method, or
436-
/// [`build_recorder`][PrometheusBuilder::build_recorder], provide the flexibility to do so.
429+
/// In most cases, users should prefer to use [`install`][PrometheusBuilder::install] to create and install the
430+
/// recorder and exporter automatically for them. If a caller is combining recorders, or needs to schedule the
431+
/// exporter to run in a particular way, this method, or [`build_recorder`][PrometheusBuilder::build_recorder],
432+
/// provide the flexibility to do so.
437433
///
438434
/// ## Panics
439435
///
440436
/// This method must be called from within an existing Tokio runtime or it will panic.
441437
///
442438
/// ## Errors
443439
///
444-
/// If there is an error while building the recorder and exporter, an error variant will be
445-
/// returned describing the error.
440+
/// If there is an error while building the recorder and exporter, an error variant will be returned describing the
441+
/// error.
446442
#[warn(clippy::too_many_lines)]
447443
#[cfg(any(feature = "http-listener", feature = "push-gateway"))]
448444
#[cfg_attr(docsrs, doc(cfg(any(feature = "http-listener", feature = "push-gateway"))))]
@@ -495,6 +491,9 @@ impl PrometheusBuilder {
495491
}
496492

497493
/// Builds the recorder and returns it.
494+
///
495+
/// The caller is responsible for ensuring that upkeep is run periodically. See the **Upkeep and maintenance**
496+
/// section in the top-level crate documentation for more information.
498497
pub fn build_recorder(self) -> PrometheusRecorder {
499498
self.build_with_clock(Clock::new())
500499
}

0 commit comments

Comments
 (0)