opentelemetry_spanprocessor_any/metrics/
descriptor.rs

1use crate::metrics::{InstrumentConfig, InstrumentKind, NumberKind};
2use crate::sdk::InstrumentationLibrary;
3use fnv::FnvHasher;
4use std::borrow::Cow;
5use std::hash::{Hash, Hasher};
6
7/// Descriptor contains all the settings that describe an instrument, including
8/// its name, metric kind, number kind, and the configurable options.
9#[derive(Clone, Debug, PartialEq)]
10pub struct Descriptor {
11    name: String,
12    instrument_kind: InstrumentKind,
13    number_kind: NumberKind,
14    pub(crate) config: InstrumentConfig,
15    attribute_hash: u64,
16}
17
18impl Descriptor {
19    /// Create a new descriptor
20    pub fn new<T: Into<Cow<'static, str>>>(
21        name: String,
22        instrumentation_name: T,
23        instrumentation_version: Option<T>,
24        instrument_kind: InstrumentKind,
25        number_kind: NumberKind,
26    ) -> Self {
27        let mut hasher = FnvHasher::default();
28        name.hash(&mut hasher);
29        let instrumentation_name = instrumentation_name.into();
30        let instrumentation_version = instrumentation_version.map(Into::<Cow<'static, str>>::into);
31        instrumentation_name.as_ref().hash(&mut hasher);
32        instrumentation_version.as_ref().hash(&mut hasher);
33        instrument_kind.hash(&mut hasher);
34        number_kind.hash(&mut hasher);
35        let config =
36            InstrumentConfig::with_instrumentation(instrumentation_name, instrumentation_version);
37
38        Descriptor {
39            name,
40            instrument_kind,
41            number_kind,
42            config,
43            attribute_hash: hasher.finish(),
44        }
45    }
46
47    /// The metric instrument's name.
48    pub fn name(&self) -> &str {
49        self.name.as_str()
50    }
51
52    /// The specific kind of instrument.
53    pub fn instrument_kind(&self) -> &InstrumentKind {
54        &self.instrument_kind
55    }
56
57    /// NumberKind returns whether this instrument is declared over int64, float64, or uint64
58    /// values.
59    pub fn number_kind(&self) -> &NumberKind {
60        &self.number_kind
61    }
62
63    /// A human-readable description of the metric instrument.
64    pub fn description(&self) -> Option<&String> {
65        self.config.description.as_ref()
66    }
67
68    /// Assign a new description
69    pub fn set_description(&mut self, description: String) {
70        self.config.description = Some(description);
71    }
72
73    /// Unit describes the units of the metric instrument.
74    pub fn unit(&self) -> Option<&str> {
75        self.config.unit.as_ref().map(|unit| unit.as_ref())
76    }
77
78    /// The name of the library that provided instrumentation for this instrument.
79    pub fn instrumentation_name(&self) -> Cow<'static, str> {
80        self.config.instrumentation_name()
81    }
82
83    /// The version of library that provided instrumentation for this instrument. Optional
84    pub fn instrumentation_version(&self) -> Option<Cow<'static, str>> {
85        self.config.instrumentation_version()
86    }
87
88    /// Instrumentation library reference
89    pub fn instrumentation_library(&self) -> &InstrumentationLibrary {
90        &self.config.instrumentation_library
91    }
92
93    /// The pre-computed hash of the descriptor data
94    pub fn attribute_hash(&self) -> u64 {
95        self.attribute_hash
96    }
97}