io.smallrye.metrics.MetricRegistries Java Examples

The following examples show how to use io.smallrye.metrics.MetricRegistries. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ExportersMetricScalingTest.java    From smallrye-metrics with Apache License 2.0 7 votes vote down vote up
/**
 * Given a Gauge with unit=dollars (custom unit),
 * check that the statistics from OpenMetricsExporter will be presented in dollars.
 */
@Test
public void gauge_customUnit_openMetrics() {
    MetricRegistry registry = MetricRegistries.get(MetricRegistry.Type.APPLICATION);
    Metadata metadata = Metadata.builder()
            .withName("gauge1")
            .withType(MetricType.GAUGE)
            .withUnit("dollars")
            .build();
    Gauge<Long> gaugeInstance = () -> 3L;
    registry.register(metadata, gaugeInstance);

    OpenMetricsExporter exporter = new OpenMetricsExporter();
    String exported = exporter.exportOneMetric(MetricRegistry.Type.APPLICATION, new MetricID("gauge1")).toString();

    Assert.assertThat(exported, containsString("application_gauge1_dollars 3.0"));
}
 
Example #2
Source File: QuarkusJaxRsMetricsFilter.java    From quarkus with Apache License 2.0 6 votes vote down vote up
private void finishRequest(Long start, Class<?> resourceClass, Method resourceMethod) {
    long value = System.nanoTime() - start;
    MetricID metricID = getMetricID(resourceClass, resourceMethod);

    MetricRegistry registry = MetricRegistries.get(MetricRegistry.Type.BASE);
    if (!registry.getMetadata().containsKey(metricID.getName())) {
        // if no metric with this name exists yet, register it
        Metadata metadata = Metadata.builder()
                .withName(metricID.getName())
                .withDescription(
                        "The number of invocations and total response time of this RESTful resource method since the start of the server.")
                .withUnit(MetricUnits.NANOSECONDS)
                .build();
        registry.simpleTimer(metadata, metricID.getTagsAsArray());
    }
    registry.simpleTimer(metricID.getName(), metricID.getTagsAsArray())
            .update(Duration.ofNanos(value));
}
 
Example #3
Source File: OpenMetricsExporter.java    From smallrye-metrics with Apache License 2.0 6 votes vote down vote up
@Override
public StringBuilder exportOneMetric(MetricRegistry.Type scope, MetricID metricID) {
    alreadyExportedNames.set(new HashSet<>());
    MetricRegistry registry = MetricRegistries.get(scope);
    Map<MetricID, Metric> metricMap = registry.getMetrics();

    Metric m = metricMap.get(metricID);

    Map<MetricID, Metric> outMap = new HashMap<>(1);
    outMap.put(metricID, m);

    StringBuilder sb = new StringBuilder();
    exposeEntries(scope, sb, registry, outMap);
    alreadyExportedNames.set(null);
    return sb;
}
 
Example #4
Source File: JsonMetadataExporter.java    From smallrye-metrics with Apache License 2.0 6 votes vote down vote up
@Override
public StringBuilder exportMetricsByName(MetricRegistry.Type scope, String name) {
    MetricRegistry registry = MetricRegistries.get(scope);
    if (registry == null) {
        return null;
    }

    Metadata metadata = registry.getMetadata().get(name);

    if (metadata == null) {
        return null;
    }

    JsonObjectBuilder builder = JsonProviderHolder.get().createObjectBuilder();
    metricJSON(builder, name, metadata, getKnownTagsByMetricName(registry, name));
    return stringify(builder.build());

}
 
Example #5
Source File: SmallRyeMetricsProcessor.java    From quarkus with Apache License 2.0 6 votes vote down vote up
@BuildStep
void beans(BuildProducer<AdditionalBeanBuildItem> additionalBeans,
        BuildProducer<UnremovableBeanBuildItem> unremovableBeans) {
    additionalBeans.produce(new AdditionalBeanBuildItem(MetricProducer.class,
            MetricNameFactory.class,
            MetricRegistries.class,
            MetricsInterceptor.class,
            MeteredInterceptor.class,
            ConcurrentGaugeInterceptor.class,
            CountedInterceptor.class,
            TimedInterceptor.class,
            SimplyTimedInterceptor.class,
            MetricsRequestHandler.class));
    unremovableBeans.produce(new UnremovableBeanBuildItem(
            new UnremovableBeanBuildItem.BeanClassNameExclusion(MetricsRequestHandler.class.getName())));
}
 
Example #6
Source File: JsonMetadataExporterTest.java    From smallrye-metrics with Apache License 2.0 6 votes vote down vote up
@Test
public void exportByMetricNameWithMultipleMetrics() {
    JsonMetadataExporter exporter = new JsonMetadataExporter();
    MetricRegistry applicationRegistry = MetricRegistries.get(MetricRegistry.Type.APPLICATION);
    applicationRegistry.counter("counter1", new Tag("key1", "value1"));
    applicationRegistry.counter("counter1", new Tag("key1", "value2"));
    applicationRegistry.counter("counter1", new Tag("key1", "value3"));

    String result = exporter.exportMetricsByName(MetricRegistry.Type.APPLICATION, "counter1").toString();
    JsonObject json = Json.createReader(new StringReader(result)).read().asJsonObject();

    JsonArray outerTagsArray = json.getJsonObject("counter1").getJsonArray("tags");
    assertEquals(3, outerTagsArray.size());

    JsonArray innerArray1 = outerTagsArray.getJsonArray(0);
    assertEquals(1, innerArray1.size());
    assertEquals("key1=value1", innerArray1.getString(0));

    JsonArray innerArray2 = outerTagsArray.getJsonArray(1);
    assertEquals(1, innerArray2.size());
    assertEquals("key1=value2", innerArray2.getString(0));

    JsonArray innerArray3 = outerTagsArray.getJsonArray(2);
    assertEquals(1, innerArray3.size());
    assertEquals("key1=value3", innerArray3.getString(0));
}
 
Example #7
Source File: MetricCdiInjectionExtension.java    From smallrye-metrics with Apache License 2.0 6 votes vote down vote up
private void addInterceptorBindings(@Observes BeforeBeanDiscovery bbd, BeanManager manager) {
    SmallRyeMetricsLogging.log.logSmallRyeMetricsVersion(getImplementationVersion().orElse("unknown"));

    String extensionName = MetricCdiInjectionExtension.class.getName();

    // It seems that fraction deployment module cannot be picked up as a CDI bean archive - see also SWARM-1725
    for (Class clazz : new Class[] {
            MetricProducer.class,
            MetricNameFactory.class,
            GaugeRegistrationInterceptor.class,
            MetricRegistries.class,

            MeteredInterceptor.class,
            CountedInterceptor.class,
            ConcurrentGaugeInterceptor.class,
            TimedInterceptor.class,
            SimplyTimedInterceptor.class,
            MetricsRequestHandler.class
    }) {
        bbd.addAnnotatedType(manager.createAnnotatedType(clazz), extensionName + "_" + clazz.getName());
    }
}
 
Example #8
Source File: JsonMetadataExporterTest.java    From smallrye-metrics with Apache License 2.0 6 votes vote down vote up
@Test
public void exportByMetricNameWithOneMetricMultipleTags() {
    JsonMetadataExporter exporter = new JsonMetadataExporter();
    MetricRegistry applicationRegistry = MetricRegistries.get(MetricRegistry.Type.APPLICATION);
    applicationRegistry.counter("counter1",
            new Tag("key1", "value1"),
            new Tag("color", "blue"));

    String result = exporter.exportMetricsByName(MetricRegistry.Type.APPLICATION, "counter1").toString();
    JsonObject json = Json.createReader(new StringReader(result)).read().asJsonObject();

    JsonArray outerTagsArray = json.getJsonObject("counter1").getJsonArray("tags");
    assertEquals(1, outerTagsArray.size());
    JsonArray innerArray = outerTagsArray.getJsonArray(0);
    assertEquals(2, innerArray.size());
    assertEquals("color=blue", innerArray.getString(0));
    assertEquals("key1=value1", innerArray.getString(1));
}
 
