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

pl.allegro.tech.hermes.common.metric.WorkloadMetrics Maven / Gradle / Ivy

The newest version!
package pl.allegro.tech.hermes.common.metric;

import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tags;
import io.micrometer.core.instrument.search.Search;
import pl.allegro.tech.hermes.metrics.HermesTimer;

import java.util.Collection;
import java.util.Set;
import java.util.function.ToDoubleFunction;

public class WorkloadMetrics {

    private static final String CONSUMER_ID_TAG = "consumer-id";
    private static final String KAFKA_CLUSTER_TAG = "kafka-cluster";

    private final MeterRegistry meterRegistry;
    private final GaugeRegistrar gaugeRegistrar;

    WorkloadMetrics(MeterRegistry meterRegistry) {
        this.meterRegistry = meterRegistry;
        this.gaugeRegistrar = new GaugeRegistrar(meterRegistry);
    }

    public  void registerAllAssignmentsGauge(T obj, String kafkaCluster, ToDoubleFunction f) {
        gaugeRegistrar.registerGauge(
                "workload.all-assignments",
                obj,
                f,
                Tags.of(KAFKA_CLUSTER_TAG, kafkaCluster)
        );
    }

    public  void registerMissingResourcesGauge(T obj, String kafkaCluster, ToDoubleFunction f) {
        gaugeRegistrar.registerGauge(
                "workload.missing-resources",
                obj,
                f,
                Tags.of(KAFKA_CLUSTER_TAG, kafkaCluster)
        );
    }

    public  void registerDeletedAssignmentsGauge(T obj, String kafkaCluster, ToDoubleFunction f) {
        gaugeRegistrar.registerGauge(
                "workload.deleted-assignments",
                obj,
                f,
                Tags.of(KAFKA_CLUSTER_TAG, kafkaCluster)
        );
    }

    public  void registerCreatedAssignmentsGauge(T obj, String kafkaCluster, ToDoubleFunction f) {
        gaugeRegistrar.registerGauge(
                "workload.created-assignments",
                obj,
                f,
                Tags.of(KAFKA_CLUSTER_TAG, kafkaCluster)
        );
    }

    public HermesTimer rebalanceDurationTimer(String kafkaCluster) {
        return HermesTimer.from(
                meterRegistry.timer("workload.rebalance-duration", Tags.of(KAFKA_CLUSTER_TAG, kafkaCluster))
        );
    }

    public  void registerRunningSubscriptionsGauge(T obj, ToDoubleFunction f) {
        gaugeRegistrar.registerGauge("workload.subscriptions.running", obj, f);
    }

    public  void registerAssignedSubscriptionsGauge(T obj, ToDoubleFunction f) {
        gaugeRegistrar.registerGauge("workload.subscriptions.assigned", obj, f);
    }

    public  void registerMissingSubscriptionsGauge(T obj, ToDoubleFunction f) {
        gaugeRegistrar.registerGauge("workload.subscriptions.missing", obj, f);
    }

    public  void registerOversubscribedGauge(T obj, ToDoubleFunction f) {
        gaugeRegistrar.registerGauge("workload.subscriptions.oversubscribed", obj, f);
    }

    public  void registerOperationsPerSecondGauge(T obj, ToDoubleFunction f) {
        gaugeRegistrar.registerGauge("workload.weighted.ops", obj, f);
    }

    public  void registerCpuUtilizationGauge(T obj, ToDoubleFunction f) {
        gaugeRegistrar.registerGauge("workload.weighted.cpu-utilization", obj, f);
    }

    public  void registerCurrentScoreGauge(String consumerId, T obj, ToDoubleFunction f) {
        gaugeRegistrar.registerGauge(
                "workload.weighted.current-score",
                obj,
                f,
                Tags.of(CONSUMER_ID_TAG, consumerId)
        );
    }

    public  void registerProposedErrorGauge(String consumerId, T obj, ToDoubleFunction f) {
        gaugeRegistrar.registerGauge(
                "workload.weighted.proposed-error",
                obj,
                f,
                Tags.of(CONSUMER_ID_TAG, consumerId)
        );
    }

    public  void registerScoringErrorGauge(String consumerId, T obj, ToDoubleFunction f) {
        gaugeRegistrar.registerGauge(
                "workload.weighted.scoring-error",
                obj,
                f,
                Tags.of(CONSUMER_ID_TAG, consumerId)
        );
    }

    public  void registerCurrentWeightGauge(String consumerId, T obj, ToDoubleFunction f) {
        gaugeRegistrar.registerGauge(
                "workload.weighted.current-weight.ops",
                obj,
                f,
                Tags.of(CONSUMER_ID_TAG, consumerId)
        );
    }

    public  void registerProposedWeightGauge(String consumerId, T obj, ToDoubleFunction f) {
        gaugeRegistrar.registerGauge(
                "workload.weighted.proposed-weight.ops",
                obj,
                f,
                Tags.of(CONSUMER_ID_TAG, consumerId)
        );
    }

    public void unregisterAllWorkloadWeightedGaugesForConsumerIds(Set consumerIds) {
        Collection gauges = Search.in(meterRegistry)
                .tag(CONSUMER_ID_TAG, consumerIds::contains)
                .name(s -> s.startsWith("workload.weighted"))
                .gauges();
        for (Gauge gauge : gauges) {
            meterRegistry.remove(gauge);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy