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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// ## skip check-dropped-events ##

#[cfg(feature = "sinks-splunk_hec")]
pub use self::sink::*;
#[cfg(feature = "sources-splunk_hec")]
pub use self::source::*;

#[cfg(feature = "sinks-splunk_hec")]
mod sink {
    use metrics::{counter, gauge};
    use serde_json::Error;
    use vector_lib::internal_event::InternalEvent;
    use vector_lib::internal_event::{
        error_stage, error_type, ComponentEventsDropped, UNINTENTIONAL,
    };

    use crate::{
        event::metric::{MetricKind, MetricValue},
        sinks::splunk_hec::common::acknowledgements::HecAckApiError,
    };

    #[derive(Debug)]
    pub struct SplunkEventEncodeError {
        pub error: vector_lib::Error,
    }

    impl InternalEvent for SplunkEventEncodeError {
        fn emit(self) {
            let reason = "Failed to encode Splunk HEC event as JSON.";
            error!(
                message = reason,
                error = ?self.error,
                error_code = "serializing_json",
                error_type = error_type::ENCODER_FAILED,
                stage = error_stage::PROCESSING,
                internal_log_rate_limit = true,
            );
            counter!(
                "component_errors_total",
                "error_code" => "serializing_json",
                "error_type" => error_type::ENCODER_FAILED,
                "stage" => error_stage::PROCESSING,
            )
            .increment(1);
            emit!(ComponentEventsDropped::<UNINTENTIONAL> { count: 1, reason });
        }
    }

    #[derive(Debug)]
    pub(crate) struct SplunkInvalidMetricReceivedError<'a> {
        pub value: &'a MetricValue,
        pub kind: &'a MetricKind,
        pub error: crate::Error,
    }

    impl<'a> InternalEvent for SplunkInvalidMetricReceivedError<'a> {
        fn emit(self) {
            error!(
                message = "Invalid metric received.",
                error = ?self.error,
                error_type = error_type::INVALID_METRIC,
                stage = error_stage::PROCESSING,
                value = ?self.value,
                kind = ?self.kind,
                internal_log_rate_limit = true,
            );
            counter!(
                "component_errors_total",
                "error_type" => error_type::INVALID_METRIC,
                "stage" => error_stage::PROCESSING,
            )
            .increment(1);
            counter!(
                "component_discarded_events_total",
                "error_type" => error_type::INVALID_METRIC,
                "stage" => error_stage::PROCESSING,
            )
            .increment(1);
        }
    }

    #[derive(Debug)]
    pub struct SplunkResponseParseError {
        pub error: Error,
    }

    impl InternalEvent for SplunkResponseParseError {
        fn emit(self) {
            error!(
                message = "Unable to parse Splunk HEC response. Acknowledging based on initial 200 OK.",
                error = ?self.error,
                error_code = "invalid_response",
                error_type = error_type::PARSER_FAILED,
                stage = error_stage::SENDING,
                internal_log_rate_limit = true,
            );
            counter!(
                "component_errors_total",
                "error_code" => "invalid_response",
                "error_type" => error_type::PARSER_FAILED,
                "stage" => error_stage::SENDING,
            )
            .increment(1);
        }
    }

    #[derive(Debug)]
    pub struct SplunkIndexerAcknowledgementAPIError {
        pub message: &'static str,
        pub error: HecAckApiError,
    }

    impl InternalEvent for SplunkIndexerAcknowledgementAPIError {
        fn emit(self) {
            error!(
                message = self.message,
                error = ?self.error,
                error_code = "indexer_ack_failed",
                error_type = error_type::ACKNOWLEDGMENT_FAILED,
                stage = error_stage::SENDING,
                internal_log_rate_limit = true,
            );
            counter!(
                "component_errors_total",
                "error_code" => "indexer_ack_failed",
                "error_type" => error_type::ACKNOWLEDGMENT_FAILED,
                "stage" => error_stage::SENDING,
            )
            .increment(1);
        }
    }

    #[derive(Debug)]
    pub struct SplunkIndexerAcknowledgementUnavailableError<E> {
        pub error: E,
    }

    impl<E: std::fmt::Display> InternalEvent for SplunkIndexerAcknowledgementUnavailableError<E> {
        fn emit(self) {
            error!(
                message = "Internal indexer acknowledgement client unavailable. Acknowledging based on initial 200 OK.",
                error = %self.error,
                error_code = "indexer_ack_unavailable",
                error_type = error_type::ACKNOWLEDGMENT_FAILED,
                stage = error_stage::SENDING,
                internal_log_rate_limit = true,
            );
            counter!(
                "component_errors_total",
                "error_code" => "indexer_ack_unavailable",
                "error_type" => error_type::ACKNOWLEDGMENT_FAILED,
                "stage" => error_stage::SENDING,
            )
            .increment(1);
        }
    }

    pub struct SplunkIndexerAcknowledgementAckAdded;

    impl InternalEvent for SplunkIndexerAcknowledgementAckAdded {
        fn emit(self) {
            gauge!("splunk_pending_acks").increment(1.0);
        }
    }

    pub struct SplunkIndexerAcknowledgementAcksRemoved {
        pub count: f64,
    }

    impl InternalEvent for SplunkIndexerAcknowledgementAcksRemoved {
        fn emit(self) {
            gauge!("splunk_pending_acks").decrement(self.count);
        }
    }

    pub struct SplunkEventTimestampInvalidType<'a> {
        pub r#type: &'a str,
    }

    impl<'a> InternalEvent for SplunkEventTimestampInvalidType<'a> {
        fn emit(self) {
            warn!(
                message =
                    "Timestamp was an unexpected type. Deferring to Splunk to set the timestamp.",
                invalid_type = self.r#type,
                internal_log_rate_limit = true
            );
        }
    }

    pub struct SplunkEventTimestampMissing;

    impl InternalEvent for SplunkEventTimestampMissing {
        fn emit(self) {
            warn!(
                message = "Timestamp was not found. Deferring to Splunk to set the timestamp.",
                internal_log_rate_limit = true
            );
        }
    }
}

#[cfg(feature = "sources-splunk_hec")]
mod source {
    use metrics::counter;
    use vector_lib::internal_event::InternalEvent;

    use crate::sources::splunk_hec::ApiError;
    use vector_lib::internal_event::{error_stage, error_type};

    #[derive(Debug)]
    pub struct SplunkHecRequestBodyInvalidError {
        pub error: std::io::Error,
    }

    impl InternalEvent for SplunkHecRequestBodyInvalidError {
        fn emit(self) {
            error!(
                message = "Invalid request body.",
                error = ?self.error,
                error_code = "invalid_request_body",
                error_type = error_type::PARSER_FAILED,
                stage = error_stage::PROCESSING,
                internal_log_rate_limit = true
            );
            counter!(
                "component_errors_total",
                "error_code" => "invalid_request_body",
                "error_type" => error_type::PARSER_FAILED,
                "stage" => error_stage::PROCESSING,
            )
            .increment(1);
        }
    }

    #[derive(Debug)]
    pub struct SplunkHecRequestError {
        pub(crate) error: ApiError,
    }

    impl InternalEvent for SplunkHecRequestError {
        fn emit(self) {
            error!(
                message = "Error processing request.",
                error = ?self.error,
                error_type = error_type::REQUEST_FAILED,
                stage = error_stage::RECEIVING,
                internal_log_rate_limit = true
            );
            counter!(
                "component_errors_total",
                "error_type" => error_type::REQUEST_FAILED,
                "stage" => error_stage::RECEIVING,
            )
            .increment(1);
        }
    }
}