Example #9
Source File: JsonExporterTest.java    From smallrye-metrics with Apache License 2.0 6 votes vote down vote up
@Test
public void testGauges() {
    JsonExporter exporter = new JsonExporter();
    MetricRegistry registry = MetricRegistries.get(MetricRegistry.Type.APPLICATION);

    Gauge<Long> gaugeWithoutTags = () -> 1L;
    Gauge<Long> gaugeRed = () -> 2L;
    Gauge<Long> gaugeBlue = () -> 3L;

    final Metadata metadata = new MetadataBuilder()
            .withType(MetricType.GAUGE)
            .withName("mygauge")
            .build();

    registry.register(metadata, gaugeWithoutTags);
    registry.register(metadata, gaugeRed, new Tag("color", "red"));
    registry.register(metadata, gaugeBlue, new Tag("color", "blue"), new Tag("foo", "bar"));

    String result = exporter.exportMetricsByName(MetricRegistry.Type.APPLICATION, "mygauge").toString();
    System.out.println(result);
    JsonObject json = Json.createReader(new StringReader(result)).read().asJsonObject();

    assertEquals(1, json.getInt("mygauge"));
    assertEquals(2, json.getInt("mygauge;color=red"));
    assertEquals(3, json.getInt("mygauge;color=blue;foo=bar"));
}
 
Example #10
Source File: ExportersMetricScalingTest.java    From smallrye-metrics with Apache License 2.0 6 votes vote down vote up
/**
 * Given a Timer with unit=MINUTES,
 * check that the statistics from OpenMetricsExporter will be correctly converted to SECONDS.
 */
@Test
public void timer_openMetrics() {
    MetricRegistry registry = MetricRegistries.get(MetricRegistry.Type.APPLICATION);
    Metadata metadata = Metadata.builder()
            .withName("timer1")
            .withType(MetricType.TIMER)
            .withUnit(MetricUnits.MINUTES)
            .build();
    Timer metric = registry.timer(metadata);
    metric.update(Duration.ofHours(1));
    metric.update(Duration.ofHours(2));
    metric.update(Duration.ofHours(3));

    OpenMetricsExporter exporter = new OpenMetricsExporter();
    String exported = exporter.exportOneMetric(MetricRegistry.Type.APPLICATION, new MetricID("timer1")).toString();

    Assert.assertThat(exported, containsString("application_timer1_seconds{quantile=\"0.5\"} 7200.0"));
    Assert.assertThat(exported, containsString("application_timer1_mean_seconds 7200.0"));
    Assert.assertThat(exported, containsString("application_timer1_min_seconds 3600.0"));
    Assert.assertThat(exported, containsString("application_timer1_max_seconds 10800.0"));
}
 
Example #11
Source File: ExportersMetricScalingTest.java    From smallrye-metrics with Apache License 2.0 6 votes vote down vote up
/**
 * Given a Timer with unit=MINUTES,
 * check that the statistics from JsonExporter will be presented in MINUTES.
 */
@Test
public void timer_json() {
    MetricRegistry registry = MetricRegistries.get(MetricRegistry.Type.APPLICATION);
    Metadata metadata = Metadata.builder()
            .withName("timer1")
            .withType(MetricType.TIMER)
            .withUnit(MetricUnits.MINUTES)
            .build();
    Timer metric = registry.timer(metadata);
    metric.update(Duration.ofHours(1));
    metric.update(Duration.ofHours(2));
    metric.update(Duration.ofHours(3));

    JsonExporter exporter = new JsonExporter();
    String exported = exporter.exportOneMetric(MetricRegistry.Type.APPLICATION, new MetricID("timer1")).toString();

    JsonObject json = Json.createReader(new StringReader(exported)).read().asJsonObject().getJsonObject("timer1");
    assertEquals(120.0, json.getJsonNumber("p50").doubleValue(), 0.001);
    assertEquals(120.0, json.getJsonNumber("mean").doubleValue(), 0.001);
    assertEquals(60.0, json.getJsonNumber("min").doubleValue(), 0.001);
    assertEquals(180.0, json.getJsonNumber("max").doubleValue(), 0.001);
    assertEquals(360.0, json.getJsonNumber("elapsedTime").doubleValue(), 0.001);
}
 
Example #12
Source File: ExportersMetricScalingTest.java    From smallrye-metrics with Apache License 2.0 6 votes vote down vote up
/**
 * Given a Histogram with unit=MINUTES,
 * check that the statistics from OpenMetricsExporter will be presented in SECONDS.
 */
