Skip to content

Commit 6207882

Browse files
lalitbcijothomas
andauthored
Insert tracing event name into LogRecord::event_name instead of attributes (#1928)
Co-authored-by: Cijo Thomas <cithomas@microsoft.com>
1 parent a2e435f commit 6207882

File tree

4 files changed

+55
-38
lines changed

4 files changed

+55
-38
lines changed

opentelemetry-appender-tracing/CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
- [1869](https://github.com/open-telemetry/opentelemetry-rust/pull/1869) Utilize the `LogRecord::set_target()` method to pass the tracing target to the SDK.
66
Exporters might use the target to override the instrumentation scope, which previously contained "opentelemetry-appender-tracing".
77

8+
- **Breaking** [1928](https://github.com/open-telemetry/opentelemetry-rust/pull/1928) Insert tracing event name into LogRecord::event_name instead of attributes.
9+
- If using a custom exporter, then they must serialize this field directly from LogRecord::event_name instead of iterating over the attributes. OTLP Exporter is modified to handle this.
10+
811
## v0.4.0
912

1013
- Removed unwanted dependency on opentelemetry-sdk.

opentelemetry-appender-tracing/src/layer.rs

+16-23
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ use opentelemetry::{
33
Key,
44
};
55
use std::borrow::Cow;
6-
use tracing_core::{Level, Metadata};
6+
use tracing_core::Level;
7+
#[cfg(feature = "experimental_metadata_attributes")]
8+
use tracing_core::Metadata;
79
#[cfg(feature = "experimental_metadata_attributes")]
810
use tracing_log::NormalizeEvent;
911
use tracing_subscriber::Layer;
@@ -39,13 +41,6 @@ impl<'a, LR: LogRecord> EventVisitor<'a, LR> {
3941
fn new(log_record: &'a mut LR) -> Self {
4042
EventVisitor { log_record }
4143
}
42-
fn visit_metadata(&mut self, meta: &Metadata) {
43-
self.log_record
44-
.add_attribute(Key::new("name"), AnyValue::from(meta.name()));
45-
46-
#[cfg(feature = "experimental_metadata_attributes")]
47-
self.visit_experimental_metadata(meta);
48-
}
4944

5045
#[cfg(feature = "experimental_metadata_attributes")]
5146
fn visit_experimental_metadata(&mut self, meta: &Metadata) {
@@ -170,15 +165,17 @@ where
170165

171166
//let mut log_record: LogRecord = LogRecord::default();
172167
let mut log_record = self.logger.create_log_record();
168+
log_record.set_target(meta.target().to_string());
169+
log_record.set_event_name(meta.name());
173170
log_record.set_severity_number(severity_of_level(meta.level()));
174171
log_record.set_severity_text(meta.level().to_string().into());
175-
log_record.set_target(meta.target().to_string());
176-
177172
let mut visitor = EventVisitor::new(&mut log_record);
178-
visitor.visit_metadata(meta);
173+
#[cfg(feature = "experimental_metadata_attributes")]
174+
visitor.visit_experimental_metadata(meta);
179175
// Visit fields.
180176
event.record(&mut visitor);
181177

178+
//emit record
182179
self.logger.emit(log_record);
183180
}
184181

@@ -261,10 +258,9 @@ mod tests {
261258
.clone()
262259
.expect("Attributes are expected");
263260
#[cfg(not(feature = "experimental_metadata_attributes"))]
264-
assert_eq!(attributes.len(), 4);
261+
assert_eq!(attributes.len(), 3);
265262
#[cfg(feature = "experimental_metadata_attributes")]
266-
assert_eq!(attributes.len(), 9);
267-
assert!(attributes.contains(&(Key::new("name"), "my-event-name".into())));
263+
assert_eq!(attributes.len(), 8);
268264
assert!(attributes.contains(&(Key::new("event_id"), 20.into())));
269265
assert!(attributes.contains(&(Key::new("user_name"), "otel".into())));
270266
assert!(attributes.contains(&(Key::new("user_email"), "otel@opentelemetry.io".into())));
@@ -358,10 +354,9 @@ mod tests {
358354
.clone()
359355
.expect("Attributes are expected");
360356
#[cfg(not(feature = "experimental_metadata_attributes"))]
361-
assert_eq!(attributes.len(), 4);
357+
assert_eq!(attributes.len(), 3);
362358
#[cfg(feature = "experimental_metadata_attributes")]
363-
assert_eq!(attributes.len(), 9);
364-
assert!(attributes.contains(&(Key::new("name"), "my-event-name".into())));
359+
assert_eq!(attributes.len(), 8);
365360
assert!(attributes.contains(&(Key::new("event_id"), 20.into())));
366361
assert!(attributes.contains(&(Key::new("user_name"), "otel".into())));
367362
assert!(attributes.contains(&(Key::new("user_email"), "otel@opentelemetry.io".into())));
@@ -419,6 +414,7 @@ mod tests {
419414
assert!(log.record.trace_context.is_none());
420415

421416
// Validate attributes
417+
#[cfg(feature = "experimental_metadata_attributes")]
422418
let attributes: Vec<(Key, AnyValue)> = log
423419
.record
424420
.attributes
@@ -427,9 +423,7 @@ mod tests {
427423

428424
// Attributes can be polluted when we don't use this feature.
429425
#[cfg(feature = "experimental_metadata_attributes")]
430-
assert_eq!(attributes.len(), 6);
431-
432-
assert!(attributes.contains(&(Key::new("name"), "log event".into())));
426+
assert_eq!(attributes.len(), 5);
433427

434428
#[cfg(feature = "experimental_metadata_attributes")]
435429
{
@@ -516,6 +510,7 @@ mod tests {
516510
);
517511

518512
// validate attributes.
513+
#[cfg(feature = "experimental_metadata_attributes")]
519514
let attributes: Vec<(Key, AnyValue)> = log
520515
.record
521516
.attributes
@@ -524,9 +519,7 @@ mod tests {
524519

525520
// Attributes can be polluted when we don't use this feature.
526521
#[cfg(feature = "experimental_metadata_attributes")]
527-
assert_eq!(attributes.len(), 6);
528-
529-
assert!(attributes.contains(&(Key::new("name"), "log event".into())));
522+
assert_eq!(attributes.len(), 5);
530523

531524
#[cfg(feature = "experimental_metadata_attributes")]
532525
{

opentelemetry-proto/src/transform/logs.rs

+16-5
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,22 @@ pub mod tonic {
9292
severity_number: severity_number.into(),
9393
severity_text: log_record.severity_text.map(Into::into).unwrap_or_default(),
9494
body: log_record.body.map(Into::into),
95-
attributes: log_record
96-
.attributes
97-
.map(Attributes::from_iter)
98-
.unwrap_or_default()
99-
.0,
95+
attributes: {
96+
let mut attributes = log_record
97+
.attributes
98+
.map(Attributes::from_iter)
99+
.unwrap_or_default()
100+
.0;
101+
if let Some(event_name) = log_record.event_name.as_ref() {
102+
attributes.push(KeyValue {
103+
key: "name".into(),
104+
value: Some(AnyValue {
105+
value: Some(Value::StringValue(event_name.to_string())),
106+
}),
107+
})
108+
}
109+
attributes
110+
},
100111
dropped_attributes_count: 0,
101112
flags: trace_context
102113
.map(|ctx| {

opentelemetry-stdout/src/logs/transform.rs

+20-10
Original file line numberDiff line numberDiff line change
@@ -131,16 +131,26 @@ impl From<opentelemetry_sdk::export::logs::LogData> for LogRecord {
131131
.severity_number
132132
.map(|u| u as u32)
133133
.unwrap_or_default(),
134-
attributes: value
135-
.record
136-
.attributes
137-
.map(|attrs| {
138-
attrs
139-
.into_iter()
140-
.map(|(key, value)| (key, value).into())
141-
.collect()
142-
})
143-
.unwrap_or_default(),
134+
attributes: {
135+
let mut attributes = value
136+
.record
137+
.attributes
138+
.unwrap_or_default()
139+
.into_iter()
140+
.map(|(key, value)| (key, value).into())
141+
.collect::<Vec<_>>();
142+
143+
if let Some(event_name) = &value.record.event_name {
144+
attributes.push(
145+
(
146+
opentelemetry::Key::from("name"),
147+
opentelemetry::Value::String(event_name.clone().into()),
148+
)
149+
.into(),
150+
)
151+
}
152+
attributes
153+
},
144154
dropped_attributes_count: 0,
145155
severity_text: value.record.severity_text,
146156
body: value.record.body.map(|a| a.into()),

0 commit comments

Comments
 (0)