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

Optional serde support for SpanContext #1075

Closed
wants to merge 5 commits 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
1 change: 1 addition & 0 deletions opentelemetry-api/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- Fix `SpanRef::set_attributes` mutability requirement. [#1038](https://github.com/open-telemetry/opentelemetry-rust/pull/1038)
- Move OrderMap module to root of otel-api crate. [#1061](https://github.com/open-telemetry/opentelemetry-rust/pull/1061)
- Fix `SpanContext` should implement serde::Serialize, Deserialize. [#1074](https://github.com/open-telemetry/opentelemetry-rust/issues/1074)

## v0.19.0
### Added
Expand Down
1 change: 1 addition & 0 deletions opentelemetry-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ futures-util = { version = "0.3", default-features = false, features = ["std", "
indexmap = "1.8"
once_cell = "1.12.0"
pin-project-lite = { version = "0.2", optional = true }
serde = { version = "1.0", optional = true, features = ["derive"] }
thiserror = "1"
tokio-stream = { version = "0.1", optional = true }
urlencoding = "2.1.2"
Expand Down
20 changes: 20 additions & 0 deletions opentelemetry-api/src/trace/span_context.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::trace::{TraceError, TraceResult};
#[cfg(feature = "serde")]
use serde::{self, Deserialize, Serialize};
use std::collections::VecDeque;
use std::fmt;
use std::hash::Hash;
Expand All @@ -17,6 +19,7 @@ use thiserror::Error;
///
/// [trace-flags]: https://www.w3.org/TR/trace-context/#trace-flags
#[derive(Clone, Debug, Default, PartialEq, Eq, Copy, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct TraceFlags(u8);

impl TraceFlags {
Expand Down Expand Up @@ -87,6 +90,7 @@ impl fmt::LowerHex for TraceFlags {
///
/// The id is valid if it contains at least one non-zero byte.
#[derive(Clone, PartialEq, Eq, Copy, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct TraceId(u128);

impl TraceId {
Expand Down Expand Up @@ -148,6 +152,7 @@ impl fmt::LowerHex for TraceId {
///
/// The id is valid if it contains at least one non-zero byte.
#[derive(Clone, PartialEq, Eq, Copy, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct SpanId(u64);

impl SpanId {
Expand Down Expand Up @@ -213,6 +218,7 @@ impl fmt::LowerHex for SpanId {
///
/// [W3C specification]: https://www.w3.org/TR/trace-context/#tracestate-header
#[derive(Clone, Debug, Default, Eq, PartialEq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct TraceState(Option<VecDeque<(String, String)>>);

impl TraceState {
Expand Down Expand Up @@ -448,10 +454,15 @@ impl From<TraceStateError> for TraceError {
/// [`Span`]: crate::trace::Span
/// [W3C TraceContext specification]: https://www.w3.org/TR/trace-context
#[derive(Clone, Debug, PartialEq, Hash, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct SpanContext {
trace_id: TraceId,
span_id: SpanId,
trace_flags: TraceFlags,
#[cfg_attr(
feature = "serde",
serde(skip, default = "SpanContext::deserialize_is_remote")
)]
is_remote: bool,
trace_state: TraceState,
}
Expand Down Expand Up @@ -525,6 +536,15 @@ impl SpanContext {
pub fn trace_state(&self) -> &TraceState {
&self.trace_state
}

/// Provides the value for self.is_remote when deserializing.
/// Since deserializing implies the span context was propagated, this
/// value is always true.
/// See https://serde.rs/field-attrs.html#default--path
#[cfg(feature = "serde")]
fn deserialize_is_remote() -> bool {
true
}
}

#[cfg(test)]
Expand Down
1 change: 1 addition & 0 deletions opentelemetry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ testing = ["opentelemetry_api/testing", "opentelemetry_sdk/testing"]
rt-tokio = ["opentelemetry_sdk/rt-tokio"]
rt-tokio-current-thread = ["opentelemetry_sdk/rt-tokio-current-thread"]
rt-async-std = ["opentelemetry_sdk/rt-async-std"]
serde = ["opentelemetry_api/serde"]