@Test
public void histogram_openMetrics() {
    MetricRegistry registry = MetricRegistries.get(MetricRegistry.Type.APPLICATION);
    Metadata metadata = Metadata.builder()
            .withName("histogram1")
            .withType(MetricType.HISTOGRAM)
            .withUnit(MetricUnits.MINUTES)
            .build();
    Histogram metric = registry.histogram(metadata);
    metric.update(30);
    metric.update(40);
    metric.update(50);

    OpenMetricsExporter exporter = new OpenMetricsExporter();
    String exported = exporter.exportOneMetric(MetricRegistry.Type.APPLICATION, new MetricID("histogram1")).toString();

    Assert.assertThat(exported, containsString("application_histogram1_min_seconds 1800.0"));
    Assert.assertThat(exported, containsString("application_histogram1_max_seconds 3000.0"));
    Assert.assertThat(exported, containsString("application_histogram1_mean_seconds 2400.0"));
    Assert.assertThat(exported, containsString("application_histogram1_seconds{quantile=\"0.5\"} 2400.0"));
}
 
Example #13
Source File: ExportersMetricScalingTest.java    From smallrye-metrics with Apache License 2.0 6 votes vote down vote up
/**
 * Given a Histogram with unit=dollars (custom unit),
 * check that the statistics from OpenMetricsExporter will be presented in dollars.
 */
@Test
public void histogram_customunit_openMetrics() {
    MetricRegistry registry = MetricRegistries.get(MetricRegistry.Type.APPLICATION);
    Metadata metadata = Metadata.builder()
            .withName("histogram1")
            .withType(MetricType.HISTOGRAM)
            .withUnit("dollars")
            .build();
    Histogram metric = registry.histogram(metadata);
    metric.update(30);
    metric.update(40);
    metric.update(50);

    OpenMetricsExporter exporter = new OpenMetricsExporter();
    String exported = exporter.exportOneMetric(MetricRegistry.Type.APPLICATION, new MetricID("histogram1")).toString();

    Assert.assertThat(exported, containsString("application_histogram1_min_dollars 30.0"));
    Assert.assertThat(exported, containsString("application_histogram1_max_dollars 50.0"));
    Assert.assertThat(exported, containsString("application_histogram1_mean_dollars 40.0"));
    Assert.assertThat(exported, containsString("application_histogram1_dollars{quantile=\"0.5\"} 40.0"));
}
 
Example #14
Source File: ExportersMetricScalingTest.java    From smallrye-metrics with Apache License 2.0 6 votes vote down vote up
/**
 * Given a Histogram with unit=MINUTES,
 * check that the statistics from JsonExporter will be presented in MINUTES.
 */
@Test
public void histogram_json() {
    MetricRegistry registry = MetricRegistries.get(MetricRegistry.Type.APPLICATION);
    Metadata metadata = Metadata.builder()
            .withName("timer1")
            .withType(MetricType.TIMER)
            .withUnit(MetricUnits.MINUTES)
            .build();
    Timer metric = registry.timer(metadata);
    metric.update(Duration.ofHours(1));
    metric.update(Duration.ofHours(2));
    metric.update(Duration.ofHours(3));

    JsonExporter exporter = new JsonExporter();
    String exported = exporter.exportOneMetric(MetricRegistry.Type.APPLICATION, new MetricID("timer1")).toString();

    JsonObject json = Json.createReader(new StringReader(exported)).read().asJsonObject().getJsonObject("timer1");
    assertEquals(120.0, json.getJsonNumber("p50").doubleValue(), 0.001);
    assertEquals(120.0, json.getJsonNumber("mean").doubleValue(), 0.001);
    assertEquals(60.0, json.getJsonNumber("min").doubleValue(), 0.001);
    assertEquals(180.0, json.getJsonNumber("max").doubleValue(), 0.001);
}
 
Example #15
Source File: JsonExporterTest.java    From smallrye-metrics with Apache License 2.0 6 votes vote down vote up
@Test
public void testExportOfDifferentHistogramImplementations() {

    JsonExporter exporter = new JsonExporter();
    MetricRegistry applicationRegistry = MetricRegistries.get(MetricRegistry.Type.APPLICATION);

    // the export should behave identical for any class derived from Histogram
    Histogram[] histograms = { new HistogramImpl(new ExponentiallyDecayingReservoir()), new SomeHistogram() };
    int idx = 0;
    for (Histogram h : histograms) {
        String name = "histo_" + idx++;
        applicationRegistry.register(name, h);
        StringBuilder out = exporter.exportOneMetric(MetricRegistry.Type.APPLICATION, new MetricID(name));
        assertNotNull(out);
        System.out.println(out.toString());
        List<String> lines = Arrays.asList(out.toString().split(LINE_SEPARATOR));
        assertEquals(1, lines.stream().filter(line -> line.contains("\"" + name + "\"")).count());
        assertEquals(1, lines.stream().filter(line -> line.contains("\"count\": 0")).count());
    }
}
 
Example #16
Source File: ExportersMetricScalingTest.java    From smallrye-metrics with Apache License 2.0 6 votes vote down vote up
/**
 * Given a Counter,
 * check that the statistics from JsonExporter will not be scaled in any way.
 */
@Test
public void counter_json() {
    MetricRegistry registry = MetricRegistries.get(MetricRegistry.Type.APPLICATION);
    Metadata metadata = Metadata.builder()
            .withName("counter1")
            .withType(MetricType.COUNTER)
            .build();
    Counter metric = registry.counter(metadata);
    metric.inc(10);
    metric.inc(20);
    metric.inc(30);

    JsonExporter exporter = new JsonExporter();
    String exported = exporter.exportOneMetric(MetricRegistry.Type.APPLICATION, new MetricID("counter1")).toString();

    JsonObject json = Json.createReader(new StringReader(exported)).read().asJsonObject();
    assertEquals(60, json.getInt("counter1"));
}
 
Example #17
Source File: SmallRyeMetricsRecorder.java    From quarkus with Apache License 2.0 6 votes vote down vote up
public void registerMetricFromProducer(String beanId, MetricType metricType,
        String metricName, String[] tags, String description,
        String displayName, String unit) {
    ArcContainer container = Arc.container();
    InjectableBean<Object> injectableBean = container.bean(beanId);
    BeanManager beanManager = container.beanManager();
    Metric reference = (Metric) beanManager.getReference(injectableBean, Metric.class,
            beanManager.createCreationalContext(injectableBean));
    MetricRegistry registry = MetricRegistries.get(MetricRegistry.Type.APPLICATION);
    Metadata metadata = Metadata.builder()
            .withType(metricType)
            .withName(metricName)
            .withDescription(description)
            .withDisplayName(displayName)
            .withUnit(unit)
            .notReusable()
            .build();
    registry.register(metadata, reference, TagsUtils.parseTagsAsArray(tags));
}
 
Example #18
Source File: ExportersMetricScalingTest.java    From smallrye-metrics with Apache License 2.0 6 votes vote down vote up
/**
 * Given a Meter,
 * check that the statistics from JsonExporter will be presented as per_second.
 */
@Test
public void meter_json() throws InterruptedException {
    MetricRegistry registry = MetricRegistries.get(MetricRegistry.Type.APPLICATION);
    Metadata metadata = Metadata.builder()
            .withName("meter1")
            .withType(MetricType.METERED)
            .build();
    Meter metric = registry.meter(metadata);
    metric.mark(10);
    TimeUnit.SECONDS.sleep(1);

    JsonExporter exporter = new JsonExporter();
    String exported = exporter.exportOneMetric(MetricRegistry.Type.APPLICATION, new MetricID("meter1")).toString();

    JsonObject json = Json.createReader(new StringReader(exported)).read().asJsonObject().getJsonObject("meter1");
    assertEquals(10, json.getInt("count"));
    double meanRate = json.getJsonNumber("meanRate").doubleValue();
    Assert.assertTrue("meanRate should be between 1 and 10 but is " + meanRate,
            meanRate > 1 && meanRate < 10);
}
 
Example #19
Source File: ExportersMetricScalingTest.java    From smallrye-metrics with Apache License 2.0 6 votes vote down vote up
/**
 * Given a Gauge with unit=MINUTES,
 * check that the statistics from OpenMetricsExporter will be presented in SECONDS.
 */
@Test
public void gauge_openMetrics() {
    MetricRegistry registry = MetricRegistries.get(MetricRegistry.Type.APPLICATION);
    Metadata metadata = Metadata.builder()
            .withName("gauge1")
            .withType(MetricType.GAUGE)
            .withUnit(MetricUnits.MINUTES)
            .build();
    Gauge<Long> gaugeInstance = () -> 3L;
    registry.register(metadata, gaugeInstance);

    OpenMetricsExporter exporter = new OpenMetricsExporter();
    String exported = exporter.exportOneMetric(MetricRegistry.Type.APPLICATION, new MetricID("gauge1")).toString();

    Assert.assertThat(exported, containsString("application_gauge1_seconds 180.0"));
}
 
Example #20
Source File: ExportersMetricScalingTest.java    From smallrye-metrics with Apache License 2.0 6 votes vote down vote up
/**
 * Given a Gauge with unit=MINUTES,
 * check that the statistics from OpenMetricsExporter will be presented in MINUTES.
 */
@Test
public void gauge_json() {
    MetricRegistry registry = MetricRegistries.get(MetricRegistry.Type.APPLICATION);
    Metadata metadata = Metadata.builder()
            .withName("gauge1")
            .withType(MetricType.GAUGE)
            .withUnit(MetricUnits.MINUTES)
            .build();
    Gauge<Long> gaugeInstance = () -> 3L;
    registry.register(metadata, gaugeInstance);

    JsonExporter exporter = new JsonExporter();
    String exported = exporter.exportOneMetric(MetricRegistry.Type.APPLICATION, new MetricID("gauge1")).toString();

    JsonObject json = Json.createReader(new StringReader(exported)).read().asJsonObject();
    assertEquals(3, json.getInt("gauge1"));
}
 
Example #21
Source File: JsonExporterTest.java    From smallrye-metrics with Apache License 2.0 6 votes vote down vote up
@Test
public void testExportOfDifferentMeterImplementations() {

    JsonExporter exporter = new JsonExporter();
    MetricRegistry applicationRegistry = MetricRegistries.get(MetricRegistry.Type.APPLICATION);

    // the export should behave identical for any class derived from Meter
    Meter[] meters = { new MeterImpl(), new SomeMeter() };
    int idx = 0;
    for (Meter m : meters) {
        String name = "meter_" + idx++;
        applicationRegistry.register(name, m);
        StringBuilder out = exporter.exportOneMetric(MetricRegistry.Type.APPLICATION, new MetricID(name));
        assertNotNull(out);
        List<String> lines = Arrays.asList(out.toString().split(LINE_SEPARATOR));
        assertEquals(1, lines.stream().filter(line -> line.contains("\"" + name + "\"")).count());
        assertEquals(1, lines.stream().filter(line -> line.contains("\"count\": 0")).count());
    }
}
 
Example #22
Source File: OpenMetricsExporterTest.java    From smallrye-metrics with Apache License 2.0 6 votes vote down vote up
@Test
public void testUptimeGaugeUnitConversion() {
    OpenMetricsExporter exporter = new OpenMetricsExporter();
    MetricRegistry baseRegistry = MetricRegistries.get(MetricRegistry.Type.BASE);

    Gauge gauge = new MGaugeImpl(JmxWorker.instance(), "java.lang:type=Runtime/Uptime");
    Metadata metadata = new ExtendedMetadata("jvm.uptime", "display name", "description", MetricType.GAUGE, "milliseconds");
    baseRegistry.register(metadata, gauge);

    long actualUptime /* in ms */ = ManagementFactory.getRuntimeMXBean().getUptime();
    double actualUptimeInSeconds = actualUptime / 1000.0;

    StringBuilder out = exporter.exportOneMetric(MetricRegistry.Type.BASE, new MetricID("jvm.uptime"));
    assertNotNull(out);

    double valueFromOpenMetrics = -1;
    for (String line : out.toString().split(System.getProperty("line.separator"))) {
        if (line.startsWith("base_jvm_uptime_seconds")) {
            valueFromOpenMetrics /* in seconds */ = Double
                    .valueOf(line.substring("base:jvm_uptime_seconds".length()).trim());
        }
    }
    assertTrue("Value should not be -1", valueFromOpenMetrics != -1);
    assertTrue(valueFromOpenMetrics >= actualUptimeInSeconds);
}
 
