<commit_before>package com.cultofbits.elasticsearch.metrics;

import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.cluster.ClusterService;
import org.elasticsearch.common.component.AbstractLifecycleComponent;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.concurrent.EsExecutors;
import org.elasticsearch.indices.IndicesService;
import org.elasticsearch.node.service.NodeService;

public class MetricsService extends AbstractLifecycleComponent<MetricsService> {

    private final Long interval;

    private Thread thread;
    private MetricsWorker worker;


    @Inject
    public MetricsService(Settings settings,
                          NodeService nodeService, IndicesService indicesService, ClusterService clusterService) {
        super(settings);

        interval = settings.getAsLong("metrics.interval", 60000L);
        String[] indexesToInclude = clusterService.state().metaData().concreteIndices(
            IndicesOptions.strictExpandOpen(),
            settings.get("metrics.indexes-to-include", "_all").split(",")
        );
        worker = new MetricsWorker(nodeService, indicesService, interval, indexesToInclude);
    }

    @Override
    protected void doStart() throws ElasticsearchException {
        logger.info("Starting Metrics Service");

        if (interval <= 0){
            logger.info("metrics.interval <= 0, skipping Metrics Service start.");
            return;
        }

        if (thread == null || !thread.isAlive()) {

            thread = new Thread(worker, EsExecutors.threadName(settings, "metrics.exporters"));
            thread.setDaemon(true);
            thread.start();<commit_msg>improves resilience to bad index selection<commit_after>package com.cultofbits.elasticsearch.metrics;

import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.cluster.ClusterService;
import org.elasticsearch.common.component.AbstractLifecycleComponent;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.concurrent.EsExecutors;
import org.elasticsearch.indices.IndicesService;
import org.elasticsearch.node.service.NodeService;

public class MetricsService extends AbstractLifecycleComponent<MetricsService> {

    private final Long interval;

    private Thread thread;
    private MetricsWorker worker;


    @Inject
    public MetricsService(Settings settings,
                          NodeService nodeService, IndicesService indicesService, ClusterService clusterService) {
        super(settings);

        interval = settings.getAsLong("metrics.interval", 60000L);
        String[] indexesToInclude = clusterService.state().metaData().concreteIndices(
            IndicesOptions.lenientExpandOpen(),
            settings.get("metrics.indexes-to-include", "_all").split(",")
        );

        logger.info("Will track stats for the indices {}, resolved from {}",
                    indexesToInclude,
                    settings.get("metrics.indexes-to-include", "_all"));
        worker = new MetricsWorker(nodeService, indicesService, interval, indexesToInclude);
    }

    @Override
    protected void doStart() throws ElasticsearchException {
        logger.info("Starting Metrics Service");

        if (interval <= 0){
            logger.info("metrics.interval <= 0, skipping Metrics Service start.");
            return;
        }

        if (thread == null || !thread.isAlive()) {

            thread = new Thread(worker, EsExecutors.threadName(settings, "metrics.exporters"));
            thread.setDaemon(true);
            thread.start();
{'fraction_non_alphanumeric': 0.09798270893371758, 'fraction_numerical': 0.0036678019386953103, 'mean_word_length': 3.5183431952662723, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4628342', 'n_tokens_mistral': 947, 'n_tokens_neox': 932, 'n_words': 197}