Ignore accept header in requests to metrics service (#1345)

Signed-off-by: Adrian Sutton <adrian.sutton@consensys.net>
pull/1348/head
Adrian Sutton 4 years ago committed by GitHub
parent 3612be4fba
commit 80173d6fe6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      CHANGELOG.md
  2. 6
      metrics/core/src/main/java/org/hyperledger/besu/metrics/prometheus/MetricsHttpService.java
  3. 18
      metrics/core/src/test/java/org/hyperledger/besu/metrics/prometheus/MetricsHttpServiceTest.java

@ -8,6 +8,8 @@
### Bug Fixes ### Bug Fixes
* The metrics HTTP server no longer rejects requests containing `Accept` header that doesn't precisely match the prometheus text format [\#1345](https://github.com/hyperledger/besu/pull/1345)
#### Previously identified known issues #### Previously identified known issues
- [Logs queries missing results against chain head](KNOWN_ISSUES.md#Logs-queries-missing-results-against-chain-head) - [Logs queries missing results against chain head](KNOWN_ISSUES.md#Logs-queries-missing-results-against-chain-head)

@ -91,11 +91,7 @@ class MetricsHttpService implements MetricsService {
router.route("/").method(HttpMethod.GET).handler(this::handleEmptyRequest); router.route("/").method(HttpMethod.GET).handler(this::handleEmptyRequest);
// Endpoint for Prometheus metrics monitoring. // Endpoint for Prometheus metrics monitoring.
router router.route("/metrics").method(HttpMethod.GET).handler(this::metricsRequest);
.route("/metrics")
.method(HttpMethod.GET)
.produces(TextFormat.CONTENT_TYPE_004)
.handler(this::metricsRequest);
final CompletableFuture<?> resultFuture = new CompletableFuture<>(); final CompletableFuture<?> resultFuture = new CompletableFuture<>();
httpServer httpServer

@ -21,6 +21,7 @@ import static org.hyperledger.besu.util.NetworkUtility.urlForSocketAddress;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.util.Properties; import java.util.Properties;
import io.prometheus.client.exporter.common.TextFormat;
import io.vertx.core.Vertx; import io.vertx.core.Vertx;
import okhttp3.OkHttpClient; import okhttp3.OkHttpClient;
import okhttp3.Request; import okhttp3.Request;
@ -174,6 +175,23 @@ public class MetricsHttpServiceTest {
} }
} }
@Test
// There is only one available representation so content negotiation should not be used
public void acceptHeaderIgnored() throws Exception {
final Request metricsRequest =
new Request.Builder().addHeader("Accept", "text/xml").url(baseUrl + "/metrics").build();
try (final Response resp = client.newCall(metricsRequest).execute()) {
assertThat(resp.code()).isEqualTo(200);
// Check general format of result, it maps to java.util.Properties
final Properties props = new Properties();
props.load(resp.body().byteStream());
// We should have JVM metrics already loaded, verify a simple key.
assertThat(props).containsKey("jvm_threads_deadlocked");
assertThat(resp.header("Content-Type")).contains(TextFormat.CONTENT_TYPE_004);
}
}
private Request buildGetRequest(final String path) { private Request buildGetRequest(final String path) {
return new Request.Builder().get().url(baseUrl + path).build(); return new Request.Builder().get().url(baseUrl + path).build();
} }

Loading…
Cancel
Save