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
use async_graphql::{Object, Union};

use crate::api::schema::events::log::Log;
use crate::api::schema::events::metric::Metric;
use crate::api::schema::events::trace::Trace;
use vector_lib::tap::controller::TapPayload;
use vector_lib::tap::notification::Notification;

/// This wrapper struct hoists `message` up from [`Notification`] for a more
/// natural querying experience. While ideally [`Notification`] would be a
/// GraphQL interface with a common `message` field, an interface cannot be
/// directly nested into the union of [`super::OutputEventsPayload`].
///
/// The GraphQL specification forbids such a nesting:
/// <http://spec.graphql.org/October2021/#sel-HAHdfFDABABkG3_I>
#[derive(Debug, Clone)]
pub struct EventNotification {
    pub notification: Notification,
}

#[Object]
/// A notification regarding events observation
impl EventNotification {
    /// Notification details
    async fn notification(&self) -> &Notification {
        &self.notification
    }

    /// The human-readable message associated with the notification
    async fn message(&self) -> &str {
        self.notification.as_str()
    }
}

#[derive(Union, Debug, Clone)]
#[allow(clippy::large_enum_variant)]
/// An event or a notification
pub enum OutputEventsPayload {
    /// Log event
    Log(Log),

    /// Metric event
    Metric(Metric),

    // Notification
    Notification(EventNotification),

    /// Trace event
    Trace(Trace),
}

/// Convert an `api::TapPayload` to the equivalent GraphQL type.
pub(crate) fn from_tap_payload_to_output_events(t: TapPayload) -> Vec<OutputEventsPayload> {
    match t {
        TapPayload::Log(output, log_array) => log_array
            .into_iter()
            .map(|log| OutputEventsPayload::Log(Log::new(output.clone(), log)))
            .collect(),
        TapPayload::Metric(output, metric_array) => metric_array
            .into_iter()
            .map(|metric| OutputEventsPayload::Metric(Metric::new(output.clone(), metric)))
            .collect(),
        TapPayload::Notification(notification) => {
            vec![OutputEventsPayload::Notification(EventNotification {
                notification,
            })]
        }
        TapPayload::Trace(output, trace_array) => trace_array
            .into_iter()
            .map(|trace| OutputEventsPayload::Trace(Trace::new(output.clone(), trace)))
            .collect(),
    }
}