Skip to content
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

fix otlp/json exporter #1882

Merged
merged 9 commits into from
Jun 18, 2024
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
2 changes: 2 additions & 0 deletions opentelemetry-otlp/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ now use `.with_resource(RESOURCE::default())` to configure Resource when using
These methods would also no longer set the global tracer provider. It would now be the responsibility of users to set it by calling `global::set_tracer_provider(tracer_provider.clone());`. Refer to the [basic-otlp](https://github.com/open-telemetry/opentelemetry-rust/blob/main/opentelemetry-otlp/examples/basic-otlp/src/main.rs) and [basic-otlp-http](https://github.com/open-telemetry/opentelemetry-rust/blob/main/opentelemetry-otlp/examples/basic-otlp-http/src/main.rs) examples on how to initialize OTLP Trace Exporter.
- **Breaking** Correct the misspelling of "webkpi" to "webpki" in features [#1842](https://github.com/open-telemetry/opentelemetry-rust/pull/1842)
- Bump MSRV to 1.70 [#1840](https://github.com/open-telemetry/opentelemetry-rust/pull/1840)
- Fixing the OTLP HTTP/JSON exporter. [#1882](https://github.com/open-telemetry/opentelemetry-rust/pull/1882) - The exporter was broken in the
previous release.

## v0.16.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,6 @@ fn span_eq(left: &Span, right: &Span) -> bool {
pub fn read_spans_from_json(file: File) -> Vec<ResourceSpans> {
let reader = std::io::BufReader::new(file);

let trace_data: TracesData = serde_json::from_reader(reader).unwrap();
let trace_data: TracesData = serde_json::from_reader(reader).expect("Failed to read json file");
trace_data.resource_spans
}
27 changes: 27 additions & 0 deletions opentelemetry-otlp/tests/integration_test/tests/traces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ use opentelemetry::{
trace::{TraceContextExt, Tracer},
Key, KeyValue,
};
use opentelemetry_proto::tonic::trace::v1::TracesData;
use opentelemetry_sdk::{runtime, trace as sdktrace, Resource};
use std::error::Error;
use std::fs::File;
use std::io::Write;
use std::os::unix::fs::MetadataExt;

fn init_tracer_provider() -> Result<sdktrace::TracerProvider, TraceError> {
Expand Down Expand Up @@ -83,3 +85,28 @@ pub fn test_assert_span_eq() {

TraceAsserter::new(spans.clone(), spans).assert();
}

#[test]
pub fn test_serde() {
let spans = read_spans_from_json(
File::open("./expected/traces.json").expect("Failed to read traces.json"),
);
let json = serde_json::to_string_pretty(&TracesData {
resource_spans: spans,
})
.expect("Failed to serialize spans to json");

// Write to file.
let mut file = File::create("./expected/serialized_traces.json").unwrap();
file.write_all(json.as_bytes()).unwrap();

let left = read_spans_from_json(
File::open("./expected/traces.json").expect("Failed to read traces.json"),
);
let right = read_spans_from_json(
File::open("./expected/serialized_traces.json")
.expect("Failed to read serialized_traces.json"),
);

TraceAsserter::new(left, right).assert();
}
22 changes: 17 additions & 5 deletions opentelemetry-proto/src/proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,26 @@ pub(crate) mod serializers {

// AnyValue <-> KeyValue conversion
pub fn serialize_to_value<S>(value: &Option<AnyValue>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
where
S: Serializer,
{
// Serialize any_value::Value using its own implementation
// If value is None, it will be serialized as such
match value {
Some(any_value) => match &any_value.value {
Some(Value::IntValue(i)) => serialize_i64_to_string(i, serializer),
Some(Value::IntValue(i)) => {
// Attempt to create a struct to wrap the intValue
let mut state = match serializer.serialize_struct("Value", 1) {
Ok(s) => s,
Err(e) => return Err(e), // Handle the error or return it
};

// Attempt to serialize the intValue field
if let Err(e) = state.serialize_field("intValue", &i.to_string()) {
return Err(e); // Handle the error or return it
}

// Finalize the struct serialization
state.end()
},
Some(value) => value.serialize(serializer),
None => serializer.serialize_none(),
},
Expand Down
Loading