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

org.metricssampler.extensions.base.SelfMetricsReader Maven / Gradle / Ivy

package org.metricssampler.extensions.base;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.metricssampler.reader.AbstractMetricsReader;
import org.metricssampler.reader.BulkMetricsReader;
import org.metricssampler.reader.MetricName;
import org.metricssampler.reader.MetricValue;
import org.metricssampler.reader.SimpleMetricName;
import org.metricssampler.resources.SamplerStats;
import org.metricssampler.resources.SamplerTask;
import org.metricssampler.resources.SharedResource;
import org.metricssampler.service.GlobalRegistry;

public class SelfMetricsReader extends AbstractMetricsReader implements BulkMetricsReader{
	public SelfMetricsReader(final SelfInputConfig config) {
		super(config);
	}

	@Override
	public void open() {
		// nothing to do here
	}

	@Override
	public void close() {
		// nothing to do here
	}

	@Override
	public Iterable readNames() {
		return readAllMetrics().keySet();
	}

	@Override
	public Map readAllMetrics() {
		final Map result = new HashMap<>();
		final GlobalRegistry registry = GlobalRegistry.getInstance();
		final long timestamp = System.currentTimeMillis();
		for(final SamplerTask task : registry.getTasks()) {
			final SamplerStats stats = task.getStats();
			final String prefix = "samplers." + task.getName() + ".";
			result.put(new SimpleMetricName(prefix + "activeTime", "The number of seconds since the last activation of the sampler"), new MetricValue(timestamp, stats.getActiveTime()));
			result.put(new SimpleMetricName(prefix + "sampleSuccessCount", "The total number of successful samplings"), new MetricValue(timestamp, stats.getSampleSuccessCount()));
			result.put(new SimpleMetricName(prefix + "sampleFailureCount", "The total number of failed samplings due to unexpected exception"), new MetricValue(timestamp, stats.getSampleFailureCount()));
			result.put(new SimpleMetricName(prefix + "connectCount", "The total number of times the reader tried to connect to the input"), new MetricValue(timestamp, stats.getConnectCount()));
			result.put(new SimpleMetricName(prefix + "disconnectCount", "The total number of times the reader tried to disconnect from the input"), new MetricValue(timestamp, stats.getDisconnectCount()));
			result.put(new SimpleMetricName(prefix + "metricsCount", "The total number of metrics sampled the last time"), new MetricValue(timestamp, stats.getMetricsCount()));
			result.put(new SimpleMetricName(prefix + "sampleDuration", "The last sample duration in seconds"), new MetricValue(timestamp, stats.getSampleDuration()));
		}

		for (final SharedResource sharedResource : registry.getSharedResources()) {
			final Map stats = sharedResource.getStats();
			for (final Entry entry : stats.entrySet()) {
				result.put(new SimpleMetricName(entry.getKey(), ""), new MetricValue(timestamp, entry.getValue()));
			}
		}
		return result;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy