Skip to content

Instantly share code, notes, and snippets.

@jkschneider
Created May 29, 2018 21:36
Show Gist options
  • Save jkschneider/5769315f823dbebd143610ed2d24c058 to your computer and use it in GitHub Desktop.
Save jkschneider/5769315f823dbebd143610ed2d24c058 to your computer and use it in GitHub Desktop.
Mapping from Spectator -> Micrometer
package io.micrometer.spectator;
import com.netflix.spectator.api.*;
import io.micrometer.core.instrument.MeterRegistry;
import java.util.Collections;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
public class MicrometerRegistry extends AbstractRegistry {
private final MeterRegistry registry;
public MicrometerRegistry(MeterRegistry registry) {
super(new Clock() {
@Override
public long wallTime() {
return registry.config().clock().wallTime();
}
@Override
public long monotonicTime() {
return registry.config().clock().monotonicTime();
}
});
this.registry = registry;
}
@Override
protected MicrometerCounter newCounter(Id id) {
return new MicrometerCounter(id, registry.counter(id.name(), mapTags(id)));
}
@Override
protected DistributionSummary newDistributionSummary(Id id) {
return null;
}
@Override
protected Timer newTimer(Id id) {
return null;
}
@Override
protected Gauge newGauge(Id id) {
return null;
}
private Iterable<io.micrometer.core.instrument.Tag> mapTags(Id id) {
return StreamSupport.stream(id.tags().spliterator(), false)
.map(t -> io.micrometer.core.instrument.Tag.of(t.key(), t.value()))
.collect(Collectors.toList());
}
}
class MicrometerCounter implements Counter {
private final io.micrometer.core.instrument.Counter counter;
private final Id id;
MicrometerCounter(Id id, io.micrometer.core.instrument.Counter counter) {
this.id = id;
this.counter = counter;
}
@Override
public void increment() {
counter.increment();
}
@Override
public void increment(long amount) {
counter.increment(amount);
}
@Override
public long count() {
return (long) counter.count();
}
@Override
public Id id() {
return id;
}
@Override
public Iterable<Measurement> measure() {
return Collections.emptyList(); // this will never be called
}
@Override
public boolean hasExpired() {
return false;
}
}
@mkw
Copy link

mkw commented Nov 30, 2018

Would you mind adding a license to this?

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