Search in sources :

Example 1 with Timer

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

the class TimedInterceptor method applyInterceptor.

@Override
protected <E extends Member & AnnotatedElement> Object applyInterceptor(InvocationContext context, E element) throws Exception {
    String name = resolver.timed(bean.getBeanClass(), element).metricName();
    Timer timer = (Timer) registry.getMetrics().get(name);
    if (timer == null) {
        throw new IllegalStateException("No timer with name [" + name + "] found in registry [" + registry + "]");
    }
    Timer.Context time = timer.time();
    try {
        return context.proceed();
    } finally {
        time.stop();
    }
}
Also used : Timer(org.eclipse.microprofile.metrics.Timer)

Example 2 with Timer

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

the class OpenMetricsExporterTest method exportTimer.

@Test
public void exportTimer() {
    Timer timer = mock(Timer.class);
    when(timer.getElapsedTime()).thenReturn(Duration.ofMillis(23L));
    when(timer.getCount()).thenReturn(80L);
    when(timer.getMeanRate()).thenReturn(0.004292520715985437d);
    when(timer.getOneMinuteRate()).thenReturn(2.794076465421066E-14d);
    when(timer.getFiveMinuteRate()).thenReturn(4.800392614619373E-4d);
    when(timer.getFifteenMinuteRate()).thenReturn(0.01063191047532505d);
    Snapshot snapshot = mock(Snapshot.class);
    when(timer.getSnapshot()).thenReturn(snapshot);
    when(snapshot.getMin()).thenReturn(169916L);
    when(snapshot.getMax()).thenReturn(560869L);
    when(snapshot.getMean()).thenReturn(415041d);
    when(snapshot.getStdDev()).thenReturn(652907d);
    when(snapshot.getMedian()).thenReturn(293324d);
    when(snapshot.get75thPercentile()).thenReturn(344914d);
    when(snapshot.get95thPercentile()).thenReturn(543647d);
    when(snapshot.get98thPercentile()).thenReturn(2706543d);
    when(snapshot.get99thPercentile()).thenReturn(5608694d);
    when(snapshot.get999thPercentile()).thenReturn(5608694d);
    MetricID metricID = new MetricID("response_time");
    Metadata metadata = Metadata.builder().withName(metricID.getName()).withDescription("Server response time for /index.html").withUnit(MetricUnits.NANOSECONDS).build();
    assertOutputEqualsFile("Timer.txt", metricID, timer, metadata);
}
Also used : Snapshot(org.eclipse.microprofile.metrics.Snapshot) MetricID(org.eclipse.microprofile.metrics.MetricID) Timer(org.eclipse.microprofile.metrics.Timer) SimpleTimer(org.eclipse.microprofile.metrics.SimpleTimer) Metadata(org.eclipse.microprofile.metrics.Metadata) Test(org.junit.Test)

Example 3 with Timer

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

the class TimedInterceptorTest method assertTimed.

private void assertTimed(int expectedStartCount) throws Exception {
    Method element = TestUtils.getTestMethod();
    Class<?> bean = getClass();
    AnnotationReader<Timed> reader = AnnotationReader.TIMED;
    Timer timer = MetricUtils.getOrRegisterByMetadataAndTags(registry, Timer.class, reader.metadata(bean, element), reader.tags(reader.annotation(bean, element)));
    TimedInterceptor.proceedTimed(context, element, bean, registry::getMetric);
    assertEquals(expectedStartCount + 1, timer.getCount());
    TimedInterceptor.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 {
        TimedInterceptor.proceedTimed(context, element, bean, (metricId, type) -> null);
        fail("Expected a IllegalStateException because the metric does not exist");
    } catch (IllegalStateException ex) {
        assertEquals("No Timer 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 {
        TimedInterceptor.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());
    // test should run in < 1 sec
    assertTrue(timer.getMeanRate() > 3d);
}
Also used : Timer(org.eclipse.microprofile.metrics.Timer) Timed(org.eclipse.microprofile.metrics.annotation.Timed) Method(java.lang.reflect.Method)

Example 4 with Timer

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

the class JsonExporterGetTest method exportTimer.

@Test
public void exportTimer() {
    Timer timer = mock(Timer.class);
    when(timer.getElapsedTime()).thenReturn(Duration.ofMillis(45678L));
    when(timer.getCount()).thenReturn(29382L);
    when(timer.getMeanRate()).thenReturn(12.185627192860734d);
    when(timer.getOneMinuteRate()).thenReturn(12.563d);
    when(timer.getFiveMinuteRate()).thenReturn(12.364d);
    when(timer.getFifteenMinuteRate()).thenReturn(12.126d);
    Snapshot snapshot = mock(Snapshot.class);
    when(timer.getSnapshot()).thenReturn(snapshot);
    when(snapshot.getMin()).thenReturn(169916L);
    when(snapshot.getMax()).thenReturn(5608694L);
    when(snapshot.getMean()).thenReturn(415041.00024926325d);
    when(snapshot.getStdDev()).thenReturn(652907.9633011606d);
    when(snapshot.getMedian()).thenReturn(293324.0d);
    when(snapshot.get75thPercentile()).thenReturn(344914d);
    when(snapshot.get95thPercentile()).thenReturn(543647d);
    when(snapshot.get98thPercentile()).thenReturn(2706543d);
    when(snapshot.get99thPercentile()).thenReturn(5608694d);
    when(snapshot.get999thPercentile()).thenReturn(5608694d);
    // example uses same values for both timers so we can get away with just one
    // but conceptually those should be two different timer instances
    export(new MetricID("responseTime"), timer);
    export(new MetricID("responseTime", new Tag("servlet", "two")), timer);
    assertOutputEqualsFile("Timer.json");
}
Also used : Snapshot(org.eclipse.microprofile.metrics.Snapshot) MetricID(org.eclipse.microprofile.metrics.MetricID) Timer(org.eclipse.microprofile.metrics.Timer) SimpleTimer(org.eclipse.microprofile.metrics.SimpleTimer) Tag(org.eclipse.microprofile.metrics.Tag) Test(org.junit.Test)

Example 5 with Timer

use of org.eclipse.microprofile.metrics.Timer 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

Timer (org.eclipse.microprofile.metrics.Timer)6 MetricID (org.eclipse.microprofile.metrics.MetricID)3 SimpleTimer (org.eclipse.microprofile.metrics.SimpleTimer)3 Snapshot (org.eclipse.microprofile.metrics.Snapshot)2 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 Tag (org.eclipse.microprofile.metrics.Tag)1 Timed (org.eclipse.microprofile.metrics.annotation.Timed)1