Skip to content

Commit

Permalink
LogAppender - 5% perf gain and reduce heap alloc for SeverityText (#1997
Browse files Browse the repository at this point in the history
)
  • Loading branch information
cijothomas authored Aug 7, 2024
1 parent d8bb764 commit 83ba87f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
2 changes: 2 additions & 0 deletions opentelemetry-appender-tracing/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## vNext

- Reduce heap allocation by using `&'static str` for `SeverityText`.

## v0.5.0

- [1869](https://github.com/open-telemetry/opentelemetry-rust/pull/1869) Utilize the `LogRecord::set_target()` method to pass the tracing target to the SDK.
Expand Down
2 changes: 1 addition & 1 deletion opentelemetry-appender-tracing/benches/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
| noop_layer_disabled | 12 ns |
| noop_layer_enabled | 25 ns |
| ot_layer_disabled | 19 ns |
| ot_layer_enabled | 305 ns |
| ot_layer_enabled | 280 ns |
*/

use async_trait::async_trait;
Expand Down
12 changes: 11 additions & 1 deletion opentelemetry-appender-tracing/src/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ where
log_record.set_target(meta.target().to_string());
log_record.set_event_name(meta.name());
log_record.set_severity_number(severity_of_level(meta.level()));
log_record.set_severity_text(meta.level().to_string().into());
log_record.set_severity_text(severity_text_of_level(meta.level()).into());
let mut visitor = EventVisitor::new(&mut log_record);
#[cfg(feature = "experimental_metadata_attributes")]
visitor.visit_experimental_metadata(meta);
Expand Down Expand Up @@ -201,6 +201,16 @@ const fn severity_of_level(level: &Level) -> Severity {
}
}

const fn severity_text_of_level(level: &Level) -> &'static str {
match *level {
Level::TRACE => "TRACE",
Level::DEBUG => "DEBUG",
Level::INFO => "INFO",
Level::WARN => "WARN",
Level::ERROR => "ERROR",
}
}

#[cfg(test)]
mod tests {
use crate::layer;
Expand Down

0 comments on commit 83ba87f

Please sign in to comment.