Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions opentelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## vNext

- **Added** `StringValue::from_static_str` to initialize a constant `StringValue`.

## 0.32.0

Released 2026-May-08
Expand Down
14 changes: 14 additions & 0 deletions opentelemetry/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,20 @@ impl AsRef<str> for StringValue {
}

impl StringValue {
/// Create a new const `StringValue`.
///
/// This is useful for making static label values:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

let labels = &[KeyValue::new("key", "value")];

^ couldn't we already this today?

/// ```rust
/// # use opentelemetry::{StringValue, KeyValue, Key, Value};
/// let labels = &[KeyValue::new(
/// Key::from_static_str("key"),
/// StringValue::from_static_str("value"),
/// )];
/// ```
pub const fn from_static_str(value: &'static str) -> Self {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could we add a small doctest showing the actual const use case here?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

added

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Unfortunately, we can't make a KeyValue at compile time with the current initializer. I changed the example.

StringValue(OtelString::Static(value))
}

/// Returns a string slice to this value
pub fn as_str(&self) -> &str {
self.0.as_str()
Expand Down
Loading