Skip to content
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

Capture and Log Proxy Response Time in Agent Debugger #352

Merged
merged 11 commits into from
Jan 23, 2025
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public abstract class BaseResponse {

protected String response;
protected long responseTime = -1;
protected long proxyResponseTime = -1;
protected int httpCode = -1;
protected String rspMessage = "";
protected HashMap<String, String> headers = new HashMap<String, String>();
Expand All @@ -52,6 +53,7 @@ public String getLogMsg() {
sb.append("RESPONSE HTTP CODE: ").append(this.httpCode).append(NEWLINE)
.append("RESPONSE HTTP MSG: ").append(this.rspMessage).append(NEWLINE)
.append("RESPONSE TIME: ").append(responseTime).append(NEWLINE)
.append("PROXY RESPONSE TIME: ").append(proxyResponseTime).append(NEWLINE)
.append("RESPONSE SIZE: ").append(getResponseSize()).append(NEWLINE);
for (Entry<String, String> mapEntry : headers.entrySet()) {
sb.append("RESPONSE HEADER: ")
Expand Down Expand Up @@ -84,6 +86,7 @@ public String convertToCSV() {
sb.append(this.httpCode).append(",");
sb.append(this.rspMessage).append(",");
sb.append(responseTime).append(",");
sb.append(proxyResponseTime).append(",");
sb.append(getResponseSize()).append(",");
headers.forEach((key, value) -> sb.append(key).append(" = ").append(value.replace(",", "")).append(","));
cookies.forEach((key, value) -> sb.append(key).append(" = ").append(value).append(","));
Expand Down Expand Up @@ -171,6 +174,14 @@ public void setResponseBody(String body) {
this.response = body;
}

public long getProxyResponseTime() {
return this.proxyResponseTime;
}

public void setProxyResponseTime(long proxyResponseTime) {
this.proxyResponseTime = proxyResponseTime;
}

/**
*
* @param byteArray
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ public void testLogResponse_1()
throws Exception {
BaseResponse fixture = new MockResponse();
fixture.responseTime = 1L;
fixture.proxyResponseTime = 1L;
fixture.httpCode = 1;
fixture.response = "";
fixture.responseByteArray = new byte[] {};
Expand All @@ -451,6 +452,7 @@ public void testLogResponse_1()
assertEquals("RESPONSE HTTP CODE: 1\n" +
"RESPONSE HTTP MSG: \n" +
"RESPONSE TIME: 1\n" +
"PROXY RESPONSE TIME: 1\n" +
"RESPONSE SIZE: 0\n" +
"RESPONSE BODY: not logged because null is not a content-type.\n", fixture.getLogMsg());
}
Expand All @@ -466,6 +468,7 @@ public void testLogResponse_2() {
assertEquals("RESPONSE HTTP CODE: -1\n" +
"RESPONSE HTTP MSG: \n" +
"RESPONSE TIME: -1\n" +
"PROXY RESPONSE TIME: -1\n" +
"RESPONSE SIZE: 12\n" +
"RESPONSE HEADER: content-type = text/html\n" +
"RESPONSE COOKIE: testHeadersKey = testHeadersValue\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,20 @@ private void processResponse(byte[] bResponse, long waitTime, BaseRequest reques
response.setHeader(header.getName(), header.getValue());
}

// Extract proxy response/service time header
String proxyResponseTimeHeader = response.getHttpHeader("x-envoy-upstream-service-time");
if (proxyResponseTimeHeader != null) {
try {
long proxyResponseTime = Long.parseLong(proxyResponseTimeHeader);
response.setProxyResponseTime(proxyResponseTime);
} catch (NumberFormatException e) {
LOG.warn("could not parse proxy service time header: " + proxyResponseTimeHeader);
response.setProxyResponseTime(-1);
}
} else {
response.setProxyResponseTime(-1);
}

Cookie[] cookies = httpstate.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,20 @@ private void processResponse(byte[] bResponse, long waitTime, BaseRequest reques
response.setHeader(header.getName(), header.getValue());
}

// Extract proxy response/service time header
String proxyResponseTimeHeader = response.getHttpHeader("x-envoy-upstream-service-time");
if (proxyResponseTimeHeader != null) {
try {
long proxyResponseTime = Long.parseLong(proxyResponseTimeHeader);
response.setProxyResponseTime(proxyResponseTime);
} catch (NumberFormatException e) {
LOG.warn("could not parse proxy service time header: " + proxyResponseTimeHeader);
response.setProxyResponseTime(-1);
}
} else {
response.setProxyResponseTime(-1);
}

if (context.getCookieStore().getCookies() != null) {
for (Cookie cookie : context.getCookieStore().getCookies()) {
response.setCookie(cookie.getName(), cookie.getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,20 @@ private void processResponse(byte[] bResponse, long waitTime, BaseRequest reques
}
}

// Extract Proxy response/service time header
List<String> proxyResponseTimeHeaders = headers.map().get("x-envoy-upstream-service-time");
if (proxyResponseTimeHeaders != null && !proxyResponseTimeHeaders.isEmpty()) {
try {
long proxyResponseTime = Long.parseLong(proxyResponseTimeHeaders.get(0));
response.setProxyResponseTime(proxyResponseTime);
} catch (NumberFormatException e) {
LOG.warn("could not parse proxy service time header: " + proxyResponseTimeHeaders.get(0));
response.setProxyResponseTime(-1);
}
} else {
response.setProxyResponseTime(-1);
}

List<HttpCookie> cookies = cookieManager.getCookieStore().getCookies();
if (!cookies.isEmpty()) {
for (HttpCookie cookie : cookies) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ public void setChoiceComboBoxOptions(JComboBox<TankClientChoice> cb) {
cb.addItem(new TankClientChoice("Apache HttpClient 3.1", "com.intuit.tank.httpclient3.TankHttpClient3"));
cb.addItem(new TankClientChoice("Apache HttpClient 4.5", "com.intuit.tank.httpclient4.TankHttpClient4"));
cb.addItem(new TankClientChoice("JDK Http Client", "com.intuit.tank.httpclientjdk.TankHttpClientJDK"));
cb.setSelectedIndex(2);
cb.setSelectedIndex(1);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1014,14 +1014,25 @@ public RequestResponsePanel getRequestResponsePanel() {

public void exportCSV(File csvOutputFile) {
try (PrintWriter pw = new PrintWriter(csvOutputFile)) {
pw.println("URL,HTTP Code,HTTP Msg,Response Time,Response Size,Headers...,Cookies...");
pw.println("URL,HTTP Code,HTTP Msg,Response Time,Proxy Response Time,Response Size,Headers...,Cookies...");
steps.stream()
.filter(step -> step.getResponse() != null)
.map(step -> step.getRequest().getRequestUrl() + "," + step.getResponse().convertToCSV())
.forEach(pw::println);
.forEach(step -> {
String escapedUrl = escapeCSV(step.getRequest().getRequestUrl());
pw.println(escapedUrl + "," + step.getResponse().convertToCSV());
});
} catch ( FileNotFoundException e) {
LOG.error("Error exporting CSV: {}", String.valueOf(e));
}
}

private String escapeCSV(String value) {
if (value == null) {
return "";
}
if (value.contains(",") || value.contains("\"") || value.contains("\n")) {
return "\"" + value.replace("\"", "\"\"") + "\"";
}
return value;
}
}