Skip to content

Commit

Permalink
test serde for i64
Browse files Browse the repository at this point in the history
  • Loading branch information
lalitb committed Jun 5, 2024
1 parent f1cdaca commit bfbd517
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
{
"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": {
"intValue": "100"
}
}
],
"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();
}
7 changes: 6 additions & 1 deletion opentelemetry-proto/src/proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@ pub(crate) mod serializers {
// 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)) => {
// Create a struct to wrap the intValue
let mut state = serializer.serialize_struct("Value", 1)?;
state.serialize_field("intValue", &i.to_string())?;
state.end()
},
Some(value) => value.serialize(serializer),
None => serializer.serialize_none(),
},
Expand Down

0 comments on commit bfbd517

Please sign in to comment.