Skip to content

Commit

Permalink
enhancement(http_server source): Configurable http response code (#18208
Browse files Browse the repository at this point in the history
)

* make http response code customizable for http-server source

* fix tests

* update docs

* Apply suggestions from code review

Co-authored-by: neuronull <neuronull@pm.me>

* update generated docs

* add unit test

* fix clippy errors

* update licenses

* fix cue

---------

Co-authored-by: neuronull <neuronull@pm.me>
  • Loading branch information
kunalmohan and neuronull authored Aug 18, 2023
1 parent a6262cd commit afdc66e
Show file tree
Hide file tree
Showing 12 changed files with 146 additions and 5 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ hashbrown = { version = "0.14.0", default-features = false, optional = true, fea
headers = { version = "0.3.8", default-features = false }
hostname = { version = "0.3.1", default-features = false }
http = { version = "0.2.9", default-features = false }
http-serde = "1.1.2"
http-body = { version = "0.4.5", default-features = false }
hyper = { version = "0.14.27", default-features = false, features = ["client", "runtime", "http1", "http2", "server", "stream"] }
hyper-openssl = { version = "0.9.2", default-features = false }
Expand Down
1 change: 1 addition & 0 deletions LICENSE-3rdparty.csv
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ hostname,https://github.com/svartalf/hostname,MIT,"fengcen <fengcen.love@gmail.c
http,https://github.com/hyperium/http,MIT OR Apache-2.0,"Alex Crichton <alex@alexcrichton.com>, Carl Lerche <me@carllerche.com>, Sean McArthur <sean@seanmonstar.com>"
http-body,https://github.com/hyperium/http-body,MIT,"Carl Lerche <me@carllerche.com>, Lucio Franco <luciofranco14@gmail.com>, Sean McArthur <sean@seanmonstar.com>"
http-range-header,https://github.com/MarcusGrass/parse-range-headers,MIT,The http-range-header Authors
http-serde,https://gitlab.com/kornelski/http-serde,Apache-2.0 OR MIT,Kornel <kornel@geekhood.net>
http-types,https://github.com/http-rs/http-types,MIT OR Apache-2.0,Yoshua Wuyts <yoshuawuyts@gmail.com>
httparse,https://github.com/seanmonstar/httparse,MIT OR Apache-2.0,Sean McArthur <sean@seanmonstar.com>
httpdate,https://github.com/pyfisch/httpdate,MIT OR Apache-2.0,Pyfisch <pyfisch@posteo.org>
Expand Down
1 change: 1 addition & 0 deletions lib/vector-config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ snafu = { version = "0.7.5", default-features = false }
toml = { version = "0.7.6", default-features = false }
tracing = { version = "0.1.34", default-features = false }
url = { version = "2.4.0", default-features = false, features = ["serde"] }
http = { version = "0.2.9", default-features = false }
vrl.workspace = true
vector-config-common = { path = "../vector-config-common" }
vector-config-macros = { path = "../vector-config-macros" }
Expand Down
35 changes: 35 additions & 0 deletions lib/vector-config/src/http.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use http::StatusCode;
use serde_json::Value;
use std::cell::RefCell;

use crate::{
schema::{generate_number_schema, SchemaGenerator, SchemaObject},
Configurable, GenerateError, Metadata, ToValue,
};

impl ToValue for StatusCode {
fn to_value(&self) -> Value {
serde_json::to_value(self.as_u16()).expect("Could not convert HTTP status code to JSON")
}
}

impl Configurable for StatusCode {
fn referenceable_name() -> Option<&'static str> {
Some("http::StatusCode")
}

fn is_optional() -> bool {
true
}

fn metadata() -> Metadata {
let mut metadata = Metadata::default();
metadata.set_description("HTTP response status code");
metadata.set_default_value(StatusCode::OK);
metadata
}

fn generate_schema(_: &RefCell<SchemaGenerator>) -> Result<SchemaObject, GenerateError> {
Ok(generate_number_schema::<u16>())
}
}
1 change: 1 addition & 0 deletions lib/vector-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ pub use self::configurable::{Configurable, ConfigurableRef, ToValue};
mod errors;
pub use self::errors::{BoundDirection, GenerateError};
mod external;
mod http;
mod metadata;
pub use self::metadata::Metadata;
mod named;
Expand Down
1 change: 1 addition & 0 deletions src/sources/heroku_logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ impl SourceConfig for LogplexConfig {
self.address,
"events",
HttpMethod::Post,
StatusCode::OK,
true,
&self.tls,
&self.auth,
Expand Down
67 changes: 66 additions & 1 deletion src/sources/http_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use codecs::{
};

use http::{StatusCode, Uri};
use http_serde;
use lookup::{lookup_v2::OptionalValuePath, owned_value_path, path};
use tokio_util::codec::Decoder as _;
use vector_config::configurable_component;
Expand Down Expand Up @@ -129,6 +130,13 @@ pub struct SimpleHttpConfig {
#[serde(default = "default_http_method")]
method: HttpMethod,

/// Specifies the HTTP response status code that will be returned on successful requests.
#[configurable(metadata(docs::examples = 202))]
#[configurable(metadata(docs::numeric_type = "uint"))]
#[serde(with = "http_serde::status_code")]
#[serde(default = "default_http_response_code")]
response_code: StatusCode,

#[configurable(derived)]
tls: Option<TlsEnableableConfig>,

Expand Down Expand Up @@ -242,6 +250,7 @@ impl Default for SimpleHttpConfig {
path: default_path(),
path_key: default_path_key(),
method: default_http_method(),
response_code: default_http_response_code(),
strict_path: true,
framing: None,
decoding: Some(default_decoding()),
Expand Down Expand Up @@ -289,6 +298,10 @@ fn default_path_key() -> OptionalValuePath {
OptionalValuePath::from(owned_value_path!("path"))
}

const fn default_http_response_code() -> StatusCode {
StatusCode::OK
}

/// Removes duplicates from the list, and logs a `warn!()` for each duplicate removed.
fn remove_duplicates(mut list: Vec<String>, list_name: &str) -> Vec<String> {
list.sort();
Expand Down Expand Up @@ -328,6 +341,7 @@ impl SourceConfig for SimpleHttpConfig {
self.address,
self.path.as_str(),
self.method,
self.response_code,
self.strict_path,
&self.tls,
&self.auth,
Expand Down Expand Up @@ -478,7 +492,7 @@ mod tests {
Compression,
};
use futures::Stream;
use http::{HeaderMap, Method};
use http::{HeaderMap, Method, StatusCode};
use lookup::lookup_v2::OptionalValuePath;
use similar_asserts::assert_eq;

Expand Down Expand Up @@ -506,6 +520,7 @@ mod tests {
path_key: &'a str,
path: &'a str,
method: &'a str,
response_code: StatusCode,
strict_path: bool,
status: EventStatus,
acknowledgements: bool,
Expand All @@ -529,6 +544,7 @@ mod tests {
headers,
encoding: None,
query_parameters,
response_code,
tls: None,
auth: None,
strict_path,
Expand Down Expand Up @@ -639,6 +655,7 @@ mod tests {
"http_path",
"/",
"POST",
StatusCode::OK,
true,
EventStatus::Delivered,
true,
Expand Down Expand Up @@ -683,6 +700,7 @@ mod tests {
"http_path",
"/",
"POST",
StatusCode::OK,
true,
EventStatus::Delivered,
true,
Expand Down Expand Up @@ -720,6 +738,7 @@ mod tests {
"http_path",
"/",
"POST",
StatusCode::OK,
true,
EventStatus::Delivered,
true,
Expand Down Expand Up @@ -751,6 +770,7 @@ mod tests {
"http_path",
"/",
"POST",
StatusCode::OK,
true,
EventStatus::Delivered,
true,
Expand Down Expand Up @@ -787,6 +807,7 @@ mod tests {
"http_path",
"/",
"POST",
StatusCode::OK,
true,
EventStatus::Delivered,
true,
Expand Down Expand Up @@ -830,6 +851,7 @@ mod tests {
"http_path",
"/",
"POST",
StatusCode::OK,
true,
EventStatus::Delivered,
true,
Expand Down Expand Up @@ -879,6 +901,7 @@ mod tests {
"http_path",
"/",
"POST",
StatusCode::OK,
true,
EventStatus::Delivered,
true,
Expand Down Expand Up @@ -964,6 +987,7 @@ mod tests {
"http_path",
"/",
"POST",
StatusCode::OK,
true,
EventStatus::Delivered,
true,
Expand Down Expand Up @@ -1005,6 +1029,7 @@ mod tests {
"http_path",
"/",
"POST",
StatusCode::OK,
true,
EventStatus::Delivered,
true,
Expand Down Expand Up @@ -1055,6 +1080,7 @@ mod tests {
"http_path",
"/",
"POST",
StatusCode::OK,
true,
EventStatus::Delivered,
true,
Expand Down Expand Up @@ -1084,6 +1110,7 @@ mod tests {
"vector_http_path",
"/event/path",
"POST",
StatusCode::OK,
true,
EventStatus::Delivered,
true,
Expand Down Expand Up @@ -1123,6 +1150,7 @@ mod tests {
"vector_http_path",
"/event",
"POST",
StatusCode::OK,
false,
EventStatus::Delivered,
true,
Expand Down Expand Up @@ -1182,6 +1210,7 @@ mod tests {
"vector_http_path",
"/",
"POST",
StatusCode::OK,
true,
EventStatus::Delivered,
true,
Expand All @@ -1196,6 +1225,39 @@ mod tests {
);
}

#[tokio::test]
async fn http_status_code() {
assert_source_compliance(&HTTP_PUSH_SOURCE_TAGS, async move {
let (rx, addr) = source(
vec![],
vec![],
"http_path",
"/",
"POST",
StatusCode::ACCEPTED,
true,
EventStatus::Delivered,
true,
None,
None,
)
.await;

spawn_collect_n(
async move {
assert_eq!(
StatusCode::ACCEPTED,
send(addr, "{\"key1\":\"value1\"}").await
);
},
rx,
1,
)
.await;
})
.await;
}

#[tokio::test]
async fn http_delivery_failure() {
assert_source_compliance(&HTTP_PUSH_SOURCE_TAGS, async {
Expand All @@ -1205,6 +1267,7 @@ mod tests {
"http_path",
"/",
"POST",
StatusCode::OK,
true,
EventStatus::Rejected,
true,
Expand Down Expand Up @@ -1234,6 +1297,7 @@ mod tests {
"http_path",
"/",
"POST",
StatusCode::OK,
true,
EventStatus::Rejected,
false,
Expand Down Expand Up @@ -1265,6 +1329,7 @@ mod tests {
"http_path",
"/",
"GET",
StatusCode::OK,
true,
EventStatus::Delivered,
true,
Expand Down
1 change: 1 addition & 0 deletions src/sources/prometheus/remote_write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ impl SourceConfig for PrometheusRemoteWriteConfig {
self.address,
"",
HttpMethod::Post,
StatusCode::OK,
true,
&self.tls,
&self.auth,
Expand Down
Loading

0 comments on commit afdc66e

Please sign in to comment.