1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#![cfg_attr(not(feature = "std"), no_std)]

use core::{fmt, num::NonZeroU64};
use tracing_serde_structured::{
    SerializeId, SerializeLevel, SerializeMetadata, SerializeRecordFields, SerializeSpanFields,
};

#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub enum TraceEvent<'a> {
    /// Sent by the target periodically when not actively tracing, to indicate
    /// liveness, or to ack a [`HostRequest::SetMaxLevel`].
    Heartbeat(Option<SerializeLevel>),
    RegisterMeta {
        id: MetaId,

        #[serde(borrow)]
        meta: SerializeMetadata<'a>,
    },

    Event {
        parent: Option<SerializeId>,
        #[serde(borrow)]
        fields: SerializeRecordFields<'a>,
        meta: MetaId,
    },

    NewSpan {
        id: SerializeId,
        meta: MetaId,
        parent: Option<SerializeId>,
        is_root: bool,
        #[serde(borrow)]
        fields: SerializeSpanFields<'a>,
    },

    Enter(SerializeId),
    Exit(SerializeId),
    CloneSpan(SerializeId),
    DropSpan(SerializeId),

    /// The target put some data on the ground. Probably because a buffer was
    /// full.
    Discarded {
        new_spans: usize,
        span_activity: usize,
        events: usize,
        metas: usize,
    },
}

/// Requests sent from a host to a trace target.
#[derive(serde::Serialize, serde::Deserialize)]
pub enum HostRequest {
    /// Sets the maximum tracing level. Traces above this verbosity level will
    /// be discarded.
    ///
    /// This may cause the trace target to send new metadata to the host.
    SetMaxLevel(Option<SerializeLevel>), // TODO(eliza): add a keepalive?
}

#[derive(Copy, Clone, Hash, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct MetaId(NonZeroU64);

impl From<tracing_core::callsite::Identifier> for MetaId {
    fn from(id: tracing_core::callsite::Identifier) -> Self {
        Self(NonZeroU64::new(id.0 as *const _ as *const () as u64).expect("non-zero"))
    }
}

impl fmt::Debug for MetaId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "MetaId({:x})", self.0)
    }
}

impl fmt::Display for MetaId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:x}", self.0)
    }
}