Skip to content

Commit 1bcbe77

Browse files
authored
fix: use InstrumentationLibrary in metrics. (#393)
Currently we don't use InstrumentationLibrary to store the instrumentation library information. This commit switch to use that.
1 parent 47b54ee commit 1bcbe77

File tree

15 files changed

+88
-45
lines changed

15 files changed

+88
-45
lines changed

opentelemetry-prometheus/tests/integration_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ fn test_add() {
1313
.with_resource(Resource::new(vec![KeyValue::new("R", "V")]))
1414
.init();
1515

16-
let meter = exporter.provider().unwrap().meter("test");
16+
let meter = exporter.provider().unwrap().meter("test", None);
1717

1818
let up_down_counter = meter.f64_up_down_counter("updowncounter").init();
1919
let counter = meter.f64_counter("counter").init();

opentelemetry/benches/ddsketch.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ fn ddsketch(data: Vec<f64>) {
2828
DDSKetchAggregator::new(&DDSketchConfig::new(0.001, 2048, 1e-9), NumberKind::F64);
2929
let descriptor = Descriptor::new(
3030
"test".to_string(),
31-
"test".to_string(),
31+
"test",
32+
None,
3233
InstrumentKind::ValueRecorder,
3334
NumberKind::F64,
3435
);
@@ -53,7 +54,8 @@ fn array(data: Vec<f64>) {
5354
let aggregator = ArrayAggregator::default();
5455
let descriptor = Descriptor::new(
5556
"test".to_string(),
56-
"test".to_string(),
57+
"test",
58+
None,
5759
InstrumentKind::ValueRecorder,
5860
NumberKind::F64,
5961
);

opentelemetry/benches/metric.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ impl Processor for BenchProcessor {
131131
fn build_meter() -> Meter {
132132
let processor = Arc::new(BenchProcessor::default());
133133
let core = accumulator(processor).build();
134-
Meter::new("benches", Arc::new(core))
134+
Meter::new("benches", None, Arc::new(core))
135135
}
136136

137137
criterion_group!(benches, counters);
Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,39 @@
1+
use crate::sdk::InstrumentationLibrary;
12
use crate::Unit;
23

34
/// Config contains some options for metrics of any kind.
45
#[derive(Clone, Debug, PartialEq, Hash)]
56
pub struct InstrumentConfig {
67
pub(crate) description: Option<String>,
78
pub(crate) unit: Option<Unit>,
8-
pub(crate) instrumentation_name: String,
9-
pub(crate) instrumentation_version: Option<String>,
9+
pub(crate) instrumentation_library: InstrumentationLibrary,
1010
}
1111

1212
impl InstrumentConfig {
1313
/// Create a new config from instrumentation name
14-
pub fn with_instrumentation_name(instrumentation_name: String) -> Self {
14+
pub fn with_instrumentation_name(instrumentation_name: &'static str) -> Self {
1515
InstrumentConfig {
1616
description: None,
1717
unit: None,
18-
instrumentation_name,
19-
instrumentation_version: None,
18+
instrumentation_library: InstrumentationLibrary {
19+
name: instrumentation_name,
20+
version: None,
21+
},
2022
}
2123
}
2224

23-
/// Create a new config with instrumentation name and version
25+
/// Create a new config with instrumentation name and optional version
2426
pub fn with_instrumentation(
25-
instrumentation_name: String,
26-
instrumentation_version: String,
27+
instrumentation_name: &'static str,
28+
instrumentation_version: Option<&'static str>,
2729
) -> Self {
2830
InstrumentConfig {
2931
description: None,
3032
unit: None,
31-
instrumentation_name,
32-
instrumentation_version: Some(instrumentation_version),
33+
instrumentation_library: InstrumentationLibrary {
34+
name: instrumentation_name,
35+
version: instrumentation_version,
36+
},
3337
}
3438
}
3539

@@ -44,12 +48,12 @@ impl InstrumentConfig {
4448
}
4549

4650
/// Instrumentation name is the name given to the Meter that created this instrument.
47-
pub fn instrumentation_name(&self) -> &String {
48-
&self.instrumentation_name
51+
pub fn instrumentation_name(&self) -> &'static str {
52+
self.instrumentation_library.name
4953
}
5054

5155
/// Instrumentation version returns the version of instrumentation
52-
pub fn instrumentation_version(&self) -> Option<&String> {
53-
self.instrumentation_version.as_ref()
56+
pub fn instrumentation_version(&self) -> Option<&'static str> {
57+
self.instrumentation_library.version
5458
}
5559
}

opentelemetry/src/api/metrics/counter.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ impl<'a, T> CounterBuilder<'a, T> {
6969
meter,
7070
descriptor: Descriptor::new(
7171
name,
72-
meter.instrumentation_name().to_string(),
72+
meter.instrumentation_library().name,
73+
meter.instrumentation_library().version,
7374
InstrumentKind::Counter,
7475
number_kind,
7576
),

opentelemetry/src/api/metrics/descriptor.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,26 @@ impl Descriptor {
1717
/// Create a new descriptor
1818
pub fn new(
1919
name: String,
20-
instrumentation_name: String,
20+
instrumentation_name: &'static str,
21+
instrumentation_version: Option<&'static str>,
2122
instrument_kind: InstrumentKind,
2223
number_kind: NumberKind,
2324
) -> Self {
2425
let mut hasher = FnvHasher::default();
2526
name.hash(&mut hasher);
2627
instrumentation_name.hash(&mut hasher);
28+
instrumentation_version.hash(&mut hasher);
2729
instrument_kind.hash(&mut hasher);
2830
number_kind.hash(&mut hasher);
2931

3032
Descriptor {
3133
name,
3234
instrument_kind,
3335
number_kind,
34-
config: InstrumentConfig::with_instrumentation_name(instrumentation_name),
36+
config: InstrumentConfig::with_instrumentation(
37+
instrumentation_name,
38+
instrumentation_version,
39+
),
3540
attribute_hash: hasher.finish(),
3641
}
3742
}
@@ -68,8 +73,8 @@ impl Descriptor {
6873
}
6974

7075
/// The name of the library that provided instrumentation for this instrument.
71-
pub fn instrumentation_name(&self) -> &str {
72-
self.config.instrumentation_name.as_str()
76+
pub fn instrumentation_name(&self) -> &'static str {
77+
self.config.instrumentation_name()
7378
}
7479

7580
/// The pre-computed hash of the descriptor data

opentelemetry/src/api/metrics/meter.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::sdk::InstrumentationLibrary;
12
use crate::{
23
metrics::{
34
sdk_api, AsyncRunner, BatchObserver, BatchObserverCallback, CounterBuilder, Descriptor,
@@ -18,31 +19,45 @@ pub trait MeterProvider: fmt::Debug {
1819
/// empty, then a implementation defined default name will be used instead.
1920
///
2021
/// [`Meter`]: struct.Meter.html
21-
fn meter(&self, instrumentation_name: &str) -> Meter;
22+
fn meter(
23+
&self,
24+
instrumentation_name: &'static str,
25+
instrumentation_version: Option<&'static str>,
26+
) -> Meter;
2227
}
2328

2429
/// Meter is the OpenTelemetry metric API, based on a sdk-defined `MeterCore`
2530
/// implementation and the `Meter` library name.
2631
#[derive(Debug)]
2732
pub struct Meter {
28-
instrumentation_name: String,
33+
instrumentation_library: InstrumentationLibrary,
2934
core: Arc<dyn sdk_api::MeterCore + Send + Sync>,
3035
}
3136

3237
impl Meter {
3338
/// Create a new named meter from a sdk implemented core
34-
pub fn new<T: Into<String>>(
39+
pub fn new<T: Into<&'static str>>(
3540
instrumentation_name: T,
41+
instrumentation_version: Option<T>,
3642
core: Arc<dyn sdk_api::MeterCore + Send + Sync>,
3743
) -> Self {
3844
Meter {
39-
instrumentation_name: instrumentation_name.into(),
45+
instrumentation_library: InstrumentationLibrary::new(
46+
instrumentation_name.into(),
47+
instrumentation_version.map(Into::into),
48+
),
4049
core,
4150
}
4251
}
4352

53+
#[deprecated(note = "use instrumentation_library() instead")]
54+
#[allow(dead_code)]
4455
pub(crate) fn instrumentation_name(&self) -> &str {
45-
self.instrumentation_name.as_str()
56+
self.instrumentation_library.name
57+
}
58+
59+
pub(crate) fn instrumentation_library(&self) -> InstrumentationLibrary {
60+
self.instrumentation_library
4661
}
4762

4863
/// Creates a new floating point `ValueObserverBuilder` instrument with the

opentelemetry/src/api/metrics/noop.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use std::any::Any;
1818
use std::sync::Arc;
1919

2020
lazy_static::lazy_static! {
21-
static ref NOOP_DESCRIPTOR: Descriptor = Descriptor::new(String::new(), "noop".to_string(), InstrumentKind::Counter, NumberKind::U64);
21+
static ref NOOP_DESCRIPTOR: Descriptor = Descriptor::new(String::new(), "noop", None, InstrumentKind::Counter, NumberKind::U64);
2222
}
2323

2424
/// A no-op instance of a `MetricProvider`
@@ -35,8 +35,8 @@ impl NoopMeterProvider {
3535
}
3636

3737
impl MeterProvider for NoopMeterProvider {
38-
fn meter(&self, name: &str) -> Meter {
39-
Meter::new(name, Arc::new(NoopMeterCore::new()))
38+
fn meter(&self, name: &'static str, version: Option<&'static str>) -> Meter {
39+
Meter::new(name, version, Arc::new(NoopMeterCore::new()))
4040
}
4141
}
4242

opentelemetry/src/api/metrics/observer.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ impl<'a, T> SumObserverBuilder<'a, T> {
5656
meter,
5757
descriptor: Descriptor::new(
5858
name,
59-
meter.instrumentation_name().to_string(),
59+
meter.instrumentation_library().name,
60+
meter.instrumentation_library().version,
6061
InstrumentKind::SumObserver,
6162
number_kind,
6263
),
@@ -142,7 +143,8 @@ impl<'a, T> UpDownSumObserverBuilder<'a, T> {
142143
meter,
143144
descriptor: Descriptor::new(
144145
name,
145-
meter.instrumentation_name().to_string(),
146+
meter.instrumentation_library().name,
147+
meter.instrumentation_library().version,
146148
InstrumentKind::UpDownSumObserver,
147149
number_kind,
148150
),
@@ -227,7 +229,8 @@ impl<'a, T> ValueObserverBuilder<'a, T> {
227229
meter,
228230
descriptor: Descriptor::new(
229231
name,
230-
meter.instrumentation_name().to_string(),
232+
meter.instrumentation_library().name,
233+
meter.instrumentation_library().version,
231234
InstrumentKind::ValueObserver,
232235
number_kind,
233236
),

opentelemetry/src/api/metrics/registry.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ pub fn meter_provider(core: Arc<dyn MeterCore + Send + Sync>) -> RegistryMeterPr
2020
pub struct RegistryMeterProvider(Arc<dyn MeterCore + Send + Sync>);
2121

2222
impl MeterProvider for RegistryMeterProvider {
23-
fn meter(&self, name: &str) -> Meter {
24-
Meter::new(name, self.0.clone())
23+
fn meter(&self, name: &'static str, version: Option<&'static str>) -> Meter {
24+
Meter::new(name, version, self.0.clone())
2525
}
2626
}
2727

0 commit comments

Comments
 (0)