Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

opentelemetry: allocate fewer strings for recording events #1917

Merged
merged 2 commits into from Feb 9, 2022

Conversation

hawkw
Copy link
Member

@hawkw hawkw commented Feb 9, 2022

Motivation

Currently, the tracing-opentelemetry subscriber will allocate several
strings for opentelemetry key-value fields in its on_event
implementation. Some of these allocations are not necessary, since
opentelemetry::Value::String can take a Cow, allowing &'static strs
to be used without allocating a new String.

Since this happens for every event that's recorded to opentelemetry,
this probably has a meaningful performance impact.

Solution

This branch makes two primary changes:

  • Change the on_event method to subscriber to use &'static strs for
    event targets when possible, similarly to how we did this for source
    locations in opentelemetry: forward event metadata #1911. This way, when events were not recorded via the
    tracing-log adapter, we will use the &'static tracing metadata
    string for their targets, rather than allocating a new String. New
    Strings are only allocated when an event came from the log crate
    and its target is not valid for the 'static lifetime.
  • Use Level::as_str for the Level key-value field, instead of
    Level::to_string. to_string calls fmt::Display and returns a
    String, while as_str returns an &'static str. This way, levels
    will never allocate a String.

This changes the `tracing-opentelemetry` subscriber to use
`&'static str`s for event targets when possible, similarly to how we did
this for source locations in #1911.

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
Currently, the OpenTelemetry subscriber calls `level.to_string()` to
produce a string that's added as a key-value pair when recording
`tracing` events. This means a new `String` will be allocated for _all_
events.

This can be avoided by using `Level::as_str` instead, which returns an
`&'static str`.  Because `opentelemetry`'s `String` values are `Cow`'s,
we can use the `&'static str` without allocating a `String`.

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
Copy link
Collaborator

@jtescher jtescher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🥳

@hawkw hawkw merged commit 4e65750 into master Feb 9, 2022
@hawkw hawkw deleted the eliza/more-cow branch February 9, 2022 19:17
hawkw added a commit that referenced this pull request Feb 11, 2022
## Motivation

Currently, the `tracing-opentelemetry` subscriber will allocate several
strings for opentelemetry key-value fields in its `on_event`
implementation. Some of these allocations are not necessary, since
`opentelemetry::Value::String` can take a `Cow`, allowing `&'static str`s
to be used without allocating a new `String`.

Since this happens for _every_ event that's recorded to opentelemetry,
this probably has a meaningful performance impact.

## Solution

This branch makes two primary changes:

+ Change the `on_event` method to subscriber to use `&'static str`s for
  event targets when possible, similarly to how we did this for source
  locations in #1911. This way, when events were not recorded via the
  `tracing-log` adapter, we will use the `&'static` tracing metadata
  string for their targets, rather than allocating a new `String`. New
  `String`s are only allocated when an event came from the `log` crate
  and its target is not valid for the `'static` lifetime.

* Use `Level::as_str` for the `Level` key-value field, instead of
  `Level::to_string`. `to_string` calls `fmt::Display` and returns a
  `String`, while `as_str` returns an `&'static str`. This way, levels
  will never allocate a `String`.
hawkw added a commit that referenced this pull request Feb 11, 2022
## Motivation

Currently, the `tracing-opentelemetry` subscriber will allocate several
strings for opentelemetry key-value fields in its `on_event`
implementation. Some of these allocations are not necessary, since
`opentelemetry::Value::String` can take a `Cow`, allowing `&'static str`s
to be used without allocating a new `String`.

Since this happens for _every_ event that's recorded to opentelemetry,
this probably has a meaningful performance impact.

## Solution

This branch makes two primary changes:

+ Change the `on_event` method to subscriber to use `&'static str`s for
  event targets when possible, similarly to how we did this for source
  locations in #1911. This way, when events were not recorded via the
  `tracing-log` adapter, we will use the `&'static` tracing metadata
  string for their targets, rather than allocating a new `String`. New
  `String`s are only allocated when an event came from the `log` crate
  and its target is not valid for the `'static` lifetime.

* Use `Level::as_str` for the `Level` key-value field, instead of
  `Level::to_string`. `to_string` calls `fmt::Display` and returns a
  `String`, while `as_str` returns an `&'static str`. This way, levels
  will never allocate a `String`.
