Skip to content

Commit 19dba69

Browse files
cijothomaslalitb
andauthored
Appender-Tracing - extend spl treatment of message when recording str (#2689)
Co-authored-by: Lalit Kumar Bhasin <[email protected]>
1 parent 6648d74 commit 19dba69

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

opentelemetry-appender-tracing/CHANGELOG.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,32 @@
55
Fixes [1682](https://github.com/open-telemetry/opentelemetry-rust/issues/1682).
66
"spec_unstable_logs_enabled" feature now do not suppress logs for other layers.
77

8+
The special treatment of the "message" field has been extended when recording
9+
string values. With this change, when a log is emitted with a field named
10+
"message" (and string value), its value is directly assigned to the LogRecord’s
11+
body rather than being stored as an attribute named "message". This offers a
12+
slight performance improvement over previous.
13+
14+
For example, the below will now produce LogRecord with the message value
15+
populated as LogRecord's body:
16+
17+
```rust
18+
error!(name: "my-event-name", target: "my-system", event_id = 20, user_name = "otel", user_email = "[email protected]", message = "This is an example message");
19+
```
20+
21+
Previously, Body was only populated when the below style was used.
22+
23+
```rust
24+
error!(name: "my-event-name", target: "my-system", event_id = 20, user_name = "otel", user_email = "[email protected]", "This is an example message");
25+
```
26+
27+
This style, while slightly slower, should still be used when the value is not a
28+
simple string, but require format arguments as in the below example.
29+
30+
```rust
31+
error!(name: "my-event-name", target: "my-system", event_id = 20, user_name = "otel", user_email = "[email protected]", "This is an example message with format arguments {} and {}", "foo", "bar");
32+
```
33+
834
## 0.28.1
935

1036
Released 2025-Feb-12

opentelemetry-appender-tracing/src/layer.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,14 @@ impl<LR: LogRecord> tracing::field::Visit for EventVisitor<'_, LR> {
8888
if is_duplicated_metadata(field.name()) {
8989
return;
9090
}
91-
//TODO: Consider special casing "message" to populate body and document
92-
// to users to use message field for log message, to avoid going to the
93-
// record_debug, which has dyn dispatch, string allocation and
94-
// formatting cost.
95-
9691
//TODO: Fix heap allocation. Check if lifetime of &str can be used
9792
// to optimize sync exporter scenario.
98-
self.log_record
99-
.add_attribute(Key::new(field.name()), AnyValue::from(value.to_owned()));
93+
if field.name() == "message" {
94+
self.log_record.set_body(AnyValue::from(value.to_owned()));
95+
} else {
96+
self.log_record
97+
.add_attribute(Key::new(field.name()), AnyValue::from(value.to_owned()));
98+
}
10099
}
101100

102101
fn record_bool(&mut self, field: &tracing_core::Field, value: bool) {

0 commit comments

Comments
 (0)