All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gs.api.accelrx.monitor.remote.RemoteMetricsSource Maven / Gradle / Ivy

The newest version!
package com.gs.api.accelrx.monitor.remote;

import io.reactivex.rxjava3.core.Maybe;
import io.reactivex.rxjava3.core.Observable;
import io.vertx.ext.web.client.WebClientOptions;
import io.vertx.rxjava3.core.RxHelper;
import io.vertx.rxjava3.core.Vertx;
import io.vertx.rxjava3.ext.web.client.WebClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.TimeUnit;

public class RemoteMetricsSource {
    private static final Logger logger = LoggerFactory.getLogger(RemoteMetricsSource.class);

    private final Vertx vertx;
    private final long initialDelayMs;
    private final long delayMs;

    private final String requestURI;
    private final WebClient webClient;
    private final MetricsPrefixer metricsPrefixer;

    private RemoteMetricsSource(Vertx vertx, long initialDelayMs, long delayMs, String requestURI, WebClient webClient, String prefix) {
        this.vertx = vertx;
        this.initialDelayMs = initialDelayMs;
        this.delayMs = delayMs;
        this.requestURI = requestURI;
        this.webClient = webClient;
        this.metricsPrefixer = new MetricsPrefixer(prefix);
    }

    public static Observable create(Vertx vertx, RemoteMetricsSourceConfig config) {
        WebClientOptions webClientOptions = new WebClientOptions()
                .setDefaultHost(config.host())
                .setDefaultPort(config.port())
                .setSsl(config.ssl());
        WebClient webClient = WebClient.create(vertx, webClientOptions);
        RemoteMetricsSource remoteMetricsSource = new RemoteMetricsSource(vertx,
                config.initialDelay().toMillis(), config.delay().toMillis(), config.path(), webClient, config.prefix());
        return remoteMetricsSource.start();
    }

    public Observable start() {
        return Observable.interval(initialDelayMs, delayMs, TimeUnit.MILLISECONDS, RxHelper.scheduler(vertx))
                .switchMap(ignoredLong -> {
                    var httpRequest = webClient.get(requestURI);
                    if (logger.isDebugEnabled()) {
                        logger.debug("Querying remote metrics endpoint={}://{}:{}{}",
                                httpRequest.ssl() ? "https" : "http",
                                httpRequest.host(),
                                httpRequest.port(),
                                httpRequest.uri()
                        );
                    }
                    return httpRequest
                            .rxSend()
                            .flatMapMaybe(httpResponse -> {
                                if (httpResponse.statusCode() >= 300) {
                                    logger.error("Failed to get metrics: statusCode={} , statusMessage=\"{}\"", httpResponse.statusCode(), httpResponse.statusMessage());
                                    return Maybe.empty();
                                }
                                return Maybe.just(httpResponse.bodyAsString());
                            })
                            .map(metricsPrefixer::addPrefix)
                            .toObservable()
                            .onErrorResumeNext(throwable -> {
                                logger.error("Failed to get metrics", throwable);
                                return Observable.empty();
                            });
                })
                .startWithItem("")
                .replay(1)
                .autoConnect(0);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy