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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use core::fmt;
use once_cell::sync::OnceCell;
use opentelemetry::metrics::{MetricsError, Result};
use opentelemetry_sdk::metrics::{
    reader::{AggregationSelector, MetricProducer},
    ManualReaderBuilder,
};
use std::sync::{Arc, Mutex};

use crate::{Collector, PrometheusExporter, ResourceSelector};

/// [PrometheusExporter] configuration options
#[derive(Default)]
pub struct ExporterBuilder {
    registry: Option<prometheus::Registry>,
    disable_target_info: bool,
    without_units: bool,
    without_counter_suffixes: bool,
    namespace: Option<String>,
    disable_scope_info: bool,
    reader: ManualReaderBuilder,
    resource_selector: ResourceSelector,
}

impl fmt::Debug for ExporterBuilder {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ExporterBuilder")
            .field("registry", &self.registry)
            .field("disable_target_info", &self.disable_target_info)
            .field("without_units", &self.without_units)
            .field("without_counter_suffixes", &self.without_counter_suffixes)
            .field("namespace", &self.namespace)
            .field("disable_scope_info", &self.disable_scope_info)
            .finish()
    }
}

impl ExporterBuilder {
    /// Disables exporter's addition of unit suffixes to metric names.
    ///
    /// By default, metric names include a unit suffix to follow Prometheus naming
    /// conventions. For example, the counter metric `request.duration`, with unit
    /// `ms` would become `request_duration_milliseconds_total`.
    ///
    /// With this option set, the name would instead be `request_duration_total`.
    pub fn without_units(mut self) -> Self {
        self.without_units = true;
        self
    }

    /// Disables exporter's addition `_total` suffixes on counters.
    ///
    /// By default, metric names include a `_total` suffix to follow Prometheus
    /// naming conventions. For example, the counter metric `happy.people` would
    /// become `happy_people_total`. With this option set, the name would instead be
    /// `happy_people`.
    pub fn without_counter_suffixes(mut self) -> Self {
        self.without_counter_suffixes = true;
        self
    }

    /// Configures the exporter to not export the resource `target_info` metric.
    ///
    /// If not specified, the exporter will create a `target_info` metric containing
    /// the metrics' [Resource] attributes.
    ///
    /// [Resource]: opentelemetry_sdk::Resource
    pub fn without_target_info(mut self) -> Self {
        self.disable_target_info = true;
        self
    }

    /// Configures the exporter to not export the `otel_scope_info` metric.
    ///
    /// If not specified, the exporter will create a `otel_scope_info` metric
    /// containing the metrics' Instrumentation Scope, and also add labels about
    /// Instrumentation Scope to all metric points.
    pub fn without_scope_info(mut self) -> Self {
        self.disable_scope_info = true;
        self
    }

    /// Configures the exporter to prefix metrics with the given namespace.
    ///
    /// Metrics such as `target_info` and `otel_scope_info` are not prefixed since
    /// these have special behavior based on their name.
    pub fn with_namespace(mut self, namespace: impl Into<String>) -> Self {
        let mut namespace = namespace.into();

        // namespace and metric names should be separated with an underscore,
        // adds a trailing underscore if there is not one already.
        if !namespace.ends_with('_') {
            namespace.push('_')
        }

        self.namespace = Some(namespace);
        self
    }

    /// Configures which [prometheus::Registry] the exporter will use.
    ///
    /// If no registry is specified, the prometheus default is used.
    pub fn with_registry(mut self, registry: prometheus::Registry) -> Self {
        self.registry = Some(registry);
        self
    }

    /// Configure the [AggregationSelector] the exporter will use.
    ///
    /// If no selector is provided, the [DefaultAggregationSelector] is used.
    ///
    /// [DefaultAggregationSelector]: opentelemetry_sdk::metrics::reader::DefaultAggregationSelector
    pub fn with_aggregation_selector(mut self, agg: impl AggregationSelector + 'static) -> Self {
        self.reader = self.reader.with_aggregation_selector(agg);
        self
    }

    /// Configures whether to export resource as attributes with every metric.
    ///
    /// Note that this is orthogonal to the `target_info` metric, which can be disabled using `without_target_info`.
    ///
    /// If you called `without_target_info` and `with_resource_selector` with `ResourceSelector::None`, resource will not be exported at all.
    pub fn with_resource_selector(
        mut self,
        resource_selector: impl Into<ResourceSelector>,
    ) -> Self {
        self.resource_selector = resource_selector.into();
        self
    }

    /// Registers an external [MetricProducer] with this reader.
    ///
    /// The producer is used as a source of aggregated metric data which is
    /// incorporated into metrics collected from the SDK.
    pub fn with_producer(mut self, producer: impl MetricProducer + 'static) -> Self {
        self.reader = self.reader.with_producer(producer);
        self
    }

    /// Creates a new [PrometheusExporter] from this configuration.
    pub fn build(self) -> Result<PrometheusExporter> {
        let reader = Arc::new(self.reader.build());

        let collector = Collector {
            reader: Arc::clone(&reader),
            disable_target_info: self.disable_target_info,
            without_units: self.without_units,
            without_counter_suffixes: self.without_counter_suffixes,
            disable_scope_info: self.disable_scope_info,
            create_target_info_once: OnceCell::new(),
            namespace: self.namespace,
            inner: Mutex::new(Default::default()),
            resource_selector: self.resource_selector,
            resource_labels_once: OnceCell::new(),
        };

        let registry = self.registry.unwrap_or_default();
        registry
            .register(Box::new(collector))
            .map_err(|e| MetricsError::Other(e.to_string()))?;

        Ok(PrometheusExporter { reader })
    }
}