Skip to content

json: ser/de bytes as base64 strings not an array of bytes #2471

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

Merged
merged 4 commits into from
Feb 5, 2025
Merged
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
3 changes: 2 additions & 1 deletion opentelemetry-proto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ testing = ["opentelemetry/testing"]
# add ons
internal-logs = ["tracing"]
with-schemars = ["schemars"]
with-serde = ["serde", "hex"]
with-serde = ["serde", "hex", "base64"]

[dependencies]
tonic = { workspace = true, optional = true, features = ["codegen", "prost"] }
Expand All @@ -57,6 +57,7 @@ schemars = { version = "0.8", optional = true }
serde = { workspace = true, optional = true, features = ["serde_derive"] }
hex = { version = "0.4.3", optional = true }
tracing = {workspace = true, optional = true} # optional for opentelemetry internal logging
base64 = { version = "0.22.1", optional = true }

[dev-dependencies]
opentelemetry = { features = ["testing"], path = "../opentelemetry" }
Expand Down
11 changes: 9 additions & 2 deletions opentelemetry-proto/src/proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ pub(crate) mod serializers {
map.serialize_entry("intValue", &i.to_string());
map.end()
}
Some(Value::BytesValue(b)) => {
let mut map = serializer.serialize_map(Some(1))?;
map.serialize_entry("bytesValue", &base64::encode(b));
map.end()
}
Some(value) => value.serialize(serializer),
None => serializer.serialize_none(),
}
Expand Down Expand Up @@ -127,8 +132,10 @@ pub(crate) mod serializers {
value = Some(any_value::Value::KvlistValue(kv));
}
"bytesValue" => {
let bytes = map.next_value()?;
value = Some(any_value::Value::BytesValue(bytes));
let base64: String = map.next_value()?;
let decoded = base64::decode(base64.as_bytes())
.map_err(|e| de::Error::custom(e))?;
value = Some(any_value::Value::BytesValue(decoded));
}
_ => {
//skip unknown keys, and handle error later.
Expand Down
28 changes: 22 additions & 6 deletions opentelemetry-proto/tests/json_serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,12 +274,22 @@ mod json_serde {
kind: 2,
start_time_unix_nano: 1544712660000000000,
end_time_unix_nano: 1544712661000000000,
attributes: vec![KeyValue {
key: String::from("my.span.attr"),
value: Some(AnyValue {
value: Some(Value::StringValue(String::from("some value"))),
}),
}],
attributes: vec![
KeyValue {
key: String::from("my.span.attr"),
value: Some(AnyValue {
value: Some(Value::StringValue(String::from(
"some value",
))),
}),
},
KeyValue {
key: String::from("my.span.bytes.attr"),
value: Some(AnyValue {
value: Some(Value::BytesValue(vec![0x80, 0x80, 0x80])),
}),
},
],
dropped_attributes_count: 1,
events: vec![Event {
time_unix_nano: 1544712660500000000,
Expand Down Expand Up @@ -369,6 +379,12 @@ mod json_serde {
"value": {
"stringValue": "some value"
}
},
{
"key": "my.span.bytes.attr",
"value": {
"bytesValue": "gICA"
}
}
],
"droppedAttributesCount": 1,
Expand Down