Skip to content
Merged
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
82 changes: 72 additions & 10 deletions src/convert/tonic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use core::convert::Infallible;
use std::borrow::Cow;

use itoa::Buffer as IntegerBuffer;
use ryu::Buffer as FloatBuffer;
use tonic::{
Code, Status,
metadata::{MetadataMap, MetadataValue}
Expand Down Expand Up @@ -109,14 +110,15 @@ fn insert_retry(meta: &mut MetadataMap, retry: RetryAdvice) {
}

fn attach_metadata(meta: &mut MetadataMap, metadata: &Metadata) {
let mut formatter = MetadataValueFormatter::new();
for (name, value, redaction) in metadata.iter_with_redaction() {
if !matches!(redaction, FieldRedaction::None) {
continue;
}
if !is_safe_metadata_key(name) {
continue;
}
if let Some(serialized) = metadata_value_to_ascii(value) {
if let Some(serialized) = metadata_value_to_ascii(value, &mut formatter) {
insert_ascii(meta, name, serialized);
}
}
Expand All @@ -135,19 +137,55 @@ fn insert_ascii(meta: &mut MetadataMap, key: &'static str, value: impl AsRef<str
}
}

fn metadata_value_to_ascii(value: &FieldValue) -> Option<Cow<'_, str>> {
#[derive(Debug)]
enum MetadataAscii<'a> {
Static(&'static str),
Buffer(&'a str),
Owned(String)
}

impl AsRef<str> for MetadataAscii<'_> {
fn as_ref(&self) -> &str {
match self {
Self::Static(text) | Self::Buffer(text) => text,
Self::Owned(text) => text.as_str()
}
}
}

#[derive(Default)]
struct MetadataValueFormatter {
integers: IntegerBuffer,
floats: FloatBuffer
}

impl MetadataValueFormatter {
fn new() -> Self {
Self {
integers: IntegerBuffer::new(),
floats: FloatBuffer::new()
}
}
}

fn metadata_value_to_ascii<'a>(
value: &FieldValue,
formatter: &'a mut MetadataValueFormatter
) -> Option<MetadataAscii<'a>> {
match value {
FieldValue::Str(value) => {
let text = value.as_ref();
is_ascii_metadata_value(text).then_some(Cow::Borrowed(text))
is_ascii_metadata_value(text).then_some(MetadataAscii::Static(text))
Comment on lines +171 to +178

Choose a reason for hiding this comment

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

[P0] Accepting owned strings no longer compiles

The new MetadataAscii::Static arm assumes every FieldValue::Str is backed by a &'static str. However, FieldValue::Str stores a Cow<'static, str> and callers frequently pass owned Strings via field::str(...). For those owned cases value.as_ref() only yields a borrow tied to the field, and cannot be coerced to 'static, so this branch fails to compile once an owned string is encountered. Previously we returned a borrowed Cow<'_, str> which worked for both borrowed and owned strings. Please use an owned/buffer variant for dynamic strings instead of Static to keep the API usable.

Useful? React with 👍 / 👎.

}
FieldValue::I64(value) => Some(MetadataAscii::Buffer(formatter.integers.format(*value))),
FieldValue::U64(value) => Some(MetadataAscii::Buffer(formatter.integers.format(*value))),
FieldValue::F64(value) => Some(MetadataAscii::Buffer(formatter.floats.format(*value))),
FieldValue::Bool(value) => {
Some(MetadataAscii::Static(if *value { "true" } else { "false" }))
}
FieldValue::I64(value) => Some(Cow::Owned(value.to_string())),
FieldValue::U64(value) => Some(Cow::Owned(value.to_string())),
FieldValue::F64(value) => Some(Cow::Owned(value.to_string())),
FieldValue::Bool(value) => Some(Cow::Borrowed(if *value { "true" } else { "false" })),
FieldValue::Uuid(value) => Some(Cow::Owned(value.to_string())),
FieldValue::Duration(value) => Some(Cow::Owned(duration_to_string(*value))),
FieldValue::Ip(value) => Some(Cow::Owned(value.to_string())),
FieldValue::Uuid(value) => Some(MetadataAscii::Owned(value.to_string())),
FieldValue::Duration(value) => Some(MetadataAscii::Owned(duration_to_string(*value))),
FieldValue::Ip(value) => Some(MetadataAscii::Owned(value.to_string())),
#[cfg(feature = "serde_json")]
FieldValue::Json(_) => None
}
Expand Down Expand Up @@ -217,6 +255,30 @@ mod tests {
);
}

#[test]
fn numeric_metadata_is_rendered_consistently() {
let err = AppError::service("numbers")
.with_field(field::i64("signed", -42))
.with_field(field::u64("unsigned", 9000))
.with_field(field::f64("ratio", 1.25));
let status = Status::from(err);
let metadata = status.metadata();
assert_eq!(
metadata.get("signed").and_then(|value| value.to_str().ok()),
Some("-42")
);
assert_eq!(
metadata
.get("unsigned")
.and_then(|value| value.to_str().ok()),
Some("9000")
);
assert_eq!(
metadata.get("ratio").and_then(|value| value.to_str().ok()),
Some("1.25")
);
}

#[test]
fn timeout_status_carries_ascii_metadata() {
let status = Status::from(AppError::timeout("deadline exceeded").with_retry_after_secs(7));
Expand Down
Loading