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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//! Configuration functionality for the `AMQP` sink.
use crate::{amqp::AmqpConfig, sinks::prelude::*};
use lapin::{types::ShortString, BasicProperties};
use std::sync::Arc;
use vector_lib::codecs::TextSerializerConfig;

use super::sink::AmqpSink;

/// AMQP properties configuration.
#[configurable_component]
#[configurable(title = "Configure the AMQP message properties.")]
#[derive(Clone, Debug, Default)]
pub struct AmqpPropertiesConfig {
    /// Content-Type for the AMQP messages.
    pub(crate) content_type: Option<String>,

    /// Content-Encoding for the AMQP messages.
    pub(crate) content_encoding: Option<String>,

    /// Expiration for AMQP messages (in milliseconds)
    pub(crate) expiration_ms: Option<u64>,
}

impl AmqpPropertiesConfig {
    pub(super) fn build(&self) -> BasicProperties {
        let mut prop = BasicProperties::default();
        if let Some(content_type) = &self.content_type {
            prop = prop.with_content_type(ShortString::from(content_type.clone()));
        }
        if let Some(content_encoding) = &self.content_encoding {
            prop = prop.with_content_encoding(ShortString::from(content_encoding.clone()));
        }
        if let Some(expiration_ms) = &self.expiration_ms {
            prop = prop.with_expiration(ShortString::from(expiration_ms.to_string()));
        }
        prop
    }
}

/// Configuration for the `amqp` sink.
///
/// Supports AMQP version 0.9.1
#[configurable_component(sink(
    "amqp",
    "Send events to AMQP 0.9.1 compatible brokers like RabbitMQ."
))]
#[derive(Clone, Debug)]
pub struct AmqpSinkConfig {
    /// The exchange to publish messages to.
    pub(crate) exchange: Template,

    /// Template used to generate a routing key which corresponds to a queue binding.
    pub(crate) routing_key: Option<Template>,

    /// AMQP message properties.
    pub(crate) properties: Option<AmqpPropertiesConfig>,

    #[serde(flatten)]
    pub(crate) connection: AmqpConfig,

    #[configurable(derived)]
    pub(crate) encoding: EncodingConfig,

    #[configurable(derived)]
    #[serde(
        default,
        deserialize_with = "crate::serde::bool_or_struct",
        skip_serializing_if = "crate::serde::is_default"
    )]
    pub(crate) acknowledgements: AcknowledgementsConfig,
}

impl Default for AmqpSinkConfig {
    fn default() -> Self {
        Self {
            exchange: Template::try_from("vector").unwrap(),
            routing_key: None,
            properties: None,
            encoding: TextSerializerConfig::default().into(),
            connection: AmqpConfig::default(),
            acknowledgements: AcknowledgementsConfig::default(),
        }
    }
}

impl GenerateConfig for AmqpSinkConfig {
    fn generate_config() -> toml::Value {
        toml::from_str(
            r#"connection_string = "amqp://localhost:5672/%2f"
            routing_key = "user_id"
            exchange = "test"
            encoding.codec = "json""#,
        )
        .unwrap()
    }
}

#[async_trait::async_trait]
#[typetag::serde(name = "amqp")]
impl SinkConfig for AmqpSinkConfig {
    async fn build(&self, _cx: SinkContext) -> crate::Result<(VectorSink, Healthcheck)> {
        let sink = AmqpSink::new(self.clone()).await?;
        let hc = healthcheck(Arc::clone(&sink.channel)).boxed();
        Ok((VectorSink::from_event_streamsink(sink), hc))
    }

    fn input(&self) -> Input {
        Input::new(DataType::Log)
    }

    fn acknowledgements(&self) -> &AcknowledgementsConfig {
        &self.acknowledgements
    }
}

pub(super) async fn healthcheck(channel: Arc<lapin::Channel>) -> crate::Result<()> {
    trace!("Healthcheck started.");

    if !channel.status().connected() {
        return Err(Box::new(std::io::Error::new(
            std::io::ErrorKind::BrokenPipe,
            "Not Connected",
        )));
    }

    trace!("Healthcheck completed.");
    Ok(())
}

#[test]
pub fn generate_config() {
    crate::test_util::test_generate_config::<AmqpSinkConfig>();
}