1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//! Measurements the pipeline takes for a host that exports metrics.
//!
//! The engine is shared with hosts that have no metrics pipeline -- a
//! Cloudflare Worker, a test -- and builds for wasm32, so it does not depend on
//! OpenTelemetry or any other metrics crate. Instead a host that does export
//! metrics installs an [`Observer`] once, at start-up, and the pipeline calls
//! it with what it measured.
//!
//! # What it costs when nothing is installed
//!
//! One load of an initialised-or-not flag per measurement point, and nothing
//! else: the clock is not even read unless an observer is there to be told.
//! That is the point of doing it this way rather than always timing and
//! throwing the result away -- a request that nobody is measuring pays for
//! nothing it does not use.
//!
//! # Why a process-wide hook
//!
//! Metrics are process-wide anyway: an OpenTelemetry meter provider and a
//! Prometheus registry are each one per process, and so is the endpoint a
//! scraper reads. Threading an observer through [`State`](crate::State) would
//! give every host a method to implement for something only one of them uses.
use OnceLock;
use Duration;
// `std::time` natively; on wasm32, where `Instant::now()` panics, the
// JavaScript host's clock.
use Instant;
/// What a host wants to be told about. Every method has a default that
/// ignores the measurement, so an observer names only what it records, and a
/// measurement added here later breaks no host's build.
static OBSERVER: = new;
/// Install the observer, once per process.
///
/// Returns the observer back if one is already installed: the first one
/// stays, since measurements already sent to it would otherwise be split
/// between two pipelines.
/// A timer for the wait for a transaction, started only if somebody will be
/// told what it measured.
pub
/// Report what [`begin_timer`] started.
pub