Skip to content

Commit a566aac

Browse files
authored
Merge branch 'main' into global-error-handler-cleanup-logs-sdk
2 parents e99195b + 3f5c230 commit a566aac

File tree

19 files changed

+175
-175
lines changed

19 files changed

+175
-175
lines changed

examples/metrics-advanced/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ publish = false
88
[dependencies]
99
opentelemetry = { path = "../../opentelemetry", features = ["metrics"] }
1010
opentelemetry_sdk = { path = "../../opentelemetry-sdk", features = ["metrics", "rt-tokio"] }
11-
opentelemetry-stdout = { path = "../../opentelemetry-stdout", features = ["metrics"]}
11+
opentelemetry-stdout = { path = "../../opentelemetry-stdout", features = ["metrics"] }
1212
tokio = { workspace = true, features = ["full"] }
1313
serde_json = { workspace = true }

examples/metrics-advanced/src/main.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use opentelemetry::global;
22
use opentelemetry::Key;
33
use opentelemetry::KeyValue;
4+
use opentelemetry_sdk::metrics::reader::DeltaTemporalitySelector;
45
use opentelemetry_sdk::metrics::{
56
Aggregation, Instrument, PeriodicReader, SdkMeterProvider, Stream,
67
};
@@ -44,7 +45,11 @@ fn init_meter_provider() -> opentelemetry_sdk::metrics::SdkMeterProvider {
4445
}
4546
};
4647

47-
let exporter = opentelemetry_stdout::MetricsExporterBuilder::default().build();
48+
// Build exporter using Delta Temporality.
49+
let exporter = opentelemetry_stdout::MetricsExporterBuilder::default()
50+
.with_temporality_selector(DeltaTemporalitySelector::new())
51+
.build();
52+
4853
let reader = PeriodicReader::builder(exporter, runtime::Tokio).build();
4954
let provider = SdkMeterProvider::builder()
5055
.with_reader(reader)

opentelemetry-appender-log/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## vNext
44

55
- Bump MSRV to 1.70 [#2179](https://github.com/open-telemetry/opentelemetry-rust/pull/2179)
6+
- [2193](https://github.com/open-telemetry/opentelemetry-rust/pull/2193) `opentelemetry-appender-log`: Output experimental code attributes
67

78
## v0.26.0
89
Released 2024-Sep-30

opentelemetry-appender-log/Cargo.toml

+13-4
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,26 @@ rust-version = "1.70"
1111
edition = "2021"
1212

1313
[dependencies]
14-
opentelemetry = { version = "0.26", path = "../opentelemetry", features = ["logs"]}
15-
log = { workspace = true, features = ["kv", "std"]}
14+
opentelemetry = { version = "0.26", path = "../opentelemetry", features = [
15+
"logs",
16+
] }
17+
log = { workspace = true, features = ["kv", "std"] }
1618
serde = { workspace = true, optional = true, features = ["std"] }
19+
opentelemetry-semantic-conventions = { path = "../opentelemetry-semantic-conventions", optional = true, features = [
20+
"semconv_experimental",
21+
] }
1722

1823
[features]
1924
logs_level_enabled = ["opentelemetry/logs_level_enabled"]
2025
with-serde = ["log/kv_serde", "serde"]
26+
experimental_metadata_attributes = ["dep:opentelemetry-semantic-conventions"]
2127

2228
[dev-dependencies]
23-
opentelemetry_sdk = { path = "../opentelemetry-sdk", features = [ "testing", "logs_level_enabled" ] }
24-
opentelemetry-stdout = { path = "../opentelemetry-stdout", features = ["logs"]}
29+
opentelemetry_sdk = { path = "../opentelemetry-sdk", features = [
30+
"testing",
31+
"logs_level_enabled",
32+
] }
33+
opentelemetry-stdout = { path = "../opentelemetry-stdout", features = ["logs"] }
2534
log = { workspace = true, features = ["kv_serde"] }
2635
tokio = { workspace = true }
2736
serde = { workspace = true, features = ["std", "derive"] }

opentelemetry-appender-log/src/lib.rs

+72
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ use opentelemetry::{
9999
logs::{AnyValue, LogRecord, Logger, LoggerProvider, Severity},
100100
Key,
101101
};
102+
#[cfg(feature = "experimental_metadata_attributes")]
103+
use opentelemetry_semantic_conventions::attribute::{CODE_FILEPATH, CODE_LINENO, CODE_NAMESPACE};
102104
use std::borrow::Cow;
103105

104106
pub struct OpenTelemetryLogBridge<P, L>
@@ -130,6 +132,28 @@ where
130132
log_record.set_severity_number(severity_of_level(record.level()));
131133
log_record.set_severity_text(record.level().as_str());
132134
log_record.set_body(AnyValue::from(record.args().to_string()));
135+
136+
#[cfg(feature = "experimental_metadata_attributes")]
137+
{
138+
if let Some(filepath) = record.file() {
139+
log_record.add_attribute(
140+
Key::new(CODE_FILEPATH),
141+
AnyValue::from(filepath.to_string()),
142+
);
143+
}
144+
145+
if let Some(line_no) = record.line() {
146+
log_record.add_attribute(Key::new(CODE_LINENO), AnyValue::from(line_no));
147+
}
148+
149+
if let Some(module) = record.module_path() {
150+
log_record.add_attribute(
151+
Key::new(CODE_NAMESPACE),
152+
AnyValue::from(module.to_string()),
153+
);
154+
}
155+
}
156+
133157
log_record.add_attributes(log_attributes(record.key_values()));
134158
log_record.set_target(record.metadata().target().to_string());
135159

@@ -1127,6 +1151,54 @@ mod tests {
11271151
}
11281152
}
11291153

1154+
#[cfg(feature = "experimental_metadata_attributes")]
1155+
#[test]
1156+
fn logbridge_code_attributes() {
1157+
use opentelemetry_semantic_conventions::attribute::{
1158+
CODE_FILEPATH, CODE_LINENO, CODE_NAMESPACE,
1159+
};
1160+
1161+
let exporter = InMemoryLogsExporter::default();
1162+
1163+
let logger_provider = LoggerProvider::builder()
1164+
.with_simple_exporter(exporter.clone())
1165+
.build();
1166+
1167+
let otel_log_appender = OpenTelemetryLogBridge::new(&logger_provider);
1168+
1169+
otel_log_appender.log(
1170+
&log::RecordBuilder::new()
1171+
.level(log::Level::Warn)
1172+
.args(format_args!("WARN"))
1173+
.file(Some("src/main.rs"))
1174+
.module_path(Some("service"))
1175+
.line(Some(101))
1176+
.build(),
1177+
);
1178+
1179+
let logs = exporter.get_emitted_logs().unwrap();
1180+
1181+
let get = |needle: &str| -> Option<AnyValue> {
1182+
logs[0].record.attributes_iter().find_map(|(k, v)| {
1183+
if k.as_str() == needle {
1184+
Some(v.clone())
1185+
} else {
1186+
None
1187+
}
1188+
})
1189+
};
1190+
1191+
assert_eq!(
1192+
Some(AnyValue::String(StringValue::from("src/main.rs"))),
1193+
get(CODE_FILEPATH)
1194+
);
1195+
assert_eq!(
1196+
Some(AnyValue::String(StringValue::from("service"))),
1197+
get(CODE_NAMESPACE)
1198+
);
1199+
assert_eq!(Some(AnyValue::Int(101)), get(CODE_LINENO));
1200+
}
1201+
11301202
#[test]
11311203
fn test_flush() {
11321204
let exporter = InMemoryLogsExporter::default();

opentelemetry-otlp/src/metric.rs

+2-31
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use opentelemetry_sdk::{
1414
metrics::{
1515
data::{ResourceMetrics, Temporality},
1616
exporter::PushMetricsExporter,
17-
reader::{DefaultTemporalitySelector, TemporalitySelector},
17+
reader::{DefaultTemporalitySelector, DeltaTemporalitySelector, TemporalitySelector},
1818
InstrumentKind, PeriodicReader, SdkMeterProvider,
1919
},
2020
runtime::Runtime,
@@ -169,7 +169,7 @@ where
169169
///
170170
/// [exporter-docs]: https://github.com/open-telemetry/opentelemetry-specification/blob/a1c13d59bb7d0fb086df2b3e1eaec9df9efef6cc/specification/metrics/sdk_exporters/otlp.md#additional-configuration
171171
pub fn with_delta_temporality(self) -> Self {
172-
self.with_temporality_selector(DeltaTemporalitySelector)
172+
self.with_temporality_selector(DeltaTemporalitySelector::new())
173173
}
174174
}
175175

@@ -237,35 +237,6 @@ impl<RT, EB: Debug> Debug for OtlpMetricPipeline<RT, EB> {
237237
}
238238
}
239239

240-
/// A temporality selector that returns [`Delta`][Temporality::Delta] for all
241-
/// instruments except `UpDownCounter` and `ObservableUpDownCounter`.
242-
///
243-
/// This temporality selector is equivalent to OTLP Metrics Exporter's
244-
/// `Delta` temporality preference (see [its documentation][exporter-docs]).
245-
///
246-
/// [exporter-docs]: https://github.com/open-telemetry/opentelemetry-specification/blob/a1c13d59bb7d0fb086df2b3e1eaec9df9efef6cc/specification/metrics/sdk_exporters/otlp.md#additional-configuration
247-
#[derive(Debug)]
248-
struct DeltaTemporalitySelector;
249-
250-
impl TemporalitySelector for DeltaTemporalitySelector {
251-
#[rustfmt::skip]
252-
fn temporality(&self, kind: InstrumentKind) -> Temporality {
253-
match kind {
254-
InstrumentKind::Counter
255-
| InstrumentKind::Histogram
256-
| InstrumentKind::ObservableCounter
257-
| InstrumentKind::Gauge
258-
| InstrumentKind::ObservableGauge => {
259-
Temporality::Delta
260-
}
261-
InstrumentKind::UpDownCounter
262-
| InstrumentKind::ObservableUpDownCounter => {
263-
Temporality::Cumulative
264-
}
265-
}
266-
}
267-
}
268-
269240
/// An interface for OTLP metrics clients
270241
#[async_trait]
271242
pub trait MetricsClient: fmt::Debug + Send + Sync + 'static {

opentelemetry-sdk/benches/metric.rs

+1-23
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use opentelemetry_sdk::{
1010
metrics::{
1111
data::{ResourceMetrics, Temporality},
1212
new_view,
13-
reader::{MetricReader, TemporalitySelector},
13+
reader::{DeltaTemporalitySelector, MetricReader, TemporalitySelector},
1414
Aggregation, Instrument, InstrumentKind, ManualReader, Pipeline, SdkMeterProvider, Stream,
1515
View,
1616
},
@@ -44,28 +44,6 @@ impl MetricReader for SharedReader {
4444
}
4545
}
4646

47-
/// Configure delta temporality for all [InstrumentKind]
48-
///
49-
/// [Temporality::Delta] will be used for all instrument kinds if this
50-
/// [TemporalitySelector] is used.
51-
#[derive(Clone, Default, Debug)]
52-
pub struct DeltaTemporalitySelector {
53-
pub(crate) _private: (),
54-
}
55-
56-
impl DeltaTemporalitySelector {
57-
/// Create a new default temporality selector.
58-
pub fn new() -> Self {
59-
Self::default()
60-
}
61-
}
62-
63-
impl TemporalitySelector for DeltaTemporalitySelector {
64-
fn temporality(&self, _kind: InstrumentKind) -> Temporality {
65-
Temporality::Delta
66-
}
67-
}
68-
6947
// * Summary *
7048

7149
// rustc 1.68.0 (2c8cc3432 2023-03-06)

opentelemetry-sdk/src/metrics/instrument.rs

+3-27
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::{borrow::Cow, collections::HashSet, sync::Arc};
22

33
use opentelemetry::{
4-
metrics::{AsyncInstrument, SyncCounter, SyncGauge, SyncHistogram, SyncUpDownCounter},
4+
metrics::{AsyncInstrument, SyncInstrument},
55
Key, KeyValue,
66
};
77

@@ -252,32 +252,8 @@ pub(crate) struct ResolvedMeasures<T> {
252252
pub(crate) measures: Vec<Arc<dyn Measure<T>>>,
253253
}
254254

255-
impl<T: Copy + 'static> SyncCounter<T> for ResolvedMeasures<T> {
256-
fn add(&self, val: T, attrs: &[KeyValue]) {
257-
for measure in &self.measures {
258-
measure.call(val, attrs)
259-
}
260-
}
261-
}
262-
263-
impl<T: Copy + 'static> SyncUpDownCounter<T> for ResolvedMeasures<T> {
264-
fn add(&self, val: T, attrs: &[KeyValue]) {
265-
for measure in &self.measures {
266-
measure.call(val, attrs)
267-
}
268-
}
269-
}
270-
271-
impl<T: Copy + 'static> SyncGauge<T> for ResolvedMeasures<T> {
272-
fn record(&self, val: T, attrs: &[KeyValue]) {
273-
for measure in &self.measures {
274-
measure.call(val, attrs)
275-
}
276-
}
277-
}
278-
279-
impl<T: Copy + 'static> SyncHistogram<T> for ResolvedMeasures<T> {
280-
fn record(&self, val: T, attrs: &[KeyValue]) {
255+
impl<T: Copy + 'static> SyncInstrument<T> for ResolvedMeasures<T> {
256+
fn measure(&self, val: T, attrs: &[KeyValue]) {
281257
for measure in &self.measures {
282258
measure.call(val, attrs)
283259
}

opentelemetry-sdk/src/metrics/noop.rs

+3-24
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
use opentelemetry::{
2-
metrics::{
3-
AsyncInstrument, InstrumentProvider, SyncCounter, SyncGauge, SyncHistogram,
4-
SyncUpDownCounter,
5-
},
2+
metrics::{AsyncInstrument, InstrumentProvider, SyncInstrument},
63
KeyValue,
74
};
85

@@ -34,26 +31,8 @@ impl NoopSyncInstrument {
3431
}
3532
}
3633

37-
impl<T> SyncCounter<T> for NoopSyncInstrument {
38-
fn add(&self, _value: T, _attributes: &[KeyValue]) {
39-
// Ignored
40-
}
41-
}
42-
43-
impl<T> SyncUpDownCounter<T> for NoopSyncInstrument {
44-
fn add(&self, _value: T, _attributes: &[KeyValue]) {
45-
// Ignored
46-
}
47-
}
48-
49-
impl<T> SyncHistogram<T> for NoopSyncInstrument {
50-
fn record(&self, _value: T, _attributes: &[KeyValue]) {
51-
// Ignored
52-
}
53-
}
54-
55-
impl<T> SyncGauge<T> for NoopSyncInstrument {
56-
fn record(&self, _value: T, _attributes: &[KeyValue]) {
34+
impl<T> SyncInstrument<T> for NoopSyncInstrument {
35+
fn measure(&self, _value: T, _attributes: &[KeyValue]) {
5736
// Ignored
5837
}
5938
}

opentelemetry-sdk/src/metrics/reader.rs

+38
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,41 @@ impl TemporalitySelector for DefaultTemporalitySelector {
8686
Temporality::Cumulative
8787
}
8888
}
89+
90+
/// A temporality selector that returns [`Delta`][Temporality::Delta] for all
91+
/// instruments except `UpDownCounter` and `ObservableUpDownCounter`.
92+
///
93+
/// This temporality selector is equivalent to OTLP Metrics Exporter's
94+
/// `Delta` temporality preference (see [its documentation][exporter-docs]).
95+
///
96+
/// [exporter-docs]: https://github.com/open-telemetry/opentelemetry-specification/blob/a1c13d59bb7d0fb086df2b3e1eaec9df9efef6cc/specification/metrics/sdk_exporters/otlp.md#additional-configuration
97+
#[derive(Clone, Default, Debug)]
98+
pub struct DeltaTemporalitySelector {
99+
pub(crate) _private: (),
100+
}
101+
102+
impl DeltaTemporalitySelector {
103+
/// Create a new default temporality selector.
104+
pub fn new() -> Self {
105+
Self::default()
106+
}
107+
}
108+
109+
impl TemporalitySelector for DeltaTemporalitySelector {
110+
#[rustfmt::skip]
111+
fn temporality(&self, kind: InstrumentKind) -> Temporality {
112+
match kind {
113+
InstrumentKind::Counter
114+
| InstrumentKind::Histogram
115+
| InstrumentKind::ObservableCounter
116+
| InstrumentKind::Gauge
117+
| InstrumentKind::ObservableGauge => {
118+
Temporality::Delta
119+
}
120+
InstrumentKind::UpDownCounter
121+
| InstrumentKind::ObservableUpDownCounter => {
122+
Temporality::Cumulative
123+
}
124+
}
125+
}
126+
}

opentelemetry/CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
- Bump MSRV to 1.70 [#2179](https://github.com/open-telemetry/opentelemetry-rust/pull/2179)
66
- Add `LogRecord::set_trace_context`; an optional method conditional on the `trace` feature for setting trace context on a log record.
7-
- Remove unnecessary public methods named `as_any` from `AsyncInstrument` trait and the implementing instruments: `ObservableCounter`, `ObservableGauge`, and `ObservableUpDownCounter` [#2187](https://github.com/open-telemetry/opentelemetry-rust/issues/2187)
7+
- Removed unnecessary public methods named `as_any` from `AsyncInstrument` trait and the implementing instruments: `ObservableCounter`, `ObservableGauge`, and `ObservableUpDownCounter` [#2187](https://github.com/open-telemetry/opentelemetry-rust/issues/2187)
8+
- Introduced `SyncInstrument` trait to replace the individual synchronous instrument traits (`SyncCounter`, `SyncGauge`, `SyncHistogram`, `SyncUpDownCounter`) which are meant for SDK implementation. [#2207](https://github.com/open-telemetry/opentelemetry-rust/issues/2207)
89

910
## v0.26.0
1011
Released 2024-Sep-30

opentelemetry/src/common.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ pub enum Value {
227227
}
228228

229229
/// Wrapper for string-like values
230-
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
230+
#[derive(Clone, PartialEq, Eq, Hash)]
231231
pub struct StringValue(OtelString);
232232

233233
impl fmt::Debug for StringValue {

0 commit comments

Comments
 (0)