|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.cluster; |
| 10 | + |
| 11 | +import org.opensearch.telemetry.metrics.Counter; |
| 12 | +import org.opensearch.telemetry.metrics.Histogram; |
| 13 | +import org.opensearch.telemetry.metrics.MetricsRegistry; |
| 14 | +import org.opensearch.telemetry.metrics.tags.Tags; |
| 15 | + |
| 16 | +import java.util.Objects; |
| 17 | +import java.util.Optional; |
| 18 | + |
| 19 | +/** |
| 20 | + * Class containing metrics (counters/latency) specific to ClusterManager. |
| 21 | + * |
| 22 | + * @opensearch.internal |
| 23 | + */ |
| 24 | +public final class ClusterManagerMetrics { |
| 25 | + |
| 26 | + private static final String LATENCY_METRIC_UNIT_MS = "ms"; |
| 27 | + private static final String COUNTER_METRICS_UNIT = "1"; |
| 28 | + |
| 29 | + public final Histogram clusterStateAppliersHistogram; |
| 30 | + public final Histogram clusterStateListenersHistogram; |
| 31 | + public final Histogram rerouteHistogram; |
| 32 | + public final Histogram clusterStateComputeHistogram; |
| 33 | + public final Histogram clusterStatePublishHistogram; |
| 34 | + |
| 35 | + public final Counter leaderCheckFailureCounter; |
| 36 | + public final Counter followerChecksFailureCounter; |
| 37 | + |
| 38 | + public ClusterManagerMetrics(MetricsRegistry metricsRegistry) { |
| 39 | + clusterStateAppliersHistogram = metricsRegistry.createHistogram( |
| 40 | + "cluster.state.appliers.latency", |
| 41 | + "Histogram for tracking the latency of cluster state appliers", |
| 42 | + LATENCY_METRIC_UNIT_MS |
| 43 | + ); |
| 44 | + clusterStateListenersHistogram = metricsRegistry.createHistogram( |
| 45 | + "cluster.state.listeners.latency", |
| 46 | + "Histogram for tracking the latency of cluster state listeners", |
| 47 | + LATENCY_METRIC_UNIT_MS |
| 48 | + ); |
| 49 | + rerouteHistogram = metricsRegistry.createHistogram( |
| 50 | + "allocation.reroute.latency", |
| 51 | + "Histogram for recording latency of shard re-routing", |
| 52 | + LATENCY_METRIC_UNIT_MS |
| 53 | + ); |
| 54 | + clusterStateComputeHistogram = metricsRegistry.createHistogram( |
| 55 | + "cluster.state.new.compute.latency", |
| 56 | + "Histogram for recording time taken to compute new cluster state", |
| 57 | + LATENCY_METRIC_UNIT_MS |
| 58 | + ); |
| 59 | + clusterStatePublishHistogram = metricsRegistry.createHistogram( |
| 60 | + "cluster.state.publish.success.latency", |
| 61 | + "Histogram for recording time taken to publish a new cluster state", |
| 62 | + LATENCY_METRIC_UNIT_MS |
| 63 | + ); |
| 64 | + followerChecksFailureCounter = metricsRegistry.createCounter( |
| 65 | + "followers.checker.failure.count", |
| 66 | + "Counter for number of failed follower checks", |
| 67 | + COUNTER_METRICS_UNIT |
| 68 | + ); |
| 69 | + leaderCheckFailureCounter = metricsRegistry.createCounter( |
| 70 | + "leader.checker.failure.count", |
| 71 | + "Counter for number of failed leader checks", |
| 72 | + COUNTER_METRICS_UNIT |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + public void recordLatency(Histogram histogram, Double value) { |
| 77 | + histogram.record(value); |
| 78 | + } |
| 79 | + |
| 80 | + public void recordLatency(Histogram histogram, Double value, Optional<Tags> tags) { |
| 81 | + if (Objects.isNull(tags) || tags.isEmpty()) { |
| 82 | + histogram.record(value); |
| 83 | + return; |
| 84 | + } |
| 85 | + histogram.record(value, tags.get()); |
| 86 | + } |
| 87 | + |
| 88 | + public void incrementCounter(Counter counter, Double value) { |
| 89 | + incrementCounter(counter, value, Optional.empty()); |
| 90 | + } |
| 91 | + |
| 92 | + public void incrementCounter(Counter counter, Double value, Optional<Tags> tags) { |
| 93 | + if (Objects.isNull(tags) || tags.isEmpty()) { |
| 94 | + counter.add(value); |
| 95 | + return; |
| 96 | + } |
| 97 | + counter.add(value, tags.get()); |
| 98 | + } |
| 99 | +} |
0 commit comments