vector/api/schema/events/
output.rs

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