com.gs.api.accelrx.monitor.prometheus.AccelRxMeterRegistry Maven / Gradle / Ivy
The newest version!
package com.gs.api.accelrx.monitor.prometheus;
import com.gs.api.accelrx.APIContract;
import com.gs.api.accelrx.monitor.util.ManifestAttributes;
import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.binder.jvm.ClassLoaderMetrics;
import io.micrometer.core.instrument.binder.jvm.JvmGcMetrics;
import io.micrometer.core.instrument.binder.jvm.JvmMemoryMetrics;
import io.micrometer.core.instrument.binder.jvm.JvmThreadMetrics;
import io.micrometer.core.instrument.binder.system.ProcessorMetrics;
import io.micrometer.core.instrument.config.MeterFilter;
import io.micrometer.prometheus.PrometheusConfig;
import io.micrometer.prometheus.PrometheusMeterRegistry;
import io.vertx.micrometer.Label;
import io.vertx.micrometer.backends.BackendRegistries;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class AccelRxMeterRegistry extends PrometheusMeterRegistry {
private static final Logger LOG = LoggerFactory.getLogger(AccelRxMeterRegistry.class);
private final MicrometerContractMeterFilter contractMeterFilter;
private AccelRxMeterRegistry() {
super(PrometheusConfig.DEFAULT);
registerAccelRxManifestAttributes();
LOG.info("Binding Basic Metrics to Prometheus");
new ClassLoaderMetrics().bindTo(this);
new ProcessorMetrics().bindTo(this);
new JvmGcMetrics().bindTo(this);
new JvmMemoryMetrics().bindTo(this);
new JvmThreadMetrics().bindTo(this);
this.contractMeterFilter = MicrometerContractMeterFilter.create();
config().meterFilter(MicrometerDomainRestrictedQuantileFilter.create());
config().meterFilter(contractMeterFilter.pathFilter());
config().meterFilter(contractMeterFilter.pathNormalizationFilter());
config().meterFilter(remoteTagFilter());
}
private void registerAccelRxManifestAttributes() {
List tags = Stream.of(
ManifestAttributes.IMPLEMENTATION_VERSION,
ManifestAttributes.BUILD_DATE,
ManifestAttributes.BUILD_NUMBER,
ManifestAttributes.COMMIT_REVISION)
.map(this::getTagFromManifestAttributes)
.filter(Optional::isPresent)
.map(Optional::get)
.collect(Collectors.toList());
Gauge.builder("accelrx_build_info", 1, Number::doubleValue)
.description("Accel.rx version")
.tags(tags)
.register(this);
}
private Optional getTagFromManifestAttributes(String attributeName) {
String attributeValue = ManifestAttributes.get().get(attributeName);
return Optional.ofNullable(attributeValue)
.map(value -> value.replace('-', '_'))
.map(value -> Tag.of(attributeName.toLowerCase(), value));
}
public static AccelRxMeterRegistry create() {
return new AccelRxMeterRegistry();
}
public static AccelRxMeterRegistry lookup() {
return Objects.requireNonNullElseGet(internalLookup(), AccelRxMeterRegistry::create);
}
public void registerContractPaths(APIContract contract) {
contract.operationPaths().forEach(contractMeterFilter::registerContractPath);
}
private static AccelRxMeterRegistry internalLookup() {
MeterRegistry registry = BackendRegistries.getDefaultNow();
if (registry instanceof AccelRxMeterRegistry) {
return ((AccelRxMeterRegistry) registry);
} else {
return null;
}
}
private MeterFilter remoteTagFilter() {
return new MeterFilter() {
@Override
public Meter.Id map(Meter.Id id) {
if (id.getName().startsWith("vertx.http.server")) {
return MeterFilter.ignoreTags(Label.REMOTE.toString()).map(id);
} else {
return id;
}
}
};
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy