Skip to content

Commit c7ad406

Browse files
authored
Merge branch 'main' into update-tracing-version
2 parents 6a317bc + fbc3c70 commit c7ad406

File tree

20 files changed

+231
-178
lines changed

20 files changed

+231
-178
lines changed

opentelemetry-appender-log/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -846,11 +846,11 @@ mod tests {
846846

847847
assert_eq!(logs.len(), 5);
848848
for log in logs {
849-
let body: String = match log.record.body.as_ref().unwrap() {
849+
let body: String = match log.record.body().unwrap() {
850850
super::AnyValue::String(s) => s.to_string(),
851851
_ => panic!("AnyValue::String expected"),
852852
};
853-
assert_eq!(body, log.record.severity_text.unwrap());
853+
assert_eq!(body, log.record.severity_text().unwrap());
854854
}
855855
}
856856

opentelemetry-appender-tracing/src/layer.rs

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -334,10 +334,10 @@ mod tests {
334334

335335
// Validate common fields
336336
assert_eq!(log.instrumentation.name(), "opentelemetry-appender-tracing");
337-
assert_eq!(log.record.severity_number, Some(Severity::Error));
337+
assert_eq!(log.record.severity_number(), Some(Severity::Error));
338338

339339
// Validate trace context is none.
340-
assert!(log.record.trace_context.is_none());
340+
assert!(log.record.trace_context().is_none());
341341

342342
// Validate attributes
343343
#[cfg(not(feature = "experimental_metadata_attributes"))]
@@ -428,25 +428,20 @@ mod tests {
428428

429429
// validate common fields.
430430
assert_eq!(log.instrumentation.name(), "opentelemetry-appender-tracing");
431-
assert_eq!(log.record.severity_number, Some(Severity::Error));
431+
assert_eq!(log.record.severity_number(), Some(Severity::Error));
432432

433433
// validate trace context.
434-
assert!(log.record.trace_context.is_some());
434+
assert!(log.record.trace_context().is_some());
435435
assert_eq!(
436-
log.record.trace_context.as_ref().unwrap().trace_id,
436+
log.record.trace_context().unwrap().trace_id,
437437
trace_id_expected
438438
);
439439
assert_eq!(
440-
log.record.trace_context.as_ref().unwrap().span_id,
440+
log.record.trace_context().unwrap().span_id,
441441
span_id_expected
442442
);
443443
assert_eq!(
444-
log.record
445-
.trace_context
446-
.as_ref()
447-
.unwrap()
448-
.trace_flags
449-
.unwrap(),
444+
log.record.trace_context().unwrap().trace_flags.unwrap(),
450445
TraceFlags::SAMPLED
451446
);
452447

@@ -526,10 +521,10 @@ mod tests {
526521

527522
// Validate common fields
528523
assert_eq!(log.instrumentation.name(), "opentelemetry-appender-tracing");
529-
assert_eq!(log.record.severity_number, Some(Severity::Error));
524+
assert_eq!(log.record.severity_number(), Some(Severity::Error));
530525

531526
// Validate trace context is none.
532-
assert!(log.record.trace_context.is_none());
527+
assert!(log.record.trace_context().is_none());
533528

534529
// Attributes can be polluted when we don't use this feature.
535530
#[cfg(feature = "experimental_metadata_attributes")]
@@ -605,25 +600,20 @@ mod tests {
605600

606601
// validate common fields.
607602
assert_eq!(log.instrumentation.name(), "opentelemetry-appender-tracing");
608-
assert_eq!(log.record.severity_number, Some(Severity::Error));
603+
assert_eq!(log.record.severity_number(), Some(Severity::Error));
609604

610605
// validate trace context.
611-
assert!(log.record.trace_context.is_some());
606+
assert!(log.record.trace_context().is_some());
612607
assert_eq!(
613-
log.record.trace_context.as_ref().unwrap().trace_id,
608+
log.record.trace_context().unwrap().trace_id,
614609
trace_id_expected
615610
);
616611
assert_eq!(
617-
log.record.trace_context.as_ref().unwrap().span_id,
612+
log.record.trace_context().unwrap().span_id,
618613
span_id_expected
619614
);
620615
assert_eq!(
621-
log.record
622-
.trace_context
623-
.as_ref()
624-
.unwrap()
625-
.trace_flags
626-
.unwrap(),
616+
log.record.trace_context().unwrap().trace_flags.unwrap(),
627617
TraceFlags::SAMPLED
628618
);
629619

opentelemetry-otlp/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@
162162
//! .with_max_events_per_span(64)
163163
//! .with_max_attributes_per_span(16)
164164
//! .with_max_events_per_span(16)
165-
//! .with_resource(Resource::builder_empty().with_attributes(vec![KeyValue::new("service.name", "example")]).build()),
165+
//! .with_resource(Resource::builder_empty().with_attributes([KeyValue::new("service.name", "example")]).build()),
166166
//! ).build();
167167
//! global::set_tracer_provider(tracer_provider);
168168
//! let tracer = global::tracer("tracer-name");
@@ -186,7 +186,7 @@
186186
//!
187187
//! let provider = opentelemetry_sdk::metrics::SdkMeterProvider::builder()
188188
//! .with_reader(reader)
189-
//! .with_resource(Resource::builder_empty().with_attributes(vec![KeyValue::new("service.name", "example")]).build())
189+
//! .with_resource(Resource::builder_empty().with_attributes([KeyValue::new("service.name", "example")]).build())
190190
//! .build();
191191
//! # }
192192
//!

opentelemetry-proto/src/transform/logs.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ pub mod tonic {
5858

5959
impl From<&opentelemetry_sdk::logs::LogRecord> for LogRecord {
6060
fn from(log_record: &opentelemetry_sdk::logs::LogRecord) -> Self {
61-
let trace_context = log_record.trace_context.as_ref();
62-
let severity_number = match log_record.severity_number {
61+
let trace_context = log_record.trace_context();
62+
let severity_number = match log_record.severity_number() {
6363
Some(Severity::Trace) => SeverityNumber::Trace,
6464
Some(Severity::Trace2) => SeverityNumber::Trace2,
6565
Some(Severity::Trace3) => SeverityNumber::Trace3,
@@ -88,8 +88,8 @@ pub mod tonic {
8888
};
8989

9090
LogRecord {
91-
time_unix_nano: log_record.timestamp.map(to_nanos).unwrap_or_default(),
92-
observed_time_unix_nano: to_nanos(log_record.observed_timestamp.unwrap()),
91+
time_unix_nano: log_record.timestamp().map(to_nanos).unwrap_or_default(),
92+
observed_time_unix_nano: to_nanos(log_record.observed_timestamp().unwrap()),
9393
attributes: {
9494
let attributes: Vec<KeyValue> = log_record
9595
.attributes_iter()
@@ -102,7 +102,7 @@ pub mod tonic {
102102
.collect();
103103
#[cfg(feature = "populate-logs-event-name")]
104104
{
105-
if let Some(event_name) = &log_record.event_name {
105+
if let Some(event_name) = &log_record.event_name() {
106106
let mut attributes_with_name = attributes;
107107
attributes_with_name.push(KeyValue {
108108
key: "event.name".into(),
@@ -119,8 +119,11 @@ pub mod tonic {
119119
attributes
120120
},
121121
severity_number: severity_number.into(),
122-
severity_text: log_record.severity_text.map(Into::into).unwrap_or_default(),
123-
body: log_record.body.clone().map(Into::into),
122+
severity_text: log_record
123+
.severity_text()
124+
.map(Into::into)
125+
.unwrap_or_default(),
126+
body: log_record.body().cloned().map(Into::into),
124127
dropped_attributes_count: 0,
125128
flags: trace_context
126129
.map(|ctx| {
@@ -170,7 +173,7 @@ pub mod tonic {
170173
.schema_url()
171174
.map(ToOwned::to_owned)
172175
.unwrap_or_default(),
173-
scope: Some((instrumentation, log_record.target.clone()).into()),
176+
scope: Some((instrumentation, log_record.target().cloned()).into()),
174177
log_records: vec![log_record.into()],
175178
}],
176179
}
@@ -193,8 +196,8 @@ pub mod tonic {
193196
>,
194197
(log_record, instrumentation)| {
195198
let key = log_record
196-
.target
197-
.clone()
199+
.target()
200+
.cloned()
198201
.unwrap_or_else(|| Cow::Owned(instrumentation.name().to_owned()));
199202
scope_map
200203
.entry(key)

opentelemetry-sdk/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@
9696
- Bump msrv to 1.75.0.
9797

9898

99+
- *Breaking* : [#2314](https://github.com/open-telemetry/opentelemetry-rust/pull/2314)
100+
- The LogRecord struct has been updated:
101+
- All fields are now pub(crate) instead of pub.
102+
- Getter methods have been introduced to access field values.
103+
This change impacts custom exporter and processor developers by requiring updates to code that directly accessed LogRecord fields. They must now use the provided getter methods (e.g., `log_record.event_name()` instead of `log_record.event_name`).
104+
99105
## 0.27.1
100106

101107
Released 2024-Nov-27

opentelemetry-sdk/src/logs/log_emitter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ mod tests {
448448
let user_provided_resource_config_provider = super::LoggerProvider::builder()
449449
.with_resource(
450450
Resource::builder()
451-
.with_attributes(vec![
451+
.with_attributes([
452452
KeyValue::new("my-custom-key", "my-custom-value"),
453453
KeyValue::new("my-custom-key2", "my-custom-value2"),
454454
])

opentelemetry-sdk/src/logs/log_processor.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ mod tests {
769769
.with_log_processor(processor)
770770
.with_resource(
771771
Resource::builder_empty()
772-
.with_attributes(vec![
772+
.with_attributes([
773773
KeyValue::new("k1", "v1"),
774774
KeyValue::new("k2", "v3"),
775775
KeyValue::new("k3", "v3"),
@@ -796,7 +796,7 @@ mod tests {
796796
.with_log_processor(processor)
797797
.with_resource(
798798
Resource::builder_empty()
799-
.with_attributes(vec![
799+
.with_attributes([
800800
KeyValue::new("k1", "v1"),
801801
KeyValue::new("k2", "v3"),
802802
KeyValue::new("k3", "v3"),

opentelemetry-sdk/src/logs/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ mod tests {
2828
fn logging_sdk_test() {
2929
// Arrange
3030
let resource = Resource::builder_empty()
31-
.with_attributes(vec![
31+
.with_attributes([
3232
KeyValue::new("k1", "v1"),
3333
KeyValue::new("k2", "v2"),
3434
KeyValue::new("k3", "v3"),

opentelemetry-sdk/src/logs/record.rs

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,28 @@ pub(crate) type LogRecordAttributes =
2424
/// is provided to `LogExporter`s as input.
2525
pub struct LogRecord {
2626
/// Event name. Optional as not all the logging API support it.
27-
pub event_name: Option<&'static str>,
27+
pub(crate) event_name: Option<&'static str>,
2828

2929
/// Target of the log record
30-
pub target: Option<Cow<'static, str>>,
30+
pub(crate) target: Option<Cow<'static, str>>,
3131

3232
/// Record timestamp
33-
pub timestamp: Option<SystemTime>,
33+
pub(crate) timestamp: Option<SystemTime>,
3434

3535
/// Timestamp for when the record was observed by OpenTelemetry
36-
pub observed_timestamp: Option<SystemTime>,
36+
pub(crate) observed_timestamp: Option<SystemTime>,
3737

3838
/// Trace context for logs associated with spans
39-
pub trace_context: Option<TraceContext>,
39+
pub(crate) trace_context: Option<TraceContext>,
4040

4141
/// The original severity string from the source
42-
pub severity_text: Option<&'static str>,
42+
pub(crate) severity_text: Option<&'static str>,
4343

4444
/// The corresponding severity value, normalized
45-
pub severity_number: Option<Severity>,
45+
pub(crate) severity_number: Option<Severity>,
4646

4747
/// Record body
48-
pub body: Option<AnyValue>,
48+
pub(crate) body: Option<AnyValue>,
4949

5050
/// Additional attributes associated with this record
5151
pub(crate) attributes: LogRecordAttributes,
@@ -118,7 +118,56 @@ impl opentelemetry::logs::LogRecord for LogRecord {
118118
}
119119

120120
impl LogRecord {
121+
/// Returns the event name
122+
#[inline]
123+
pub fn event_name(&self) -> Option<&'static str> {
124+
self.event_name
125+
}
126+
127+
/// Returns the target
128+
#[inline]
129+
pub fn target(&self) -> Option<&Cow<'static, str>> {
130+
self.target.as_ref()
131+
}
132+
133+
/// Returns the timestamp
134+
#[inline]
135+
pub fn timestamp(&self) -> Option<SystemTime> {
136+
self.timestamp
137+
}
138+
139+
/// Returns the observed timestamp
140+
#[inline]
141+
pub fn observed_timestamp(&self) -> Option<SystemTime> {
142+
self.observed_timestamp
143+
}
144+
145+
/// Returns the trace context
146+
#[inline]
147+
pub fn trace_context(&self) -> Option<&TraceContext> {
148+
self.trace_context.as_ref()
149+
}
150+
151+
/// Returns the severity text
152+
#[inline]
153+
pub fn severity_text(&self) -> Option<&'static str> {
154+
self.severity_text
155+
}
156+
157+
/// Returns the severity number
158+
#[inline]
159+
pub fn severity_number(&self) -> Option<Severity> {
160+
self.severity_number
161+
}
162+
163+
/// Returns the body
164+
#[inline]
165+
pub fn body(&self) -> Option<&AnyValue> {
166+
self.body.as_ref()
167+
}
168+
121169
/// Provides an iterator over the attributes.
170+
#[inline]
122171
pub fn attributes_iter(&self) -> impl Iterator<Item = &(Key, AnyValue)> {
123172
self.attributes.iter().filter_map(|opt| opt.as_ref())
124173
}

opentelemetry-sdk/src/metrics/internal/aggregate.rs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
use std::{marker, sync::Arc};
1+
use std::{
2+
marker,
3+
mem::replace,
4+
ops::DerefMut,
5+
sync::{Arc, Mutex},
6+
time::SystemTime,
7+
};
28

39
use opentelemetry::KeyValue;
410

@@ -53,6 +59,44 @@ where
5359
}
5460
}
5561

62+
pub(crate) struct AggregateTime {
63+
pub start: SystemTime,
64+
pub current: SystemTime,
65+
}
66+
67+
/// Initialized [`AggregateTime`] for specific [`Temporality`]
68+
pub(crate) struct AggregateTimeInitiator(Mutex<SystemTime>);
69+
70+
impl AggregateTimeInitiator {
71+
pub(crate) fn delta(&self) -> AggregateTime {
72+
let current_time = SystemTime::now();
73+
let start_time = self
74+
.0
75+
.lock()
76+
.map(|mut start| replace(start.deref_mut(), current_time))
77+
.unwrap_or(current_time);
78+
AggregateTime {
79+
start: start_time,
80+
current: current_time,
81+
}
82+
}
83+
84+
pub(crate) fn cumulative(&self) -> AggregateTime {
85+
let current_time = SystemTime::now();
86+
let start_time = self.0.lock().map(|start| *start).unwrap_or(current_time);
87+
AggregateTime {
88+
start: start_time,
89+
current: current_time,
90+
}
91+
}
92+
}
93+
94+
impl Default for AggregateTimeInitiator {
95+
fn default() -> Self {
96+
Self(Mutex::new(SystemTime::now()))
97+
}
98+
}
99+
56100
/// Builds aggregate functions
57101
pub(crate) struct AggregateBuilder<T> {
58102
/// The temporality used for the returned aggregate functions.

0 commit comments

Comments
 (0)