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

Prevent http route resolution for requests made directly to server with 4xx response codes. #30568

Open
wants to merge 1 commit into
base: integration
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2024 IBM Corporation and others.
* Copyright (c) 2024, 2025 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -137,13 +137,18 @@ public void atSendResponse(@This Object probedHttpDispatcherLinkObj, @Args Objec
tl_startNanos.set(System.nanoTime());
HttpStatAttributes.Builder builder = HttpStatAttributes.builder();


boolean is4xxCode = false;

/*
* Get Status Code (and Exception if it exists)
*/
if (myargs.length > 0) {
if (myargs[0] != null && myargs[0] instanceof StatusCodes) {
StatusCodes statCode = (StatusCodes) myargs[0];

builder.withResponseStatus(statCode.getIntCode());
is4xxCode = (statCode.getIntCode() % 400 <= 99 ) ? true : false;
}

if (myargs[2] != null && myargs[2] instanceof Exception) {
Expand All @@ -165,8 +170,10 @@ public void atSendResponse(@This Object probedHttpDispatcherLinkObj, @Args Objec
try {

HttpRequest httpRequest = httpDispatcherLink.getRequest();
if (!is4xxCode) {
builder.withHttpRoute(httpRequest.getURI());
}

builder.withHttpRoute(httpRequest.getURI());
builder.withRequestMethod(httpRequest.getMethod());
builder.withScheme(httpRequest.getScheme());
resolveNetworkProtocolInfo(httpRequest.getVersion(), builder);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2024 IBM Corporation and others.
* Copyright (c) 2024, 2025 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -259,14 +259,14 @@ protected boolean validateMpMetricsHttp(String vendorMetricsOutput, String route
+ "\",http_request_method=\""
+ requestMethod
+ "\",http_response_status_code=\"" + responseStatus
+ "\",http_route=\"" + route
+ ((route != null) ? ("\",http_route=\"" + route) : "\",http_route=\"")
+ "\",mp_scope=\"vendor\",network_protocol_version=\"1\\.[01]\",server_address=\"localhost\",server_port=\"[0-9]+\",url_scheme=\"http\",\\} ";

String sumMatchString = "http_server_request_duration_seconds_sum\\{error_type=\"" + errorType
+ "\",http_request_method=\""
+ requestMethod
+ "\",http_response_status_code=\"" + responseStatus
+ "\",http_route=\"" + route
+ ((route != null) ? ("\",http_route=\"" + route) : "\",http_route=\"")
+ "\",mp_scope=\"vendor\",network_protocol_version=\"1\\.[01]\",server_address=\"localhost\",server_port=\"[0-9]+\",url_scheme=\"http\",\\} ";

return validatePrometheusHTTPMetricCount(vendorMetricsOutput, route, responseStatus, requestMethod, errorType, expectedCount, countMatchString) &&
Expand Down Expand Up @@ -325,15 +325,15 @@ protected boolean validateMpTelemetryHttp(String appName, String vendorMetricsOu
countMatchString = "http_server_request_duration_seconds_count\\{http_request_method=\""
+ requestMethod
+ "\",http_response_status_code=\"" + responseStatus
+ "\",http_route=\"" + route
+ ((route != null) ? ("\",http_route=\"" + route) : "")
+ "\",instance=\"[a-zA-Z0-9-]*\""
+ ",job=\"" + appName
+ "\",network_protocol_version=\"1\\.[01]\",server_address=\"localhost\",server_port=\"[0-9]+\",url_scheme=\"http\"\\} ";

sumMatchString = "http_server_request_duration_seconds_sum\\{http_request_method=\""
+ requestMethod
+ "\",http_response_status_code=\"" + responseStatus
+ "\",http_route=\"" + route
+ ((route != null) ? ("\",http_route=\"" + route) : "")
+ "\",instance=\"[a-zA-Z0-9-]*\""
+ ",job=\"" + appName
+ "\",network_protocol_version=\"1\\.[01]\",server_address=\"localhost\",server_port=\"[0-9]+\",url_scheme=\"http\"\\} ";
Expand All @@ -342,7 +342,7 @@ protected boolean validateMpTelemetryHttp(String appName, String vendorMetricsOu
+ "\",http_request_method=\""
+ requestMethod
+ "\",http_response_status_code=\"" + responseStatus
+ "\",http_route=\"" + route
+ ((route != null) ? ("\",http_route=\"" + route) : "")
+ "\",instance=\"[a-zA-Z0-9-]*\""
+ ",job=\"" + appName
+ "\",network_protocol_version=\"1\\.[01]\",server_address=\"localhost\",server_port=\"[0-9]+\",url_scheme=\"http\"\\} ";
Expand All @@ -351,7 +351,7 @@ protected boolean validateMpTelemetryHttp(String appName, String vendorMetricsOu
+ "\",http_request_method=\""
+ requestMethod
+ "\",http_response_status_code=\"" + responseStatus
+ "\",http_route=\"" + route
+ ((route != null) ? ("\",http_route=\"" + route) : "")
+ "\",instance=\"[a-zA-Z0-9-]*\""
+ ",job=\"" + appName
+ "\",network_protocol_version=\"1\\.[01]\",server_address=\"localhost\",server_port=\"[0-9]+\",url_scheme=\"http\"\\} ";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2024 IBM Corporation and others.
* Copyright (c) 2024, 2025 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -89,4 +89,27 @@ public void c_noApp_splashPage() throws Exception {

}

@Test
public void c_noApp_nonExistent() throws Exception {

assertTrue(server.isStarted());

String route = "/madeThisUp";
String requestMethod = HttpMethod.GET;
String responseStatus = "404";

String res = requestHttpServlet(route, server, requestMethod);

//Allow time for the collector to receive and expose metrics
assertTrueRetryWithTimeout(() -> validateMpTelemetryHttp(Constants.OTEL_SERVICE_NOT_SET, getContainerCollectorMetrics(container), null, responseStatus, requestMethod));

String route2 = "/anotherMadeThisUp";
String res2 = requestHttpServlet(route, server, requestMethod);

//Allow time for the collector to receive and expose metrics
assertTrueRetryWithTimeout(() -> validateMpTelemetryHttp(Constants.OTEL_SERVICE_NOT_SET, getContainerCollectorMetrics(container), null, responseStatus, requestMethod, null,
">1", null));

}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2024 IBM Corporation and others.
* Copyright (c) 2024, 2025 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -84,4 +84,24 @@ public void noApp_splashPage() throws Exception {

}

@Test
public void noApp_nonExistent() throws Exception {

assertTrue(server.isStarted());

String route = "/madeThisUp";
String requestMethod = HttpMethod.GET;
String responseStatus = "404";

String res = requestHttpServlet(route, server, requestMethod);

assertTrue(validateMpMetricsHttp(getVendorMetrics(server), null, responseStatus, requestMethod));

String route2 = "/anotherMadeThisUp";
String res2 = requestHttpServlet(route, server, requestMethod);

assertTrue(validateMpMetricsHttp(getVendorMetrics(server), null, responseStatus, requestMethod, null, ">1", null));

}

}