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

com.taotao.boot.monitor.collect.CollectorMetrics Maven / Gradle / Ivy

The newest version!
package com.taotao.boot.monitor.collect;

import com.taotao.boot.monitor.model.Report;
import com.taotao.boot.monitor.model.Report.ReportItem;
import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.binder.BaseUnits;
import io.micrometer.core.instrument.binder.MeterBinder;

import java.util.Arrays;
import java.util.List;

/**
 * 监控指标采集 添加actuator/metrics指标
 */
public class CollectorMetrics implements MeterBinder {


	private final HealthCheckProvider healthCheckProvider;

	public CollectorMetrics(HealthCheckProvider healthCheckProvider) {
		this.healthCheckProvider = healthCheckProvider;
	}

	@Override
	public void bindTo(MeterRegistry meterRegistry) {
		Report report = healthCheckProvider.getReport(false);
		List keys = report.getKeys();

		keys.forEach(field -> {
			String[] fields = field.split("\\.");
			List tags = Arrays.stream(fields).map(f -> Tag.of(f, f)).toList();
			ReportItem reportItem = report.getByKey(field);
			if (null != reportItem) {
				Gauge.builder(field.replace("_","."), healthCheckProvider, (healthCheckProvider1) -> {
						Report report1 = healthCheckProvider1.getReport(false);
						ReportItem reportItem1 = report1.getByKey(field);
						if (null != reportItem1) {
							Object value = reportItem1.getValue();
							if (value instanceof Number number) {
								return number.doubleValue();
							}
							else {
								return 0;
							}
						}

						return 0;
					})
					.tags(tags)
					//.tag(METRIC_CATEGORY, name)
					.description(reportItem.getDesc())
					.baseUnit(BaseUnits.MILLISECONDS)
					.register(meterRegistry);
			}
		});

	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy