You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, opentelemetry represents strings in key-value pairs (i.e. Keys and Value::Strings) using Cow<'static, str>. This has the advantage that when a string constant is used, it isn't necessary to allocate a String on the heap for it. If a string is dynamically constructed at runtime, a String is used instead.
One deficiency of this approach is that oftentimes, a string may be dynamically determined at runtime, but repeated many times in an application's traces. In this case, such a string is represented as a String, and cloning it requires allocating memory and copying all of the string's bytes into that allocation. This has an unfortunate performance cost, especially when the same string is repeated a large number of times. It would be nice if it was also possible to use an Arc<str> to represent strings in key-value pairs. That way, cloning a repeated string to emit it as part of a trace would only require incrementing a reference count, reducing the overhead of using the string (and decreasing overall memory use).
I think it should be possible to support Arc<str> as a representation for Keys without making a breaking change, since Key is a newtype whose field is private. Although the Key::new constructor takes S: Into<Cow<'static, str>>, we could add a new from_arc (or similar) constructor. Then, the internal representation of Key could be changed to something like this:
Unfortunately, though, a similar change for Value isn't possible without breaking API compatibility, because Value is a pub enum without #[non_exhaustive].
The text was updated successfully, but these errors were encountered:
Currently,
opentelemetry
represents strings in key-value pairs (i.e.Key
s andValue::String
s) usingCow<'static, str>
. This has the advantage that when a string constant is used, it isn't necessary to allocate aString
on the heap for it. If a string is dynamically constructed at runtime, aString
is used instead.One deficiency of this approach is that oftentimes, a string may be dynamically determined at runtime, but repeated many times in an application's traces. In this case, such a string is represented as a
String
, and cloning it requires allocating memory and copying all of the string's bytes into that allocation. This has an unfortunate performance cost, especially when the same string is repeated a large number of times. It would be nice if it was also possible to use anArc<str>
to represent strings in key-value pairs. That way, cloning a repeated string to emit it as part of a trace would only require incrementing a reference count, reducing the overhead of using the string (and decreasing overall memory use).This came up while reviewing a change to the
tracing
crate's OpenTelemetry integration (tokio-rs/tracing#2135 (comment)).I think it should be possible to support
Arc<str>
as a representation forKey
s without making a breaking change, sinceKey
is a newtype whose field is private. Although theKey::new
constructor takesS: Into<Cow<'static, str>>
, we could add a newfrom_arc
(or similar) constructor. Then, the internal representation ofKey
could be changed to something like this:Unfortunately, though, a similar change for
Value
isn't possible without breaking API compatibility, becauseValue
is apub enum
without#[non_exhaustive]
.The text was updated successfully, but these errors were encountered: