public void collectMetrics()

in src/main/java/org/opensearch/performanceanalyzer/collectors/ShardStateCollector.java [73:130]


    public void collectMetrics(long startTime) {
        if (!controller.isCollectorEnabled(configOverridesWrapper, getCollectorName())) {
            return;
        }
        long mCurrT = System.currentTimeMillis();
        if (OpenSearchResources.INSTANCE.getClusterService() == null) {
            return;
        }
        ClusterState clusterState = OpenSearchResources.INSTANCE.getClusterService().state();
        boolean inActiveShard = false;
        try {
            value.setLength(0);
            value.append(PerformanceAnalyzerMetrics.getJsonCurrentMilliSeconds())
                    .append(PerformanceAnalyzerMetrics.sMetricNewLineDelimitor);
            RoutingTable routingTable = clusterState.routingTable();
            String[] indices = routingTable.indicesRouting().keys().toArray(String.class);
            for (String index : indices) {
                List<ShardRouting> allShardsIndex = routingTable.allShards(index);
                value.append(
                        createJsonObject(
                                AllMetrics.ShardStateDimension.INDEX_NAME.toString(), index));
                for (ShardRouting shard : allShardsIndex) {
                    String nodeName = StringUtils.EMPTY;
                    if (shard.assignedToNode()) {
                        nodeName = clusterState.nodes().get(shard.currentNodeId()).getName();
                    }
                    if (shard.state() != ShardRoutingState.STARTED) {
                        inActiveShard = true;
                        value.append(PerformanceAnalyzerMetrics.sMetricNewLineDelimitor)
                                .append(
                                        new ShardStateMetrics(
                                                        shard.getId(),
                                                        shard.primary()
                                                                ? SHARD_PRIMARY.toString()
                                                                : SHARD_REPLICA.toString(),
                                                        nodeName,
                                                        shard.state().name())
                                                .serialize());
                    }
                }
            }
            value.append(PerformanceAnalyzerMetrics.sMetricNewLineDelimitor);
            if (inActiveShard) {
                saveMetricValues(value.toString(), startTime);
            }
            PerformanceAnalyzerApp.WRITER_METRICS_AGGREGATOR.updateStat(
                    WriterMetrics.SHARD_STATE_COLLECTOR_EXECUTION_TIME,
                    "",
                    System.currentTimeMillis() - mCurrT);
        } catch (Exception ex) {
            PerformanceAnalyzerApp.ERRORS_AND_EXCEPTIONS_AGGREGATOR.updateStat(
                    ExceptionsAndErrors.SHARD_STATE_COLLECTOR_ERROR, "", 1);
            LOG.debug(
                    "Exception in Collecting Shard Metrics: {} for startTime {}",
                    () -> ex.toString(),
                    () -> startTime);
        }
    }