Skip to content

Commit

Permalink
updates gauge usage and added index_uuid as tag
Browse files Browse the repository at this point in the history
Signed-off-by: Atharva Sharma <[email protected]>
  • Loading branch information
atharvasharma61 committed Jul 22, 2024
1 parent cb51403 commit d3fda9d
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;

Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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);
Expand All @@ -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(
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit d3fda9d

Please sign in to comment.