-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathconfig.rs
349 lines (289 loc) · 10.9 KB
/
config.rs
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
//! Configuration for the `http` sink.
use codecs::{
encoding::{Framer, Serializer},
CharacterDelimitedEncoder,
};
use http::{header::AUTHORIZATION, HeaderName, HeaderValue, Method, Request, StatusCode};
use hyper::Body;
use indexmap::IndexMap;
use crate::{
codecs::{EncodingConfigWithFraming, SinkType},
http::{Auth, HttpClient, MaybeAuth},
sinks::{
prelude::*,
util::{
http::{HttpStatusRetryLogic, RequestConfig},
RealtimeSizeBasedDefaultBatchSettings, UriSerde,
},
},
};
use super::{
encoder::HttpEncoder,
request_builder::HttpRequestBuilder,
service::{HttpResponse, HttpService, HttpSinkRequestBuilder},
sink::HttpSink,
};
const CONTENT_TYPE_TEXT: &str = "text/plain";
const CONTENT_TYPE_NDJSON: &str = "application/x-ndjson";
const CONTENT_TYPE_JSON: &str = "application/json";
/// Configuration for the `http` sink.
#[configurable_component(sink("http", "Deliver observability event data to an HTTP server."))]
#[derive(Clone, Debug)]
#[serde(deny_unknown_fields)]
pub(super) struct HttpSinkConfig {
/// The full URI to make HTTP requests to.
///
/// This should include the protocol and host, but can also include the port, path, and any other valid part of a URI.
#[configurable(metadata(docs::examples = "https://10.22.212.22:9000/endpoint"))]
pub(super) uri: UriSerde,
/// The HTTP method to use when making the request.
#[serde(default)]
pub(super) method: HttpMethod,
#[configurable(derived)]
pub(super) auth: Option<Auth>,
/// A list of custom headers to add to each request.
#[configurable(deprecated)]
#[configurable(metadata(
docs::additional_props_description = "An HTTP request header and it's value."
))]
pub(super) headers: Option<IndexMap<String, String>>,
#[configurable(derived)]
#[serde(default)]
pub(super) compression: Compression,
#[serde(flatten)]
pub(super) encoding: EncodingConfigWithFraming,
/// A string to prefix the payload with.
///
/// This option is ignored if the encoding is not character delimited JSON.
///
/// If specified, the `payload_suffix` must also be specified and together they must produce a valid JSON object.
#[configurable(metadata(docs::examples = "{\"data\":"))]
#[serde(default)]
pub(super) payload_prefix: String,
/// A string to suffix the payload with.
///
/// This option is ignored if the encoding is not character delimited JSON.
///
/// If specified, the `payload_prefix` must also be specified and together they must produce a valid JSON object.
#[configurable(metadata(docs::examples = "}"))]
#[serde(default)]
pub(super) payload_suffix: String,
#[configurable(derived)]
#[serde(default)]
pub(super) batch: BatchConfig<RealtimeSizeBasedDefaultBatchSettings>,
#[configurable(derived)]
#[serde(default)]
pub(super) request: RequestConfig,
#[configurable(derived)]
pub(super) tls: Option<TlsConfig>,
#[configurable(derived)]
#[serde(
default,
deserialize_with = "crate::serde::bool_or_struct",
skip_serializing_if = "crate::serde::skip_serializing_if_default"
)]
pub(super) acknowledgements: AcknowledgementsConfig,
}
/// HTTP method.
///
/// A subset of the HTTP methods described in [RFC 9110, section 9.1][rfc9110] are supported.
///
/// [rfc9110]: https://datatracker.ietf.org/doc/html/rfc9110#section-9.1
#[configurable_component]
#[derive(Clone, Copy, Debug, Derivative, Eq, PartialEq)]
#[serde(rename_all = "snake_case")]
#[derivative(Default)]
pub(super) enum HttpMethod {
/// GET.
Get,
/// HEAD.
Head,
/// POST.
#[derivative(Default)]
Post,
/// PUT.
Put,
/// DELETE.
Delete,
/// OPTIONS.
Options,
/// TRACE.
Trace,
/// PATCH.
Patch,
}
impl From<HttpMethod> for Method {
fn from(http_method: HttpMethod) -> Self {
match http_method {
HttpMethod::Head => Self::HEAD,
HttpMethod::Get => Self::GET,
HttpMethod::Post => Self::POST,
HttpMethod::Put => Self::PUT,
HttpMethod::Patch => Self::PATCH,
HttpMethod::Delete => Self::DELETE,
HttpMethod::Options => Self::OPTIONS,
HttpMethod::Trace => Self::TRACE,
}
}
}
impl HttpSinkConfig {
fn build_http_client(&self, cx: &SinkContext) -> crate::Result<HttpClient> {
let tls = TlsSettings::from_options(&self.tls)?;
Ok(HttpClient::new(tls, cx.proxy())?)
}
pub(super) fn build_encoder(&self) -> crate::Result<Encoder<Framer>> {
let (framer, serializer) = self.encoding.build(SinkType::MessageBased)?;
Ok(Encoder::<Framer>::new(framer, serializer))
}
}
impl GenerateConfig for HttpSinkConfig {
fn generate_config() -> toml::Value {
toml::from_str(
r#"uri = "https://10.22.212.22:9000/endpoint"
encoding.codec = "json""#,
)
.unwrap()
}
}
async fn healthcheck(uri: UriSerde, auth: Option<Auth>, client: HttpClient) -> crate::Result<()> {
let auth = auth.choose_one(&uri.auth)?;
let uri = uri.with_default_parts();
let mut request = Request::head(&uri.uri).body(Body::empty()).unwrap();
if let Some(auth) = auth {
auth.apply(&mut request);
}
let response = client.send(request).await?;
match response.status() {
StatusCode::OK => Ok(()),
status => Err(HealthcheckError::UnexpectedStatus { status }.into()),
}
}
pub(super) fn validate_headers(
headers: &IndexMap<String, String>,
configures_auth: bool,
) -> crate::Result<IndexMap<HeaderName, HeaderValue>> {
let headers = crate::sinks::util::http::validate_headers(headers)?;
for name in headers.keys() {
if configures_auth && name == AUTHORIZATION {
return Err("Authorization header can not be used with defined auth options".into());
}
}
Ok(headers)
}
pub(super) fn validate_payload_wrapper(
payload_prefix: &str,
payload_suffix: &str,
encoder: &Encoder<Framer>,
) -> crate::Result<(String, String)> {
let payload = [payload_prefix, "{}", payload_suffix].join("");
match (
encoder.serializer(),
encoder.framer(),
serde_json::from_str::<serde_json::Value>(&payload),
) {
(
Serializer::Json(_),
Framer::CharacterDelimited(CharacterDelimitedEncoder { delimiter: b',' }),
Err(_),
) => Err("Payload prefix and suffix wrapper must produce a valid JSON object.".into()),
_ => Ok((payload_prefix.to_owned(), payload_suffix.to_owned())),
}
}
#[async_trait]
#[typetag::serde(name = "http")]
impl SinkConfig for HttpSinkConfig {
async fn build(&self, cx: SinkContext) -> crate::Result<(VectorSink, Healthcheck)> {
let batch_settings = self.batch.validate()?.into_batcher_settings()?;
let encoder = self.build_encoder()?;
let transformer = self.encoding.transformer();
let mut request = self.request.clone();
request.add_old_option(self.headers.clone());
let headers = validate_headers(&request.headers, self.auth.is_some())?;
let (payload_prefix, payload_suffix) =
validate_payload_wrapper(&self.payload_prefix, &self.payload_suffix, &encoder)?;
let client = self.build_http_client(&cx)?;
let healthcheck = match cx.healthcheck.uri {
Some(healthcheck_uri) => {
healthcheck(healthcheck_uri, self.auth.clone(), client.clone()).boxed()
}
None => future::ok(()).boxed(),
};
let content_type = {
use Framer::*;
use Serializer::*;
match (encoder.serializer(), encoder.framer()) {
(RawMessage(_) | Text(_), _) => Some(CONTENT_TYPE_TEXT.to_owned()),
(Json(_), NewlineDelimited(_)) => Some(CONTENT_TYPE_NDJSON.to_owned()),
(Json(_), CharacterDelimited(CharacterDelimitedEncoder { delimiter: b',' })) => {
Some(CONTENT_TYPE_JSON.to_owned())
}
_ => None,
}
};
let request_builder = HttpRequestBuilder {
encoder: HttpEncoder::new(encoder, transformer, payload_prefix, payload_suffix),
compression: self.compression,
};
let content_encoding = self.compression.is_compressed().then(|| {
self.compression
.content_encoding()
.expect("Encoding should be specified for compression.")
.to_string()
});
let http_service_request_builder = HttpSinkRequestBuilder::new(
self.uri.with_default_parts(),
self.method,
self.auth.choose_one(&self.uri.auth)?,
headers,
content_type,
content_encoding,
);
let service = HttpService::new(client, http_service_request_builder);
let request_limits = self.request.tower.unwrap_with(&Default::default());
let retry_logic =
HttpStatusRetryLogic::new(|req: &HttpResponse| req.http_response.status());
let service = ServiceBuilder::new()
.settings(request_limits, retry_logic)
.service(service);
let sink = HttpSink::new(service, batch_settings, request_builder);
Ok((VectorSink::from_event_streamsink(sink), healthcheck))
}
fn input(&self) -> Input {
Input::new(self.encoding.config().1.input_type())
}
fn acknowledgements(&self) -> &AcknowledgementsConfig {
&self.acknowledgements
}
}
impl ValidatableComponent for HttpSinkConfig {
fn validation_configuration() -> ValidationConfiguration {
use codecs::{JsonSerializerConfig, MetricTagValues};
use std::str::FromStr;
let config = Self {
uri: UriSerde::from_str("http://127.0.0.1:9000/endpoint")
.expect("should never fail to parse"),
method: HttpMethod::Post,
encoding: EncodingConfigWithFraming::new(
None,
JsonSerializerConfig::new(MetricTagValues::Full).into(),
Transformer::default(),
),
auth: None,
headers: None,
compression: Compression::default(),
batch: BatchConfig::default(),
request: RequestConfig::default(),
tls: None,
acknowledgements: AcknowledgementsConfig::default(),
payload_prefix: String::new(),
payload_suffix: String::new(),
};
let external_resource = ExternalResource::new(
ResourceDirection::Push,
HttpResourceConfig::from_parts(config.uri.uri.clone(), Some(config.method.into())),
config.encoding.clone(),
);
ValidationConfiguration::from_sink(Self::NAME, config, Some(external_resource))
}
}
register_validatable_component!(HttpSinkConfig);