Giter VIP home page Giter VIP logo

Comments (4)

DebugSteven avatar DebugSteven commented on July 17, 2024 2

I'd like to take a shot at it!

from tracing.

DebugSteven avatar DebugSteven commented on July 17, 2024

I've been struggling a bit getting this issue done & I've talked with Eliza out of band about it. I thought I would go ahead & push up some of my work on this issue on my fork. Maybe some other folks have ideas about how we can get serde compatability.

The problem I'm running into is how exactly to implement Visit. All the Serializer methods consume self by-value but the Visit methods take &mut self. Another issue is that all the serde methods return Result & tokio-trace returns (). That makes it so we have to unwrap all the values. What makes sense to proceed on this issue? Do you have any thoughts @LucioFranco?

https://github.com/DebugSteven/tokio-trace-nursery/tree/tokio-trace-serde

from tracing.

LucioFranco avatar LucioFranco commented on July 17, 2024

@DebugSteven hmm, I took a look, I'm not sure I understand where the issue is with Serializer consuming self? Could you point out where you are seeing this issue?

I think for the second option, we probably just want to drop the field maybe? Serde can fail since its more general but in a logging/tracing context, it makes sense for it to not fail since im not sure what you'd do with the failed data? We prob dont want to log the error either since that will get noisy.

from tracing.

hawkw avatar hawkw commented on July 17, 2024

The problem I'm running into is how exactly to implement Visit. All the Serializer methods consume self by-value but the Visit methods take &mut self.

When serializing something that implements Visit, you're probably going to be using a SerializeMap rather than a Serializer, since we want to record fields as map key/value pairs. SerializeMap's serialize functions take &mut self.

Another issue is that all the serde methods return Result & tokio-trace returns (). That makes it so we have to unwrap all the values. What makes sense to proceed on this issue?

I'd probably recommend something like this (note that this is just a sketch):

struct SerdeVisitor<S: SerializeMap> {
     serializer: S,
     state: Result<(), S::Error>
} 

impl<S: SerializeMap> Visit for SerdeVisitor<S> {
    fn record_bool(&mut self, field: &Field, value: bool) {
        // If previous fields serialized successfully, continue serializing, 
        // otherwise, short-circuit and do nothing.
        if self.state.is_ok() {
            self.state = self.serializer
                .serialize_entry(field.name(), value)
                .map(|_| ());
        }
    }

    // ...
}

impl<S: SerializeMap> SerdeVisitor<S> {

    /// Completes serializing the visited object, returning `Ok(())` if all
    /// fields were serialized correctly, or `Err(S::Error)` if a field could
    /// not be serialized.
    fn finish(self) -> Result<S::Ok, S::Error> {
        self.state?;
        self.serializer.end()
    }
}

One other thing: after speaking to you a bit out of band, I realised something important that I don't think I made clear enough in the original issue description. In most cases, the right way to implement serialization is to try and serialize all the data exactly as it's represented in process, so that the same representation can be reconstructed. That's not what we want to do here --- tokio-trace has a lot of fairly complex types with complex relationships for performance reasons, but those often map to much simpler concepts. For example, the Field type is internally represented as a callsite ID and an index to a static string in an array. The general approach would be to serialize the index and callsite ID, so the same Field struct can be reconstructed. Instead, though, we want to serialize the string returned by calling "name" on the Field --- we don't want to expose these internal representation details that are in most cases the result of performance optimizations. Similarly, a ValueSet should not be deserialized as a ValueSet, but as a map.

from tracing.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.