hawkw added a commit that referenced this pull request Feb 11, 2022
## Motivation

Currently, the `tracing-opentelemetry` subscriber will allocate several
strings for opentelemetry key-value fields in its `on_event`
implementation. Some of these allocations are not necessary, since
`opentelemetry::Value::String` can take a `Cow`, allowing `&'static str`s
to be used without allocating a new `String`.

Since this happens for _every_ event that's recorded to opentelemetry,
this probably has a meaningful performance impact.

## Solution

This branch makes two primary changes:

+ Change the `on_event` method to subscriber to use `&'static str`s for
  event targets when possible, similarly to how we did this for source
  locations in #1911. This way, when events were not recorded via the
  `tracing-log` adapter, we will use the `&'static` tracing metadata
  string for their targets, rather than allocating a new `String`. New
  `String`s are only allocated when an event came from the `log` crate
  and its target is not valid for the `'static` lifetime.

* Use `Level::as_str` for the `Level` key-value field, instead of
  `Level::to_string`. `to_string` calls `fmt::Display` and returns a
  `String`, while `as_str` returns an `&'static str`. This way, levels
  will never allocate a `String`.
hawkw added a commit that referenced this pull request Feb 11, 2022
# 0.17.1 (February 11, 2022)

### Added

- `OpenTelemetryLayer` can now add detailed location information to
  forwarded events (defaults to on) ([#1911])
- `OpenTelemetryLayer::with_event_location` to control whether source
  locations are recorded ([#1911])
### Changed

- Avoid unnecessary allocations to improve performance when recording
  events ([#1917])

Thanks to @djc for contributing to this release!

[#1917]: #1917
[#1911]: #1911

Closes #1919
hawkw added a commit that referenced this pull request Feb 11, 2022
# 0.17.1 (February 11, 2022)

### Added

- `OpenTelemetryLayer` can now add detailed location information to
  forwarded events (defaults to on) ([#1911])
- `OpenTelemetryLayer::with_event_location` to control whether source
  locations are recorded ([#1911])
### Changed

- Avoid unnecessary allocations to improve performance when recording
  events ([#1917])

Thanks to @djc for contributing to this release!

[#1917]: #1917
[#1911]: #1911

Closes #1919
hawkw added a commit that referenced this pull request Feb 21, 2022
# 0.17.2 (February 21, 2022)

This release fixes [an issue][#1944] introduced in v0.17.1 where
`tracing-opentelemetry` could not be compiled with
`default-features = false`.

### Fixed

- Compilation failure with `tracing-log` feature disabled ([#1949])

[#1949]: #1917
[#1944]: #1944
hawkw added a commit that referenced this pull request Feb 21, 2022
# 0.17.2 (February 21, 2022)

This release fixes [an issue][#1944] introduced in v0.17.1 where
`tracing-opentelemetry` could not be compiled with
`default-features = false`.

### Fixed

- Compilation failure with `tracing-log` feature disabled ([#1949])

[#1949]: #1917
[#1944]: #1944
davidbarsky pushed a commit to tokio-rs/tracing-opentelemetry that referenced this pull request Mar 21, 2023
# 0.17.1 (February 11, 2022)

### Added

- `OpenTelemetryLayer` can now add detailed location information to
  forwarded events (defaults to on) ([#1911])
- `OpenTelemetryLayer::with_event_location` to control whether source
  locations are recorded ([#1911])
### Changed

- Avoid unnecessary allocations to improve performance when recording
  events ([#1917])

Thanks to @djc for contributing to this release!

[#1917]: tokio-rs/tracing#1917
[#1911]: tokio-rs/tracing#1911

Closes #1919
davidbarsky pushed a commit to tokio-rs/tracing-opentelemetry that referenced this pull request Mar 21, 2023
# 0.17.2 (February 21, 2022)

This release fixes [an issue][#1944] introduced in v0.17.1 where
`tracing-opentelemetry` could not be compiled with
`default-features = false`.

### Fixed

- Compilation failure with `tracing-log` feature disabled ([#1949])

[#1949]: tokio-rs/tracing#1917
[#1944]: tokio-rs/tracing#1944
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants