Skip to content

Commit 3976f3d

Browse files
utpillacijothomaslalitbTommyCpp
authoredSep 17, 2024··
[Breaking] Enforce meter name, version schema_url to be static string slices (#2112)
Co-authored-by: Cijo Thomas <cijo.thomas@gmail.com> Co-authored-by: Lalit Kumar Bhasin <labhas@microsoft.com> Co-authored-by: Zhongyang Wu <zhongyang.wu@outlook.com>
1 parent d0a4fd0 commit 3976f3d

File tree

5 files changed

+27
-116
lines changed

5 files changed

+27
-116
lines changed
 

‎opentelemetry-sdk/src/metrics/meter_provider.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use core::fmt;
22
use std::{
3-
borrow::Cow,
43
collections::HashMap,
54
sync::{
65
atomic::{AtomicBool, Ordering},
@@ -139,9 +138,9 @@ impl Drop for SdkMeterProviderInner {
139138
impl MeterProvider for SdkMeterProvider {
140139
fn versioned_meter(
141140
&self,
142-
name: impl Into<Cow<'static, str>>,
143-
version: Option<impl Into<Cow<'static, str>>>,
144-
schema_url: Option<impl Into<Cow<'static, str>>>,
141+
name: &'static str,
142+
version: Option<&'static str>,
143+
schema_url: Option<&'static str>,
145144
attributes: Option<Vec<KeyValue>>,
146145
) -> Meter {
147146
if self.inner.is_shutdown.load(Ordering::Relaxed) {

‎opentelemetry-sdk/src/testing/metrics/in_memory_exporter.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use std::sync::{Arc, Mutex};
4141
/// .build();
4242
///
4343
/// // Create and record metrics using the MeterProvider
44-
/// let meter = meter_provider.meter(std::borrow::Cow::Borrowed("example"));
44+
/// let meter = meter_provider.meter("example");
4545
/// let counter = meter.u64_counter("my_counter").init();
4646
/// counter.add(1, &[KeyValue::new("key", "value")]);
4747
///

‎opentelemetry/src/global/metrics.rs

+14-97
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,13 @@
11
use crate::metrics::{self, Meter, MeterProvider};
22
use crate::KeyValue;
3-
use core::fmt;
43
use once_cell::sync::Lazy;
5-
use std::{
6-
borrow::Cow,
7-
sync::{Arc, RwLock},
8-
};
4+
use std::sync::{Arc, RwLock};
95

10-
/// The global `MeterProvider` singleton.
11-
static GLOBAL_METER_PROVIDER: Lazy<RwLock<GlobalMeterProvider>> = Lazy::new(|| {
12-
RwLock::new(GlobalMeterProvider::new(
13-
metrics::noop::NoopMeterProvider::new(),
14-
))
15-
});
16-
17-
/// Allows a specific [MeterProvider] to be used generically by the
18-
/// [GlobalMeterProvider] by mirroring the interface and boxing the return types.
19-
trait ObjectSafeMeterProvider {
20-
/// Creates a versioned named meter instance that is a trait object through the underlying
21-
/// [MeterProvider].
22-
fn versioned_meter_cow(
23-
&self,
24-
name: Cow<'static, str>,
25-
version: Option<Cow<'static, str>>,
26-
schema_url: Option<Cow<'static, str>>,
27-
attributes: Option<Vec<KeyValue>>,
28-
) -> Meter;
29-
}
30-
31-
impl<P> ObjectSafeMeterProvider for P
32-
where
33-
P: MeterProvider,
34-
{
35-
/// Return a versioned boxed tracer
36-
fn versioned_meter_cow(
37-
&self,
38-
name: Cow<'static, str>,
39-
version: Option<Cow<'static, str>>,
40-
schema_url: Option<Cow<'static, str>>,
41-
attributes: Option<Vec<KeyValue>>,
42-
) -> Meter {
43-
self.versioned_meter(name, version, schema_url, attributes)
44-
}
45-
}
6+
type GlobalMeterProvider = Arc<dyn MeterProvider + Send + Sync>;
467

47-
/// Represents the globally configured [`MeterProvider`] instance for this
48-
/// application.
49-
#[derive(Clone)]
50-
pub struct GlobalMeterProvider {
51-
provider: Arc<dyn ObjectSafeMeterProvider + Send + Sync>,
52-
}
53-
54-
impl fmt::Debug for GlobalMeterProvider {
55-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56-
f.debug_struct("GlobalMeterProvider").finish()
57-
}
58-
}
59-
60-
impl MeterProvider for GlobalMeterProvider {
61-
fn versioned_meter(
62-
&self,
63-
name: impl Into<Cow<'static, str>>,
64-
version: Option<impl Into<Cow<'static, str>>>,
65-
schema_url: Option<impl Into<Cow<'static, str>>>,
66-
attributes: Option<Vec<KeyValue>>,
67-
) -> Meter {
68-
self.provider.versioned_meter_cow(
69-
name.into(),
70-
version.map(Into::into),
71-
schema_url.map(Into::into),
72-
attributes,
73-
)
74-
}
75-
}
76-
77-
impl GlobalMeterProvider {
78-
/// Create a new global meter provider
79-
fn new<P>(provider: P) -> Self
80-
where
81-
P: MeterProvider + Send + Sync + 'static,
82-
{
83-
GlobalMeterProvider {
84-
provider: Arc::new(provider),
85-
}
86-
}
87-
}
8+
/// The global `MeterProvider` singleton.
9+
static GLOBAL_METER_PROVIDER: Lazy<RwLock<GlobalMeterProvider>> =
10+
Lazy::new(|| RwLock::new(Arc::new(metrics::noop::NoopMeterProvider::new())));
8811

8912
/// Sets the given [`MeterProvider`] instance as the current global meter
9013
/// provider.
@@ -95,25 +18,24 @@ where
9518
let mut global_provider = GLOBAL_METER_PROVIDER
9619
.write()
9720
.expect("GLOBAL_METER_PROVIDER RwLock poisoned");
98-
*global_provider = GlobalMeterProvider::new(new_provider);
21+
*global_provider = Arc::new(new_provider);
9922
}
10023

101-
/// Returns an instance of the currently configured global [`MeterProvider`]
102-
/// through [`GlobalMeterProvider`].
24+
/// Returns an instance of the currently configured global [`MeterProvider`].
10325
pub fn meter_provider() -> GlobalMeterProvider {
10426
GLOBAL_METER_PROVIDER
10527
.read()
10628
.expect("GLOBAL_METER_PROVIDER RwLock poisoned")
10729
.clone()
10830
}
10931

110-
/// Creates a named [`Meter`] via the configured [`GlobalMeterProvider`].
32+
/// Creates a named [`Meter`] via the currently configured global [`MeterProvider`].
11133
///
11234
/// If the name is an empty string, the provider will use a default name.
11335
///
11436
/// This is a more convenient way of expressing `global::meter_provider().meter(name)`.
115-
pub fn meter(name: impl Into<Cow<'static, str>>) -> Meter {
116-
meter_provider().meter(name.into())
37+
pub fn meter(name: &'static str) -> Meter {
38+
meter_provider().meter(name)
11739
}
11840

11941
/// Creates a [`Meter`] with the name, version and schema url.
@@ -138,15 +60,10 @@ pub fn meter(name: impl Into<Cow<'static, str>>) -> Meter {
13860
/// );
13961
/// ```
14062
pub fn meter_with_version(
141-
name: impl Into<Cow<'static, str>>,
142-
version: Option<impl Into<Cow<'static, str>>>,
143-
schema_url: Option<impl Into<Cow<'static, str>>>,
63+
name: &'static str,
64+
version: Option<&'static str>,
65+
schema_url: Option<&'static str>,
14466
attributes: Option<Vec<KeyValue>>,
14567
) -> Meter {
146-
meter_provider().versioned_meter(
147-
name.into(),
148-
version.map(Into::into),
149-
schema_url.map(Into::into),
150-
attributes,
151-
)
68+
meter_provider().versioned_meter(name, version, schema_url, attributes)
15269
}

‎opentelemetry/src/metrics/meter.rs

+5-10
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,8 @@ pub trait MeterProvider {
3939
/// Some(vec![KeyValue::new("key", "value")]),
4040
/// );
4141
/// ```
42-
fn meter(&self, name: impl Into<Cow<'static, str>>) -> Meter {
43-
self.versioned_meter(
44-
name,
45-
None::<Cow<'static, str>>,
46-
None::<Cow<'static, str>>,
47-
None,
48-
)
42+
fn meter(&self, name: &'static str) -> Meter {
43+
self.versioned_meter(name, None, None, None)
4944
}
5045

5146
/// Returns a new versioned meter with a given name.
@@ -56,9 +51,9 @@ pub trait MeterProvider {
5651
/// default name will be used instead.
5752
fn versioned_meter(
5853
&self,
59-
name: impl Into<Cow<'static, str>>,
60-
version: Option<impl Into<Cow<'static, str>>>,
61-
schema_url: Option<impl Into<Cow<'static, str>>>,
54+
name: &'static str,
55+
version: Option<&'static str>,
56+
schema_url: Option<&'static str>,
6257
attributes: Option<Vec<KeyValue>>,
6358
) -> Meter;
6459
}

‎opentelemetry/src/metrics/noop.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::{
1010
},
1111
KeyValue,
1212
};
13-
use std::{any::Any, borrow::Cow, sync::Arc};
13+
use std::{any::Any, sync::Arc};
1414

1515
/// A no-op instance of a `MetricProvider`
1616
#[derive(Debug, Default)]
@@ -28,9 +28,9 @@ impl NoopMeterProvider {
2828
impl MeterProvider for NoopMeterProvider {
2929
fn versioned_meter(
3030
&self,
31-
_name: impl Into<Cow<'static, str>>,
32-
_version: Option<impl Into<Cow<'static, str>>>,
33-
_schema_url: Option<impl Into<Cow<'static, str>>>,
31+
_name: &'static str,
32+
_version: Option<&'static str>,
33+
_schema_url: Option<&'static str>,
3434
_attributes: Option<Vec<KeyValue>>,
3535
) -> Meter {
3636
Meter::new(Arc::new(NoopMeterCore::new()))

0 commit comments

Comments
 (0)
Please sign in to comment.