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

Implement serde test #1860

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
{
"resourceSpans": [
{
"resource": {
"attributes": [
{
"key": "service.name",
"value": {
"stringValue": "basic-otlp-tracing-example"
}
}
],
"droppedAttributesCount": 0
},
"scopeSpans": [
{
"scope": {
"name": "ex.com/basic",
"version": "",
"attributes": [],
"droppedAttributesCount": 0
},
"spans": [
{
"traceId": "9b458af7378cba65253d7042d34fc72e",
"spanId": "cd7cf7bf939930b7",
"traceState": "",
"parentSpanId": "d58cf2d702a061e0",
"flags": 0,
"name": "Sub operation...",
"kind": 1,
"startTimeUnixNano": "1703985537070566698",
"endTimeUnixNano": "1703985537070572718",
"attributes": [
{
"key": "lemons",
"value": {
"stringValue": "five"
}
}
],
"droppedAttributesCount": 0,
"events": [
{
"timeUnixNano": "1703985537070567697",
"name": "Sub span event",
"attributes": [],
"droppedAttributesCount": 0
}
],
"droppedEventsCount": 0,
"links": [],
"droppedLinksCount": 0,
"status": {
"message": "",
"code": 0
}
}
],
"schemaUrl": ""
}
],
"schemaUrl": ""
},
{
"resource": {
"attributes": [
{
"key": "service.name",
"value": {
"stringValue": "basic-otlp-tracing-example"
}
}
],
"droppedAttributesCount": 0
},
"scopeSpans": [
{
"scope": {
"name": "ex.com/basic",
"version": "",
"attributes": [],
"droppedAttributesCount": 0
},
"spans": [
{
"traceId": "9b458af7378cba65253d7042d34fc72e",
"spanId": "d58cf2d702a061e0",
"traceState": "",
"parentSpanId": "",
"flags": 0,
"name": "operation",
"kind": 1,
"startTimeUnixNano": "1703985537070558635",
"endTimeUnixNano": "1703985537070580454",
"attributes": [
{
"key": "ex.com/another",
"value": {
"stringValue": "yes"
}
}
],
"droppedAttributesCount": 0,
"events": [
{
"timeUnixNano": "1703985537070563326",
"name": "Nice operation!",
"attributes": [
{
"key": "bogons",
"value": "100"
Copy link
Contributor

@ramgdev ramgdev Jun 4, 2024

Choose a reason for hiding this comment

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

The string value here should use Value::String during serialization instead of plain string for attributes. When a number is preferred on deserialization, then it should be serialized as Value::I64.

}
],
"droppedAttributesCount": 0
}
],
"droppedEventsCount": 0,
"links": [],
"droppedLinksCount": 0,
"status": {
"message": "",
"code": 0
}
}
],
"schemaUrl": ""
}
],
"schemaUrl": ""
}
]
}
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
}
17 changes: 17 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,18 @@ 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();
}
Loading