opentelemetry_sdk/metrics/
view.rs

1use super::instrument::{Instrument, Stream};
2
3/// Used to customize the metrics that are output by the SDK.
4///
5/// Here are some examples when a [View] might be needed:
6///
7/// * Customize which Instruments are to be processed/ignored. For example, an
8///   instrumented library can provide both temperature and humidity, but the
9///   application developer might only want temperature.
10/// * Customize the aggregation - if the default aggregation associated with the
11///   [Instrument] does not meet the needs of the user. For example, an HTTP client
12///   library might expose HTTP client request duration as Histogram by default,
13///   but the application developer might only want the total count of outgoing
14///   requests.
15/// * Customize which attribute(s) are to be reported on metrics. For example,
16///   an HTTP server library might expose HTTP verb (e.g. GET, POST) and HTTP
17///   status code (e.g. 200, 301, 404). The application developer might only care
18///   about HTTP status code (e.g. reporting the total count of HTTP requests for
19///   each HTTP status code). There could also be extreme scenarios in which the
20///   application developer does not need any attributes (e.g. just get the total
21///   count of all incoming requests).
22///
23/// # Example Custom View
24///
25/// View is implemented for all `Fn(&Instrument) -> Option<Stream>`.
26///
27/// ```
28/// use opentelemetry_sdk::metrics::{Instrument, SdkMeterProvider, Stream};
29///
30/// // return streams for the given instrument
31/// let my_view = |i: &Instrument| {
32///   // return Some(Stream) or
33///   None
34/// };
35///
36/// let provider = SdkMeterProvider::builder().with_view(my_view).build();
37/// # drop(provider)
38/// ```
39pub(crate) trait View: Send + Sync + 'static {
40    /// Defines how data should be collected for certain instruments.
41    ///
42    /// Return [Stream] to use for matching [Instrument]s,
43    /// otherwise if there is no match, return `None`.
44    fn match_inst(&self, inst: &Instrument) -> Option<Stream>;
45}
46
47impl<T> View for T
48where
49    T: Fn(&Instrument) -> Option<Stream> + Send + Sync + 'static,
50{
51    fn match_inst(&self, inst: &Instrument) -> Option<Stream> {
52        self(inst)
53    }
54}