Skip to content

Commit afdc66e

Browse files
enhancement(http_server source): Configurable http response code (vectordotdev#18208)
* make http response code customizable for http-server source * fix tests * update docs * Apply suggestions from code review Co-authored-by: neuronull <[email protected]> * update generated docs * add unit test * fix clippy errors * update licenses * fix cue --------- Co-authored-by: neuronull <[email protected]>
1 parent a6262cd commit afdc66e

File tree

12 files changed

+146
-5
lines changed

12 files changed

+146
-5
lines changed

Cargo.lock

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ hashbrown = { version = "0.14.0", default-features = false, optional = true, fea
275275
headers = { version = "0.3.8", default-features = false }
276276
hostname = { version = "0.3.1", default-features = false }
277277
http = { version = "0.2.9", default-features = false }
278+
http-serde = "1.1.2"
278279
http-body = { version = "0.4.5", default-features = false }
279280
hyper = { version = "0.14.27", default-features = false, features = ["client", "runtime", "http1", "http2", "server", "stream"] }
280281
hyper-openssl = { version = "0.9.2", default-features = false }

LICENSE-3rdparty.csv

+1
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ hostname,https://github.com/svartalf/hostname,MIT,"fengcen <[email protected]
252252
http,https://github.com/hyperium/http,MIT OR Apache-2.0,"Alex Crichton <[email protected]>, Carl Lerche <[email protected]>, Sean McArthur <[email protected]>"
253253
http-body,https://github.com/hyperium/http-body,MIT,"Carl Lerche <[email protected]>, Lucio Franco <[email protected]>, Sean McArthur <[email protected]>"
254254
http-range-header,https://github.com/MarcusGrass/parse-range-headers,MIT,The http-range-header Authors
255+
http-serde,https://gitlab.com/kornelski/http-serde,Apache-2.0 OR MIT,Kornel <[email protected]>
255256
http-types,https://github.com/http-rs/http-types,MIT OR Apache-2.0,Yoshua Wuyts <[email protected]>
256257
httparse,https://github.com/seanmonstar/httparse,MIT OR Apache-2.0,Sean McArthur <[email protected]>
257258
httpdate,https://github.com/pyfisch/httpdate,MIT OR Apache-2.0,Pyfisch <[email protected]>

lib/vector-config/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ snafu = { version = "0.7.5", default-features = false }
2626
toml = { version = "0.7.6", default-features = false }
2727
tracing = { version = "0.1.34", default-features = false }
2828
url = { version = "2.4.0", default-features = false, features = ["serde"] }
29+
http = { version = "0.2.9", default-features = false }
2930
vrl.workspace = true
3031
vector-config-common = { path = "../vector-config-common" }
3132
vector-config-macros = { path = "../vector-config-macros" }

lib/vector-config/src/http.rs

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use http::StatusCode;
2+
use serde_json::Value;
3+
use std::cell::RefCell;
4+
5+
use crate::{
6+
schema::{generate_number_schema, SchemaGenerator, SchemaObject},
7+
Configurable, GenerateError, Metadata, ToValue,
8+
};
9+
10+
impl ToValue for StatusCode {
11+
fn to_value(&self) -> Value {
12+
serde_json::to_value(self.as_u16()).expect("Could not convert HTTP status code to JSON")
13+
}
14+
}
15+
16+
impl Configurable for StatusCode {
17+
fn referenceable_name() -> Option<&'static str> {
18+
Some("http::StatusCode")
19+
}
20+
21+
fn is_optional() -> bool {
22+
true
23+
}
24+
25+
fn metadata() -> Metadata {
26+
let mut metadata = Metadata::default();
27+
metadata.set_description("HTTP response status code");
28+
metadata.set_default_value(StatusCode::OK);
29+
metadata
30+
}
31+
32+
fn generate_schema(_: &RefCell<SchemaGenerator>) -> Result<SchemaObject, GenerateError> {
33+
Ok(generate_number_schema::<u16>())
34+
}
35+
}

lib/vector-config/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ pub use self::configurable::{Configurable, ConfigurableRef, ToValue};
117117
mod errors;
118118
pub use self::errors::{BoundDirection, GenerateError};
119119
mod external;
120+
mod http;
120121
mod metadata;
121122
pub use self::metadata::Metadata;
122123
mod named;

src/sources/heroku_logs.rs

+1
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ impl SourceConfig for LogplexConfig {
176176
self.address,
177177
"events",
178178
HttpMethod::Post,
179+
StatusCode::OK,
179180
true,
180181
&self.tls,
181182
&self.auth,

src/sources/http_server.rs

+66-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use codecs::{
99
};
1010

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

133+
/// Specifies the HTTP response status code that will be returned on successful requests.
134+
#[configurable(metadata(docs::examples = 202))]
135+
#[configurable(metadata(docs::numeric_type = "uint"))]
136+
#[serde(with = "http_serde::status_code")]
137+
#[serde(default = "default_http_response_code")]
138+
response_code: StatusCode,
139+
132140
#[configurable(derived)]
133141
tls: Option<TlsEnableableConfig>,
134142

@@ -242,6 +250,7 @@ impl Default for SimpleHttpConfig {
242250
path: default_path(),
243251
path_key: default_path_key(),
244252
method: default_http_method(),
253+
response_code: default_http_response_code(),
245254
strict_path: true,
246255
framing: None,
247256
decoding: Some(default_decoding()),
@@ -289,6 +298,10 @@ fn default_path_key() -> OptionalValuePath {
289298
OptionalValuePath::from(owned_value_path!("path"))
290299
}
291300

301+
const fn default_http_response_code() -> StatusCode {
302+
StatusCode::OK
303+
}
304+
292305
/// Removes duplicates from the list, and logs a `warn!()` for each duplicate removed.
293306
fn remove_duplicates(mut list: Vec<String>, list_name: &str) -> Vec<String> {
294307
list.sort();
@@ -328,6 +341,7 @@ impl SourceConfig for SimpleHttpConfig {
328341
self.address,
329342
self.path.as_str(),
330343
self.method,
344+
self.response_code,
331345
self.strict_path,
332346
&self.tls,
333347
&self.auth,
@@ -478,7 +492,7 @@ mod tests {
478492
Compression,
479493
};
480494
use futures::Stream;
481-
use http::{HeaderMap, Method};
495+
use http::{HeaderMap, Method, StatusCode};
482496
use lookup::lookup_v2::OptionalValuePath;
483497
use similar_asserts::assert_eq;
484498

@@ -506,6 +520,7 @@ mod tests {
506520
path_key: &'a str,
507521
path: &'a str,
508522
method: &'a str,
523+
response_code: StatusCode,
509524
strict_path: bool,
510525
status: EventStatus,
511526
acknowledgements: bool,
@@ -529,6 +544,7 @@ mod tests {
529544
headers,
530545
encoding: None,
531546
query_parameters,
547+
response_code,
532548
tls: None,
533549
auth: None,
534550
strict_path,
@@ -639,6 +655,7 @@ mod tests {
639655
"http_path",
640656
"/",
641657
"POST",
658+
StatusCode::OK,
642659
true,
643660
EventStatus::Delivered,
644661
true,
@@ -683,6 +700,7 @@ mod tests {
683700
"http_path",
684701
"/",
685702
"POST",
703+
StatusCode::OK,
686704
true,
687705
EventStatus::Delivered,
688706
true,
@@ -720,6 +738,7 @@ mod tests {
720738
"http_path",
721739
"/",
722740
"POST",
741+
StatusCode::OK,
723742
true,
724743
EventStatus::Delivered,
725744
true,
@@ -751,6 +770,7 @@ mod tests {
751770
"http_path",
752771
"/",
753772
"POST",
773+
StatusCode::OK,
754774
true,
755775
EventStatus::Delivered,
756776
true,
@@ -787,6 +807,7 @@ mod tests {
787807
"http_path",
788808
"/",
789809
"POST",
810+
StatusCode::OK,
790811
true,
791812
EventStatus::Delivered,
792813
true,
@@ -830,6 +851,7 @@ mod tests {
830851
"http_path",
831852
"/",
832853
"POST",
854+
StatusCode::OK,
833855
true,
834856
EventStatus::Delivered,
835857
true,
@@ -879,6 +901,7 @@ mod tests {
879901
"http_path",
880902
"/",
881903
"POST",
904+
StatusCode::OK,
882905
true,
883906
EventStatus::Delivered,
884907
true,
@@ -964,6 +987,7 @@ mod tests {
964987
"http_path",
965988
"/",
966989
"POST",
990+
StatusCode::OK,
967991
true,
968992
EventStatus::Delivered,
969993
true,
@@ -1005,6 +1029,7 @@ mod tests {
10051029
"http_path",
10061030
"/",
10071031
"POST",
1032+
StatusCode::OK,
10081033
true,
10091034
EventStatus::Delivered,
10101035
true,
@@ -1055,6 +1080,7 @@ mod tests {
10551080
"http_path",
10561081
"/",
10571082
"POST",
1083+
StatusCode::OK,
10581084
true,
10591085
EventStatus::Delivered,
10601086
true,
@@ -1084,6 +1110,7 @@ mod tests {
10841110
"vector_http_path",
10851111
"/event/path",
10861112
"POST",
1113+
StatusCode::OK,
10871114
true,
10881115
EventStatus::Delivered,
10891116
true,
@@ -1123,6 +1150,7 @@ mod tests {
11231150
"vector_http_path",
11241151
"/event",
11251152
"POST",
1153+
StatusCode::OK,
11261154
false,
11271155
EventStatus::Delivered,
11281156
true,
@@ -1182,6 +1210,7 @@ mod tests {
11821210
"vector_http_path",
11831211
"/",
11841212
"POST",
1213+
StatusCode::OK,
11851214
true,
11861215
EventStatus::Delivered,
11871216
true,
@@ -1196,6 +1225,39 @@ mod tests {
11961225
);
11971226
}
11981227

1228+
#[tokio::test]
1229+
async fn http_status_code() {
1230+
assert_source_compliance(&HTTP_PUSH_SOURCE_TAGS, async move {
1231+
let (rx, addr) = source(
1232+
vec![],
1233+
vec![],
1234+
"http_path",
1235+
"/",
1236+
"POST",
1237+
StatusCode::ACCEPTED,
1238+
true,
1239+
EventStatus::Delivered,
1240+
true,
1241+
None,
1242+
None,
1243+
)
1244+
.await;
1245+
1246+
spawn_collect_n(
1247+
async move {
1248+
assert_eq!(
1249+
StatusCode::ACCEPTED,
1250+
send(addr, "{\"key1\":\"value1\"}").await
1251+
);
1252+
},
1253+
rx,
1254+
1,
1255+
)
1256+
.await;
1257+
})
1258+
.await;
1259+
}
1260+
11991261
#[tokio::test]
12001262
async fn http_delivery_failure() {
12011263
assert_source_compliance(&HTTP_PUSH_SOURCE_TAGS, async {
@@ -1205,6 +1267,7 @@ mod tests {
12051267
"http_path",
12061268
"/",
12071269
"POST",
1270+
StatusCode::OK,
12081271
true,
12091272
EventStatus::Rejected,
12101273
true,
@@ -1234,6 +1297,7 @@ mod tests {
12341297
"http_path",
12351298
"/",
12361299
"POST",
1300+
StatusCode::OK,
12371301
true,
12381302
EventStatus::Rejected,
12391303
false,
@@ -1265,6 +1329,7 @@ mod tests {
12651329
"http_path",
12661330
"/",
12671331
"GET",
1332+
StatusCode::OK,
12681333
true,
12691334
EventStatus::Delivered,
12701335
true,

src/sources/prometheus/remote_write.rs

+1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ impl SourceConfig for PrometheusRemoteWriteConfig {
8080
self.address,
8181
"",
8282
HttpMethod::Post,
83+
StatusCode::OK,
8384
true,
8485
&self.tls,
8586
&self.auth,

0 commit comments

Comments
 (0)