-
Notifications
You must be signed in to change notification settings - Fork 2.2k
chore(http sink): refactor to new style #18200
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
a16de09
First iteration, unit tests pass.
neuronull 969653d
Extract common Service functionality for HTTP based stream sinks.
neuronull 5dc4921
docs touchup
neuronull 10fef82
Touch ups
neuronull d320b10
spell checker
neuronull a496821
fix rust doc
neuronull 44c99b3
hopefully fix encoding regression
neuronull 4aa3eca
Try item sized batching
neuronull 68ff83c
Refactor a bit
neuronull 98c3113
cleanup
neuronull 152de8a
cleanup
neuronull 1e696d4
doc clean up
neuronull 06381de
extract common service code for re use in other HTTP sinks
neuronull 24a28f7
Merge branch 'master' into neuronull/sink_newstyle_refactor_http
neuronull 9efe546
feeback sw
neuronull c22f4d3
Merge branch 'master' into neuronull/sink_newstyle_refactor_http
neuronull 2ea6181
duplicate
neuronull 2bfe0d0
feedback ds
neuronull b2fac79
feedback ds
neuronull 5316602
clippy
neuronull 10c2248
fix write issue
neuronull 7cd6683
fix docs
neuronull File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,326 @@ | ||
| //! 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::{get_http_scheme_from_uri, Auth, HttpClient, MaybeAuth}, | ||
| sinks::{ | ||
| prelude::*, | ||
| util::{ | ||
| http::RequestConfig, | ||
| http_service::{HttpRetryLogic, HttpService}, | ||
| RealtimeSizeBasedDefaultBatchSettings, UriSerde, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| use super::{ | ||
| encoder::HttpEncoder, request_builder::HttpRequestBuilder, service::HttpSinkRequestBuilder, | ||
| sink::HttpSink, | ||
| }; | ||
|
|
||
| /// 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 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 uri: UriSerde, | ||
|
|
||
| /// The HTTP method to use when making the request. | ||
| #[serde(default)] | ||
| pub method: HttpMethod, | ||
|
|
||
| #[configurable(derived)] | ||
| pub 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 headers: Option<IndexMap<String, String>>, | ||
|
|
||
| #[configurable(derived)] | ||
| #[serde(default)] | ||
| pub compression: Compression, | ||
|
|
||
| #[serde(flatten)] | ||
| pub 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 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 payload_suffix: String, | ||
|
|
||
| #[configurable(derived)] | ||
| #[serde(default)] | ||
| pub batch: BatchConfig<RealtimeSizeBasedDefaultBatchSettings>, | ||
|
|
||
| #[configurable(derived)] | ||
| #[serde(default)] | ||
| pub request: RequestConfig, | ||
|
|
||
| #[configurable(derived)] | ||
| pub tls: Option<TlsConfig>, | ||
|
|
||
| #[configurable(derived)] | ||
| #[serde( | ||
| default, | ||
| deserialize_with = "crate::serde::bool_or_struct", | ||
| skip_serializing_if = "crate::serde::skip_serializing_if_default" | ||
| )] | ||
| pub 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 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.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 endpoint = self.uri.with_default_parts(); | ||
|
|
||
| let protocol = get_http_scheme_from_uri(&endpoint.uri); | ||
|
|
||
| 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 request_builder = HttpRequestBuilder { | ||
| encoder: HttpEncoder::new(encoder.clone(), transformer), | ||
| }; | ||
|
|
||
| let http_service_request_builder = HttpSinkRequestBuilder { | ||
| uri: self.uri.with_default_parts(), | ||
| method: self.method, | ||
| auth: self.auth.choose_one(&self.uri.auth)?, | ||
| headers, | ||
| payload_prefix, | ||
| payload_suffix, | ||
| compression: self.compression, | ||
| encoder, | ||
| }; | ||
|
|
||
| let service = HttpService::new(http_service_request_builder, client, protocol.to_string()); | ||
|
|
||
| let request_limits = self.request.tower.unwrap_with(&Default::default()); | ||
|
|
||
| let service = ServiceBuilder::new() | ||
| .settings(request_limits, HttpRetryLogic) | ||
| .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); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| //! Encoding for the `http` sink. | ||
|
|
||
| use crate::{ | ||
| event::Event, | ||
| sinks::util::encoding::{write_all, Encoder as SinkEncoder}, | ||
| }; | ||
| use bytes::BytesMut; | ||
| use codecs::encoding::Framer; | ||
| use std::io; | ||
| use tokio_util::codec::Encoder as _; | ||
|
|
||
| use crate::sinks::prelude::*; | ||
|
|
||
| #[derive(Clone, Debug)] | ||
| pub(super) struct HttpEncoder { | ||
| pub(super) encoder: Encoder<Framer>, | ||
| pub(super) transformer: Transformer, | ||
| } | ||
|
|
||
| impl HttpEncoder { | ||
| /// Creates a new `HttpEncoder`. | ||
| pub(super) const fn new(encoder: Encoder<Framer>, transformer: Transformer) -> Self { | ||
| Self { | ||
| encoder, | ||
| transformer, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl SinkEncoder<Vec<Event>> for HttpEncoder { | ||
| fn encode_input( | ||
| &self, | ||
| mut input: Vec<Event>, | ||
| writer: &mut dyn io::Write, | ||
| ) -> io::Result<(usize, GroupedCountByteSize)> { | ||
| let mut encoder = self.encoder.clone(); | ||
| let mut byte_size = telemetry().create_request_count_byte_size(); | ||
| let mut body = BytesMut::new(); | ||
|
|
||
| for event in input.iter_mut() { | ||
| self.transformer.transform(event); | ||
| byte_size.add_event(event, event.estimated_json_encoded_size_of()); | ||
| } | ||
|
|
||
| for event in input.into_iter() { | ||
| encoder | ||
| .encode(event, &mut body) | ||
| .map_err(|_| io::Error::new(io::ErrorKind::Other, "unable to encode event"))?; | ||
| } | ||
|
|
||
| let body = body.freeze(); | ||
|
|
||
| write_all(writer, 1, body.as_ref()).map(|()| (body.len(), byte_size)) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| //! The HTTP [`vector_core::sink::VectorSink`]. | ||
| //! | ||
| //! This module contains the [`vector_core::sink::VectorSink`] instance that is responsible for | ||
| //! taking a stream of [`vector_core::event::Event`]s and forwarding them to an HTTP server. | ||
|
|
||
| mod config; | ||
| mod encoder; | ||
| mod request_builder; | ||
| mod service; | ||
| mod sink; | ||
|
|
||
| #[cfg(test)] | ||
| mod tests; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.