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
use futures_util::future::ready;

use crate::sinks::{prelude::*, util::buffer::metrics::MetricNormalizer};

use super::{
    encoder::AppsignalEncoder,
    normalizer::AppsignalMetricsNormalizer,
    request_builder::{AppsignalRequest, AppsignalRequestBuilder},
};

pub(super) struct AppsignalSink<S> {
    pub(super) service: S,
    pub(super) compression: Compression,
    pub(super) transformer: Transformer,
    pub(super) batch_settings: BatcherSettings,
}

impl<S> AppsignalSink<S>
where
    S: Service<AppsignalRequest> + Send + 'static,
    S::Future: Send + 'static,
    S::Response: DriverResponse + Send + 'static,
    S::Error: std::fmt::Debug + Into<crate::Error> + Send,
{
    pub(super) async fn run_inner(self: Box<Self>, input: BoxStream<'_, Event>) -> Result<(), ()> {
        let service = ServiceBuilder::new().service(self.service);
        let mut normalizer = MetricNormalizer::<AppsignalMetricsNormalizer>::default();

        input
            .filter_map(move |event| {
                ready(if let Event::Metric(metric) = event {
                    normalizer.normalize(metric).map(Event::Metric)
                } else {
                    Some(event)
                })
            })
            .batched(self.batch_settings.as_byte_size_config())
            .request_builder(
                default_request_builder_concurrency_limit(),
                AppsignalRequestBuilder {
                    compression: self.compression,
                    encoder: AppsignalEncoder {
                        transformer: self.transformer.clone(),
                    },
                },
            )
            .filter_map(|request| async move {
                match request {
                    Err(error) => {
                        emit!(SinkRequestBuildError { error });
                        None
                    }
                    Ok(req) => Some(req),
                }
            })
            .into_driver(service)
            .run()
            .await
    }
}

#[async_trait::async_trait]
impl<S> StreamSink<Event> for AppsignalSink<S>
where
    S: Service<AppsignalRequest> + Send + 'static,
    S::Future: Send + 'static,
    S::Response: DriverResponse + Send + 'static,
    S::Error: std::fmt::Debug + Into<crate::Error> + Send,
{
    async fn run(
        self: Box<Self>,
        input: futures_util::stream::BoxStream<'_, Event>,
    ) -> Result<(), ()> {
        self.run_inner(input).await
    }
}