-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
(cherry picked from commit 38e84a2) Signed-off-by: Chenyang Ji <[email protected]> Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
929141c
commit 71b84c4
Showing
14 changed files
with
262 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/main/java/org/opensearch/plugin/insights/core/metrics/OperationalMetric.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.plugin.insights.core.metrics; | ||
|
||
import java.util.Locale; | ||
|
||
public enum OperationalMetric { | ||
LOCAL_INDEX_READER_PARSING_EXCEPTIONS("Number of errors when parsing with LocalIndexReader"), | ||
LOCAL_INDEX_EXPORTER_BULK_FAILURES("Number of failures when ingesting Query Insights data to local indices"), | ||
LOCAL_INDEX_EXPORTER_EXCEPTIONS("Number of exceptions in Query Insights LocalIndexExporter"), | ||
INVALID_EXPORTER_TYPE_FAILURES("Number of invalid exporter type failures"), | ||
INVALID_INDEX_PATTERN_EXCEPTIONS("Number of invalid index pattern exceptions"), | ||
DATA_INGEST_EXCEPTIONS("Number of exceptions during data ingest in Query Insights"), | ||
QUERY_CATEGORIZE_EXCEPTIONS("Number of exceptions when categorizing the queries"), | ||
EXPORTER_FAIL_TO_CLOSE_EXCEPTION("Number of failures when closing the exporter"); | ||
|
||
private final String description; | ||
|
||
OperationalMetric(String description) { | ||
this.description = description; | ||
} | ||
|
||
public String getDescription() { | ||
return description; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.format(Locale.ROOT, "%s (%s)", name(), description); | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
src/main/java/org/opensearch/plugin/insights/core/metrics/OperationalMetricsCounter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.plugin.insights.core.metrics; | ||
|
||
import java.util.Locale; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.stream.Stream; | ||
import org.opensearch.telemetry.metrics.Counter; | ||
import org.opensearch.telemetry.metrics.MetricsRegistry; | ||
import org.opensearch.telemetry.metrics.tags.Tags; | ||
|
||
/** | ||
* Class contains all the Counters related to search query types. | ||
*/ | ||
public final class OperationalMetricsCounter { | ||
private static final String PREFIX = "search.insights."; | ||
private static final String CLUSTER_NAME_TAG = "cluster_name"; | ||
private static final String UNIT = "1"; | ||
|
||
private final String clusterName; | ||
private final MetricsRegistry metricsRegistry; | ||
private final ConcurrentHashMap<OperationalMetric, Counter> metricCounterMap; | ||
|
||
private static OperationalMetricsCounter instance; | ||
|
||
/** | ||
* Constructor of OperationalMetricsCounter | ||
* @param metricsRegistry the OTel metrics registry | ||
*/ | ||
private OperationalMetricsCounter(String clusterName, MetricsRegistry metricsRegistry) { | ||
this.clusterName = clusterName; | ||
this.metricsRegistry = metricsRegistry; | ||
this.metricCounterMap = new ConcurrentHashMap<>(); | ||
Stream.of(OperationalMetric.values()).forEach(name -> metricCounterMap.computeIfAbsent(name, this::createMetricCounter)); | ||
} | ||
|
||
/** | ||
* Initializes the singleton instance of OperationalMetricsCounter. | ||
* This method must be called once before accessing the instance. | ||
* | ||
* @param clusterName the name of the cluster | ||
* @param metricsRegistry the OTel metrics registry | ||
*/ | ||
public static synchronized void initialize(String clusterName, MetricsRegistry metricsRegistry) { | ||
instance = new OperationalMetricsCounter(clusterName, metricsRegistry); | ||
} | ||
|
||
/** | ||
* Get the singleton instance of OperationalMetricsCounter. | ||
* | ||
* @return the singleton instance | ||
* @throws IllegalStateException if the instance is not yet initialized | ||
*/ | ||
public static synchronized OperationalMetricsCounter getInstance() { | ||
if (instance == null) { | ||
throw new IllegalStateException("OperationalMetricsCounter is not initialized. Call initialize() first."); | ||
} | ||
return instance; | ||
} | ||
|
||
/** | ||
* Increment the operational metrics counter, attaching custom tags | ||
* | ||
* @param metricName name of the metric | ||
* @param customTags custom tags of this metric | ||
*/ | ||
public void incrementCounter(OperationalMetric metricName, Tags customTags) { | ||
Counter counter = metricCounterMap.computeIfAbsent(metricName, this::createMetricCounter); | ||
Tags metricsTags = (customTags == null ? Tags.create() : customTags).addTag(CLUSTER_NAME_TAG, clusterName); | ||
counter.add(1, metricsTags); | ||
} | ||
|
||
/** | ||
* Increment the operational metrics counter | ||
* | ||
* @param metricName name of the metric | ||
*/ | ||
public void incrementCounter(OperationalMetric metricName) { | ||
this.incrementCounter(metricName, null); | ||
} | ||
|
||
private Counter createMetricCounter(OperationalMetric metricName) { | ||
return metricsRegistry.createCounter( | ||
PREFIX + metricName.toString().toLowerCase(Locale.ROOT) + ".count", | ||
metricName.getDescription(), | ||
UNIT | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...test/java/org/opensearch/plugin/insights/core/metrics/OperationalMetricsCounterTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.plugin.insights.core.metrics; | ||
|
||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import org.mockito.ArgumentCaptor; | ||
import org.opensearch.telemetry.metrics.Counter; | ||
import org.opensearch.telemetry.metrics.MetricsRegistry; | ||
import org.opensearch.telemetry.metrics.tags.Tags; | ||
import org.opensearch.test.OpenSearchTestCase; | ||
|
||
/** | ||
* Unit tests for the {@link OperationalMetricsCounter} class. | ||
*/ | ||
public class OperationalMetricsCounterTests extends OpenSearchTestCase { | ||
private static final String CLUSTER_NAME = "test-cluster"; | ||
|
||
public void testSingletonInitializationAndIncrement() { | ||
Counter mockCounter = mock(Counter.class); | ||
MetricsRegistry metricsRegistry = mock(MetricsRegistry.class); | ||
// Stub the createCounter method to return the mockCounter | ||
when(metricsRegistry.createCounter(any(), any(), any())).thenReturn(mockCounter); | ||
OperationalMetricsCounter.initialize(CLUSTER_NAME, metricsRegistry); | ||
OperationalMetricsCounter instance = OperationalMetricsCounter.getInstance(); | ||
ArgumentCaptor<String> nameCaptor = ArgumentCaptor.forClass(String.class); | ||
verify(metricsRegistry, times(8)).createCounter(nameCaptor.capture(), any(), eq("1")); | ||
assertNotNull(instance); | ||
instance.incrementCounter(OperationalMetric.LOCAL_INDEX_READER_PARSING_EXCEPTIONS); | ||
instance.incrementCounter(OperationalMetric.LOCAL_INDEX_READER_PARSING_EXCEPTIONS); | ||
instance.incrementCounter(OperationalMetric.LOCAL_INDEX_READER_PARSING_EXCEPTIONS); | ||
verify(mockCounter, times(3)).add(eq(1.0), any(Tags.class)); | ||
} | ||
} |
Oops, something went wrong.