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

Serde support #576

Closed
jtescher opened this issue Jun 19, 2021 · 12 comments
Closed

Serde support #576

jtescher opened this issue Jun 19, 2021 · 12 comments
Labels
question Further information is requested

Comments

@jtescher
Copy link
Member

Should the core opentelemetry crate support serde? There are standard serialization formats, it's unclear that this is necessary (or compliant with the intention of the spec).

@jtescher jtescher added the question Further information is requested label Jun 19, 2021
@jtescher jtescher mentioned this issue Jun 19, 2021
15 tasks
@TommyCpp
Copy link
Contributor

TommyCpp commented Jun 20, 2021

I don't believe the spec explicitly requires to provide a serialization formats but rather it leave this questions to each propagator and exporter. I think in some case the serde could make things easier for exporter/propagator writers but usually exporters/propagators need some kind of conversion before serialization. Thus, serde is not a must here.

We could provide some supports like using a serde-otlp crate to do serialization and deserialization for opentelemetry-otlp. That should help ppl who want to implement their own otlp exporter for any reason.

@frigus02
Copy link
Member

Good question. I'd say we probably don't need it. How does serde support work with backwards compatibility? If we add another field to a struct, which implements Serialize and Deserialize, would that count as a breaking change?

I like the idea of a separate crate in case there is demand for serialization and deserialization for some of the types.

@jtescher
Copy link
Member Author

Removed in #738

@slinkydeveloper
Copy link

Hi, is there any alternative available right now for this?

@slinkydeveloper
Copy link

slinkydeveloper commented Sep 21, 2022

For whoever stumbles on this, I needed to serialize SpanContext and my workaround is to use serde_with paired with an intermediate struct:

#[derive(Serialize, Deserialize)]
struct SpanContextDef {
    trace_id: [u8; 16],
    span_id: [u8; 8],
    trace_flags: u8,
    is_remote: bool,
    trace_state: String,
}

// Provide a conversion to construct the remote type.
impl From<SpanContextDef> for SpanContext {
    fn from(def: SpanContextDef) -> Self {
        SpanContext::new(
            TraceId::from_bytes(def.trace_id), SpanId::from_bytes(def.span_id), TraceFlags::new(def.trace_flags), def.is_remote, TraceState::from_str(&def.trace_state).unwrap()
        )
    }
}

impl From<SpanContext> for SpanContextDef {
    fn from(ctx: SpanContext) -> Self {
        Self {
            trace_id: ctx.trace_id().to_bytes(),
            span_id: ctx.span_id().to_bytes(),
            trace_flags: ctx.trace_flags().to_u8(),
            is_remote: ctx.is_remote(),
            trace_state: ctx.trace_state().header()
        }
    }
}

// In the struct where you need SpanContext
#[serde_as(as = "FromInto<SpanContextDef>")] 
SpanContext spanContext

See https://docs.rs/serde_with/2.0.1/serde_with/guide/serde_as_transformations/index.html#convert-to-an-intermediate-type-using-into for more details about serde_with

@joshtriplett
Copy link

I've been actively using this, and was surprised to find that it had disappeared when I upgraded opentelemetry. I'd love to register interest in this feature returning in the future, if at all possible.

@jtescher
Copy link
Member Author

jtescher commented Oct 3, 2022

I think providing serde support for SpanContext could make sense as that data is meant to be serialized in various formats (and potentially the data that is intended to be used by exporters as well). The other structs that supported it previously probably go against the spirit of the otel spec as I understand it.

@joshtriplett
Copy link

@jtescher I've been using it to both serialize and deserialize Vec<SpanData>, to implement a very simple TcpSpanExporter to ship traces from a constrained server to a less constrained one. If that's what you meany by "the data that is intended to be used by exporters as well", that's exactly what I'm looking for.

@jtescher
Copy link
Member Author

jtescher commented Oct 3, 2022

@jtescher I've been using it to both serialize and deserialize Vec<SpanData>, to implement a very simple TcpSpanExporter to ship traces from a constrained server to a less constrained one. If that's what you meany by "the data that is intended to be used by exporters as well", that's exactly what I'm looking for.

Yep that's included in what I was referring to, I think we could support that.

@joshtriplett
Copy link

@jtescher Thank you!

@joshtriplett
Copy link

@jtescher Checking back in on this. Would it be possible to get serde support for Vec<SpanData>, please?

@shaun-cox
Copy link
Contributor

(I should have searched closed issues first, but stumbled upon this after starting to work on #1074, which I just opened.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

6 participants