@Override protected HttpResponse doGet(ServiceRequestContext ctx, HttpRequest req) throws Exception { final ByteArrayOutputStream stream = new ByteArrayOutputStream(); try (OutputStreamWriter writer = new OutputStreamWriter(stream)) { TextFormat.write004(writer, collectorRegistry.metricFamilySamples()); } return HttpResponse.of(HttpStatus.OK, CONTENT_TYPE_004, stream.toByteArray()); }
@Override public void handleRequest(final HttpServerExchange exchange) throws Exception { Writer writer = new StringWriter(); try { TextFormat.write004(writer, registry.metricFamilySamples()); } catch (IOException e) { logger.error("error on put result:", e); } exchange.getResponseSender().send(writer.toString()); } }
private void assertTypeHelpTimer(String timerName) throws IOException { final StringWriter stringWriter = new StringWriter(); TextFormat.write004(stringWriter, registry.metricFamilySamples()); assertThat(stringWriter.toString()).containsOnlyOnce("# HELP " + timerName + "_seconds "); assertThat(stringWriter.toString()).containsOnlyOnce("# TYPE " + timerName + "_seconds "); assertThat(stringWriter.toString()).containsOnlyOnce("# HELP " + timerName + "_meter "); assertThat(stringWriter.toString()).containsOnlyOnce("# TYPE " + timerName + "_meter "); }
@Test public void testSameMetricNameDifferentTags() throws Exception { metricRegistry.register(name("jvm_memory_pools").tag("memory_pool", "PS-Survivor-Space").tag("type", "usage").build(), (Gauge<Double>) () -> 0.6795399983723959); metricRegistry.register(name("jvm_memory_pools").tag("memory_pool", "PS-Survivor-Space").tag("type", "used").build(), (Gauge<Long>) () -> 2137648L); final StringWriter stringWriter = new StringWriter(); TextFormat.write004(stringWriter, registry.metricFamilySamples()); assertThat(stringWriter.toString()).containsOnlyOnce("# HELP"); assertThat(stringWriter.toString()).containsOnlyOnce("# TYPE"); }
/** * Returns the given value, or null if it doesn't exist. * <p> * This is inefficient, and intended only for use in unittests. */ public Double getSampleValue(String name, String[] labelNames, String[] labelValues) { for (Collector.MetricFamilySamples metricFamilySamples : Collections.list(metricFamilySamples())) { for (Collector.MetricFamilySamples.Sample sample : metricFamilySamples.samples) { if (sample.name.equals(name) && Arrays.equals(sample.labelNames.toArray(), labelNames) && Arrays.equals(sample.labelValues.toArray(), labelValues)) { return sample.value; } } } return null; }
/** * Push samples from the given registry to Graphite. */ public void push(CollectorRegistry registry) throws IOException { Socket s = new Socket(host, port); BufferedWriter writer = new BufferedWriter(new PrintWriter(s.getOutputStream())); Matcher m = INVALID_GRAPHITE_CHARS.matcher(""); long now = System.currentTimeMillis() / 1000; for (Collector.MetricFamilySamples metricFamilySamples: Collections.list(registry.metricFamilySamples())) { for (Collector.MetricFamilySamples.Sample sample: metricFamilySamples.samples) { m.reset(sample.name); writer.write(m.replaceAll("_")); for (int i = 0; i < sample.labelNames.size(); ++i) { m.reset(sample.labelValues.get(i)); writer.write("." + sample.labelNames.get(i) + "." + m.replaceAll("_")); } writer.write(" " + sample.value + " " + now + "\n"); } } writer.close(); s.close(); }
@ReadOperation(produces = TextFormat.CONTENT_TYPE_004) public String scrape() { try { Writer writer = new StringWriter(); TextFormat.write004(writer, this.collectorRegistry.metricFamilySamples()); return writer.toString(); } catch (IOException ex) { // This actually never happens since StringWriter::write() doesn't throw any // IOException throw new RuntimeException("Writing metrics failed", ex); } }
if (!method.equals("DELETE")) { BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream(), "UTF-8")); TextFormat.write004(writer, registry.metricFamilySamples()); writer.flush(); writer.close();
/** * @see io.apicurio.hub.api.metrics.IApiMetrics#getCurrentMetricsInfo() */ @Override public String getCurrentMetricsInfo() throws IOException { StringWriter writer = new StringWriter(); TextFormat.write004(writer, CollectorRegistry.defaultRegistry.metricFamilySamples()); String content = writer.getBuffer().toString(); return content; }
@ResponseBody @RequestMapping(produces = TextFormat.CONTENT_TYPE_004) void writeMetrics(HttpServletResponse response) throws IOException { try (Writer writer = response.getWriter()) { TextFormat.write004(writer, CollectorRegistry.defaultRegistry.metricFamilySamples()); } } }
/** * @see io.apicurio.hub.editing.metrics.IEditingMetrics#getCurrentMetricsInfo() */ @Override public String getCurrentMetricsInfo() throws IOException { StringWriter writer = new StringWriter(); TextFormat.write004(writer, CollectorRegistry.defaultRegistry.metricFamilySamples()); String content = writer.getBuffer().toString(); return content; }
/** * @see io.apicurio.hub.editing.metrics.IEditingMetrics#getCurrentMetricsInfo() */ @Override public String getCurrentMetricsInfo() throws IOException { StringWriter writer = new StringWriter(); TextFormat.write004(writer, CollectorRegistry.defaultRegistry.metricFamilySamples()); String content = writer.getBuffer().toString(); return content; }
@GET @Path("/metrics") @Produces(MediaType.TEXT_PLAIN) public StreamingOutput metrics() { return output -> { try (final Writer writer = new OutputStreamWriter(output)) { TextFormat.write004(writer, CollectorRegistry.defaultRegistry.metricFamilySamples()); } }; }
@ResponseBody @RequestMapping(produces = TextFormat.CONTENT_TYPE_004) void writeMetrics(HttpServletResponse response) throws IOException { try (Writer writer = response.getWriter()) { TextFormat.write004(writer, CollectorRegistry.defaultRegistry.metricFamilySamples()); } } }
@Override public String getTextFormat() { try { StringWriter result = new StringWriter(); TextFormat.write004(result, registry.metricFamilySamples()); return result.toString(); } catch (IOException e) { throw new RuntimeException("Unexpected error when writing metrics to a String: " + e.getMessage(), e); } } }
/** * Content that should be included in the response body for an endpoint designate for * Prometheus to scrape from. */ public String scrape() { Writer writer = new StringWriter(); try { TextFormat.write004(writer, registry.metricFamilySamples()); } catch (IOException e) { // This actually never happens since StringWriter::write() doesn't throw any IOException throw new RuntimeException(e); } return writer.toString(); }
@Override public ResponseEntity<String> invoke() { try { Writer writer = new StringWriter(); TextFormat.write004(writer, collectorRegistry.metricFamilySamples()); return ResponseEntity.ok() .header(CONTENT_TYPE, TextFormat.CONTENT_TYPE_004) .body(writer.toString()); } catch (IOException e) { // This actually never happens since StringWriter::write() doesn't throw any IOException throw new RuntimeException("Writing metrics failed", e); } } }
@Override public ResponseEntity<String> invoke() { try { Writer writer = new StringWriter(); TextFormat.write004(writer, collectorRegistry.metricFamilySamples()); return ResponseEntity.ok() .header(CONTENT_TYPE, TextFormat.CONTENT_TYPE_004) .body(writer.toString()); } catch (IOException e) { // This actually never happens since StringWriter::write() doesn't throw any IOException throw new RuntimeException("Writing metrics failed", e); } } }
private static void respondMetrics(CollectorRegistry registry, HttpExchange httpExchange) throws IOException { StringWriter respBodyWriter = new StringWriter(); respBodyWriter.write("# Metrics will become visible when they are updated for the first time.\n"); TextFormat.write004(respBodyWriter, registry.metricFamilySamples()); byte[] respBody = respBodyWriter.toString().getBytes("UTF-8"); httpExchange.getResponseHeaders().put("Context-Type", Collections.singletonList("text/plain; charset=UTF-8")); httpExchange.sendResponseHeaders(200, respBody.length); httpExchange.getResponseBody().write(respBody); httpExchange.getResponseBody().close(); }
public static HandlerFunction<ServerResponse> scrape(PrometheusMeterRegistry registry) { return (request) -> ServerResponse.ok().header("Content-Type", TextFormat.CONTENT_TYPE_004) .body(Mono.fromSupplier(() -> { Writer writer = new StringWriter(); try { TextFormat.write004(writer, registry.getPrometheusRegistry().metricFamilySamples()); } catch (IOException e) { // This actually never happens since StringWriter::write() doesn't throw any IOException throw new RuntimeException(e); } return writer.toString(); }), String.class); } }