@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()); }
/** * Creates a new {@link alluxio.metrics.sink.PrometheusMetricsServlet} with a * {@link MetricRegistry}. * * @param registry the metric registry to register */ public PrometheusMetricsServlet(MetricRegistry registry) { mCollectorRegistry = new CollectorRegistry(); mCollectorRegistry.register(new DropwizardExports(registry)); }
@Test public void testInstrumentsSuccessfulCall() throws Exception { // Given final CollectorRegistry registry = new CollectorRegistry(); final CallMeter timer = CallMeter.ofCollectorRegistry("some_call", "Some help", registry); // When timer.executeRunnable(() -> { try { Thread.sleep(50); } catch (InterruptedException e) { fail(); } }); // Then assertThat(registry.getSampleValue( "some_call_total", new String[]{}, new String[]{})) .isEqualTo(1.0); assertThat(registry.getSampleValue( "some_call_failures_total", new String[]{}, new String[]{})) .isEqualTo(0.0); assertThat(registry.getSampleValue( "some_call_latency_count", new String[]{}, new String[]{})) .isEqualTo(1.0); }
/** * Returns a newly-created {@link PrometheusMeterRegistry} instance with a new {@link CollectorRegistry}. */ public static PrometheusMeterRegistry newRegistry() { return newRegistry(new CollectorRegistry()); }
private void process(ServerRequest req, ServerResponse res) { Set<String> filters = new HashSet<>(req.queryParams().all("name[]")); Enumeration<Collector.MetricFamilySamples> mfs = collectorRegistry.filteredMetricFamilySamples(filters); res.headers().contentType(CONTENT_TYPE); res.send(compose(mfs)); }
@Test public void testTimer() throws IOException, InterruptedException { Timer t = metricRegistry.timer(name("timer").tag("foo", "bar").build()); Timer.Context time = t.time(); Thread.sleep(1L); time.stop(); // We slept for 1Ms so we ensure that all timers are above 1ms: assertThat(registry.getSampleValue("timer_seconds", new String[]{"foo", "quantile"}, new String[]{"bar", "0.99"})).isGreaterThan(1 / 1000); assertThat(registry.getSampleValue("timer_seconds_count", new String[]{"foo"}, new String[]{"bar"})).isEqualTo(1); assertThat(registry.getSampleValue("timer_seconds_sum", new String[]{"foo"}, new String[]{"bar"})).isEqualTo(-1); assertThat(registry.getSampleValue("timer_meter_m1", new String[]{"foo"}, new String[]{"bar"})).isNotNull(); assertThat(registry.getSampleValue("timer_meter_total", new String[]{"foo"}, new String[]{"bar"})).isNotNull(); assertEquals(Double.valueOf(1.0D), registry.getSampleValue("timer_seconds_count", new String[]{"foo"}, new String[]{"bar"})); assertNotNull("Metric timer_seconds_count should exist", registry.getSampleValue("timer_seconds_count", new String[]{"foo"}, new String[]{"bar"})); assertTypeHelpTimer("timer"); }
public void terminate() { CollectorRegistry.defaultRegistry.unregister(storeCollector); CollectorRegistry.defaultRegistry.unregister(entryCollector); CollectorRegistry.defaultRegistry.unregister(metaCollector); CollectorRegistry.defaultRegistry.unregister(sinkCollector); CollectorRegistry.defaultRegistry.unregister(parserCollector); }
final CollectorRegistry registry = new CollectorRegistry(); assertThat(registry.getSampleValue( "some_call_total", new String[]{}, .isEqualTo(1.0); assertThat(registry.getSampleValue( "some_call_failures_total", new String[]{}, .isEqualTo(1.0); assertThat(registry.getSampleValue( "some_call_latency_count", new String[]{},
@Setup(Level.Iteration) public void setup() { registry = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT, new CollectorRegistry(), Clock.SYSTEM); summary = DistributionSummary.builder("summary") .publishPercentileHistogram() .register(registry); }
public String writeRegistry(Set<String> metricsToInclude) { try { Writer writer = new StringWriter(); TextFormat.write004(writer, collectorRegistry.filteredMetricFamilySamples(metricsToInclude)); return writer.toString(); } catch (IOException e) { // This actually never happens since StringWriter::write() doesn't throw any IOException throw new RuntimeException("Writing metrics failed", e); } } }
@Test public void testHistogram() throws IOException { Histogram hist = metricRegistry.histogram(name("hist").build()); int i = 0; while (i < 100) { hist.update(i); i += 1; } assertEquals(new Double(100), registry.getSampleValue("hist_count")); for (String s : Arrays.asList("0.75", "0.95", "0.98", "0.99")) { assertEquals(Double.valueOf((Double.valueOf(s) - 0.01) * 100), registry.getSampleValue("hist", new String[]{"quantile"}, new String[]{s})); } assertEquals(new Double(99), registry.getSampleValue("hist", new String[]{"quantile"}, new String[]{"0.999"})); }
@Override public void stop() { running = false; if (outboundCounter != null) { CollectorRegistry.defaultRegistry.unregister(outboundCounter); } if (packetsCounter != null) { CollectorRegistry.defaultRegistry.unregister(packetsCounter); } if (emptyBatchesCounter != null) { CollectorRegistry.defaultRegistry.unregister(emptyBatchesCounter); } if (errorsCounter != null) { CollectorRegistry.defaultRegistry.unregister(errorsCounter); } if (responseLatency != null) { CollectorRegistry.defaultRegistry.unregister(responseLatency); } }
@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()); } }
CollectorRegistry.defaultRegistry.register(new DropwizardExports(config.metricRegistry));
/** * Creates a new {@link alluxio.metrics.sink.PrometheusMetricsServlet} with a * {@link MetricRegistry}. * * @param registry the metric registry to register */ public PrometheusMetricsServlet(MetricRegistry registry) { mCollectorRegistry = new CollectorRegistry(); mCollectorRegistry.register(new DropwizardExports(registry)); }
@Test public void testExportsCircuitBreakerStates() { // Given final CollectorRegistry registry = new CollectorRegistry(); final Bulkhead bulkhead = Bulkhead.ofDefaults("foo"); BulkheadExports.ofIterable("boo_bulkhead", singletonList(bulkhead)).register(registry); final Supplier<Map<String, Double>> values = () -> HashSet .of("available_concurrent_calls") .map(param -> Tuple.of(param, registry.getSampleValue( "boo_bulkhead", new String[]{"name", "param"}, new String[]{"foo", param}))) .toMap(t -> t); // When final Map<String, Double> initialValues = values.get(); // Then assertThat(initialValues).isEqualTo(HashMap.of("available_concurrent_calls", 25.0)); }
/** * Pushes all metrics in a Collector, replacing only previously pushed metrics of the same name, job and grouping key. * <p> * This is useful for pushing a single Gauge. * <p> * This uses the POST HTTP method. */ public void pushAdd(Collector collector, String job, Map<String, String> groupingKey) throws IOException { CollectorRegistry registry = new CollectorRegistry(); collector.register(registry); pushAdd(registry, job, groupingKey); }
@Override protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException { resp.setStatus(HttpServletResponse.SC_OK); resp.setContentType(TextFormat.CONTENT_TYPE_004); Writer writer = resp.getWriter(); try { TextFormat.write004(writer, registry.filteredMetricFamilySamples(parse(req))); writer.flush(); } finally { writer.close(); } }
@Test public void testMeter() throws IOException, InterruptedException { Meter meter = metricRegistry.meter(name("meter").build()); meter.mark(); meter.mark(); assertEquals(new Double(2), registry.getSampleValue("meter_total")); }