Search in sources :

Example 1 with SimpleTimer

use of org.eclipse.microprofile.metrics.SimpleTimer in project Payara by payara.

the class JsonExporterGetTest method exportSimpleTimer.

@Test
public void exportSimpleTimer() {
    SimpleTimer timer = mock(SimpleTimer.class);
    when(timer.getCount()).thenReturn(1L);
    when(timer.getElapsedTime()).thenReturn(Duration.ofMillis(12300000000L));
    when(timer.getMaxTimeDuration()).thenReturn(Duration.ofMillis(3231000000L));
    when(timer.getMinTimeDuration()).thenReturn(Duration.ofMillis(25600000L));
    export(new MetricID("simple_responseTime"), timer);
    assertOutputEqualsFile("SimpleTimer.json");
}
Also used : MetricID(org.eclipse.microprofile.metrics.MetricID) SimpleTimer(org.eclipse.microprofile.metrics.SimpleTimer) Test(org.junit.Test)

Example 2 with SimpleTimer

use of org.eclipse.microprofile.metrics.SimpleTimer in project Payara by payara.

the class OpenMetricsExporterTest method exportSimpleTimer.

@Test
public void exportSimpleTimer() {
    SimpleTimer timer = mock(SimpleTimer.class);
    when(timer.getCount()).thenReturn(12L);
    when(timer.getElapsedTime()).thenReturn(Duration.ofMillis(12300L));
    when(timer.getMaxTimeDuration()).thenReturn(Duration.ofMillis(3231L));
    when(timer.getMinTimeDuration()).thenReturn(Duration.ofNanos(25600000L));
    MetricID metricID = new MetricID("response_time");
    Metadata metadata = Metadata.builder().withName(metricID.getName()).withDescription("The number of calls to this REST endpoint #(1)").build();
    assertOutputEqualsFile("SimpleTimer.txt", metricID, timer, metadata);
}
Also used : MetricID(org.eclipse.microprofile.metrics.MetricID) SimpleTimer(org.eclipse.microprofile.metrics.SimpleTimer) Metadata(org.eclipse.microprofile.metrics.Metadata) Test(org.junit.Test)

Example 3 with SimpleTimer

use of org.eclipse.microprofile.metrics.SimpleTimer in project Payara by payara.

the class SimplyTimedInterceptorTest method assertTimed.

private void assertTimed(int expectedStartCount) throws Exception {
    Method element = TestUtils.getTestMethod();
    Class<?> bean = getClass();
    AnnotationReader<SimplyTimed> reader = AnnotationReader.SIMPLY_TIMED;
    SimpleTimer timer = MetricUtils.getOrRegisterByMetadataAndTags(registry, SimpleTimer.class, reader.metadata(bean, element), reader.tags(reader.annotation(bean, element)));
    SimplyTimedInterceptor.proceedTimed(context, element, bean, registry::getMetric);
    assertEquals(expectedStartCount + 1, timer.getCount());
    SimplyTimedInterceptor.proceedTimed(context, element, bean, registry::getMetric);
    assertEquals(expectedStartCount + 2, timer.getCount());
    verify(context, times(2)).proceed();
    // now test error when it does not exist
    try {
        SimplyTimedInterceptor.proceedTimed(context, element, bean, (metricId, type) -> null);
        fail("Expected a IllegalStateException because the metric does not exist");
    } catch (IllegalStateException ex) {
        assertEquals("No SimpleTimer with ID [" + reader.metricID(bean, element) + "] found in application registry", ex.getMessage());
    }
    verify(context, times(2)).proceed();
    // test a annotated method that throws an exception
    when(context.proceed()).thenThrow(new RuntimeException("Error in method"));
    try {
        SimplyTimedInterceptor.proceedTimed(context, element, bean, registry::getMetric);
        fail("Expected a RuntimeException");
    } catch (RuntimeException ex) {
        assertEquals("Error in method", ex.getMessage());
    }
    verify(context, times(3)).proceed();
    // need to remove the 'thenThrow' behaviour
    reset(context);
    assertEquals(expectedStartCount + 3, timer.getCount());
    assertTrue(timer.getElapsedTime().toNanos() > 0);
}
Also used : SimpleTimer(org.eclipse.microprofile.metrics.SimpleTimer) SimplyTimed(org.eclipse.microprofile.metrics.annotation.SimplyTimed) Method(java.lang.reflect.Method)

Example 4 with SimpleTimer

use of org.eclipse.microprofile.metrics.SimpleTimer in project Payara by payara.

the class MetricsServiceImpl method collectRegistry.

private static void collectRegistry(String contextName, MetricRegistry registry, MonitoringDataCollector collector) {
    // OBS: this way of iterating the metrics in the registry is optimal because of its internal data organisation
    for (String name : registry.getNames()) {
        for (Entry<MetricID, Metric> entry : ((MetricRegistryImpl) registry).getMetrics(name).entrySet()) {
            MetricID metricID = entry.getKey();
            Metric metric = entry.getValue();
            try {
                MonitoringDataCollector metricCollector = tagCollector(contextName, metricID, collector);
                if (metric instanceof Counting) {
                    metricCollector.collect(toName(metricID, "Count"), ((Counting) metric).getCount());
                }
                if (metric instanceof SimpleTimer) {
                    metricCollector.collect(toName(metricID, "Duration"), ((SimpleTimer) metric).getElapsedTime().toMillis());
                }
                if (metric instanceof Timer) {
                    metricCollector.collect(toName(metricID, "MaxDuration"), ((Timer) metric).getSnapshot().getMax());
                }
                if (metric instanceof Gauge) {
                    Object value = ((Gauge<?>) metric).getValue();
                    if (value instanceof Number) {
                        metricCollector.collect(toName(metricID, getMetricUnitSuffix(registry.getMetadata(name).unit())), ((Number) value));
                    }
                }
            } catch (Exception ex) {
                LOGGER.log(Level.SEVERE, "Failed to retrieve metric: " + metricID);
                ;
            }
        }
    }
}
Also used : MonitoringDataCollector(fish.payara.monitoring.collect.MonitoringDataCollector) MetricID(org.eclipse.microprofile.metrics.MetricID) SimpleTimer(org.eclipse.microprofile.metrics.SimpleTimer) Timer(org.eclipse.microprofile.metrics.Timer) SimpleTimer(org.eclipse.microprofile.metrics.SimpleTimer) Metric(org.eclipse.microprofile.metrics.Metric) Counting(org.eclipse.microprofile.metrics.Counting) FileNotFoundException(java.io.FileNotFoundException) NoSuchRegistryException(fish.payara.microprofile.metrics.exception.NoSuchRegistryException) Gauge(org.eclipse.microprofile.metrics.Gauge)

Aggregations

SimpleTimer (org.eclipse.microprofile.metrics.SimpleTimer)4 MetricID (org.eclipse.microprofile.metrics.MetricID)3 Test (org.junit.Test)2 NoSuchRegistryException (fish.payara.microprofile.metrics.exception.NoSuchRegistryException)1 MonitoringDataCollector (fish.payara.monitoring.collect.MonitoringDataCollector)1 FileNotFoundException (java.io.FileNotFoundException)1 Method (java.lang.reflect.Method)1 Counting (org.eclipse.microprofile.metrics.Counting)1 Gauge (org.eclipse.microprofile.metrics.Gauge)1 Metadata (org.eclipse.microprofile.metrics.Metadata)1 Metric (org.eclipse.microprofile.metrics.Metric)1 Timer (org.eclipse.microprofile.metrics.Timer)1 SimplyTimed (org.eclipse.microprofile.metrics.annotation.SimplyTimed)1