Search in sources :

Example 26 with Histogram

use of com.codahale.metrics.Histogram in project metrics by dropwizard.

the class GraphiteReporterTest method reportsHistograms.

@Test
public void reportsHistograms() throws Exception {
    final Histogram histogram = mock(Histogram.class);
    when(histogram.getCount()).thenReturn(1L);
    final Snapshot snapshot = mock(Snapshot.class);
    when(snapshot.getMax()).thenReturn(2L);
    when(snapshot.getMean()).thenReturn(3.0);
    when(snapshot.getMin()).thenReturn(4L);
    when(snapshot.getStdDev()).thenReturn(5.0);
    when(snapshot.getMedian()).thenReturn(6.0);
    when(snapshot.get75thPercentile()).thenReturn(7.0);
    when(snapshot.get95thPercentile()).thenReturn(8.0);
    when(snapshot.get98thPercentile()).thenReturn(9.0);
    when(snapshot.get99thPercentile()).thenReturn(10.0);
    when(snapshot.get999thPercentile()).thenReturn(11.0);
    when(histogram.getSnapshot()).thenReturn(snapshot);
    reporter.report(map(), map(), map("histogram", histogram), map(), map());
    final InOrder inOrder = inOrder(graphite);
    inOrder.verify(graphite).connect();
    inOrder.verify(graphite).send("prefix.histogram.count", "1", timestamp);
    inOrder.verify(graphite).send("prefix.histogram.max", "2", timestamp);
    inOrder.verify(graphite).send("prefix.histogram.mean", "3.00", timestamp);
    inOrder.verify(graphite).send("prefix.histogram.min", "4", timestamp);
    inOrder.verify(graphite).send("prefix.histogram.stddev", "5.00", timestamp);
    inOrder.verify(graphite).send("prefix.histogram.p50", "6.00", timestamp);
    inOrder.verify(graphite).send("prefix.histogram.p75", "7.00", timestamp);
    inOrder.verify(graphite).send("prefix.histogram.p95", "8.00", timestamp);
    inOrder.verify(graphite).send("prefix.histogram.p98", "9.00", timestamp);
    inOrder.verify(graphite).send("prefix.histogram.p99", "10.00", timestamp);
    inOrder.verify(graphite).send("prefix.histogram.p999", "11.00", timestamp);
    inOrder.verify(graphite).flush();
    inOrder.verify(graphite).close();
    verifyNoMoreInteractions(graphite);
}
Also used : Snapshot(com.codahale.metrics.Snapshot) Histogram(com.codahale.metrics.Histogram) InOrder(org.mockito.InOrder) Test(org.junit.Test)

Example 27 with Histogram

use of com.codahale.metrics.Histogram in project metrics by dropwizard.

the class CollectdReporterTest method reportsHistograms.

@Test
public void reportsHistograms() throws Exception {
    Histogram histogram = mock(Histogram.class);
    Snapshot snapshot = mock(Snapshot.class);
    when(histogram.getCount()).thenReturn(1L);
    when(histogram.getSnapshot()).thenReturn(snapshot);
    when(snapshot.getMax()).thenReturn(2L);
    when(snapshot.getMean()).thenReturn(3.0);
    when(snapshot.getMin()).thenReturn(4L);
    when(snapshot.getStdDev()).thenReturn(5.0);
    when(snapshot.getMedian()).thenReturn(6.0);
    when(snapshot.get75thPercentile()).thenReturn(7.0);
    when(snapshot.get95thPercentile()).thenReturn(8.0);
    when(snapshot.get98thPercentile()).thenReturn(9.0);
    when(snapshot.get99thPercentile()).thenReturn(10.0);
    when(snapshot.get999thPercentile()).thenReturn(11.0);
    reporter.report(map(), map(), map("histogram", histogram), map(), map());
    for (int i = 1; i <= 11; i++) {
        assertThat(receiver.next().getValues()).containsExactly((double) i);
    }
}
Also used : Snapshot(com.codahale.metrics.Snapshot) Histogram(com.codahale.metrics.Histogram) Test(org.junit.Test)

Example 28 with Histogram

use of com.codahale.metrics.Histogram in project riposte by Nike-Inc.

the class EndpointMetricsHandlerDefaultImplTest method setupMetricRegistryMock.

