Search in sources :

Example 96 with Histogram

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

the class BootstrapTest method bringsYourOwnMetricRegistry.

@Test
void bringsYourOwnMetricRegistry() {
    final MetricRegistry newRegistry = new MetricRegistry() {

        @Override
        public Histogram histogram(String name) {
            Histogram existed = (Histogram) getMetrics().get(name);
            return existed != null ? existed : new Histogram(new UniformReservoir());
        }
    };
    bootstrap.setMetricRegistry(newRegistry);
    bootstrap.registerMetrics();
    assertThat(newRegistry.getNames()).contains("jvm.buffers.mapped.capacity", "jvm.threads.count", "jvm.memory.heap.usage", "jvm.attribute.vendor", "jvm.classloader.loaded", "jvm.filedescriptor");
}
Also used : Histogram(com.codahale.metrics.Histogram) MetricRegistry(com.codahale.metrics.MetricRegistry) UniformReservoir(com.codahale.metrics.UniformReservoir) Test(org.junit.jupiter.api.Test)

Example 97 with Histogram

use of com.codahale.metrics.Histogram in project infrautils by opendaylight.

the class MetricsFileReporter method report.

@Override
public void report(@SuppressWarnings("rawtypes") SortedMap<String, Gauge> gauges, SortedMap<String, Counter> counters, SortedMap<String, Histogram> histograms, SortedMap<String, Meter> meters, SortedMap<String, Timer> timers) {
    try {
        Calendar calendar = Calendar.getInstance();
        int hourOfTheDay = calendar.get(Calendar.HOUR_OF_DAY);
        int dayOfTheWeek = calendar.get(Calendar.DAY_OF_WEEK);
        // retains one week worth of counters
        rotateLastWeekFile(dayOfTheWeek, hourOfTheDay);
        boolean append = true;
        File file = createFile(dayOfTheWeek, hourOfTheDay);
        PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file, append), DEFAULT_ENCODING));
        pw.print("date,");
        pw.print(new Date());
        pw.println();
        pw.println("Counters:");
        for (Map.Entry<String, Counter> entry : counters.entrySet()) {
            Counter newCounter = entry.getValue();
            // avoid unnecessary write to report file
            // report the counter only if there is a change
            Long oldCounterObj = oldCounters.get(entry.getKey());
            long oldCounter = oldCounterObj != null ? oldCounterObj.longValue() : 0;
            if (newCounter.getCount() != oldCounter) {
                pw.print(entry.getKey());
                printWithSeparator(pw, "count", entry.getValue().getCount());
                printWithSeparator(pw, "diff", entry.getValue().getCount() - oldCounter);
                pw.println();
            }
        }
        pw.println("Gauges:");
        for (@SuppressWarnings("rawtypes") Map.Entry<String, Gauge> entry : gauges.entrySet()) {
            pw.print(entry.getKey());
            pw.println(entry.getValue().getValue());
        }
        pw.println("Histograms:");
        for (Map.Entry<String, Histogram> entry : histograms.entrySet()) {
            pw.print(entry.getKey());
            printWithSeparator(pw, "count", entry.getValue().getCount());
            printSampling(pw, entry.getValue());
            pw.println();
        }
        pw.println("Meters:");
        for (Map.Entry<String, Meter> entry : meters.entrySet()) {
            pw.print(entry.getKey());
            printMeter(pw, entry.getValue());
        }
        pw.println("Timers:");
        for (Map.Entry<String, Timer> entry : timers.entrySet()) {
            pw.print(entry.getKey());
            printSampling(pw, entry.getValue());
            printMeter(pw, entry.getValue());
        }
        pw.close();
    } catch (IOException e) {
        LOG.error("Failed to report counters to files", e);
    }
    counters.forEach((key, value) -> oldCounters.put(key, value.getCount()));
}
Also used : Histogram(com.codahale.metrics.Histogram) Meter(com.codahale.metrics.Meter) Calendar(java.util.Calendar) IOException(java.io.IOException) Date(java.util.Date) Gauge(com.codahale.metrics.Gauge) Counter(com.codahale.metrics.Counter) Timer(com.codahale.metrics.Timer) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) SortedMap(java.util.SortedMap) PrintWriter(java.io.PrintWriter)

Aggregations

Histogram (com.codahale.metrics.Histogram)97 Test (org.junit.Test)39 Timer (com.codahale.metrics.Timer)34 Counter (com.codahale.metrics.Counter)30 Meter (com.codahale.metrics.Meter)29 Gauge (com.codahale.metrics.Gauge)17 Snapshot (com.codahale.metrics.Snapshot)12 Map (java.util.Map)11 Test (org.testng.annotations.Test)11 MetricRegistry (com.codahale.metrics.MetricRegistry)9 DataProvider (com.tngtech.java.junit.dataprovider.DataProvider)8 IOException (java.io.IOException)8 HashMap (java.util.HashMap)8 Metric (com.codahale.metrics.Metric)6 Reservoir (com.codahale.metrics.Reservoir)6 ZonedDateTime (java.time.ZonedDateTime)6 Properties (java.util.Properties)6 SortedMap (java.util.SortedMap)6 DateTime (org.joda.time.DateTime)6 UniformReservoir (com.codahale.metrics.UniformReservoir)5