|
1 | 1 | //! This module holds all event types that are send between the client and the database.
|
2 | 2 |
|
| 3 | +// Allow module inception here, since "event" is the expected as the name for both modules. |
| 4 | +// Renaming would be possible, but would probably lead to more confusion. |
| 5 | +#[allow(clippy::module_inception)] |
| 6 | +mod event; |
| 7 | +mod event_candidate; |
3 | 8 | mod management_event;
|
4 | 9 |
|
| 10 | +pub use event::Event; |
| 11 | +pub use event_candidate::EventCandidate; |
5 | 12 | pub use management_event::ManagementEvent;
|
| 13 | +use serde::{Deserialize, Serialize}; |
| 14 | + |
| 15 | +#[cfg(feature="cloudevents")] |
| 16 | +use crate::error::EventError; |
| 17 | + |
| 18 | +/// Represents the trace information of an event. |
| 19 | +/// This is used for distributed tracing. |
| 20 | +/// It can either be a traceparent or a traceparent and tracestate. |
| 21 | +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 22 | +#[serde(untagged)] |
| 23 | +pub enum TraceInfo { |
| 24 | + // LEAVE ORDER AS IS |
| 25 | + // This is important for deserialization as the traceparent is always present |
| 26 | + |
| 27 | + /// The traceparent and tracestate of the event. |
| 28 | + /// This is used for distributed tracing. |
| 29 | + WithState { |
| 30 | + /// The traceparent of the event. |
| 31 | + /// This is used for distributed tracing. |
| 32 | + traceparent: String, |
| 33 | + /// The tracestate of the event. |
| 34 | + /// This is used for distributed tracing. |
| 35 | + tracestate: String, |
| 36 | + }, |
| 37 | + /// The traceparent of the event. |
| 38 | + /// This is used for distributed tracing. |
| 39 | + Traceparent { |
| 40 | + /// The traceparent of the event. |
| 41 | + /// This is used for distributed tracing. |
| 42 | + traceparent: String, |
| 43 | + }, |
| 44 | +} |
| 45 | + |
| 46 | +impl TraceInfo { |
| 47 | + /// Get the traceparent of the event. |
| 48 | + #[must_use] |
| 49 | + pub fn traceparent(&self) -> &str { |
| 50 | + match self { |
| 51 | + Self::Traceparent { traceparent } | Self::WithState { traceparent, .. } => traceparent, |
| 52 | + } |
| 53 | + } |
| 54 | + /// Get the tracestate of the event. |
| 55 | + #[must_use] |
| 56 | + pub fn tracestate(&self) -> Option<&str> { |
| 57 | + match self { |
| 58 | + Self::Traceparent { .. } => None, |
| 59 | + Self::WithState { tracestate, .. } => Some(tracestate), |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /// Create a new `TraceInfo` from a cloudevent. |
| 64 | + /// This will return None if the cloudevent does not contain a traceparent or tracestate. |
| 65 | + /// |
| 66 | + /// # Errors |
| 67 | + /// If the cloudevent contains a tracestate but no traceparent, an error will be returned. |
| 68 | + #[cfg(feature="cloudevents")] |
| 69 | + pub fn from_cloudevent(event: &cloudevents::Event) -> Result<Option<Self>, EventError> { |
| 70 | + let traceparent = event.extension("traceparent").map(ToString::to_string); |
| 71 | + let tracestate = event.extension("tracestate").map(ToString::to_string); |
| 72 | + |
| 73 | + match (traceparent, tracestate) { |
| 74 | + (Some(traceparent), Some(tracestate)) => Ok(Some(Self::WithState { |
| 75 | + traceparent, |
| 76 | + tracestate, |
| 77 | + })), |
| 78 | + (Some(traceparent), None) => Ok(Some(Self::Traceparent { traceparent })), |
| 79 | + (None, None) => Ok(None), |
| 80 | + (None, Some(_)) => Err(EventError::InvalidCloudevent), |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments