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

com.palantir.conjure.java.okhttp.ClientMetrics Maven / Gradle / Ivy

The newest version!
package com.palantir.conjure.java.okhttp;

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

/**
 * Conjure okhttp client response metrics.
 */
final class ClientMetrics {
    private static final String JAVA_VERSION = System.getProperty("java.version", "unknown");

    private static final String LIBRARY_NAME = "conjure-java-runtime";

    private static final String LIBRARY_VERSION = "8.13.0";

    private final TaggedMetricRegistry registry;

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

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

    /**
     * Request time, note that this does not include time spent reading the response body.
     */
    @CheckReturnValue
    Timer response(@Safe String serviceName) {
        return registry.timer(responseMetricName(serviceName));
    }

    static MetricName responseMetricName(@Safe String serviceName) {
        return MetricName.builder()
                .safeName("client.response")
                .putSafeTags("service-name", serviceName)
                .putSafeTags("libraryName", LIBRARY_NAME)
                .putSafeTags("libraryVersion", LIBRARY_VERSION)
                .putSafeTags("javaVersion", JAVA_VERSION)
                .build();
    }

    /**
     * Rate of errors received by reason and service-name. Currently only errors with reason IOException are reported.
     */
    @CheckReturnValue
    ResponseErrorBuilderReasonStage responseError() {
        return new ResponseErrorBuilder();
    }

    /**
     * Rate of deprecated endpoints being invoked.
     */
    @CheckReturnValue
    Meter deprecations(@Safe String serviceName) {
        return registry.meter(deprecationsMetricName(serviceName));
    }

    static MetricName deprecationsMetricName(@Safe String serviceName) {
        return MetricName.builder()
                .safeName("client.deprecations")
                .putSafeTags("service-name", serviceName)
                .putSafeTags("libraryName", LIBRARY_NAME)
                .putSafeTags("libraryVersion", LIBRARY_VERSION)
                .putSafeTags("javaVersion", JAVA_VERSION)
                .build();
    }

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

    interface ResponseErrorBuildStage {
        @CheckReturnValue
        Meter build();

        @CheckReturnValue
        MetricName buildMetricName();
    }

    interface ResponseErrorBuilderReasonStage {
        @CheckReturnValue
        ResponseErrorBuilderServiceNameStage reason(@Safe String reason);
    }

    interface ResponseErrorBuilderServiceNameStage {
        @CheckReturnValue
        ResponseErrorBuildStage serviceName(@Safe String serviceName);
    }

    private final class ResponseErrorBuilder
            implements ResponseErrorBuilderReasonStage, ResponseErrorBuilderServiceNameStage, ResponseErrorBuildStage {
        private String reason;

        private String serviceName;

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

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

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

        @Override
        public MetricName buildMetricName() {
            return MetricName.builder()
                    .safeName("client.response.error")
                    .putSafeTags("reason", reason)
                    .putSafeTags("service-name", serviceName)
                    .putSafeTags("libraryName", LIBRARY_NAME)
                    .putSafeTags("libraryVersion", LIBRARY_VERSION)
                    .putSafeTags("javaVersion", JAVA_VERSION)
                    .build();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy