Skip to content

Commit

Permalink
Issue 2161: set metrics endpoint content-type (#4208)
Browse files Browse the repository at this point in the history
* fix: set metrics endpoint content-type when bookie http server is enabled

* fix: checkstyle

* fix: use existing prometheus content type constant

* chore: use minimal dependency / license fixes

* Revert "chore: use minimal dependency / license fixes"

This reverts commit 7daf8be.

* Revert "fix: use existing prometheus content type constant"

This reverts commit c265c49.
  • Loading branch information
Tom Jorissen committed Mar 18, 2024
1 parent ad06dfb commit 382184e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
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);
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

0 comments on commit 382184e

Please sign in to comment.