Skip to content

Commit

Permalink
[Agent] fix http test and extend http req resp rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
qq906907952 committed Aug 31, 2023
1 parent 4dfc612 commit 8ed83ad
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 20 deletions.
4 changes: 2 additions & 2 deletions agent/resources/test/flow_generator/http/h2c_ascii.result
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
HttpInfo { is_req_end: false, is_resp_end: false, rrt: 0, proto: Grpc, is_tls: false, msg_type: Request, raw_data_type: RawProtocol, stream_id: Some(1), version: "2", trace_id: "", span_id: "", method: "POST", path: "/hipstershop.CartService/GetCart", host: "cartservice:7070", user_agent: Some("grpc-go/1.22.0"), referer: None, client_ip: "", x_request_id_0: "", x_request_id_1: "", req_content_length: Some(43), resp_content_length: None, status_code: None, status: Ok, attributes: [] } is_http: true
HttpInfo { is_req_end: false, is_resp_end: false, rrt: 0, proto: Grpc, is_tls: false, msg_type: Response, raw_data_type: RawProtocol, stream_id: Some(1), version: "2", trace_id: "", span_id: "", method: "", path: "", host: "", user_agent: None, referer: None, client_ip: "", x_request_id_0: "", x_request_id_1: "", req_content_length: None, resp_content_length: Some(21), status_code: Some(200), status: Ok, attributes: [] } is_http: true
HttpInfo { is_req_end: false, is_resp_end: false, rrt: 0, proto: Grpc, is_tls: false, msg_type: Request, raw_data_type: RawProtocol, stream_id: Some(1), version: "2", trace_id: "", span_id: "", method: "POST", path: "/hipstershop.CartService/GetCart", host: "cartservice:7070", user_agent: Some("grpc-go/1.22.0"), referer: None, client_ip: "", x_request_id_0: "", x_request_id_1: "", req_content_length: Some(43), resp_content_length: None, status_code: None, status: Ok, custom_endpoint: None, custom_result: None, custom_exception: None, attributes: [] } is_http: true
HttpInfo { is_req_end: false, is_resp_end: false, rrt: 0, proto: Grpc, is_tls: false, msg_type: Response, raw_data_type: RawProtocol, stream_id: Some(1), version: "2", trace_id: "", span_id: "", method: "", path: "", host: "", user_agent: None, referer: None, client_ip: "", x_request_id_0: "", x_request_id_1: "", req_content_length: None, resp_content_length: Some(21), status_code: Some(200), status: Ok, custom_endpoint: None, custom_result: None, custom_exception: None, attributes: [] } is_http: true
4 changes: 2 additions & 2 deletions agent/resources/test/flow_generator/http/httpv1.result
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
HttpInfo { is_req_end: false, is_resp_end: false, rrt: 0, proto: Http1, is_tls: false, msg_type: Request, raw_data_type: RawProtocol, stream_id: None, version: "1.1", trace_id: "", span_id: "", method: "POST", path: "/query?1590632942", host: "rq.cct.cloud.duba.net", user_agent: None, referer: None, client_ip: "", x_request_id_0: "", x_request_id_1: "", req_content_length: Some(85), resp_content_length: None, status_code: None, status: Ok, attributes: [] } is_http: true
HttpInfo { is_req_end: false, is_resp_end: false, rrt: 0, proto: Http1, is_tls: false, msg_type: Response, raw_data_type: RawProtocol, stream_id: None, version: "1.1", trace_id: "", span_id: "", method: "", path: "", host: "", user_agent: None, referer: None, client_ip: "", x_request_id_0: "", x_request_id_1: "", req_content_length: None, resp_content_length: Some(54), status_code: Some(200), status: Ok, attributes: [] } is_http: true
HttpInfo { is_req_end: false, is_resp_end: false, rrt: 0, proto: Http1, is_tls: false, msg_type: Request, raw_data_type: RawProtocol, stream_id: None, version: "1.1", trace_id: "", span_id: "", method: "POST", path: "/query?1590632942", host: "rq.cct.cloud.duba.net", user_agent: None, referer: None, client_ip: "", x_request_id_0: "", x_request_id_1: "", req_content_length: Some(85), resp_content_length: None, status_code: None, status: Ok, custom_endpoint: None, custom_result: None, custom_exception: None, attributes: [] } is_http: true
HttpInfo { is_req_end: false, is_resp_end: false, rrt: 0, proto: Http1, is_tls: false, msg_type: Response, raw_data_type: RawProtocol, stream_id: None, version: "1.1", trace_id: "", span_id: "", method: "", path: "", host: "", user_agent: None, referer: None, client_ip: "", x_request_id_0: "", x_request_id_1: "", req_content_length: None, resp_content_length: Some(54), status_code: Some(200), status: Ok, custom_endpoint: None, custom_result: None, custom_exception: None, attributes: [] } is_http: true
Original file line number Diff line number Diff line change
@@ -1 +1 @@
HttpInfo { is_req_end: false, is_resp_end: false, rrt: 0, proto: Http2, is_tls: false, msg_type: Request, raw_data_type: RawProtocol, stream_id: Some(1392369), version: "2", trace_id: "", span_id: "", method: "POST", path: "", host: "", user_agent: None, referer: None, client_ip: "", x_request_id_0: "", x_request_id_1: "", req_content_length: Some(0), resp_content_length: None, status_code: None, status: Ok, attributes: [] } is_http: true
HttpInfo { is_req_end: false, is_resp_end: false, rrt: 0, proto: Http2, is_tls: false, msg_type: Request, raw_data_type: RawProtocol, stream_id: Some(1392369), version: "2", trace_id: "", span_id: "", method: "POST", path: "", host: "", user_agent: None, referer: None, client_ip: "", x_request_id_0: "", x_request_id_1: "", req_content_length: Some(0), resp_content_length: None, status_code: None, status: Ok, custom_endpoint: None, custom_result: None, custom_exception: None, attributes: [] } is_http: true
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
HttpInfo { is_req_end: false, is_resp_end: false, rrt: 0, proto: Http1, is_tls: false, msg_type: Request, raw_data_type: RawProtocol, stream_id: None, version: "1.1", trace_id: "", span_id: "", method: "GET", path: "/productpage", host: "productpage:9080", user_agent: Some("curl/7.81.0-DEV"), referer: None, client_ip: "", x_request_id_0: "", x_request_id_1: "", req_content_length: None, resp_content_length: None, status_code: None, status: Ok, attributes: [] } is_http: true
HttpInfo { is_req_end: false, is_resp_end: false, rrt: 0, proto: Http1, is_tls: false, msg_type: Response, raw_data_type: RawProtocol, stream_id: None, version: "1.0", trace_id: "", span_id: "", method: "", path: "", host: "", user_agent: None, referer: None, client_ip: "", x_request_id_0: "", x_request_id_1: "", req_content_length: None, resp_content_length: None, status_code: Some(200), status: Ok, attributes: [] } is_http: true
HttpInfo { is_req_end: false, is_resp_end: false, rrt: 0, proto: Http1, is_tls: false, msg_type: Request, raw_data_type: RawProtocol, stream_id: None, version: "1.1", trace_id: "", span_id: "", method: "GET", path: "/productpage", host: "productpage:9080", user_agent: Some("curl/7.81.0-DEV"), referer: None, client_ip: "", x_request_id_0: "", x_request_id_1: "", req_content_length: None, resp_content_length: None, status_code: None, status: Ok, custom_endpoint: None, custom_result: None, custom_exception: None, attributes: [] } is_http: true
HttpInfo { is_req_end: false, is_resp_end: false, rrt: 0, proto: Http1, is_tls: false, msg_type: Response, raw_data_type: RawProtocol, stream_id: None, version: "1.0", trace_id: "", span_id: "", method: "", path: "", host: "", user_agent: None, referer: None, client_ip: "", x_request_id_0: "", x_request_id_1: "", req_content_length: None, resp_content_length: None, status_code: Some(200), status: Ok, custom_endpoint: None, custom_result: None, custom_exception: None, attributes: [] } is_http: true
18 changes: 9 additions & 9 deletions agent/resources/test/flow_generator/http/sw8.result
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
HttpInfo { is_req_end: false, is_resp_end: false, rrt: 0, proto: Http1, is_tls: false, msg_type: Request, raw_data_type: RawProtocol, stream_id: None, version: "1.1", trace_id: "3912196de0cf41f4bab8a8a8108fc3a8.63.16294441329780027", span_id: "3912196de0cf41f4bab8a8a8108fc3a8.63.16294441329780026-4", method: "POST", path: "/createOrder", host: "10.100.18.175:20880", user_agent: Some("Apache-HttpClient/4.5.10 (Java/1.8.0_212)"), referer: None, client_ip: "", x_request_id_0: "", x_request_id_1: "", req_content_length: Some(351), resp_content_length: None, status_code: None, status: Ok, attributes: [] } is_http: true
HttpInfo { is_req_end: false, is_resp_end: false, rrt: 0, proto: Http1, is_tls: false, msg_type: Response, raw_data_type: RawProtocol, stream_id: None, version: "1.1", trace_id: "", span_id: "", method: "", path: "", host: "", user_agent: None, referer: None, client_ip: "", x_request_id_0: "", x_request_id_1: "", req_content_length: None, resp_content_length: None, status_code: Some(200), status: Ok, attributes: [] } is_http: true
HttpInfo { is_req_end: false, is_resp_end: false, rrt: 0, proto: Http1, is_tls: false, msg_type: Other, raw_data_type: RawProtocol, stream_id: None, version: "", trace_id: "", span_id: "", method: "", path: "", host: "", user_agent: None, referer: None, client_ip: "", x_request_id_0: "", x_request_id_1: "", req_content_length: None, resp_content_length: None, status_code: None, status: Ok, attributes: [] } is_http: false
HttpInfo { is_req_end: false, is_resp_end: false, rrt: 0, proto: Http1, is_tls: false, msg_type: Request, raw_data_type: RawProtocol, stream_id: None, version: "1.1", trace_id: "3912196de0cf41f4bab8a8a8108fc3a8.65.16294441341700021", span_id: "3912196de0cf41f4bab8a8a8108fc3a8.65.16294441341700020-3", method: "POST", path: "/createOrder", host: "10.100.18.175:20880", user_agent: Some("Apache-HttpClient/4.5.10 (Java/1.8.0_212)"), referer: None, client_ip: "", x_request_id_0: "", x_request_id_1: "", req_content_length: Some(247), resp_content_length: None, status_code: None, status: Ok, attributes: [] } is_http: true
HttpInfo { is_req_end: false, is_resp_end: false, rrt: 0, proto: Http1, is_tls: false, msg_type: Response, raw_data_type: RawProtocol, stream_id: None, version: "1.1", trace_id: "", span_id: "", method: "", path: "", host: "", user_agent: None, referer: None, client_ip: "", x_request_id_0: "", x_request_id_1: "", req_content_length: None, resp_content_length: None, status_code: Some(200), status: Ok, attributes: [] } is_http: true
HttpInfo { is_req_end: false, is_resp_end: false, rrt: 0, proto: Http1, is_tls: false, msg_type: Other, raw_data_type: RawProtocol, stream_id: None, version: "", trace_id: "", span_id: "", method: "", path: "", host: "", user_agent: None, referer: None, client_ip: "", x_request_id_0: "", x_request_id_1: "", req_content_length: None, resp_content_length: None, status_code: None, status: Ok, attributes: [] } is_http: false
HttpInfo { is_req_end: false, is_resp_end: false, rrt: 0, proto: Http1, is_tls: false, msg_type: Request, raw_data_type: RawProtocol, stream_id: None, version: "1.1", trace_id: "3912196de0cf41f4bab8a8a8108fc3a8.56.16294441349520027", span_id: "3912196de0cf41f4bab8a8a8108fc3a8.56.16294441349520026-4", method: "POST", path: "/createOrder", host: "10.100.18.175:20880", user_agent: Some("Apache-HttpClient/4.5.10 (Java/1.8.0_212)"), referer: None, client_ip: "", x_request_id_0: "", x_request_id_1: "", req_content_length: Some(350), resp_content_length: None, status_code: None, status: Ok, attributes: [] } is_http: true
HttpInfo { is_req_end: false, is_resp_end: false, rrt: 0, proto: Http1, is_tls: false, msg_type: Response, raw_data_type: RawProtocol, stream_id: None, version: "1.1", trace_id: "", span_id: "", method: "", path: "", host: "", user_agent: None, referer: None, client_ip: "", x_request_id_0: "", x_request_id_1: "", req_content_length: None, resp_content_length: None, status_code: Some(200), status: Ok, attributes: [] } is_http: true
HttpInfo { is_req_end: false, is_resp_end: false, rrt: 0, proto: Http1, is_tls: false, msg_type: Other, raw_data_type: RawProtocol, stream_id: None, version: "", trace_id: "", span_id: "", method: "", path: "", host: "", user_agent: None, referer: None, client_ip: "", x_request_id_0: "", x_request_id_1: "", req_content_length: None, resp_content_length: None, status_code: None, status: Ok, attributes: [] } is_http: false
HttpInfo { is_req_end: false, is_resp_end: false, rrt: 0, proto: Http1, is_tls: false, msg_type: Request, raw_data_type: RawProtocol, stream_id: None, version: "1.1", trace_id: "3912196de0cf41f4bab8a8a8108fc3a8.63.16294441329780027", span_id: "3912196de0cf41f4bab8a8a8108fc3a8.63.16294441329780026-4", method: "POST", path: "/createOrder", host: "10.100.18.175:20880", user_agent: Some("Apache-HttpClient/4.5.10 (Java/1.8.0_212)"), referer: None, client_ip: "", x_request_id_0: "", x_request_id_1: "", req_content_length: Some(351), resp_content_length: None, status_code: None, status: Ok, custom_endpoint: None, custom_result: None, custom_exception: None, attributes: [] } is_http: true
HttpInfo { is_req_end: false, is_resp_end: false, rrt: 0, proto: Http1, is_tls: false, msg_type: Response, raw_data_type: RawProtocol, stream_id: None, version: "1.1", trace_id: "", span_id: "", method: "", path: "", host: "", user_agent: None, referer: None, client_ip: "", x_request_id_0: "", x_request_id_1: "", req_content_length: None, resp_content_length: None, status_code: Some(200), status: Ok, custom_endpoint: None, custom_result: None, custom_exception: None, attributes: [] } is_http: true
HttpInfo { is_req_end: false, is_resp_end: false, rrt: 0, proto: Http1, is_tls: false, msg_type: Other, raw_data_type: RawProtocol, stream_id: None, version: "", trace_id: "", span_id: "", method: "", path: "", host: "", user_agent: None, referer: None, client_ip: "", x_request_id_0: "", x_request_id_1: "", req_content_length: None, resp_content_length: None, status_code: None, status: Ok, custom_endpoint: None, custom_result: None, custom_exception: None, attributes: [] } is_http: false
HttpInfo { is_req_end: false, is_resp_end: false, rrt: 0, proto: Http1, is_tls: false, msg_type: Request, raw_data_type: RawProtocol, stream_id: None, version: "1.1", trace_id: "3912196de0cf41f4bab8a8a8108fc3a8.65.16294441341700021", span_id: "3912196de0cf41f4bab8a8a8108fc3a8.65.16294441341700020-3", method: "POST", path: "/createOrder", host: "10.100.18.175:20880", user_agent: Some("Apache-HttpClient/4.5.10 (Java/1.8.0_212)"), referer: None, client_ip: "", x_request_id_0: "", x_request_id_1: "", req_content_length: Some(247), resp_content_length: None, status_code: None, status: Ok, custom_endpoint: None, custom_result: None, custom_exception: None, attributes: [] } is_http: true
HttpInfo { is_req_end: false, is_resp_end: false, rrt: 0, proto: Http1, is_tls: false, msg_type: Response, raw_data_type: RawProtocol, stream_id: None, version: "1.1", trace_id: "", span_id: "", method: "", path: "", host: "", user_agent: None, referer: None, client_ip: "", x_request_id_0: "", x_request_id_1: "", req_content_length: None, resp_content_length: None, status_code: Some(200), status: Ok, custom_endpoint: None, custom_result: None, custom_exception: None, attributes: [] } is_http: true
HttpInfo { is_req_end: false, is_resp_end: false, rrt: 0, proto: Http1, is_tls: false, msg_type: Other, raw_data_type: RawProtocol, stream_id: None, version: "", trace_id: "", span_id: "", method: "", path: "", host: "", user_agent: None, referer: None, client_ip: "", x_request_id_0: "", x_request_id_1: "", req_content_length: None, resp_content_length: None, status_code: None, status: Ok, custom_endpoint: None, custom_result: None, custom_exception: None, attributes: [] } is_http: false
HttpInfo { is_req_end: false, is_resp_end: false, rrt: 0, proto: Http1, is_tls: false, msg_type: Request, raw_data_type: RawProtocol, stream_id: None, version: "1.1", trace_id: "3912196de0cf41f4bab8a8a8108fc3a8.56.16294441349520027", span_id: "3912196de0cf41f4bab8a8a8108fc3a8.56.16294441349520026-4", method: "POST", path: "/createOrder", host: "10.100.18.175:20880", user_agent: Some("Apache-HttpClient/4.5.10 (Java/1.8.0_212)"), referer: None, client_ip: "", x_request_id_0: "", x_request_id_1: "", req_content_length: Some(350), resp_content_length: None, status_code: None, status: Ok, custom_endpoint: None, custom_result: None, custom_exception: None, attributes: [] } is_http: true
HttpInfo { is_req_end: false, is_resp_end: false, rrt: 0, proto: Http1, is_tls: false, msg_type: Response, raw_data_type: RawProtocol, stream_id: None, version: "1.1", trace_id: "", span_id: "", method: "", path: "", host: "", user_agent: None, referer: None, client_ip: "", x_request_id_0: "", x_request_id_1: "", req_content_length: None, resp_content_length: None, status_code: Some(200), status: Ok, custom_endpoint: None, custom_result: None, custom_exception: None, attributes: [] } is_http: true
HttpInfo { is_req_end: false, is_resp_end: false, rrt: 0, proto: Http1, is_tls: false, msg_type: Other, raw_data_type: RawProtocol, stream_id: None, version: "", trace_id: "", span_id: "", method: "", path: "", host: "", user_agent: None, referer: None, client_ip: "", x_request_id_0: "", x_request_id_1: "", req_content_length: None, resp_content_length: None, status_code: None, status: Ok, custom_endpoint: None, custom_result: None, custom_exception: None, attributes: [] } is_http: false
Binary file modified agent/resources/test/plugins/wasm_test.wasm.gz
Binary file not shown.
27 changes: 25 additions & 2 deletions agent/src/flow_generator/protocol_logs/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ pub struct HttpInfo {
#[serde(rename = "response_status")]
pub status: L7ResponseStatus,

// set by wasm plugin
custom_endpoint: Option<String>,
custom_result: Option<String>,
custom_exception: Option<String>,

#[serde(skip)]
attributes: Vec<KeyVal>,
}
Expand All @@ -121,6 +126,10 @@ impl HttpInfo {
self.path = custom.req.resource;
}

