Skip to content

Instantly share code, notes, and snippets.

@jonmcewen
Last active July 14, 2022 10:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jonmcewen/0acae03337839ad6818742e20deef108 to your computer and use it in GitHub Desktop.
Save jonmcewen/0acae03337839ad6818742e20deef108 to your computer and use it in GitHub Desktop.
Hacky bridge to grab dropwizard metrics and stick them into a micrometer registry. Add as a spring-boot Bean
package com.boohoo.esbdefused.metrics;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
import com.codahale.metrics.Counter;
import com.codahale.metrics.Gauge;
import com.codahale.metrics.Histogram;
import com.codahale.metrics.Meter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.MetricRegistryListener;
import com.codahale.metrics.Sampling;
import com.codahale.metrics.Timer;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tags;
public class DropwizardToMicrometerBridge implements MetricRegistryListener {
private final MeterRegistry meterRegistry;
public DropwizardToMicrometerBridge(MeterRegistry meterRegistry, MetricRegistry metricRegistry) {
this.meterRegistry = meterRegistry;
metricRegistry.addListener(this);
}
@Override
public void onGaugeAdded(String name, Gauge<?> gauge) {
meterRegistry.gauge(name, gauge, DropwizardToMicrometerBridge::doubleValue);
}
@Override
public void onGaugeRemoved(String name) {
throw new RuntimeException("Method not implemented");
}
@Override
public void onCounterAdded(String name, Counter counter) {
meterRegistry.more().counter(name, Tags.empty(), counter, Counter::getCount);
}
@Override
public void onCounterRemoved(String name) {
throw new RuntimeException("Method not implemented");
}
@Override
public void onHistogramAdded(String name, Histogram histogram) {
meterRegistry.more().timer(name, Tags.empty(), histogram, Histogram::getCount, DropwizardToMicrometerBridge::totalTime, TimeUnit.MILLISECONDS);
}
@Override
public void onHistogramRemoved(String name) {
throw new RuntimeException("Method not implemented");
}
@Override
public void onMeterAdded(String name, Meter meter) {
meterRegistry.more().counter(name, Tags.empty(), meter, Meter::getCount);
}
@Override
public void onMeterRemoved(String name) {
throw new RuntimeException("Method not implemented");
}
@Override
public void onTimerAdded(String name, Timer timer) {
meterRegistry.more().timer(name, Tags.empty(), timer, Timer::getCount, DropwizardToMicrometerBridge::totalTime, TimeUnit.MILLISECONDS);
}
@Override
public void onTimerRemoved(String name) {
throw new RuntimeException("Method not implemented");
}
public static long totalTime(Sampling sampling) {
return Arrays.stream(sampling.getSnapshot().getValues()).sum();
}
public static double doubleValue(Gauge<?> gauge) {
if (gauge == null || gauge.getValue() == null) {
return Double.NaN;
}
Object value = gauge.getValue();
return Double.valueOf(value.toString());
}
}
@dimovelev
Copy link

dimovelev commented Aug 7, 2019

HdrReservoir throws an UnsupportedOperationException in totalTime so i had to modify it to:
Turns out HdrReservoir is something from the cassandra java driver so not applicable to all :)

    public static long totalTime(Sampling sampling) {
        if (sampling instanceof HdrReservoir) {
            return -1;
        } else {
            return Arrays.stream(sampling.getSnapshot().getValues()).sum();
        }
    }

@dreadwarrior
Copy link

Thank you so much for sharing this! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment