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

The following examples show how to use org.eclipse.microprofile.metrics.annotation.Metric. 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: Initialization_ProducerMethod_Test.java    From smallrye-metrics with Apache License 2.0 5 votes vote down vote up
@Produces
@Metric(name = "c1", absolute = true)
public Counter producer() {
    return new CounterImpl() {
        @Override
        public long getCount() {
            return 111;
        }
    };
}
 
Example #2
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 #3
Source File: MetricProducer.java    From smallrye-metrics with 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 File: SeMetricName.java    From smallrye-metrics with 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 File: SeMetricName.java    From smallrye-metrics with 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
Source File: ConcurrentGaugedMethodBeanTest.java    From microprofile-metrics with Apache License 2.0 5 votes vote down vote up
@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
Source File: CountedMethodTagBeanTest.java    From microprofile-metrics with Apache License 2.0 5 votes vote down vote up
@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
Source File: CountedMethodBeanTest.java    From microprofile-metrics with Apache License 2.0 5 votes vote down vote up
@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
Source File: MetricProducerMethodBean.java    From microprofile-metrics with Apache License 2.0 5 votes vote down vote up
@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
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 #11
Source File: GaugeInjectionBeanTest.java    From microprofile-metrics with Apache License 2.0 5 votes vote down vote up
@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 File: MetricRegistryTest.java    From microprofile-metrics with 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
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 #14
Source File: MetricAppBean.java    From microprofile-metrics with 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;
}