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

added metric for number of total responses #103

Merged
merged 1 commit into from May 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -49,8 +49,7 @@ public void filter(ContainerRequestContext req, ContainerResponseContext res) {

String resource = ResourceExtractor.getResource(req.getUriInfo());

// We are only interested in recording the response status if it was an error
// (either a 4xx or 5xx). No point in counting the successful responses
PrometheusExporter.instance().recordResponseTotal(status, req.getMethod(), resource);
if (status >= 400) {
PrometheusExporter.instance().recordResponseError(status, req.getMethod(), resource);
}
Expand Down
Expand Up @@ -55,6 +55,7 @@ public final class PrometheusExporter {
final Counter totalFailedClientLoginAttempts;
final Counter totalCodeToTokens;
final Counter totalCodeToTokensErrors;
final Counter responseTotal;
final Counter responseErrors;
final Histogram requestDuration;
final PushGateway PUSH_GATEWAY;
Expand Down Expand Up @@ -146,6 +147,12 @@ private PrometheusExporter() {
.labelNames("realm", "provider", "error", "client_id")
.register();

responseTotal = Counter.build()
.name("keycloak_response_total")
.help("Total number of responses")
.labelNames("code", "method", "resource")
.register();

responseErrors = Counter.build()
.name("keycloak_response_errors")
.help("Total number of error responses")
Expand Down Expand Up @@ -363,6 +370,17 @@ public void recordRequestDuration(double amt, String method, String resource) {
pushAsync();
}

/**
* Increase the response total count by a given method and response code
*
* @param code The returned http status code
* @param method The request method used
*/
public void recordResponseTotal(int code, String method, String resource) {
responseTotal.labels(Integer.toString(code), method, resource).inc();
pushAsync();
}

/**
* Increase the response error count by a given method and response code
*
Expand Down
Expand Up @@ -272,6 +272,16 @@ public void shouldCorrectlyRecordResponseDurations() throws IOException {
tuple("method", "GET"), tuple("resource", "admin,admin/serverinfo"));
}

@Test
public void shouldCorrectlyRecordResponseTotal() throws IOException {
PrometheusExporter.instance().recordResponseTotal(200, "GET", "admin,admin/serverinfo");
PrometheusExporter.instance().recordResponseTotal(500, "POST", "admin,admin/serverinfo");
assertGenericMetric("keycloak_response_total", 1,
tuple("code", "200"), tuple("method", "GET"), tuple("resource", "admin,admin/serverinfo"));
assertGenericMetric("keycloak_response_total", 1,
tuple("code", "500"), tuple("method", "POST"), tuple("resource", "admin,admin/serverinfo"));
}

@Test
public void shouldCorrectlyRecordResponseErrors() throws IOException {
PrometheusExporter.instance().recordResponseError(500, "POST", "admin,admin/serverinfo");
Expand Down