Skip to content

Commit dcd6927

Browse files
committed
make logrecord fields private
1 parent 3ac2d9f commit dcd6927

File tree

3 files changed

+77
-25
lines changed

3 files changed

+77
-25
lines changed

opentelemetry-proto/src/transform/logs.rs

Lines changed: 12 additions & 9 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()
@@ -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/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-stdout/src/logs/exporter.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,34 +69,34 @@ fn print_logs(batch: LogBatch<'_>) {
6969
for (i, log) in batch.iter().enumerate() {
7070
println!("Log #{}", i);
7171
let (record, _library) = log;
72-
if let Some(event_name) = record.event_name {
72+
if let Some(event_name) = record.event_name() {
7373
println!("\t EventName: {:?}", event_name);
7474
}
75-
if let Some(target) = &record.target {
75+
if let Some(target) = record.target() {
7676
println!("\t Target (Scope): {:?}", target);
7777
}
78-
if let Some(trace_context) = &record.trace_context {
78+
if let Some(trace_context) = record.trace_context() {
7979
println!("\t TraceId: {:?}", trace_context.trace_id);
8080
println!("\t SpanId: {:?}", trace_context.span_id);
8181
}
82-
if let Some(timestamp) = record.timestamp {
82+
if let Some(timestamp) = record.timestamp() {
8383
let datetime: DateTime<Utc> = timestamp.into();
8484
println!("\t Timestamp: {}", datetime.format("%Y-%m-%d %H:%M:%S%.6f"));
8585
}
86-
if let Some(timestamp) = record.observed_timestamp {
86+
if let Some(timestamp) = record.observed_timestamp() {
8787
let datetime: DateTime<Utc> = timestamp.into();
8888
println!(
8989
"\t Observed Timestamp: {}",
9090
datetime.format("%Y-%m-%d %H:%M:%S%.6f")
9191
);
9292
}
93-
if let Some(severity) = record.severity_text {
93+
if let Some(severity) = record.severity_text() {
9494
println!("\t SeverityText: {:?}", severity);
9595
}
96-
if let Some(severity) = record.severity_number {
96+
if let Some(severity) = record.severity_number() {
9797
println!("\t SeverityNumber: {:?}", severity);
9898
}
99-
if let Some(body) = &record.body {
99+
if let Some(body) = record.body() {
100100
println!("\t Body: {:?}", body);
101101
}
102102

0 commit comments

Comments
 (0)