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

Issue 2161: set metrics endpoint content-type #4208

Merged
merged 6 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
Expand Up @@ -28,6 +28,7 @@
*/
public class HttpServiceResponse {
private String body;
private String contentType;
private HttpServer.StatusCode code = HttpServer.StatusCode.OK;

public HttpServiceResponse() {}
Expand All @@ -41,6 +42,10 @@ public String getBody() {
return body;
}

public String getContentType() {
return contentType;
}

public int getStatusCode() {
return code.getValue();
}
Expand All @@ -50,6 +55,11 @@ public HttpServiceResponse setBody(String body) {
return this;
}

public HttpServiceResponse setContentType(String contentType) {
this.contentType = contentType;
return this;
}

public HttpServiceResponse setCode(HttpServer.StatusCode code) {
this.code = code;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/
package org.apache.bookkeeper.http.vertx;

import io.netty.handler.codec.http.HttpHeaderNames;
import io.vertx.core.Handler;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.HttpServerRequest;
Expand Down Expand Up @@ -56,6 +57,9 @@ void processRequest(HttpEndpointService httpEndpointService, RoutingContext cont
response = new ErrorHttpService().handle(request);
}
httpResponse.setStatusCode(response.getStatusCode());
if (response.getContentType() != null) {
httpResponse.putHeader(HttpHeaderNames.CONTENT_TYPE, response.getContentType());
}
httpResponse.end(response.getBody());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
*/
public class MetricsService implements HttpEndpointService {

public static final String PROMETHEUS_CONTENT_TYPE_004 = "text/plain; version=0.0.4; charset=utf-8";

private final ServerConfiguration conf;
private final StatsProvider statsProvider;

Expand Down Expand Up @@ -65,6 +67,7 @@ public HttpServiceResponse handle(HttpServiceRequest request) throws Exception {
statsProvider.writeAllMetrics(writer);
writer.flush();
response.setCode(StatusCode.OK);
response.setContentType(PROMETHEUS_CONTENT_TYPE_004);
tomjo marked this conversation as resolved.
Show resolved Hide resolved
response.setBody(writer.getBuffer().toString());
} catch (UnsupportedOperationException uoe) {
response.setCode(StatusCode.INTERNAL_ERROR);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.apache.bookkeeper.server.http.service;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.CALLS_REAL_METHODS;
import static org.mockito.Mockito.doAnswer;
Expand Down Expand Up @@ -55,6 +56,7 @@ public void testForbiddenMethods() throws Exception {
HttpServiceRequest request = new HttpServiceRequest().setMethod(Method.PUT);
HttpServiceResponse response = service.handle(request);
assertEquals(StatusCode.FORBIDDEN.getValue(), response.getStatusCode());
assertNull(response.getContentType());
assertEquals(
"PUT is forbidden. Should be GET method",
response.getBody());
Expand All @@ -66,6 +68,7 @@ public void testNullStatsProvider() throws Exception {
HttpServiceRequest request = new HttpServiceRequest().setMethod(Method.GET);
HttpServiceResponse response = service.handle(request);
assertEquals(StatusCode.INTERNAL_ERROR.getValue(), response.getStatusCode());
assertNull(response.getContentType());
assertEquals(
"Stats provider is not enabled. Please enable it by set statsProviderClass"
+ " on bookie configuration",
Expand All @@ -86,6 +89,7 @@ public void testWriteMetrics() throws Exception {
HttpServiceResponse response = service.handle(request);

assertEquals(StatusCode.OK.getValue(), response.getStatusCode());
assertEquals(MetricsService.PROMETHEUS_CONTENT_TYPE_004, response.getContentType());
assertEquals(content, response.getBody());
}

Expand All @@ -98,6 +102,7 @@ public void testWriteMetricsException() throws Exception {
HttpServiceResponse response = service.handle(request);

assertEquals(StatusCode.INTERNAL_ERROR.getValue(), response.getStatusCode());
assertNull(response.getContentType());
assertEquals("Exceptions are thrown when exporting metrics : write-metrics-exception",
response.getBody());
}
Expand All @@ -111,6 +116,7 @@ public void testWriteMetricsUnimplemented() throws Exception {
HttpServiceResponse response = service.handle(request);

assertEquals(StatusCode.INTERNAL_ERROR.getValue(), response.getStatusCode());
assertNull(response.getContentType());
assertEquals("Currently stats provider doesn't support exporting metrics in http service",
response.getBody());
}
Expand Down
Loading