|
| 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.plugin.insights.core.metrics; |
| 10 | + |
| 11 | +import java.util.Locale; |
| 12 | +import java.util.concurrent.ConcurrentHashMap; |
| 13 | +import java.util.stream.Stream; |
| 14 | +import org.opensearch.telemetry.metrics.Counter; |
| 15 | +import org.opensearch.telemetry.metrics.MetricsRegistry; |
| 16 | +import org.opensearch.telemetry.metrics.tags.Tags; |
| 17 | + |
| 18 | +/** |
| 19 | + * Class contains all the Counters related to search query types. |
| 20 | + */ |
| 21 | +public final class OperationalMetricsCounter { |
| 22 | + private static final String PREFIX = "search.insights."; |
| 23 | + private static final String CLUSTER_NAME_TAG = "cluster_name"; |
| 24 | + private static final String UNIT = "1"; |
| 25 | + |
| 26 | + private final String clusterName; |
| 27 | + private final MetricsRegistry metricsRegistry; |
| 28 | + private final ConcurrentHashMap<OperationalMetric, Counter> metricCounterMap; |
| 29 | + |
| 30 | + private static OperationalMetricsCounter instance; |
| 31 | + |
| 32 | + /** |
| 33 | + * Constructor of OperationalMetricsCounter |
| 34 | + * @param metricsRegistry the OTel metrics registry |
| 35 | + */ |
| 36 | + private OperationalMetricsCounter(String clusterName, MetricsRegistry metricsRegistry) { |
| 37 | + this.clusterName = clusterName; |
| 38 | + this.metricsRegistry = metricsRegistry; |
| 39 | + this.metricCounterMap = new ConcurrentHashMap<>(); |
| 40 | + Stream.of(OperationalMetric.values()).forEach(name -> metricCounterMap.computeIfAbsent(name, this::createMetricCounter)); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Initializes the singleton instance of OperationalMetricsCounter. |
| 45 | + * This method must be called once before accessing the instance. |
| 46 | + * |
| 47 | + * @param clusterName the name of the cluster |
| 48 | + * @param metricsRegistry the OTel metrics registry |
| 49 | + */ |
| 50 | + public static synchronized void initialize(String clusterName, MetricsRegistry metricsRegistry) { |
| 51 | + instance = new OperationalMetricsCounter(clusterName, metricsRegistry); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Get the singleton instance of OperationalMetricsCounter. |
| 56 | + * |
| 57 | + * @return the singleton instance |
| 58 | + * @throws IllegalStateException if the instance is not yet initialized |
| 59 | + */ |
| 60 | + public static synchronized OperationalMetricsCounter getInstance() { |
| 61 | + if (instance == null) { |
| 62 | + throw new IllegalStateException("OperationalMetricsCounter is not initialized. Call initialize() first."); |
| 63 | + } |
| 64 | + return instance; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Increment the operational metrics counter, attaching custom tags |
| 69 | + * |
| 70 | + * @param metricName name of the metric |
| 71 | + * @param customTags custom tags of this metric |
| 72 | + */ |
| 73 | + public void incrementCounter(OperationalMetric metricName, Tags customTags) { |
| 74 | + Counter counter = metricCounterMap.computeIfAbsent(metricName, this::createMetricCounter); |
| 75 | + Tags metricsTags = (customTags == null ? Tags.create() : customTags).addTag(CLUSTER_NAME_TAG, clusterName); |
| 76 | + counter.add(1, metricsTags); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Increment the operational metrics counter |
| 81 | + * |
| 82 | + * @param metricName name of the metric |
| 83 | + */ |
| 84 | + public void incrementCounter(OperationalMetric metricName) { |
| 85 | + this.incrementCounter(metricName, null); |
| 86 | + } |
| 87 | + |
| 88 | + private Counter createMetricCounter(OperationalMetric metricName) { |
| 89 | + return metricsRegistry.createCounter( |
| 90 | + PREFIX + metricName.toString().toLowerCase(Locale.ROOT) + ".count", |
| 91 | + metricName.getDescription(), |
| 92 | + UNIT |
| 93 | + ); |
| 94 | + } |
| 95 | +} |
0 commit comments