blob: cea25300f49b01248d1e5723d43add5c005ac770 [file] [log] [blame]
// Copyright (C) 2019 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.googlesource.gerrit.plugins.healthcheck;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Sets;
import com.google.gerrit.extensions.events.LifecycleListener;
import com.google.gerrit.extensions.registration.DynamicSet;
import com.google.gerrit.extensions.registration.RegistrationHandle;
import com.google.gerrit.metrics.CallbackMetric0;
import com.google.gerrit.metrics.Counter0;
import com.google.gerrit.metrics.Description;
import com.google.gerrit.metrics.MetricMaker;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.googlesource.gerrit.plugins.healthcheck.check.GlobalHealthCheck;
import com.googlesource.gerrit.plugins.healthcheck.check.HealthCheck;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
@Singleton
public class HealthCheckMetrics implements LifecycleListener {
private final DynamicSet<HealthCheck> healthChecks;
private final MetricMaker metricMaker;
private final Set<RegistrationHandle> registeredMetrics;
private final Set<Runnable> triggers;
private final GlobalHealthCheck globalHealthCheck;
@Inject
public HealthCheckMetrics(
DynamicSet<HealthCheck> healthChecks,
GlobalHealthCheck globalHealthCheck,
MetricMaker metricMaker) {
this.healthChecks = healthChecks;
this.metricMaker = metricMaker;
this.registeredMetrics = Collections.synchronizedSet(new HashSet<>());
this.triggers = Collections.synchronizedSet(new HashSet<>());
this.globalHealthCheck = globalHealthCheck;
}
@Override
public void start() {
Set<HealthCheck> allChecks = Sets.newHashSet(healthChecks);
allChecks.add(globalHealthCheck);
for (HealthCheck healthCheck : allChecks) {
String name = healthCheck.name();
Counter0 failureMetric = getFailureMetric(name);
CallbackMetric0<Long> latencyMetric = getLatencyMetric(name);
Runnable metricCallBack = getCallbackMetric(healthCheck, failureMetric, latencyMetric);
registeredMetrics.add(failureMetric);
registeredMetrics.add(metricMaker.newTrigger(latencyMetric, metricCallBack));
triggers.add(metricCallBack);
}
}
private Runnable getCallbackMetric(
HealthCheck healthCheck, Counter0 failureMetric, CallbackMetric0<Long> latencyMetric) {
return () -> {
HealthCheck.StatusSummary status = healthCheck.getLatestStatus();
latencyMetric.set(healthCheck.getLatestStatus().elapsed);
if (status.isFailure()) {
failureMetric.increment();
}
};
}
private CallbackMetric0<Long> getLatencyMetric(String name) {
return metricMaker.newCallbackMetric(
String.format("%s/latest_latency", name),
Long.class,
new Description(String.format("%s health check latency execution (ms)", name))
.setGauge()
.setUnit(Description.Units.MILLISECONDS));
}
private Counter0 getFailureMetric(String name) {
return metricMaker.newCounter(
String.format("%s/failure", name),
new Description(String.format("%s healthcheck failures count", name))
.setCumulative()
.setRate()
.setUnit("failures"));
}
@Override
public void stop() {
for (RegistrationHandle handle : registeredMetrics) {
handle.remove();
}
}
@VisibleForTesting
public void triggerAll() {
triggers.forEach(Runnable::run);
}
}