private void setupMetricRegistryMock() {
    metricRegistryMock = mock(MetricRegistry.class);
    registeredTimers = new HashMap<>();
    doAnswer(invocation -> {
        Timer originalTimer = (Timer) invocation.callRealMethod();
        return spy(originalTimer);
    }).when(instance).createAndRegisterRequestTimer(anyString(), any(MetricRegistry.class));
    doThrow(new RuntimeException("All timer creation should go through createAndRegisterRequestTimer(), *not* metricRegistry.timer(...).")).when(metricRegistryMock).timer(anyString());
    registeredMeterMocks = new HashMap<>();
    doAnswer(invocation -> {
        String name = invocation.getArgumentAt(0, String.class);
        Meter meterMock = mock(Meter.class);
        registeredMeterMocks.put(name, meterMock);
        return meterMock;
    }).when(metricRegistryMock).meter(anyString());
    registeredCounterMocks = new HashMap<>();
    doAnswer(invocation -> {
        String name = invocation.getArgumentAt(0, String.class);
        Counter counterMock = mock(Counter.class);
        registeredCounterMocks.put(name, counterMock);
        return counterMock;
    }).when(metricRegistryMock).counter(anyString());
    registeredHistogramMocks = new HashMap<>();
    doAnswer(invocation -> {
        String name = invocation.getArgumentAt(0, String.class);
        Histogram histogramMock = mock(Histogram.class);
        registeredHistogramMocks.put(name, histogramMock);
        return histogramMock;
    }).when(metricRegistryMock).histogram(anyString());
    registeredGauges = new HashMap<>();
    doAnswer(invocation -> {
        String name = invocation.getArgumentAt(0, String.class);
        Metric metric = invocation.getArgumentAt(1, Metric.class);
        if (metric instanceof Gauge)
            registeredGauges.put(name, (Gauge) metric);
        else if (metric instanceof Timer)
            registeredTimers.put(name, (Timer) metric);
        else
            throw new RuntimeException("Expected Gauge or Timer, but received: " + metric.getClass().getName());
        return metric;
    }).when(metricRegistryMock).register(anyString(), any(Metric.class));
}
Also used : Histogram(com.codahale.metrics.Histogram) Counter(com.codahale.metrics.Counter) Timer(com.codahale.metrics.Timer) Meter(com.codahale.metrics.Meter) MetricRegistry(com.codahale.metrics.MetricRegistry) Metric(com.codahale.metrics.Metric) Matchers.anyString(org.mockito.Matchers.anyString) Gauge(com.codahale.metrics.Gauge)

Example 29 with Histogram

use of com.codahale.metrics.Histogram in project riposte by Nike-Inc.

the class CodahaleMetricsListenerTest method constructor_uses_histogram_supplier_for_request_size_and_response_size_metrics.

@Test
public void constructor_uses_histogram_supplier_for_request_size_and_response_size_metrics() {
    // given
    Histogram mockHistogram = mock(Histogram.class);
    Supplier<Histogram> histogramSupplier = () -> mockHistogram;
    // when
    CodahaleMetricsListener instance = new CodahaleMetricsListener(cmcMock, endpointMetricsHandlerMock, false, null, null, histogramSupplier);
    // then
    assertThat(instance.requestSizes).isSameAs(mockHistogram);
    assertThat(instance.responseSizes).isSameAs(mockHistogram);
}
Also used : Histogram(com.codahale.metrics.Histogram) Test(org.junit.Test)

Example 30 with Histogram

use of com.codahale.metrics.Histogram in project opennms by OpenNMS.

the class SyslogReceiverJavaNetImpl method run.

/**
 * The execution context.
 */
@Override
public void run() {
    // Setup logging and create the dispatcher
    super.run();
    // get the context
    m_context = Thread.currentThread();
    // Create some metrics
    Meter packetMeter = METRICS.meter(MetricRegistry.name(getClass(), "packets"));
    Histogram packetSizeHistogram = METRICS.histogram(MetricRegistry.name(getClass(), "packetSize"));
    if (m_stop) {
        LOG.debug("Stop flag set before thread started, exiting");
        return;
    } else {
        LOG.debug("Thread context started");
    }
    // allocate a buffer
    final int length = 0xffff;
    final byte[] buffer = new byte[length];
    try {
        LOG.debug("Creating syslog socket");
        m_dgSock = new DatagramSocket(null);
    } catch (SocketException e) {
        LOG.warn("Could not create syslog socket: " + e.getMessage(), e);
        return;
    }
    // if a socket is closed.
    try {
        LOG.debug("Setting socket timeout to {}ms", SOCKET_TIMEOUT);
        m_dgSock.setSoTimeout(SOCKET_TIMEOUT);
    } catch (SocketException e) {
        LOG.warn("An I/O error occured while trying to set the socket timeout", e);
    }
    // also bound. This shouldn't have any effect at runtime.
    try {
        LOG.debug("Setting socket SO_REUSEADDR to true");
        m_dgSock.setReuseAddress(true);
    } catch (SocketException e) {
        LOG.warn("An I/O error occured while trying to set SO_REUSEADDR", e);
    }
    // Increase the receive buffer for the socket
    try {
        LOG.debug("Attempting to set receive buffer size to {}", Integer.MAX_VALUE);
        m_dgSock.setReceiveBufferSize(Integer.MAX_VALUE);
        LOG.debug("Actual receive buffer size is {}", m_dgSock.getReceiveBufferSize());
    } catch (SocketException e) {
        LOG.info("Failed to set the receive buffer to {}", Integer.MAX_VALUE, e);
    }
    try {
        LOG.debug("Opening datagram socket");
        if (m_config.getListenAddress() != null) {
            m_dgSock.bind(new InetSocketAddress(InetAddressUtils.addr(m_config.getListenAddress()), m_config.getSyslogPort()));
        } else {
            m_dgSock.bind(new InetSocketAddress(m_config.getSyslogPort()));
        }
    } catch (SocketException e) {
        LOG.info("Failed to open datagram socket", e);
    }
    // set to avoid numerous tracing message
    boolean ioInterrupted = false;
    // Construct one mutable {@link DatagramPacket} that will be used for receiving syslog messages
    DatagramPacket pkt = new DatagramPacket(buffer, length);
    // now start processing incoming requests
    while (!m_stop) {
        if (m_context.isInterrupted()) {
            LOG.debug("Thread context interrupted");
            break;
        }
        try {
            if (!ioInterrupted) {
                LOG.debug("Waiting on a datagram to arrive");
            }
            m_dgSock.receive(pkt);
            // Increment the packet counter
            packetMeter.mark();
            // Create a metric for the Syslog packet size
            packetSizeHistogram.update(length);
            final SyslogConnection connection = new SyslogConnection(pkt, true);
            m_dispatcher.send(connection);
            // reset the flag
            ioInterrupted = false;
        } catch (SocketTimeoutException e) {
            ioInterrupted = true;
            continue;
        } catch (InterruptedIOException e) {
            ioInterrupted = true;
            continue;
        } catch (IOException e) {
            if (m_stop) {
                // A SocketException can be thrown during normal shutdown so log as debug
                LOG.debug("Shutting down the datagram receipt port: " + e.getMessage());
            } else {
                LOG.error("An I/O exception occured on the datagram receipt port, exiting", e);
            }
            break;
        }
    }
    // end while status OK
    LOG.debug("Thread context exiting");
}
Also used : SocketException(java.net.SocketException) InterruptedIOException(java.io.InterruptedIOException) Histogram(com.codahale.metrics.Histogram) Meter(com.codahale.metrics.Meter) InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) SocketTimeoutException(java.net.SocketTimeoutException) DatagramSocket(java.net.DatagramSocket) DatagramPacket(java.net.DatagramPacket) SyslogConnection(org.opennms.netmgt.syslogd.api.SyslogConnection)

Aggregations

Histogram (com.codahale.metrics.Histogram)44 Test (org.junit.Test)21 Timer (com.codahale.metrics.Timer)17 Meter (com.codahale.metrics.Meter)13 Counter (com.codahale.metrics.Counter)12 Snapshot (com.codahale.metrics.Snapshot)11 MetricRegistry (com.codahale.metrics.MetricRegistry)8 Gauge (com.codahale.metrics.Gauge)7 UsingDataSet (com.lordofthejars.nosqlunit.annotation.UsingDataSet)5 ZonedDateTime (java.time.ZonedDateTime)5 Map (java.util.Map)5 DateTime (org.joda.time.DateTime)5 Metric (com.codahale.metrics.Metric)4 UniformReservoir (com.codahale.metrics.UniformReservoir)3 SortedMap (java.util.SortedMap)3 AggregateMetric (org.apache.solr.metrics.AggregateMetric)3 MetricSnapshot (backtype.storm.generated.MetricSnapshot)2 JAverageSnapshot (com.alibaba.jstorm.common.metric.codahale.JAverageSnapshot)2 ConsoleReporter (com.codahale.metrics.ConsoleReporter)2 MetricFilter (com.codahale.metrics.MetricFilter)2