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

com.palantir.dialogue.hc5.DialogueClientPoolMetrics Maven / Gradle / Ivy

package com.palantir.dialogue.hc5;

import com.codahale.metrics.Gauge;
import com.google.errorprone.annotations.CheckReturnValue;
import com.palantir.logsafe.Preconditions;
import com.palantir.logsafe.Safe;
import com.palantir.tritium.metrics.registry.MetricName;
import com.palantir.tritium.metrics.registry.TaggedMetricRegistry;
import java.util.Objects;

/**
 * Connection pool metrics from the dialogue Apache client.
 */
final class DialogueClientPoolMetrics {
    private static final String LIBRARY_NAME = "dialogue";

    private static final String LIBRARY_VERSION = Objects.requireNonNullElse(
            DialogueClientPoolMetrics.class.getPackage().getImplementationVersion(), "unknown");

    private final TaggedMetricRegistry registry;

    private DialogueClientPoolMetrics(TaggedMetricRegistry registry) {
        this.registry = registry;
    }

    static DialogueClientPoolMetrics of(TaggedMetricRegistry registry) {
        return new DialogueClientPoolMetrics(Preconditions.checkNotNull(registry, "TaggedMetricRegistry"));
    }

    /**
     * Number of connections in the client connection pool in states idle, pending, and leased.
     */
    @CheckReturnValue
    SizeBuilderClientNameStage size() {
        return new SizeBuilder();
    }

    @Override
    public String toString() {
        return "DialogueClientPoolMetrics{registry=" + registry + '}';
    }

    interface SizeBuildStage {
        void build(Gauge gauge);

        MetricName buildMetricName();
    }

    interface SizeBuilderClientNameStage {
        @CheckReturnValue
        SizeBuilderStateStage clientName(@Safe String clientName);
    }

    interface SizeBuilderStateStage {
        @CheckReturnValue
        SizeBuildStage state(@Safe String state);
    }

    private final class SizeBuilder implements SizeBuilderClientNameStage, SizeBuilderStateStage, SizeBuildStage {
        private String clientName;

        private String state;

        @Override
        public void build(Gauge gauge) {
            registry.registerWithReplacement(buildMetricName(), gauge);
        }

        @Override
        public MetricName buildMetricName() {
            return MetricName.builder()
                    .safeName("dialogue.client.pool.size")
                    .putSafeTags("client-name", clientName)
                    .putSafeTags("state", state)
                    .putSafeTags("libraryName", LIBRARY_NAME)
                    .putSafeTags("libraryVersion", LIBRARY_VERSION)
                    .build();
        }

        @Override
        public SizeBuilder clientName(@Safe String clientName) {
            Preconditions.checkState(this.clientName == null, "client-name is already set");
            this.clientName = Preconditions.checkNotNull(clientName, "client-name is required");
            return this;
        }

        @Override
        public SizeBuilder state(@Safe String state) {
            Preconditions.checkState(this.state == null, "state is already set");
            this.state = Preconditions.checkNotNull(state, "state is required");
            return this;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy