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

org.springframework.boot.actuate.metrics.MetricsEndpoint Maven / Gradle / Ivy

/*
 * Copyright 2012-2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.boot.actuate.metrics;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.stream.Collectors;

import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Statistic;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.composite.CompositeMeterRegistry;

import org.springframework.boot.actuate.endpoint.InvalidEndpointRequestException;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.boot.actuate.endpoint.annotation.Selector;
import org.springframework.lang.Nullable;

/**
 * An {@link Endpoint} for exposing the metrics held by a {@link MeterRegistry}.
 *
 * @author Jon Schneider
 * @author Phillip Webb
 * @since 2.0.0
 */
@Endpoint(id = "metrics")
public class MetricsEndpoint {

	private final MeterRegistry registry;

	public MetricsEndpoint(MeterRegistry registry) {
		this.registry = registry;
	}

	@ReadOperation
	public ListNamesResponse listNames() {
		Set names = new LinkedHashSet<>();
		collectNames(names, this.registry);
		return new ListNamesResponse(names);
	}

	private void collectNames(Set names, MeterRegistry registry) {
		if (registry instanceof CompositeMeterRegistry) {
			((CompositeMeterRegistry) registry).getRegistries()
					.forEach((member) -> collectNames(names, member));
		}
		else {
			registry.getMeters().stream().map(this::getName).forEach(names::add);
		}
	}

	private String getName(Meter meter) {
		return meter.getId().getName();
	}

	@ReadOperation
	public MetricResponse metric(@Selector String requiredMetricName,
			@Nullable List tag) {
		List tags = parseTags(tag);
		Collection meters = findFirstMatchingMeters(this.registry,
				requiredMetricName, tags);
		if (meters.isEmpty()) {
			return null;
		}
		Map samples = getSamples(meters);
		Map> availableTags = getAvailableTags(meters);
		tags.forEach((t) -> availableTags.remove(t.getKey()));
		Meter.Id meterId = meters.iterator().next().getId();
		return new MetricResponse(requiredMetricName, meterId.getDescription(),
				meterId.getBaseUnit(), asList(samples, Sample::new),
				asList(availableTags, AvailableTag::new));
	}

	private List parseTags(List tags) {
		if (tags == null) {
			return Collections.emptyList();
		}
		return tags.stream().map(this::parseTag).collect(Collectors.toList());
	}

	private Tag parseTag(String tag) {
		String[] parts = tag.split(":", 2);
		if (parts.length != 2) {
			throw new InvalidEndpointRequestException(
					"Each tag parameter must be in the form 'key:value' but was: " + tag,
					"Each tag parameter must be in the form 'key:value'");
		}
		return Tag.of(parts[0], parts[1]);
	}

	private Collection findFirstMatchingMeters(MeterRegistry registry, String name,
			Iterable tags) {
		if (registry instanceof CompositeMeterRegistry) {
			return findFirstMatchingMeters((CompositeMeterRegistry) registry, name, tags);
		}
		return registry.find(name).tags(tags).meters();
	}

	private Collection findFirstMatchingMeters(CompositeMeterRegistry composite,
			String name, Iterable tags) {
		return composite.getRegistries().stream()
				.map((registry) -> findFirstMatchingMeters(registry, name, tags))
				.filter((matching) -> !matching.isEmpty()).findFirst()
				.orElse(Collections.emptyList());
	}

	private Map getSamples(Collection meters) {
		Map samples = new LinkedHashMap<>();
		meters.forEach((meter) -> mergeMeasurements(samples, meter));
		return samples;
	}

	private void mergeMeasurements(Map samples, Meter meter) {
		meter.measure().forEach((measurement) -> samples.merge(measurement.getStatistic(),
				measurement.getValue(), mergeFunction(measurement.getStatistic())));
	}

	private BiFunction mergeFunction(Statistic statistic) {
		return Statistic.MAX.equals(statistic) ? Double::max : Double::sum;
	}

	private Map> getAvailableTags(Collection meters) {
		Map> availableTags = new HashMap<>();
		meters.forEach((meter) -> mergeAvailableTags(availableTags, meter));
		return availableTags;
	}

	private void mergeAvailableTags(Map> availableTags, Meter meter) {
		meter.getId().getTags().forEach((tag) -> {
			Set value = Collections.singleton(tag.getValue());
			availableTags.merge(tag.getKey(), value, this::merge);
		});
	}

	private  Set merge(Set set1, Set set2) {
		Set result = new HashSet<>(set1.size() + set2.size());
		result.addAll(set1);
		result.addAll(set2);
		return result;
	}

	private  List asList(Map map, BiFunction mapper) {
		return map.entrySet().stream()
				.map((entry) -> mapper.apply(entry.getKey(), entry.getValue()))
				.collect(Collectors.toList());
	}

	/**
	 * Response payload for a metric name listing.
	 */
	public static final class ListNamesResponse {

		private final Set names;

		ListNamesResponse(Set names) {
			this.names = names;
		}

		public Set getNames() {
			return this.names;
		}

	}

	/**
	 * Response payload for a metric name selector.
	 */
	public static final class MetricResponse {

		private final String name;

		private final String description;

		private final String baseUnit;

		private final List measurements;

		private final List availableTags;

		MetricResponse(String name, String description, String baseUnit,
				List measurements, List availableTags) {
			this.name = name;
			this.description = description;
			this.baseUnit = baseUnit;
			this.measurements = measurements;
			this.availableTags = availableTags;
		}

		public String getName() {
			return this.name;
		}

		public String getDescription() {
			return this.description;
		}

		public String getBaseUnit() {
			return this.baseUnit;
		}

		public List getMeasurements() {
			return this.measurements;
		}

		public List getAvailableTags() {
			return this.availableTags;
		}

	}

	/**
	 * A set of tags for further dimensional drilldown and their potential values.
	 */
	public static final class AvailableTag {

		private final String tag;

		private final Set values;

		AvailableTag(String tag, Set values) {
			this.tag = tag;
			this.values = values;
		}

		public String getTag() {
			return this.tag;
		}

		public Set getValues() {
			return this.values;
		}

	}

	/**
	 * A measurement sample combining a {@link Statistic statistic} and a value.
	 */
	public static final class Sample {

		private final Statistic statistic;

		private final Double value;

		Sample(Statistic statistic, Double value) {
			this.statistic = statistic;
			this.value = value;
		}

		public Statistic getStatistic() {
			return this.statistic;
		}

		public Double getValue() {
			return this.value;
		}

		@Override
		public String toString() {
			return "MeasurementSample{" + "statistic=" + this.statistic + ", value="
					+ this.value + '}';
		}

	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy