vector/api/schema/events/
output.rs

1use async_graphql::{Object, Union};
2
3use crate::api::schema::events::log::Log;
4use crate::api::schema::events::metric::Metric;
5use crate::api::schema::events::trace::Trace;
6use vector_lib::tap::controller::TapPayload;
7use vector_lib::tap::notification::Notification;
8
9/// This wrapper struct hoists `message` up from [`Notification`] for a more
10/// natural querying experience. While ideally [`Notification`] would be a
11/// GraphQL interface with a common `message` field, an interface cannot be
12/// directly nested into the union of [`super::OutputEventsPayload`].
13///
14/// The GraphQL specification forbids such a nesting:
15/// <http://spec.graphql.org/October2021/#sel-HAHdfFDABABkG3_I>
16#[derive(Debug, Clone)]
17pub struct EventNotification {
18    pub notification: Notification,
19}
20
21#[Object]
22/// A notification regarding events observation
23impl EventNotification {
24    /// Notification details
25    async fn notification(&self) -> &Notification {
26        &self.notification
27    }
28
29    /// The human-readable message associated with the notification
30    async fn message(&self) -> &str {
31        self.notification.as_str()
32    }
33}
34
35#[derive(Union, Debug, Clone)]
36#[allow(clippy::large_enum_variant)]
37/// An event or a notification
38pub enum OutputEventsPayload {
39    /// Log event
40    Log(Log),
41
42    /// Metric event
43    Metric(Metric),
44
45    // Notification
46    Notification(EventNotification),
47
48    /// Trace event
49    Trace(Trace),
50}
51
52/// Convert an `api::TapPayload` to the equivalent GraphQL type.
53pub(crate) fn from_tap_payload_to_output_events(t: TapPayload) -> Vec<OutputEventsPayload> {
54    match t {
55        TapPayload::Log(output, log_array) => log_array
56            .into_iter()
57            .map(|log| OutputEventsPayload::Log(Log::new(output.clone(), log)))
58            .collect(),
59        TapPayload::Metric(output, metric_array) => metric_array
60            .into_iter()
61            .map(|metric| OutputEventsPayload::Metric(Metric::new(output.clone(), metric)))
62            .collect(),
63        TapPayload::Notification(notification) => {
64            vec![OutputEventsPayload::Notification(EventNotification {
65                notification,
66            })]
67        }
68        TapPayload::Trace(output, trace_array) => trace_array
69            .into_iter()
70            .map(|trace| OutputEventsPayload::Trace(Trace::new(output.clone(), trace)))
71            .collect(),
72    }
73}