Java Code Examples for org.eclipse.microprofile.metrics.annotation.Metric

The following examples show how to use org.eclipse.microprofile.metrics.annotation.Metric. These examples are extracted from open source projects. 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
@Produces
@Metric(name = "c1", absolute = true)
public Counter producer() {
    return new CounterImpl() {
        @Override
        public long getCount() {
            return 111;
        }
    };
}
 
Example 2
@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 3
Source Project: smallrye-metrics   Source File: MetricProducer.java    License: Apache License 2.0 5 votes vote down vote up
private Tag[] getTags(InjectionPoint ip) {
    Metric metric = ip.getAnnotated().getAnnotation(Metric.class);
    if (metric != null && metric.tags().length > 0) {
        return TagsUtils.parseTagsAsArray(metric.tags());
    } else {
        return new Tag[0];
    }
}
 
Example 4
Source Project: smallrye-metrics   Source File: SeMetricName.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public String of(AnnotatedMember<?> member) {
    if (member.isAnnotationPresent(Metric.class)) {
        Metric metric = member.getAnnotation(Metric.class);
        String name = (metric.name().isEmpty()) ? member.getJavaMember().getName() : of(metric.name());
        return metric.absolute() | parameters.contains(MetricsParameter.useAbsoluteName) ? name
                : MetricRegistry.name(member.getJavaMember().getDeclaringClass(), name);
    } else {
        return parameters.contains(MetricsParameter.useAbsoluteName) ? member.getJavaMember().getName()
                : MetricRegistry.name(member.getJavaMember().getDeclaringClass(), member.getJavaMember().getName());
    }
}
 
Example 5
Source Project: smallrye-metrics   Source File: SeMetricName.java    License: Apache License 2.0 5 votes vote down vote up
private String of(AnnotatedParameter<?> parameter) {
    if (parameter.isAnnotationPresent(Metric.class)) {
        Metric metric = parameter.getAnnotation(Metric.class);
        String name = (metric.name().isEmpty()) ? getParameterName(parameter) : of(metric.name());
        return metric.absolute() | parameters.contains(MetricsParameter.useAbsoluteName) ? name
                : MetricRegistry.name(parameter.getDeclaringCallable().getJavaMember().getDeclaringClass(), name);
    } else {
        return parameters.contains(MetricsParameter.useAbsoluteName) ? getParameterName(parameter)
                : MetricRegistry.name(parameter.getDeclaringCallable().getJavaMember().getDeclaringClass(),
                        getParameterName(parameter));
    }
}
 
Example 6
@Test
@InSequence(2)
public void metricInjectionIntoTest(@Metric(name = C_GAUGE_NAME, absolute = true) ConcurrentGauge instance) {
    ConcurrentGauge cGauge = registry.getConcurrentGauge(cGaugeMID);
    assertThat("Concurrent Gauges is not registered correctly", cGauge, notNullValue());

    // Make sure that the counter registered and the bean instance are the same
    assertThat("Concurrent Gauges and bean instance are not equal", instance, is(equalTo(cGauge)));
}
 
Example 7
@Test
@InSequence(2)
public void countedTagMethodNotCalledYet(@Metric(name = "countedMethod", absolute = true, tags = {"number=one"}) Counter instanceOne,
                                         @Metric(name = "countedMethod", absolute = true, tags = {"number=two"}) Counter instanceTwo) {
    Counter counterOne = registry.getCounter(counterOneMID);
    Counter counterTwo = registry.getCounter(counterTwoMID);

    assertThat("Counter is not registered correctly", counterOne, notNullValue());
    assertThat("Counter is not registered correctly", counterTwo, notNullValue());

    // Make sure that the counter registered and the bean instance are the same
    assertThat("Counter and bean instance are not equal", instanceOne, is(equalTo(counterOne)));
    assertThat("Counter and bean instance are not equal", instanceTwo, is(equalTo(counterTwo)));
}
 
Example 8
@Test
@InSequence(2)
public void metricInjectionIntoTest(@Metric(name = "countedMethod", absolute = true) Counter instance) {
    Counter counter = registry.getCounter(counterMetricID);
    assertThat("Counter is not registered correctly", counter, notNullValue());

    // Make sure that the counter registered and the bean instance are the same
    assertThat("Counter and bean instance are not equal", instance, is(equalTo(counter)));
}
 
Example 9
@Produces
@Metric(name = "cache-hits")
Gauge<Double> cacheHitRatioGauge(final @Metric(name = "hits") Meter hits, final @Metric(name = "calls") Timer calls) {
    return new Gauge<Double>() {

        @Override
        public Double getValue() {
            return (double) hits.getCount() / (double) calls.getCount();
        }
    };

}
 
Example 10
@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 11
@Test
@InSequence(2)
public void callGaugeAfterSetterCall(
        @Metric(absolute = true, name = "org.eclipse.microprofile.metrics.tck.cdi.GaugeInjectionBean.gaugeInjection") Gauge<Long> gauge) {
    // Call the setter method and assert the gauge is up-to-date
    long value = 1L + Math.round(Math.random() * (Long.MAX_VALUE - 1L));
    bean.setGauge(value);

    assertThat("Gauge value is incorrect", gauge.getValue(), is(equalTo(value)));
}
 
Example 12
Source Project: microprofile-metrics   Source File: MetricRegistryTest.java    License: Apache License 2.0 4 votes vote down vote up
private void assertExists(Class<? extends org.eclipse.microprofile.metrics.Metric> expected, MetricID metricID) {
    assertNotNull("Metric expected to exist but was undefined: " + metricID, metrics.getMetric(metricID, expected));
}
 
Example 13
@Produces
@Metric(name = "not_registered_metric")
Counter notRegisteredMetric(MetricRegistry registry, InjectionPoint ip) {
    return registry.counter("not_registered_metric");
}
 
Example 14
Source Project: microprofile-metrics   Source File: MetricAppBean.java    License: Apache License 2.0 4 votes vote down vote up
@Produces
@Metric(name = "coffee_price_produces", unit = "USD", absolute = true,
    description = "getCoffeePriceDescription", displayName = "getCoffeePriceDisplayName")
protected org.eclipse.microprofile.metrics.Gauge<Long> getCoffeePrice() {
    return () -> 4L;
}