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

com.palantir.dialogue.core.DialogueRoundrobinMetrics Maven / Gradle / Ivy

There is a newer version: 5.4.0
Show newest version
package com.palantir.dialogue.core;

import com.codahale.metrics.Meter;
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;

/**
 * Instrumentation for the ROUND_ROBIN node selection strategy (currently implemented by BalancedChannel).
 */
final class DialogueRoundrobinMetrics {
    private static final String JAVA_VERSION = System.getProperty("java.version", "unknown");

    private static final String LIBRARY_NAME = "dialogue";

    private static final String LIBRARY_VERSION = "4.7.0";

    private final TaggedMetricRegistry registry;

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

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

    /**
     * Meter of the requests that were successfully made, tagged by the index of the host. (Note if there are >10 nodes this metric will not be recorded).
     */
    @CheckReturnValue
    SuccessBuilderChannelNameStage success() {
        return new SuccessBuilder();
    }

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

    interface SuccessBuildStage {
        @CheckReturnValue
        Meter build();

        @CheckReturnValue
        MetricName buildMetricName();
    }

    interface SuccessBuilderChannelNameStage {
        @CheckReturnValue
        SuccessBuilderHostIndexStage channelName(@Safe String channelName);
    }

    interface SuccessBuilderHostIndexStage {
        @CheckReturnValue
        SuccessBuildStage hostIndex(@Safe String hostIndex);
    }

    private final class SuccessBuilder
            implements SuccessBuilderChannelNameStage, SuccessBuilderHostIndexStage, SuccessBuildStage {
        private String channelName;

        private String hostIndex;

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

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

        @Override
        public Meter build() {
            return registry.meter(buildMetricName());
        }

        @Override
        public MetricName buildMetricName() {
            return MetricName.builder()
                    .safeName("dialogue.roundrobin.success")
                    .putSafeTags("channel-name", channelName)
                    .putSafeTags("hostIndex", hostIndex)
                    .putSafeTags("libraryName", LIBRARY_NAME)
                    .putSafeTags("libraryVersion", LIBRARY_VERSION)
                    .putSafeTags("javaVersion", JAVA_VERSION)
                    .build();
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy