Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions opentelemetry-api/src/logs/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
};
use std::{borrow::Cow, time::SystemTime};

#[derive(Debug, Clone, Default)]
#[derive(Debug, Clone)]
#[non_exhaustive]
/// LogRecord represents all data carried by a log record, and
/// is provided to `LogExporter`s as input.
Expand All @@ -13,7 +13,7 @@ pub struct LogRecord {
pub timestamp: Option<SystemTime>,

/// Timestamp for when the record was observed by OpenTelemetry
pub observed_timestamp: Option<SystemTime>,
pub observed_timestamp: SystemTime,

/// Trace context for logs associated with spans
pub trace_context: Option<TraceContext>,
Expand All @@ -30,6 +30,20 @@ pub struct LogRecord {
pub attributes: Option<Vec<(Key, AnyValue)>>,
}

impl Default for LogRecord {
fn default() -> Self {
LogRecord {
timestamp: None,
observed_timestamp: SystemTime::now(),
trace_context: None,
severity_text: None,
severity_number: None,
body: None,
attributes: None,
}
}
}

impl LogRecord {
/// Create a [`LogRecordBuilder`] to create a new Log Record
pub fn builder() -> LogRecordBuilder {
Expand Down Expand Up @@ -261,7 +275,7 @@ impl LogRecordBuilder {
pub fn with_observed_timestamp(self, timestamp: SystemTime) -> Self {
Self {
record: LogRecord {
observed_timestamp: Some(timestamp),
observed_timestamp: timestamp,
..self.record
},
}
Expand Down
5 changes: 1 addition & 4 deletions opentelemetry-proto/src/transform/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,7 @@ pub mod tonic {

LogRecord {
time_unix_nano: log_record.timestamp.map(to_nanos).unwrap_or_default(),
observed_time_unix_nano: log_record
.observed_timestamp
.map(to_nanos)
.unwrap_or_default(),
observed_time_unix_nano: to_nanos(log_record.observed_timestamp),
severity_number: severity_number.into(),
severity_text: log_record.severity_text.map(Into::into).unwrap_or_default(),
body: log_record.body.map(Into::into),
Expand Down
7 changes: 2 additions & 5 deletions opentelemetry-stdout/src/logs/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,8 @@ struct LogRecord {
serialize_with = "opt_as_unix_nano"
)]
time_unix_nano: Option<SystemTime>,
#[serde(
skip_serializing_if = "Option::is_none",
serialize_with = "opt_as_unix_nano"
)]
observed_time_unix_nano: Option<SystemTime>,
#[serde(serialize_with = "as_unix_nano")]
observed_time_unix_nano: SystemTime,
severity_number: u32,
#[serde(skip_serializing_if = "Option::is_none")]
severity_text: Option<Cow<'static, str>>,
Expand Down
12 changes: 4 additions & 8 deletions opentelemetry-user-events-logs/src/logs/exporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,10 @@ impl UserEventsExporter {
eb.add_value("__csver__", 0x0401u16, FieldFormat::HexInt, 0);

// populate CS PartA
let event_time: SystemTime;
if log_data.record.timestamp.is_some() {
event_time = log_data.record.timestamp.unwrap();
} else if log_data.record.observed_timestamp.is_some() {
event_time = log_data.record.observed_timestamp.unwrap();
} else {
event_time = SystemTime::now();
}
let event_time: SystemTime = log_data
.record
.timestamp
.unwrap_or(log_data.record.observed_timestamp);
cs_a_count += 1; // for event_time
eb.add_struct("PartA", cs_a_count, 0);
{
Expand Down