Java Code Examples for org.eclipse.microprofile.metrics.MetricRegistry#counter()

The following examples show how to use org.eclipse.microprofile.metrics.MetricRegistry#counter() . 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 6 votes vote down vote up
/**
 * Given a Counter,
 * check that the statistics from OpenMetricsExporter will not be scaled in any way.
 */
@Test
public void counter_openMetrics() {
    MetricRegistry registry = MetricRegistries.get(MetricRegistry.Type.APPLICATION);
    Metadata metadata = Metadata.builder()
            .withName("counter1")
            .withType(MetricType.COUNTER)
            .build();
    Counter metric = registry.counter(metadata);
    metric.inc(30);
    metric.inc(40);
    metric.inc(50);

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

    Assert.assertThat(exported, containsString("application_counter1_total 120.0"));
}
 
Example 2
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 3
Source File: JsonExporterTest.java    From smallrye-metrics with Apache License 2.0 6 votes vote down vote up
@Test
public void testCounters() {
    JsonExporter exporter = new JsonExporter();
    MetricRegistry registry = MetricRegistries.get(MetricRegistry.Type.APPLICATION);

    Counter counterWithoutTags = registry.counter("mycounter");
    Counter counterRed = registry.counter("mycounter", new Tag("color", "red"));
    Counter counterBlue = registry.counter("mycounter", new Tag("color", "blue"), new Tag("foo", "bar"));

    counterWithoutTags.inc(1);
    counterRed.inc(2);
    counterBlue.inc(3);

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

    assertEquals(1, json.getInt("mycounter"));
    assertEquals(2, json.getInt("mycounter;color=red"));
    assertEquals(3, json.getInt("mycounter;color=blue;foo=bar"));
}
 
Example 4
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 5
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 6
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 7
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 8
Source File: OpenMetricsExporterTest.java    From smallrye-metrics with Apache License 2.0 5 votes vote down vote up
@Test
public void exportCounters() {
    OpenMetricsExporter exporter = new OpenMetricsExporter();
    MetricRegistry registry = MetricRegistries.get(MetricRegistry.Type.APPLICATION);

    Metadata metadata = Metadata
            .builder()
            .withType(MetricType.COUNTER)
            .withName("mycounter")
            .withDescription("awesome")
            .build();
    Tag blueTag = new Tag("color", "blue");
    Counter blueCounter = registry.counter(metadata, blueTag);
    Tag greenTag = new Tag("color", "green");
    Counter greenCounter = registry.counter(metadata, greenTag);

    blueCounter.inc(10);
    greenCounter.inc(20);

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

    assertHasTypeLineExactlyOnce(result, "application_mycounter_total", "counter");
    assertHasHelpLineExactlyOnce(result, "application_mycounter_total", "awesome");

    assertHasValueLineExactlyOnce(result, "application_mycounter_total", "10.0", blueTag);
    assertHasValueLineExactlyOnce(result, "application_mycounter_total", "20.0", greenTag);

}
 
Example 9
Source File: OpenMetricsExporterTest.java    From smallrye-metrics with Apache License 2.0 5 votes vote down vote up
/**
 * Test prependsScopeToOpenMetricsName in the ExtendedMetadata
 */
@Test
public void testMicroProfileScopeInTagsWithExtendedMetadata() {

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

    Metadata metadata = new ExtendedMetadata("mycounter", "mycounter", "awesome", MetricType.COUNTER,
            "none", null, false, Optional.of(false));
    Tag colourTag = new Tag("color", "blue");
    Counter counterWithTag = registry.counter(metadata, colourTag);
    Counter counterWithoutTag = registry.counter(metadata);

    counterWithTag.inc(10);
    counterWithoutTag.inc(20);

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

    Tag microProfileScopeTag = new Tag("microprofile_scope", MetricRegistry.Type.APPLICATION.getName().toLowerCase());

    assertHasTypeLineExactlyOnce(result, "mycounter_total", "counter");
    assertHasHelpLineExactlyOnce(result, "mycounter_total", "awesome");

    assertHasValueLineExactlyOnce(result, "mycounter_total", "10.0", colourTag, microProfileScopeTag);
    assertHasValueLineExactlyOnce(result, "mycounter_total", "20.0", microProfileScopeTag);
}
 
Example 10
Source File: OpenMetricsExporterTest.java    From smallrye-metrics with Apache License 2.0 5 votes vote down vote up
/**
 * OpenMetrics exporter should only emit a HELP line if a description exists and is not empty
 */
@Test
public void testEmptyDescription() {
    OpenMetricsExporter exporter = new OpenMetricsExporter();
    MetricRegistry registry = MetricRegistries.get(MetricRegistry.Type.APPLICATION);

    Metadata metadata = Metadata.builder()
            .withName("counter_with_empty_description")
            .withDescription("").build();
    registry.counter(metadata);
    String export = exporter
            .exportOneMetric(MetricRegistry.Type.APPLICATION, new MetricID("counter_with_empty_description")).toString();
    assertThat(export, not(containsString("HELP")));
}
 
Example 11
Source File: OpenMetricsExporterTest.java    From smallrye-metrics with Apache License 2.0 5 votes vote down vote up
@Test
public void testHelpLineQuoting() {
    OpenMetricsExporter exporter = new OpenMetricsExporter();
    MetricRegistry registry = MetricRegistries.get(MetricRegistry.Type.APPLICATION);

    Metadata metadata = Metadata.builder()
            .withName("counter_with_complicated_description")
            .withDescription("hhh\\ggg\\nfff\\").build();
    registry.counter(metadata);
    String export = exporter
            .exportOneMetric(MetricRegistry.Type.APPLICATION, new MetricID("counter_with_complicated_description"))
            .toString();

    // hhh\ggg\nfff\ should become hhh\\ggg\nfff\\
    assertThat(export,
            containsString("# HELP application_counter_with_complicated_description_total hhh\\\\ggg\\nfff\\\\"));

    metadata = Metadata.builder()
            .withName("counter_with_complicated_description_2")
            .withDescription("description with \"quotes\"").build();
    registry.counter(metadata);
    export = exporter
            .exportOneMetric(MetricRegistry.Type.APPLICATION, new MetricID("counter_with_complicated_description_2"))
            .toString();

    // double quotes should stay unchanged
    assertThat(export,
            containsString("# HELP application_counter_with_complicated_description_2_total description with \"quotes\""));
}
 
Example 12
Source File: OpenMetricsExporterTest.java    From smallrye-metrics with Apache License 2.0 5 votes vote down vote up
@Test
public void testNewlineCharacterEscaping() {
    OpenMetricsExporter exporter = new OpenMetricsExporter();
    MetricRegistry registry = MetricRegistries.get(MetricRegistry.Type.APPLICATION);

    Tag tag = new Tag("a", "b\nc");
    registry.counter("mycounter", tag);

    String result = exporter.exportOneScope(MetricRegistry.Type.APPLICATION).toString();
    assertTrue(result.contains("application_mycounter_total{a=\"b\\nc\"} 0.0"));
}
 
Example 13
Source File: JsonExporterTest.java    From smallrye-metrics with Apache License 2.0 5 votes vote down vote up
@Test
public void testSemicolonInTagValue() {
    JsonExporter exporter = new JsonExporter();
    MetricRegistry registry = MetricRegistries.get(MetricRegistry.Type.APPLICATION);

    registry.counter("counter1",
            new Tag("tag1", "i;have;semicolons"),
            new Tag("tag2", "i;have;semicolons;as;well"));

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

    assertNotNull("Semicolons in tag values should be converted to underscores",
            json.getJsonNumber("counter1;tag1=i_have_semicolons;tag2=i_have_semicolons_as_well"));
}
 
Example 14
Source File: JsonExporterTest.java    From smallrye-metrics with Apache License 2.0 5 votes vote down vote up
@Test
public void testNewlineCharacterInTagValue() {
    JsonExporter exporter = new JsonExporter();
    MetricRegistry registry = MetricRegistries.get(MetricRegistry.Type.APPLICATION);

    registry.counter("counter1",
            new Tag("tag1", "i_have\n_two_lines"));

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

    assertEquals("Newline chars in tag values should be escaped as \\n",
            "counter1;tag1=i_have\n_two_lines", json.keySet().stream().findFirst().get());
}
 
Example 15
Source File: JsonMetadataExporterTest.java    From smallrye-metrics with Apache License 2.0 5 votes vote down vote up
@Test
public void exportByMetricNameWithOneMetricSingleTag() {
    JsonMetadataExporter exporter = new JsonMetadataExporter();
    MetricRegistry applicationRegistry = MetricRegistries.get(MetricRegistry.Type.APPLICATION);
    applicationRegistry.counter("counter1", new Tag("key1", "value1"));

    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(1, innerArray.size());
    assertEquals("key1=value1", innerArray.getString(0));
}
 
Example 16
Source File: JsonMetadataExporterTest.java    From smallrye-metrics with Apache License 2.0 5 votes vote down vote up
@Test
public void exportByScopeWithMultipleMetrics() {
    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("counter2", new Tag("color", "red"));

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

    // check items for counter1
    JsonArray outerTagsArray = json.getJsonObject("counter1").getJsonArray("tags");
    assertEquals(2, 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));

    // check items for counter2
    outerTagsArray = json.getJsonObject("counter2").getJsonArray("tags");
    assertEquals(1, outerTagsArray.size());

    innerArray1 = outerTagsArray.getJsonArray(0);
    assertEquals(1, innerArray1.size());
    assertEquals("color=red", innerArray1.getString(0));
}
 
Example 17
Source File: OpenMetricsExporterTest.java    From smallrye-metrics with Apache License 2.0 4 votes vote down vote up
/**
 * Test that setting the config property smallrye.metrics.usePrefixForScope to false put the scope in the tags instead
 * of prefixing the metric name with it.
 */
@Test
public void testMicroProfileScopeInTags() {

    String previousConfigValue = System.getProperty(SMALLRYE_METRICS_USE_PREFIX_FOR_SCOPE);
    try {
        System.setProperty(SMALLRYE_METRICS_USE_PREFIX_FOR_SCOPE, FALSE.toString());

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

        Metadata metadata = Metadata
                .builder()
                .withType(MetricType.COUNTER)
                .withName("mycounter")
                .withDescription("awesome")
                .build();
        Tag colourTag = new Tag("color", "blue");
        Counter counterWithTag = registry.counter(metadata, colourTag);
        Counter counterWithoutTag = registry.counter(metadata);

        counterWithTag.inc(10);
        counterWithoutTag.inc(20);

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

        Tag microProfileScopeTag = new Tag("microprofile_scope", MetricRegistry.Type.APPLICATION.getName().toLowerCase());

        assertHasTypeLineExactlyOnce(result, "mycounter_total", "counter");
        assertHasHelpLineExactlyOnce(result, "mycounter_total", "awesome");

        assertHasValueLineExactlyOnce(result, "mycounter_total", "10.0", colourTag, microProfileScopeTag);
        assertHasValueLineExactlyOnce(result, "mycounter_total", "20.0", microProfileScopeTag);

    } finally {
        if (previousConfigValue != null) {
            System.setProperty(SMALLRYE_METRICS_USE_PREFIX_FOR_SCOPE, previousConfigValue);
        } else {
            System.clearProperty(SMALLRYE_METRICS_USE_PREFIX_FOR_SCOPE);
        }
    }
}
 
Example 18
Source File: MetricProducerMethodBean.java    From microprofile-metrics with Apache License 2.0 4 votes vote down vote up
@Produces
@Metric(name = "not_registered_metric")
Counter notRegisteredMetric(MetricRegistry registry, InjectionPoint ip) {
    return registry.counter("not_registered_metric");
}
 
Example 19
Source File: MetricsTest.java    From smallrye-reactive-messaging with Apache License 2.0 4 votes vote down vote up
private Counter getCounter(String channelName) {
    MetricRegistry registry = container.select(MetricRegistry.class).get();
    return registry.counter("mp.messaging.message.count", new Tag("channel", channelName));
}
 
Example 20
Source File: SmallRyeMetricsRecorder.java    From quarkus with Apache License 2.0 4 votes vote down vote up
public void registerMetric(MetricRegistry.Type scope,
        MetadataHolder metadataHolder,
        TagHolder[] tagHolders,
        Object implementor) {
    Metadata metadata = metadataHolder.toMetadata();
    Tag[] tags = Arrays.stream(tagHolders).map(TagHolder::toTag).toArray(Tag[]::new);
    MetricRegistry registry = MetricRegistries.get(scope);

    switch (metadata.getTypeRaw()) {
        case GAUGE:
            registry.register(metadata, (Gauge) implementor, tags);
            break;
        case TIMER:
            if (implementor == null) {
                registry.timer(metadata, tags);
            } else {
                registry.register(metadata, (Timer) implementor);
            }
            break;
        case COUNTER:
            if (implementor == null) {
                registry.counter(metadata, tags);
            } else {
                registry.register(metadata, (Counter) implementor, tags);
            }
            break;
        case HISTOGRAM:
            if (implementor == null) {
                registry.histogram(metadata, tags);
            } else {
                registry.register(metadata, (Histogram) implementor, tags);
            }
            break;
        case CONCURRENT_GAUGE:
            if (implementor == null) {
                registry.concurrentGauge(metadata, tags);
            } else {
                registry.register(metadata, (ConcurrentGauge) implementor, tags);
            }
            break;
        case METERED:
            if (implementor == null) {
                registry.meter(metadata, tags);
            } else {
                registry.register(metadata, (Metered) implementor, tags);
            }
            break;
        case INVALID:
            break;
        default:
            break;
    }
}