Example #23
Source File: OpenMetricsExporterTest.java    From smallrye-metrics with Apache License 2.0 6 votes vote down vote up
@Test
public void testExportOfDifferentMeterImplementations() {

    OpenMetricsExporter exporter = new OpenMetricsExporter();
    MetricRegistry applicationRegistry = MetricRegistries.get(MetricRegistry.Type.APPLICATION);

    // the export should behave identical for any class derived from Meter
    Meter[] meters = { new MeterImpl(), new SomeMeter() };
    int idx = 0;
    for (Meter m : meters) {
        String name = "meter_" + idx++;
        applicationRegistry.register(name, m);
        String out = exporter.exportOneMetric(MetricRegistry.Type.APPLICATION, new MetricID(name)).toString();
        String expectedLine = "application_" + name + "_total 0.0";
        assertThat(out, containsString(expectedLine));
    }
}
 
Example #24
Source File: OpenMetricsExporterTest.java    From smallrye-metrics with Apache License 2.0 6 votes vote down vote up
@Test
public void testExportOfDifferentHistogramImplementations() {

    OpenMetricsExporter exporter = new OpenMetricsExporter();
    MetricRegistry applicationRegistry = MetricRegistries.get(MetricRegistry.Type.APPLICATION);

    // the export should behave identical for any class derived from Histogram
    Histogram[] histograms = { new HistogramImpl(new ExponentiallyDecayingReservoir()), new SomeHistogram() };
    int idx = 0;
    for (Histogram h : histograms) {
        String name = "histo_" + idx++;
        applicationRegistry.register(name, h);
        String out = exporter.exportOneMetric(MetricRegistry.Type.APPLICATION, new MetricID(name)).toString();
        String expectedLine = "application_" + name + "_mean 0.0";
        assertThat(out, containsString(expectedLine));
    }
}
 
Example #25
Source File: OpenMetricsExporterTest.java    From smallrye-metrics with Apache License 2.0 6 votes vote down vote up
@Test
public void testExportOfDifferentTimerImplementations() {

    OpenMetricsExporter exporter = new OpenMetricsExporter();
    MetricRegistry applicationRegistry = MetricRegistries.get(MetricRegistry.Type.APPLICATION);

    // the export should behave identical for any class derived from Timer
    Timer[] timers = { new TimerImpl(), new SomeTimer() };
    int idx = 0;
    for (Timer t : timers) {
        String name = "json_timer_" + idx++;
        applicationRegistry.register(name, t);
        String out = exporter.exportOneMetric(MetricRegistry.Type.APPLICATION, new MetricID(name)).toString();
        String expectedLine = "application_" + name + "_rate_per_second 0.0";
        assertThat(out, containsString(expectedLine));
    }
}
 
Example #26
Source File: OpenMetricsExporterTest.java    From smallrye-metrics with Apache License 2.0 6 votes vote down vote up
@Test
public void testSkippingOfScope() {
    OpenMetricsExporter exporter = new OpenMetricsExporter();
    MetricRegistry registry = MetricRegistries.get(MetricRegistry.Type.APPLICATION);

    Metadata metadata = new ExtendedMetadata("foo",
            "foo",
            "FooDescription",
            MetricType.COUNTER,
            "volts",
            null,
            false,
            Optional.of(true),
            true,
            null);
    Tag tag = new Tag("a", "b");
    registry.counter(metadata, tag);

    String result = exporter.exportOneScope(MetricRegistry.Type.APPLICATION).toString();
    System.out.println(result);
    assertHasHelpLineExactlyOnce(result, "foo_total", "FooDescription");
    assertHasTypeLineExactlyOnce(result, "foo_total", "counter");
    assertHasValueLineExactlyOnce(result, "foo_total_volts", "0.0", tag);
}
 
Example #27
Source File: OpenMetricsExporterTest.java    From smallrye-metrics with Apache License 2.0 6 votes vote down vote up
/**
 * In OpenMetrics exporter and counters, if the metric name does not end with _total, then _total should be appended
 * automatically.
 * If it ends with _total, nothing extra will be appended.
 */
@Test
public void testAppendingOfTotal() {
    OpenMetricsExporter exporter = new OpenMetricsExporter();
    MetricRegistry registry = MetricRegistries.get(MetricRegistry.Type.APPLICATION);
    Tag tag = new Tag("a", "b");

    // in this case _total should be appended
    registry.counter("counter1", tag);
    String export = exporter.exportOneMetric(MetricRegistry.Type.APPLICATION, new MetricID("counter1", tag)).toString();
    assertThat(export, containsString("application_counter1_total{a=\"b\"}"));

    // in this case _total should NOT be appended
    registry.counter("counter2_total", tag);
    export = exporter.exportOneMetric(MetricRegistry.Type.APPLICATION, new MetricID("counter2_total", tag)).toString();
    assertThat(export, containsString("application_counter2_total{a=\"b\"}"));

}
 
Example #28
Source File: OpenMetricsExporterTest.java    From smallrye-metrics with Apache License 2.0 6 votes vote down vote up
@Test
public void testNameOverride() {
    OpenMetricsExporter exporter = new OpenMetricsExporter();
    MetricRegistry registry = MetricRegistries.get(MetricRegistry.Type.APPLICATION);

    Metadata metadata = new ExtendedMetadata("voltage1",
            "Voltage",
            "Measures your electric potential",
            MetricType.GAUGE,
            "volts",
            null,
            false,
            Optional.of(true),
            false,
            "baz");
    Tag tag = new Tag("a", "b");
    registry.register(metadata, (Gauge<Long>) () -> 3L, tag);

    String result = exporter.exportOneScope(MetricRegistry.Type.APPLICATION).toString();
    System.out.println(result);
    assertHasHelpLineExactlyOnce(result, "application_baz", "Measures your electric potential");
    assertHasTypeLineExactlyOnce(result, "application_baz", "gauge");
    assertHasValueLineExactlyOnce(result, "application_baz", "3.0", tag);
}
 
Example #29
Source File: OpenMetricsExporterTest.java    From smallrye-metrics with Apache License 2.0 6 votes vote down vote up
/**
 * Test the cases where OpenMetrics keys are different from metric names.
 * For example, with a counter named gc.time, the dot will be converted to an underscore.
 * This is mainly to make sure that we don't accidentally log the HELP and TYPE lines multiple times if there are
 * multiple metrics under such name.
 */
@Test
public void testMetricsWhereKeysAreDifferentFromNames() {
    OpenMetricsExporter exporter = new OpenMetricsExporter();
    MetricRegistry registry = MetricRegistries.get(MetricRegistry.Type.APPLICATION);

    Metadata metadata = Metadata.builder()
            .withName("metric.a")
            .withType(MetricType.COUNTER)
            .withDescription("Great")
            .build();
    Tag tag1 = new Tag("tag1", "value1");
    Tag tag2 = new Tag("tag1", "value2");
    registry.counter(metadata, tag1);
    registry.counter(metadata, tag2);

    String result = exporter.exportAllScopes().toString();
    System.out.println(result);
    assertHasHelpLineExactlyOnce(result, "application_metric_a_total", "Great");
    assertHasTypeLineExactlyOnce(result, "application_metric_a_total", "counter");
}
 
Example #30
Source File: OpenMetricsExporterTest.java    From smallrye-metrics with Apache License 2.0 6 votes vote down vote up
@Test
public void exportGauges() {
    OpenMetricsExporter exporter = new OpenMetricsExporter();
    MetricRegistry registry = MetricRegistries.get(MetricRegistry.Type.APPLICATION);

    Metadata metadata = Metadata
            .builder()
            .withType(MetricType.GAUGE)
            .withName("mygauge")
            .withDescription("awesome")
            .build();
    Tag blueTag = new Tag("color", "blue");
    registry.register(metadata, (Gauge<Long>) () -> 42L, blueTag);
    Tag greenTag = new Tag("color", "green");
    registry.register(metadata, (Gauge<Long>) () -> 26L, greenTag);

    String result = exporter.exportMetricsByName(MetricRegistry.Type.APPLICATION, "mygauge").toString();
    System.out.println(result);

    assertHasTypeLineExactlyOnce(result, "application_mygauge", "gauge");
    assertHasHelpLineExactlyOnce(result, "application_mygauge", "awesome");

    assertHasValueLineExactlyOnce(result, "application_mygauge", "42.0", blueTag);
    assertHasValueLineExactlyOnce(result, "application_mygauge", "26.0", greenTag);
}