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

io.github.springboot.httpclient5.actuator.HttpClientEndpoint Maven / Gradle / Ivy

package io.github.springboot.httpclient5.actuator;

import java.util.HashMap;
import java.util.Map;
import java.util.SortedMap;

import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;

import com.codahale.metrics.Gauge;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Snapshot;
import com.codahale.metrics.Timer;

import io.github.springboot.httpclient5.core.config.HttpClient5Config;

/**
 * Http Client endpoint
 *
 * @author sru
 */
@Endpoint(id = "httpclient")
public class HttpClientEndpoint {
	private final HttpClient5Config properties;
	private final MetricRegistry registry;

	public HttpClientEndpoint(HttpClient5Config configuration, MetricRegistry registry) {
		this.properties = configuration;
		this.registry = registry;
	}

	@ReadOperation
	public Map getData() {
		final Map info = new HashMap<>();
		info.put("author", "[email protected]");
		info.put("httpcomponents", "http://hc.apache.org");
		info.put("metrics", getMetrics());
		info.put("config", properties);
		return info;
	}

	public Map getMetrics() {
		final Map metrics = new HashMap<>();
		// gauge
		final SortedMap gauges = registry
				.getGauges((name, metric) -> name.startsWith("org.apache.hc.client5.http.io.HttpClientConnectionManager."));
		for (final Map.Entry entry : gauges.entrySet()) {
			metrics.put(entry.getKey(), entry.getValue().getValue());
		}
		// timer
		final SortedMap timers = registry
				.getTimers((name, metric) -> name.startsWith("org.apache.hc.client5.http.classic.HttpClient."));
		for (final Map.Entry entry : timers.entrySet()) {
			metrics.putAll(convertTimerToMap(entry.getKey(), entry.getValue()));
		}
		return metrics;
	}

	public Map convertTimerToMap(String name, Timer timer) {
		final Map map = new HashMap<>();
		map.put(name + ".count", timer.getCount());
		map.put(name + ".oneMinuteRate", timer.getOneMinuteRate());
		map.put(name + ".fiveMinuteRate", timer.getFiveMinuteRate());
		map.put(name + ".fifteenMinuteRate", timer.getFifteenMinuteRate());
		map.put(name + ".meanRate", timer.getMeanRate());
		final Snapshot snapshot = timer.getSnapshot();
		map.put(name + ".snapshot.mean", snapshot.getMean());
		map.put(name + ".snapshot.max", snapshot.getMax());
		map.put(name + ".snapshot.min", snapshot.getMin());
		map.put(name + ".snapshot.median", snapshot.getMedian());
		map.put(name + ".snapshot.stdDev", snapshot.getStdDev());
		map.put(name + ".snapshot.75thPercentile", snapshot.get75thPercentile());
		map.put(name + ".snapshot.95thPercentile", snapshot.get95thPercentile());
		map.put(name + ".snapshot.98thPercentile", snapshot.get98thPercentile());
		map.put(name + ".snapshot.99thPercentile", snapshot.get99thPercentile());
		map.put(name + ".snapshot.999thPercentile", snapshot.get999thPercentile());
		return map;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy