Skip to content

Commit c275f05

Browse files
committed
features: Add "serialize" feature (#59)
Added optional `serialize` feature that allows `SpanData` to be serialized/deserialized using the `serde` crate. Also added a basic unit test for `SpanData` to test serialization and deserialization. Fixes: #58. Signed-off-by: James O. D. Hunt <james.o.hunt@intel.com>
1 parent 2561f85 commit c275f05

9 files changed

Lines changed: 76 additions & 0 deletions

File tree

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ lazy_static = "1.4.0"
1616
pin-project = { version = "0.4.6", optional = true }
1717
prometheus = { version = "0.7.0", optional = true }
1818
rand = { version = "0.7.2", optional = true }
19+
serde = { version = "1.0.104", features = ["derive"], optional = true }
20+
bincode = { version = "1.2.1", optional = true }
1921

2022
[dev-dependencies]
2123
hyper = "0.12.0"
@@ -28,6 +30,7 @@ tokio = { version = "0.2.10", features = ["full"] }
2830
default = ["metrics", "trace"]
2931
trace = ["rand", "pin-project"]
3032
metrics = ["prometheus"]
33+
serialize = ["serde", "bincode"]
3134

3235
[workspace]
3336
members = [

scripts/test.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
set -eu
44

55
cargo test --all "$@"
6+
cargo test --all "$@" --features="default serialize"

src/api/core.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
//! OpenTelemetry shared core date types
2+
#[cfg(feature = "serialize")]
3+
use serde::{Deserialize, Serialize};
24
use std::borrow::Cow;
35

46
/// Key used for metric `LabelSet`s and trace `Span` attributes.
7+
#[cfg_attr(feature = "serialize", derive(Deserialize, PartialEq, Serialize))]
58
#[derive(Clone, Debug)]
69
pub struct Key(Cow<'static, str>);
710

@@ -92,6 +95,7 @@ impl Into<String> for Key {
9295
}
9396

9497
/// Value types for use in `KeyValue` pairs.
98+
#[cfg_attr(feature = "serialize", derive(Deserialize, PartialEq, Serialize))]
9599
#[derive(Clone, Debug)]
96100
pub enum Value {
97101
/// bool values
@@ -140,6 +144,7 @@ impl Into<Cow<'static, str>> for Value {
140144
}
141145

142146
/// `KeyValue` pairs are used by `LabelSet`s and `Span` attributes.
147+
#[cfg_attr(feature = "serialize", derive(Deserialize, PartialEq, Serialize))]
143148
#[derive(Clone, Debug)]
144149
pub struct KeyValue {
145150
/// Dimension or event key

src/api/trace/event.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
//! # OpenTelemetry Trace Event Interface
22
3+
#[cfg(feature = "serialize")]
4+
use serde::{Deserialize, Serialize};
35
use std::time::SystemTime;
46

57
/// A `Span` has the ability to add events. Events have a time associated
68
/// with the moment when they are added to the `Span`.
9+
#[cfg_attr(feature = "serialize", derive(Deserialize, PartialEq, Serialize))]
710
#[derive(Clone, Debug)]
811
pub struct Event {
912
/// Event message

src/api/trace/link.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
//! # OpenTelemetry Trace Link Interface
2+
#[cfg(feature = "serialize")]
3+
use serde::{Deserialize, Serialize};
24

35
/// During the `Span` creation user MUST have the ability to record links to other `Span`s. Linked
46
/// `Span`s can be from the same or a different trace.
7+
#[cfg_attr(feature = "serialize", derive(Deserialize, PartialEq, Serialize))]
58
#[derive(Clone, Debug)]
69
pub struct Link {}

src/api/trace/span.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
//! implementations MUST NOT allow callers to create Spans directly. All `Span`s MUST be created
1717
//! via a Tracer.
1818
use crate::api;
19+
#[cfg(feature = "serialize")]
20+
use serde::{Deserialize, Serialize};
1921
use std::time::SystemTime;
2022

2123
/// Interface for a single operation within a trace.
@@ -168,6 +170,7 @@ pub trait Span: Send + Sync + std::fmt::Debug {
168170
/// | `PRODUCER` | | yes | | yes |
169171
/// | `CONSUMER` | | yes | yes | |
170172
/// | `INTERNAL` | | | | |
173+
#[cfg_attr(feature = "serialize", derive(Deserialize, PartialEq, Serialize))]
171174
#[derive(Clone, Debug)]
172175
pub enum SpanKind {
173176
/// Indicates that the span describes a synchronous request to
@@ -195,6 +198,7 @@ pub enum SpanKind {
195198
/// The `SpanStatus` interface represents the status of a finished `Span`.
196199
/// It's composed of a canonical code in conjunction with an optional
197200
/// descriptive message.
201+
#[cfg_attr(feature = "serialize", derive(Deserialize, PartialEq, Serialize))]
198202
#[derive(Clone, Debug)]
199203
pub enum SpanStatus {
200204
/// OK is returned on success.

src/api/trace/span_context.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
//! The spec can be viewed here: https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/api-tracing.md#spancontext
1111
//!
1212
//! [w3c TraceContext specification]: https://www.w3.org/TR/trace-context/
13+
#[cfg(feature = "serialize")]
14+
use serde::{Deserialize, Serialize};
1315

1416
const TRACE_FLAGS_BIT_MASK_SAMPLED: u8 = 0x01;
1517
const TRACE_FLAGS_BIT_MASK_UNUSED: u8 = 0xFE;
@@ -21,6 +23,7 @@ pub const TRACE_FLAG_SAMPLED: u8 = TRACE_FLAGS_BIT_MASK_SAMPLED;
2123
pub const TRACE_FLAGS_UNUSED: u8 = TRACE_FLAGS_BIT_MASK_UNUSED;
2224

2325
/// Immutable portion of a `Span` which can be serialized and propagated.
26+
#[cfg_attr(feature = "serialize", derive(Deserialize, Serialize))]
2427
#[derive(Clone, Debug, PartialEq)]
2528
pub struct SpanContext {
2629
trace_id: u128,

src/exporter/trace/mod.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Trace exporters
22
use crate::{api, sdk};
3+
#[cfg(feature = "serialize")]
4+
use serde::{Deserialize, Serialize};
35
use std::sync::Arc;
46
use std::time::SystemTime;
57

@@ -57,6 +59,7 @@ pub trait SpanExporter: Send + Sync + std::fmt::Debug {
5759

5860
/// `SpanData` contains all the information collected by a `Span` and can be used
5961
/// by exporters as a standard input.
62+
#[cfg_attr(feature = "serialize", derive(Deserialize, PartialEq, Serialize))]
6063
#[derive(Clone, Debug)]
6164
pub struct SpanData {
6265
/// Exportable `SpanContext`
@@ -80,3 +83,51 @@ pub struct SpanData {
8083
/// Span status
8184
pub status: api::SpanStatus,
8285
}
86+
87+
#[cfg(feature = "serialize")]
88+
#[cfg(test)]
89+
mod tests {
90+
use super::*;
91+
92+
#[test]
93+
fn test_serialise() {
94+
let trace_id = 7;
95+
let span_id = 99;
96+
97+
let trace_flags = 0;
98+
let remote = false;
99+
let context = api::SpanContext::new(trace_id, span_id, trace_flags, remote);
100+
101+
let parent_span_id = 1;
102+
let span_kind = api::SpanKind::Client;
103+
let name = "foo/bar baz 人?!".to_string();
104+
let start_time = SystemTime::now();
105+
let end_time = SystemTime::now();
106+
107+
let capacity = 3;
108+
let attributes = sdk::EvictedQueue::new(capacity);
109+
let message_events = sdk::EvictedQueue::new(capacity);
110+
let links = sdk::EvictedQueue::new(capacity);
111+
112+
let status = api::SpanStatus::OK;
113+
114+
let span_data = SpanData {
115+
context,
116+
parent_span_id,
117+
span_kind,
118+
name,
119+
start_time,
120+
end_time,
121+
attributes,
122+
message_events,
123+
links,
124+
status,
125+
};
126+
127+
let encoded: Vec<u8> = bincode::serialize(&span_data).unwrap();
128+
129+
let decoded: SpanData = bincode::deserialize(&encoded[..]).unwrap();
130+
131+
assert_eq!(span_data, decoded);
132+
}
133+
}

src/sdk/trace/evicted_queue.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
//! # Evicted Queue
22
3+
#[cfg(feature = "serialize")]
4+
use serde::{Deserialize, Serialize};
35
use std::collections::VecDeque;
46

57
/// This queue maintains an ordered list of elements, and a count of
68
/// dropped elements. Elements are removed from the queue in a first
79
/// in first out fashion.
10+
#[cfg_attr(feature = "serialize", derive(Deserialize, PartialEq, Serialize))]
811
#[derive(Clone, Debug)]
912
pub struct EvictedQueue<T> {
1013
queue: VecDeque<T>,

0 commit comments

Comments
 (0)