if !custom.req.endpoint.is_empty() {
self.custom_endpoint = Some(custom.req.endpoint)
}

//req write
if custom.resp.code.is_some() {
self.status_code = custom.resp.code;
Expand All @@ -130,6 +139,14 @@ impl HttpInfo {
self.status = custom.resp.status;
}

if !custom.resp.result.is_empty() {
self.custom_result = Some(custom.resp.result)
}

if !custom.resp.exception.is_empty() {
self.custom_exception = Some(custom.resp.exception)
}

//trace info rewrite
if custom.trace.trace_id.is_some() {
self.trace_id = custom.trace.trace_id.unwrap();
Expand Down Expand Up @@ -349,7 +366,12 @@ impl From<HttpInfo> for L7ProtocolSendLog {
f.path,
)
} else {
(f.method, f.path.clone(), f.host, String::new())
(
f.method,
f.path.clone(),
f.host,
f.custom_endpoint.unwrap_or_default(),
)
};

L7ProtocolSendLog {
Expand All @@ -365,7 +387,8 @@ impl From<HttpInfo> for L7ProtocolSendLog {
resp: L7Response {
status: f.status,
code: f.status_code,
..Default::default()
exception: f.custom_exception.unwrap_or_default(),
result: f.custom_result.unwrap_or_default(),
},
trace_info: Some(TraceInfo {
trace_id: Some(f.trace_id),
Expand Down
10 changes: 8 additions & 2 deletions agent/src/plugin/wasm/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ fn test_wasm_http_req() {
assert_eq!(i.req.domain.as_str(), "rewrite domain");
assert_eq!(i.req.req_type.as_str(), "rewrite req type");
assert_eq!(i.req.resource.as_str(), "rewrite resource");
assert_eq!(i.req.endpoint.as_str(), "rewrite endpoint");

let attr = i.ext_info.unwrap().attributes.unwrap();

Expand Down Expand Up @@ -245,6 +246,8 @@ fn test_wasm_http_resp() {

assert_eq!(i.resp.code.unwrap(), 599);
assert_eq!(i.resp.status, L7ResponseStatus::ServerError);
assert_eq!(i.resp.exception.as_str(), "rewrite exception");
assert_eq!(i.resp.result, "rewrite result");

let attr = i.ext_info.unwrap().attributes.unwrap();
assert_eq!(attr.len(), kv.len());
Expand Down Expand Up @@ -603,6 +606,7 @@ func (p parser) OnHttpReq(ctx *sdk.HttpReqCtx) sdk.Action {
ReqType: "rewrite req type",
Domain: "rewrite domain",
Resource: "rewrite resource",
Endpoint: "rewrite endpoint",
}, trace, attr)
}
Expand Down Expand Up @@ -642,8 +646,10 @@ func (p parser) OnHttpResp(ctx *sdk.HttpRespCtx) sdk.Action {
var code int32 = 599
status := sdk.RespStatusServerErr
return sdk.HttpRespActionAbortWithResult(&sdk.Response{
Status: &status,
Code: &code,
Status: &status,
Code: &code,
Result: "rewrite result",
Exception: "rewrite exception",
}, nil, []sdk.KeyVal{
{
Key: "user_id",
Expand Down

0 comments on commit 8ed83ad

Please sign in to comment.