From d3fda9d6c46393ecfaa1f2a014ac21d8c8dcc14c Mon Sep 17 00:00:00 2001 From: Atharva Sharma Date: Mon, 22 Jul 2024 17:25:32 +0530 Subject: [PATCH] updates gauge usage and added index_uuid as tag Signed-off-by: Atharva Sharma --- .../RTFCacheConfigMetricsCollector.java | 58 +++++++++++++++++++ .../telemetry/RTFHeapMetricsCollector.java | 18 ++++-- ...RTFNodeStatsAllShardsMetricsCollector.java | 42 +++++--------- ...deStatsAllShardsMetricsCollectorTests.java | 4 +- 4 files changed, 88 insertions(+), 34 deletions(-) create mode 100644 src/main/java/org/opensearch/performanceanalyzer/collectors/telemetry/RTFCacheConfigMetricsCollector.java diff --git a/src/main/java/org/opensearch/performanceanalyzer/collectors/telemetry/RTFCacheConfigMetricsCollector.java b/src/main/java/org/opensearch/performanceanalyzer/collectors/telemetry/RTFCacheConfigMetricsCollector.java new file mode 100644 index 00000000..544dc92d --- /dev/null +++ b/src/main/java/org/opensearch/performanceanalyzer/collectors/telemetry/RTFCacheConfigMetricsCollector.java @@ -0,0 +1,58 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.performanceanalyzer.collectors.telemetry; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.opensearch.performanceanalyzer.OpenSearchResources; +import org.opensearch.performanceanalyzer.commons.collectors.PerformanceAnalyzerMetricsCollector; +import org.opensearch.performanceanalyzer.commons.collectors.TelemetryCollector; +import org.opensearch.performanceanalyzer.commons.config.overrides.ConfigOverridesWrapper; +import org.opensearch.performanceanalyzer.commons.metrics.MetricsConfiguration; +import org.opensearch.performanceanalyzer.commons.stats.metrics.StatExceptionCode; +import org.opensearch.performanceanalyzer.commons.stats.metrics.StatMetrics; +import org.opensearch.performanceanalyzer.config.PerformanceAnalyzerController; +import org.opensearch.telemetry.metrics.MetricsRegistry; + +public class RTFCacheConfigMetricsCollector extends PerformanceAnalyzerMetricsCollector + implements TelemetryCollector { + private static final Logger LOG = LogManager.getLogger(RTFCacheConfigMetricsCollector.class); + public static final int SAMPLING_TIME_INTERVAL = + MetricsConfiguration.CONFIG_MAP.get(RTFCacheConfigMetricsCollector.class) + .samplingInterval; + private boolean metricsInitialised; + private PerformanceAnalyzerController performanceAnalyzerController; + private ConfigOverridesWrapper configOverridesWrapper; + private MetricsRegistry metricsRegistry; + + public RTFCacheConfigMetricsCollector( + PerformanceAnalyzerController performanceAnalyzerController, + ConfigOverridesWrapper configOverridesWrapper) { + super( + SAMPLING_TIME_INTERVAL, + "RTFCacheConfigMetricsCollector", + StatMetrics.CACHE_CONFIG_METRICS_COLLECTOR_EXECUTION_TIME, + StatExceptionCode.CACHE_CONFIG_METRICS_COLLECTOR_ERROR); + this.metricsInitialised = false; + this.performanceAnalyzerController = performanceAnalyzerController; + this.configOverridesWrapper = configOverridesWrapper; + } + + @Override + public void collectMetrics(long startTime) { + if (performanceAnalyzerController.isCollectorDisabled( + configOverridesWrapper, getCollectorName())) { + LOG.info("RTFDisksCollector is disabled. Skipping collection."); + return; + } + + metricsRegistry = OpenSearchResources.INSTANCE.getMetricsRegistry(); + if (metricsRegistry == null) { + LOG.error("could not get the instance of MetricsRegistry class"); + return; + } + } +} diff --git a/src/main/java/org/opensearch/performanceanalyzer/collectors/telemetry/RTFHeapMetricsCollector.java b/src/main/java/org/opensearch/performanceanalyzer/collectors/telemetry/RTFHeapMetricsCollector.java index 6872089d..859e7ad9 100644 --- a/src/main/java/org/opensearch/performanceanalyzer/collectors/telemetry/RTFHeapMetricsCollector.java +++ b/src/main/java/org/opensearch/performanceanalyzer/collectors/telemetry/RTFHeapMetricsCollector.java @@ -23,6 +23,7 @@ import org.opensearch.performanceanalyzer.config.PerformanceAnalyzerController; import org.opensearch.telemetry.metrics.Histogram; import org.opensearch.telemetry.metrics.MetricsRegistry; +import org.opensearch.telemetry.metrics.TaggedMeasurement; import org.opensearch.telemetry.metrics.tags.Tags; public class RTFHeapMetricsCollector extends PerformanceAnalyzerMetricsCollector @@ -36,6 +37,8 @@ public class RTFHeapMetricsCollector extends PerformanceAnalyzerMetricsCollector private MetricsRegistry metricsRegistry; private final String memTypeAttributeKey = "mem_type"; private boolean metricsInitialised; + private static volatile Tags heapMaxTag = Tags.EMPTY; + private static volatile Long heapMaxValue = 0L; private PerformanceAnalyzerController performanceAnalyzerController; private ConfigOverridesWrapper configOverridesWrapper; @@ -91,6 +94,13 @@ private void initialiseMetricsIfNeeded() { RTFMetrics.HeapValue.Constants.USED_VALUE, "GC Heap Used PA Metrics", RTFMetrics.MetricUnits.BYTE.toString()); + + metricsRegistry.createGauge( + RTFMetrics.HeapValue.Constants.MAX_VALUE, + "Heap Max PA metrics", + RTFMetrics.MetricUnits.BYTE.toString(), + () -> TaggedMeasurement.create(heapMaxValue, heapMaxTag)); + metricsInitialised = true; } } @@ -119,12 +129,8 @@ private void recordMetrics() { heapUsedMetrics.record( memoryUsage.getUsed(), Tags.create().addTag(memTypeAttributeKey, entry.getKey())); - metricsRegistry.createGauge( - RTFMetrics.HeapValue.Constants.MAX_VALUE, - "Heap Max PA metrics", - "", - () -> (double) memoryUsage.getMax(), - Tags.create().addTag(memTypeAttributeKey, entry.getKey())); + heapMaxTag = Tags.create().addTag(memTypeAttributeKey, entry.getKey()); + heapMaxValue = memoryUsage.getUsed(); } } } diff --git a/src/main/java/org/opensearch/performanceanalyzer/collectors/telemetry/RTFNodeStatsAllShardsMetricsCollector.java b/src/main/java/org/opensearch/performanceanalyzer/collectors/telemetry/RTFNodeStatsAllShardsMetricsCollector.java index 478baa07..6b32b3c5 100644 --- a/src/main/java/org/opensearch/performanceanalyzer/collectors/telemetry/RTFNodeStatsAllShardsMetricsCollector.java +++ b/src/main/java/org/opensearch/performanceanalyzer/collectors/telemetry/RTFNodeStatsAllShardsMetricsCollector.java @@ -139,18 +139,11 @@ configOverridesWrapper, getCollectorName())) { for (Map.Entry currentShard : currentPerShardStats.entrySet()) { ShardId shardId = (ShardId) currentShard.getKey(); ShardStats currentShardStats = (ShardStats) currentShard.getValue(); - String indexUuid = null; - if (shardId.getIndex() != null) { - indexUuid = shardId.getIndex().getUUID(); - } - if (prevPerShardStats.size() == 0) { // Populating value for the first run. recordMetrics( new NodeStatsMetricsAllShardsPerCollectionStatus(currentShardStats), - shardId.getIndexName(), - indexUuid, - String.valueOf(shardId.id())); + shardId); continue; } ShardStats prevShardStats = prevPerShardStats.get(shardId); @@ -159,17 +152,14 @@ configOverridesWrapper, getCollectorName())) { // run. recordMetrics( new NodeStatsMetricsAllShardsPerCollectionStatus(currentShardStats), - shardId.getIndexName(), - indexUuid, - String.valueOf(shardId.id())); + shardId); continue; } NodeStatsMetricsAllShardsPerCollectionStatus prevValue = new NodeStatsMetricsAllShardsPerCollectionStatus(prevShardStats); NodeStatsMetricsAllShardsPerCollectionStatus currValue = new NodeStatsMetricsAllShardsPerCollectionStatus(currentShardStats); - populateDiffMetricValue( - prevValue, currValue, shardId.getIndexName(), indexUuid, shardId.id()); + populateDiffMetricValue(prevValue, currValue, shardId); } } @@ -251,17 +241,19 @@ public void populatePerShardStats(IndicesService indicesService) { } private void recordMetrics( - NodeStatsMetricsAllShardsPerCollectionStatus metrics, - String indexName, - String indexUuid, - String shardId) { + NodeStatsMetricsAllShardsPerCollectionStatus metrics, ShardId shardId) { Tags nodeStatsMetricsTag = Tags.create() - .addTag(RTFMetrics.CommonDimension.INDEX_NAME.toString(), indexName) - .addTag(RTFMetrics.CommonDimension.SHARD_ID.toString(), shardId); - - if (indexUuid != null) { - nodeStatsMetricsTag.addTag(RTFMetrics.CommonDimension.INDEX_UUID.toString(), indexUuid); + .addTag( + RTFMetrics.CommonDimension.INDEX_NAME.toString(), + shardId.getIndexName()) + .addTag( + RTFMetrics.CommonDimension.SHARD_ID.toString(), + String.valueOf(shardId.getId())); + + if (shardId.getIndex() != null) { + nodeStatsMetricsTag.addTag( + RTFMetrics.CommonDimension.INDEX_UUID.toString(), shardId.getIndex().getUUID()); } cacheQueryMissMetrics.add(metrics.getQueryCacheMissCount(), nodeStatsMetricsTag); @@ -280,9 +272,7 @@ private void recordMetrics( public void populateDiffMetricValue( NodeStatsMetricsAllShardsPerCollectionStatus prevValue, NodeStatsMetricsAllShardsPerCollectionStatus currValue, - String indexName, - String indexUuid, - int shardId) { + ShardId shardId) { NodeStatsMetricsAllShardsPerCollectionStatus metrics = new NodeStatsMetricsAllShardsPerCollectionStatus( @@ -303,7 +293,7 @@ public void populateDiffMetricValue( 0), currValue.requestCacheInBytes); - recordMetrics(metrics, indexName, indexUuid, String.valueOf(shardId)); + recordMetrics(metrics, shardId); } public static class NodeStatsMetricsAllShardsPerCollectionStatus extends MetricStatus { diff --git a/src/test/java/org/opensearch/performanceanalyzer/collectors/telemetry/RTFNodeStatsAllShardsMetricsCollectorTests.java b/src/test/java/org/opensearch/performanceanalyzer/collectors/telemetry/RTFNodeStatsAllShardsMetricsCollectorTests.java index fbd3b126..918c766a 100644 --- a/src/test/java/org/opensearch/performanceanalyzer/collectors/telemetry/RTFNodeStatsAllShardsMetricsCollectorTests.java +++ b/src/test/java/org/opensearch/performanceanalyzer/collectors/telemetry/RTFNodeStatsAllShardsMetricsCollectorTests.java @@ -120,11 +120,11 @@ public void testCollectMetrics() throws IOException { createIndex(TEST_INDEX); rtfNodeStatsAllShardsMetricsCollector.collectMetrics(startTimeInMills); verify(rtfNodeStatsAllShardsMetricsCollector, never()) - .populateDiffMetricValue(any(), any(), anyString(), anyString(), anyInt()); + .populateDiffMetricValue(any(), any(), any()); startTimeInMills += 500; rtfNodeStatsAllShardsMetricsCollector.collectMetrics(startTimeInMills); verify(rtfNodeStatsAllShardsMetricsCollector, times(1)) - .populateDiffMetricValue(any(), any(), anyString(), anyString(), anyInt()); + .populateDiffMetricValue(any(), any(), any()); verify(cacheFieldDataEvictionCounter, atLeastOnce()).add(anyDouble(), any()); verify(cacheFieldDataSizeCounter, atLeastOnce()).add(anyDouble(), any()); verify(cacheQueryMissCounter, atLeastOnce()).add(anyDouble(), any());