All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.graylog2.telemetry.server.TelemetryClusterService Maven / Gradle / Ivy

package org.graylog2.telemetry.server;

import com.github.joschi.jadconfig.util.Duration;
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import org.graylog2.indexer.counts.Counts;
import org.graylog2.shared.stats.ThroughputStats;
import org.graylog2.system.stats.ClusterStatsService;
import org.graylog2.telemetry.TelemetryMetaData;
import org.graylog2.telemetry.dto.ClusterDataSet;

import javax.annotation.Nullable;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;

@Singleton
public class TelemetryClusterService {
    private final ClusterStatsService clusterStatsService;
    private final Counts counts;
    private final ThroughputStats throughputStats;
    private final Supplier cachedDataSet;
    private final long reportIntervalMs;

    @Inject
    public TelemetryClusterService(ClusterStatsService clusterStatsService,
                                   @Nullable Counts counts,
                                   ThroughputStats throughputStats,
                                   @Named("telemetry_cache_timeout") Duration cacheTimeout,
                                   @Named("telemetry_report_interval") Duration reportInterval) {
        this.clusterStatsService = clusterStatsService;
        this.counts = counts;
        this.throughputStats = throughputStats;
        this.cachedDataSet = Suppliers.memoizeWithExpiration(cachingSupplier(), cacheTimeout.getQuantity(), cacheTimeout.getUnit());
        this.reportIntervalMs = reportInterval.toMilliseconds();
    }

    public ClusterDataSet buildClusterDataSet() {
        return cachedDataSet.get();
    }

    private Supplier cachingSupplier() {
        return new Supplier() {
            @Override
            public ClusterDataSet get() {
                return ClusterDataSet.create(
                        String.valueOf(TelemetryMetaData.VERSION),
                        System.currentTimeMillis(),
                        reportIntervalMs,
                        counts == null ? 0 : counts.total(),
                        throughputStats.getCurrentThroughput(),
                        throughputStats.getCurrentStreamThroughputValues(),
                        clusterStatsService.clusterStats()
                );
            }
        };
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy