Java Code Examples for org.eclipse.microprofile.metrics.Counter#inc()

The following examples show how to use org.eclipse.microprofile.metrics.Counter#inc() . 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: 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 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: MetricProducerFieldBeanTest.java    From microprofile-metrics with Apache License 2.0 6 votes vote down vote up
@Test
@InSequence(2)
public void incrementCountersFromRegistry() {
    assertThat("Counters are not registered correctly", registry.getCounters(),
        allOf(
            hasKey(counter1MID),
            hasKey(counter2MID),
            not(hasKey(notRegMID))
        )
    );
    Counter counter1 = registry.getCounter(counter1MID);
    Counter counter2 = registry.getCounter(counter2MID);

    @SuppressWarnings("unchecked")
    Gauge<Double> gauge = (Gauge<Double>) registry.getGauge(ratioGaugeMID);
    assertThat("Gauge is not registered correctly", gauge, notNullValue());

    counter1.inc(Math.round(Math.random() * Integer.MAX_VALUE));
    counter2.inc(Math.round(Math.random() * Integer.MAX_VALUE));

    assertThat("Gauge value is incorrect", gauge.getValue(), is(equalTo(((double) counter1.getCount()) / ((double) counter2.getCount()))));
}
 
Example 5
Source File: NonReusableMetricInjectionBean.java    From smallrye-metrics with Apache License 2.0 5 votes vote down vote up
@Inject
public void increaseCountersOnInjecting(
        @Metric(name = "mycounter", absolute = true, tags = { "k=v1" }) Counter counter1,
        @Metric(name = "mycounter", absolute = true, tags = { "k=v2" }) Counter counter2) {
    counter1.inc(2);
    counter2.inc(3);
}
 
Example 6
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 7
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 8
Source File: MetricProducerFieldBeanTest.java    From microprofile-metrics with Apache License 2.0 5 votes vote down vote up
@InSequence(3)
public void incrementCountersFromInjection(@Metric(name = "ratioGauge", absolute = true) Gauge<Double> gauge,
                                           @Metric(name = "counter1", absolute = true) Counter counter1,
                                           @Metric(name = "counter2", absolute = true) Counter counter2) {
    counter1.inc(Math.round(Math.random() * Integer.MAX_VALUE));
    counter2.inc(Math.round(Math.random() * Integer.MAX_VALUE));

    assertThat("Gauge value is incorrect", gauge.getValue(), is(equalTo(((double) counter1.getCount()) / ((double) counter2.getCount()))));

    @SuppressWarnings("unchecked")
    Gauge<Double> gaugeFromRegistry = (Gauge<Double>) registry.getGauge(new MetricID("ratioGauge"));
    assertThat("Gauge is not registered correctly", gaugeFromRegistry, notNullValue());

    assertThat("Gauge values from registry and injection do not match", gauge.getValue(), is(equalTo(gaugeFromRegistry.getValue())));
}
 
Example 9
Source File: MetricDecorator.java    From smallrye-reactive-messaging with Apache License 2.0 4 votes vote down vote up
private Consumer<Message<?>> incrementCount(String channelName) {
    Counter counter = registry.counter("mp.messaging.message.count", new Tag("channel", channelName));
    return m -> counter.inc();
}
 
Example 10
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 11
Source File: MetricAppBean.java    From microprofile-metrics with Apache License 2.0 4 votes vote down vote up
public void countMe() {
    Counter counter = metrics.counter("metricTest.test1.count");
    counter.inc();
}