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
use std::sync::Arc;

use snafu::ResultExt;

use crate::sinks::prelude::*;

use super::{
    config::{NatsPublisher, NatsSinkConfig, NatsTowerRequestConfigDefaults},
    request_builder::{NatsEncoder, NatsRequestBuilder},
    service::{NatsResponse, NatsService},
    EncodingSnafu, NatsError,
};

pub(super) struct NatsEvent {
    pub(super) event: Event,
    pub(super) subject: String,
}

pub(super) struct NatsSink {
    request: TowerRequestConfig<NatsTowerRequestConfigDefaults>,
    transformer: Transformer,
    encoder: Encoder<()>,
    publisher: Arc<NatsPublisher>,
    subject: Template,
}

impl NatsSink {
    fn make_nats_event(&self, event: Event) -> Option<NatsEvent> {
        let subject = self
            .subject
            .render_string(&event)
            .map_err(|missing_keys| {
                emit!(TemplateRenderingError {
                    error: missing_keys,
                    field: Some("subject"),
                    drop_event: true,
                });
            })
            .ok()?;

        Some(NatsEvent { event, subject })
    }

    pub(super) async fn new(config: NatsSinkConfig) -> Result<Self, NatsError> {
        let publisher = Arc::new(config.publisher().await?);
        let transformer = config.encoding.transformer();
        let serializer = config.encoding.build().context(EncodingSnafu)?;
        let encoder = Encoder::<()>::new(serializer);
        let request = config.request;
        let subject = config.subject;

        Ok(NatsSink {
            request,
            transformer,
            encoder,
            publisher,
            subject,
        })
    }

    async fn run_inner(self: Box<Self>, input: BoxStream<'_, Event>) -> Result<(), ()> {
        let request = self.request.into_settings();

        let request_builder = NatsRequestBuilder {
            encoder: NatsEncoder {
                encoder: self.encoder.clone(),
                transformer: self.transformer.clone(),
            },
        };

        let service = ServiceBuilder::new()
            .settings(request, NatsRetryLogic)
            .service(NatsService {
                publisher: Arc::clone(&self.publisher),
            });

        input
            .filter_map(|event| std::future::ready(self.make_nats_event(event)))
            .request_builder(default_request_builder_concurrency_limit(), request_builder)
            .filter_map(|request| async move {
                match request {
                    Err(e) => {
                        error!("Failed to build NATS request: {:?}.", e);
                        None
                    }
                    Ok(req) => Some(req),
                }
            })
            .into_driver(service)
            .protocol("nats")
            .run()
            .await
    }
}

#[async_trait]
impl StreamSink<Event> for NatsSink {
    async fn run(mut self: Box<Self>, input: BoxStream<'_, Event>) -> Result<(), ()> {
        self.run_inner(input).await
    }
}

#[derive(Debug, Clone)]
pub(super) struct NatsRetryLogic;

impl RetryLogic for NatsRetryLogic {
    type Error = NatsError;
    type Response = NatsResponse;

    fn is_retriable_error(&self, _error: &Self::Error) -> bool {
        true
    }
}