Skip to content

Commit

Permalink
fix(proto): add serde(default) to Events and Status (#1485)
Browse files Browse the repository at this point in the history
This should allow all fields in event to be optional, which is needed to
decode JSON strings

## Changes
- add `serde(default)` to `Events` for tonic generated types
- add `serde(default)` to `Status` for tonic generated types

## Merge requirement checklist

* [x]
[CONTRIBUTING](https://github.com/open-telemetry/opentelemetry-rust/blob/main/CONTRIBUTING.md)
guidelines followed
* [x] Unit tests added/updated (if applicable)
* [ ] Appropriate `CHANGELOG.md` files updated for non-trivial,
user-facing changes
* [ ] Changes in public API reviewed (if applicable)
  • Loading branch information
TommyCpp authored Jan 22, 2024
1 parent 67e6a71 commit 5b456dd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ pub mod span {
#[cfg_attr(feature = "with-schemars", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "with-serde", serde(rename_all = "camelCase"))]
#[cfg_attr(feature = "with-serde", serde(default))]
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Event {
Expand Down
2 changes: 2 additions & 0 deletions opentelemetry-proto/tests/grpc_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ fn build_tonic() {
"trace.v1.ResourceSpans",
"common.v1.InstrumentationScope",
"resource.v1.Resource",
"trace.v1.Span.Event",
"trace.v1.Span.Status",
] {
builder = builder.type_attribute(
path,
Expand Down
15 changes: 15 additions & 0 deletions opentelemetry-proto/tests/json_deserialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod json_deserialize {
use opentelemetry_proto::tonic::collector::trace::v1::ExportTraceServiceRequest;
use opentelemetry_proto::tonic::common::v1::any_value::Value;
use opentelemetry_proto::tonic::common::v1::KeyValue;
use opentelemetry_proto::tonic::trace::v1::span::Event;

// copied from example json file
// see https://github.com/open-telemetry/opentelemetry-proto/blob/v1.0.0/examples/trace.json
Expand Down Expand Up @@ -69,6 +70,13 @@ mod json_deserialize {
}
"#;

const EVENT_JSON: &str = r#"
{
"name": "my_event",
"time_unix_nano": 1234567890
}
"#;

#[test]
fn test_deserialize_traces() {
let request: ExportTraceServiceRequest = serde_json::from_str(TRACES_JSON).unwrap();
Expand Down Expand Up @@ -139,4 +147,11 @@ mod json_deserialize {
Value::StringValue("my.service".to_string())
);
}

#[test]
fn test_event() {
let event_json: Event = serde_json::from_str(EVENT_JSON).unwrap();
assert_eq!(event_json.name, "my_event".to_string());
assert_eq!(event_json.attributes.len(), 0);
}
}

0 comments on commit 5b456dd

Please sign in to comment.