Skip to content

Commit

Permalink
rework
Browse files Browse the repository at this point in the history
  • Loading branch information
adriangb committed Feb 5, 2025
1 parent b017c7b commit ed039ae
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
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
18 changes: 16 additions & 2 deletions opentelemetry-proto/tests/json_serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,12 +274,20 @@ mod json_serde {
kind: 2,
start_time_unix_nano: 1544712660000000000,
end_time_unix_nano: 1544712661000000000,
attributes: vec![KeyValue {
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 +377,12 @@ mod json_serde {
"value": {
"stringValue": "some value"
}
},
{
"key": "my.span.bytes.attr",
"value": {
"bytesValue": "gICA"
}
}
],
"droppedAttributesCount": 1,
Expand Down

0 comments on commit ed039ae

Please sign in to comment.