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

com.gs.api.accelrx.monitor.AccelRxMonitorImpl Maven / Gradle / Ivy

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

import com.gs.api.accelrx.APIContract;
import com.gs.api.accelrx.AccelRxMonitor;
import com.gs.api.accelrx.monitor.handler.AboutHandler;
import com.gs.api.accelrx.monitor.handler.AboutView;
import com.gs.api.accelrx.monitor.handler.ConfigHandler;
import com.gs.api.accelrx.monitor.handler.PingHandler;
import com.gs.api.accelrx.monitor.prometheus.AccelRxMeterRegistry;
import com.gs.api.accelrx.monitor.prometheus.MetricsHandler;
import com.gs.api.accelrx.monitor.servicediscovery.ServiceDiscoveryHandler;
import io.reactivex.rxjava3.core.Completable;
import io.vertx.core.http.HttpServerOptions;
import io.vertx.core.json.JsonObject;
import io.vertx.micrometer.MicrometerMetricsOptions;
import io.vertx.micrometer.backends.BackendRegistries;
import io.vertx.rxjava3.core.Vertx;
import io.vertx.rxjava3.ext.web.Router;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class AccelRxMonitorImpl implements AccelRxMonitor {
    private static final Logger log = LoggerFactory.getLogger(AccelRxMonitorImpl.class);

    private final Vertx vertx;
    private final AccelRxMonitorConfig monitorConfig;
    private final AboutView aboutView;

    private final Map config = new HashMap<>();

    private final Set serverContracts = new HashSet<>();
    private final Set clientContracts = new HashSet<>();

    private final AccelRxMeterRegistry meterRegistry;

    private AccelRxMonitorImpl(Vertx vertx, AccelRxMeterRegistry meterRegistry, Map applicationMetadata, AccelRxMonitorConfig monitorConfig) {
        this.vertx = vertx;
        this.monitorConfig = monitorConfig;
        this.aboutView = AboutView.create(serverContracts, clientContracts, applicationMetadata);
        this.meterRegistry = meterRegistry;
        if (!vertx.isMetricsEnabled()) {
            BackendRegistries.setupBackend(new MicrometerMetricsOptions().setMicrometerRegistry(meterRegistry));
        }
    }

    public static AccelRxMonitorImpl create(Vertx vertx, AccelRxMeterRegistry meterRegistry, Map applicationMetadata) {
        return create(vertx, meterRegistry, applicationMetadata, AccelRxMonitorConfig.builder().build());
    }

    public static AccelRxMonitorImpl create(Vertx vertx, AccelRxMeterRegistry meterRegistry, Map applicationMetadata, AccelRxMonitorConfig monitorConfig) {
        return new AccelRxMonitorImpl(vertx, meterRegistry, applicationMetadata, monitorConfig);
    }

    @Override
    public AccelRxMonitor addConfig(String key, JsonObject jsonConfig) {
        config.put(key, jsonConfig);
        return this;
    }

    @Override
    public void registerClientContract(APIContract clientContract) {
        clientContracts.add(clientContract);
        meterRegistry.registerContractPaths(clientContract);
    }

    @Override
    public void registerServerContract(APIContract serverContract) {
        serverContracts.add(serverContract);
        meterRegistry.registerContractPaths(serverContract);
    }

    @Override
    public Completable start() {
        return Completable.fromAction(() -> {
            addConfig("monitor", JsonObject.mapFrom(monitorConfig));
        }).andThen(startMonitorServer(vertx));
    }

    @Override
    public JsonObject jsonConfig() {
        return new JsonObject(config);
    }

    @Override
    public Router router() {
        Router router = Router.router(vertx);

        router.get("/ping").handler(PingHandler.create());
        log.info("Ping added at '/ping'");

        router.get("/about").handler(AboutHandler.create(aboutView));
        log.info("Service info added at '/about'");

        router.get("/config").handler(ConfigHandler.create(config));
        log.info("Service config added at '/config'");

        router.route("/metrics").handler(MetricsHandler.create(vertx, meterRegistry, monitorConfig));
        log.info("Prometheus metrics added at '/metrics'");

        router.get("/serviceDiscovery").handler(ServiceDiscoveryHandler.create(vertx));
        log.info("Service discovery at '/serviceDiscovery'");

        return router;
    }

    @Override
    public AccelRxMeterRegistry meterRegistry() {
        return meterRegistry;
    }

    private Completable startMonitorServer(Vertx vertx) {
        if (monitorConfig.monitorPort() > 0) {
            log.info("Starting separate monitor on port '{}'", monitorConfig.monitorPort());

            return vertx.createHttpServer(new HttpServerOptions().setPort(monitorConfig.monitorPort()))
                    .requestHandler(router())
                    .rxListen()
                    .doOnSuccess(server -> log.info("Started monitor on port '{}' ", monitorConfig.monitorPort()))
                    .ignoreElement();
        } else {
            return Completable.complete();
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy