This commit is contained in:
2025-11-28 16:23:32 +08:00
commit a9e0e16c29
826 changed files with 89805 additions and 0 deletions

View File

@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>BladeX-Tool</artifactId>
<groupId>org.springblade</groupId>
<version>${revision}</version>
</parent>
<artifactId>blade-starter-metrics</artifactId>
<name>${project.artifactId}</name>
<version>${project.parent.version}</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator-autoconfigure</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-core</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-core</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<!-- Auto -->
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-core-auto</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,68 @@
/**
* BladeX Commercial License Agreement
* Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
* <p>
* Use of this software is governed by the Commercial License Agreement
* obtained after purchasing a license from BladeX.
* <p>
* 1. This software is for development use only under a valid license
* from BladeX.
* <p>
* 2. Redistribution of this software's source code to any third party
* without a commercial license is strictly prohibited.
* <p>
* 3. Licensees may copyright their own code but cannot use segments
* from this software for such purposes. Copyright of this software
* remains with BladeX.
* <p>
* Using this software signifies agreement to this License, and the software
* must not be used for illegal purposes.
* <p>
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
* not liable for any claims arising from secondary or illegal development.
* <p>
* Author: DreamLu (596392912@qq.com)
*/
package org.springblade.core.metrics.druid;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.jdbc.metadata.AbstractDataSourcePoolMetadata;
/**
* druid 连接池 pool meta data
*
* @author L.cm
*/
public class DruidDataSourcePoolMetadata extends AbstractDataSourcePoolMetadata<DruidDataSource> {
public DruidDataSourcePoolMetadata(DruidDataSource dataSource) {
super(dataSource);
}
@Override
public Integer getActive() {
return getDataSource().getActiveCount();
}
@Override
public Integer getMax() {
return getDataSource().getMaxActive();
}
@Override
public Integer getMin() {
return getDataSource().getMinIdle();
}
@Override
public String getValidationQuery() {
return getDataSource().getValidationQuery();
}
@Override
public Boolean getDefaultAutoCommit() {
return getDataSource().isDefaultAutoCommit();
}
}

View File

@@ -0,0 +1,136 @@
/**
* BladeX Commercial License Agreement
* Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
* <p>
* Use of this software is governed by the Commercial License Agreement
* obtained after purchasing a license from BladeX.
* <p>
* 1. This software is for development use only under a valid license
* from BladeX.
* <p>
* 2. Redistribution of this software's source code to any third party
* without a commercial license is strictly prohibited.
* <p>
* 3. Licensees may copyright their own code but cannot use segments
* from this software for such purposes. Copyright of this software
* remains with BladeX.
* <p>
* Using this software signifies agreement to this License, and the software
* must not be used for illegal purposes.
* <p>
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
* not liable for any claims arising from secondary or illegal development.
* <p>
* Author: DreamLu (596392912@qq.com)
*/
package org.springblade.core.metrics.druid;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.stat.JdbcConnectionStat;
import com.alibaba.druid.stat.JdbcDataSourceStat;
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 lombok.RequiredArgsConstructor;
import java.util.Collections;
import java.util.Map;
/**
* druid Metrics
*
* @author L.cm
*/
@RequiredArgsConstructor
public class DruidMetrics implements MeterBinder {
/**
* Prefix used for all Druid metric names.
*/
public static final String DRUID_METRIC_NAME_PREFIX = "druid";
private static final String METRIC_CATEGORY = "name";
private static final String METRIC_NAME_CONNECT_MAX_TIME = DRUID_METRIC_NAME_PREFIX + ".connections.connect.max.time";
private static final String METRIC_NAME_ALIVE_MAX_TIME = DRUID_METRIC_NAME_PREFIX + ".connections.alive.max.time";
private static final String METRIC_NAME_ALIVE_MIN_TIME = DRUID_METRIC_NAME_PREFIX + ".connections.alive.min.time";
private static final String METRIC_NAME_CONNECT_COUNT = DRUID_METRIC_NAME_PREFIX + ".connections.connect.count";
private static final String METRIC_NAME_ACTIVE_COUNT = DRUID_METRIC_NAME_PREFIX + ".connections.active.count";
private static final String METRIC_NAME_CLOSE_COUNT = DRUID_METRIC_NAME_PREFIX + ".connections.close.count";
private static final String METRIC_NAME_ERROR_COUNT = DRUID_METRIC_NAME_PREFIX + ".connections.error.count";
private static final String METRIC_NAME_CONNECT_ERROR_COUNT = DRUID_METRIC_NAME_PREFIX + ".connections.connect.error.count";
private static final String METRIC_NAME_COMMIT_COUNT = DRUID_METRIC_NAME_PREFIX + ".connections.commit.count";
private static final String METRIC_NAME_ROLLBACK_COUNT = DRUID_METRIC_NAME_PREFIX + ".connections.rollback.count";
private final Map<String, DruidDataSource> druidDataSourceMap;
private final Iterable<Tag> tags;
public DruidMetrics(Map<String, DruidDataSource> druidDataSourceMap) {
this(druidDataSourceMap, Collections.emptyList());
}
@Override
public void bindTo(MeterRegistry meterRegistry) {
druidDataSourceMap.forEach((name, dataSource) -> {
JdbcDataSourceStat dsStats = dataSource.getDataSourceStat();
JdbcConnectionStat connectionStat = dsStats.getConnectionStat();
// time
Gauge.builder(METRIC_NAME_CONNECT_MAX_TIME, connectionStat, JdbcConnectionStat::getConnectMillisMax)
.description("Connection connect max time")
.tags(tags)
.tag(METRIC_CATEGORY, name)
.baseUnit(BaseUnits.MILLISECONDS)
.register(meterRegistry);
Gauge.builder(METRIC_NAME_ALIVE_MAX_TIME, connectionStat, JdbcConnectionStat::getAliveMillisMax)
.description("Connection alive max time")
.tags(tags)
.tag(METRIC_CATEGORY, name)
.baseUnit(BaseUnits.MILLISECONDS)
.register(meterRegistry);
Gauge.builder(METRIC_NAME_ALIVE_MIN_TIME, connectionStat, JdbcConnectionStat::getAliveMillisMin)
.description("Connection alive min time")
.tags(tags)
.tag(METRIC_CATEGORY, name)
.baseUnit(BaseUnits.MILLISECONDS)
.register(meterRegistry);
// count
Gauge.builder(METRIC_NAME_ACTIVE_COUNT, connectionStat, JdbcConnectionStat::getActiveCount)
.description("Connection active count")
.tags(tags)
.tag(METRIC_CATEGORY, name)
.register(meterRegistry);
Gauge.builder(METRIC_NAME_CONNECT_COUNT, connectionStat, JdbcConnectionStat::getConnectCount)
.description("Connection connect count")
.tags(tags)
.tag(METRIC_CATEGORY, name)
.register(meterRegistry);
Gauge.builder(METRIC_NAME_CLOSE_COUNT, connectionStat, JdbcConnectionStat::getCloseCount)
.description("Connection close count")
.tags(tags)
.tag(METRIC_CATEGORY, name)
.register(meterRegistry);
Gauge.builder(METRIC_NAME_ERROR_COUNT, connectionStat, JdbcConnectionStat::getErrorCount)
.description("Connection error count")
.tags(tags)
.tag(METRIC_CATEGORY, name)
.register(meterRegistry);
Gauge.builder(METRIC_NAME_CONNECT_ERROR_COUNT, connectionStat, JdbcConnectionStat::getConnectErrorCount)
.description("Connection connect error count")
.tags(tags)
.tag(METRIC_CATEGORY, name)
.register(meterRegistry);
Gauge.builder(METRIC_NAME_COMMIT_COUNT, connectionStat, JdbcConnectionStat::getCommitCount)
.description("Connecting commit count")
.tags(tags)
.tag(METRIC_CATEGORY, name)
.register(meterRegistry);
Gauge.builder(METRIC_NAME_ROLLBACK_COUNT, connectionStat, JdbcConnectionStat::getRollbackCount)
.description("Connection rollback count")
.tags(tags)
.tag(METRIC_CATEGORY, name)
.register(meterRegistry);
});
}
}

View File

@@ -0,0 +1,107 @@
/**
* BladeX Commercial License Agreement
* Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
* <p>
* Use of this software is governed by the Commercial License Agreement
* obtained after purchasing a license from BladeX.
* <p>
* 1. This software is for development use only under a valid license
* from BladeX.
* <p>
* 2. Redistribution of this software's source code to any third party
* without a commercial license is strictly prohibited.
* <p>
* 3. Licensees may copyright their own code but cannot use segments
* from this software for such purposes. Copyright of this software
* remains with BladeX.
* <p>
* Using this software signifies agreement to this License, and the software
* must not be used for illegal purposes.
* <p>
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
* not liable for any claims arising from secondary or illegal development.
* <p>
* Author: DreamLu (596392912@qq.com)
*/
package org.springblade.core.metrics.druid;
import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.pool.DruidDataSource;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimpleMetricsExportAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.jdbc.DataSourceUnwrapper;
import org.springframework.boot.jdbc.metadata.DataSourcePoolMetadataProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.util.StringUtils;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
/**
* DruidDataSourceMetadata Provide
*
* @author L.cm
*/
@AutoConfiguration(
after = {MetricsAutoConfiguration.class, DataSourceAutoConfiguration.class, SimpleMetricsExportAutoConfiguration.class}
)
@ConditionalOnClass({DruidDataSource.class, MeterRegistry.class})
@ConditionalOnBean({DataSource.class, MeterRegistry.class})
public class DruidMetricsConfiguration {
private static final String DATASOURCE_SUFFIX = "dataSource";
@Bean
public DataSourcePoolMetadataProvider druidDataSourceMetadataProvider() {
return (dataSource) -> {
DruidDataSource druidDataSource = DataSourceUnwrapper.unwrap(dataSource, DruidDataSource.class);
if (druidDataSource != null) {
return new DruidDataSourcePoolMetadata(druidDataSource);
}
return null;
};
}
@Bean
@ConditionalOnMissingBean
public StatFilter statFilter() {
return new StatFilter();
}
@Bean
public DruidMetrics druidMetrics(ObjectProvider<Map<String, DataSource>> dataSourcesProvider) {
Map<String, DataSource> dataSourceMap = dataSourcesProvider.getIfAvailable(HashMap::new);
Map<String, DruidDataSource> druidDataSourceMap = new HashMap<>(2);
dataSourceMap.forEach((name, dataSource) -> {
// 保证连接池数据和 DataSourcePoolMetadataProvider 的一致
DruidDataSource druidDataSource = DataSourceUnwrapper.unwrap(dataSource, DruidDataSource.class);
if (druidDataSource != null) {
druidDataSourceMap.put(getDataSourceName(name), druidDataSource);
}
});
return druidDataSourceMap.isEmpty() ? null : new DruidMetrics(druidDataSourceMap);
}
/**
* Get the name of a DataSource based on its {@code beanName}.
*
* @param beanName the name of the data source bean
* @return a name for the given data source
*/
private static String getDataSourceName(String beanName) {
if (beanName.length() > DATASOURCE_SUFFIX.length()
&& StringUtils.endsWithIgnoreCase(beanName, DATASOURCE_SUFFIX)) {
return beanName.substring(0, beanName.length() - DATASOURCE_SUFFIX.length());
}
return beanName;
}
}

View File

@@ -0,0 +1,89 @@
/**
* BladeX Commercial License Agreement
* Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
* <p>
* Use of this software is governed by the Commercial License Agreement
* obtained after purchasing a license from BladeX.
* <p>
* 1. This software is for development use only under a valid license
* from BladeX.
* <p>
* 2. Redistribution of this software's source code to any third party
* without a commercial license is strictly prohibited.
* <p>
* 3. Licensees may copyright their own code but cannot use segments
* from this software for such purposes. Copyright of this software
* remains with BladeX.
* <p>
* Using this software signifies agreement to this License, and the software
* must not be used for illegal purposes.
* <p>
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
* not liable for any claims arising from secondary or illegal development.
* <p>
* Author: DreamLu (596392912@qq.com)
*/
package org.springblade.core.metrics.sentinel;
import com.alibaba.csp.sentinel.metric.extension.MetricExtension;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import io.micrometer.core.instrument.Metrics;
import io.micrometer.core.instrument.Tags;
import org.springblade.core.auto.service.AutoService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
/**
* Sentinel Metrics Extension
*
* @author L.cm
*/
@AutoService(MetricExtension.class)
public class SentinelMetricsExtension implements MetricExtension {
public static final String PASS_REQUESTS_TOTAL = "sentinel_pass_requests_total";
public static final String BLOCK_REQUESTS_TOTAL = "sentinel_block_requests_total";
public static final String SUCCESS_REQUESTS_TOTAL = "sentinel_success_requests_total";
public static final String EXCEPTION_REQUESTS_TOTAL = "sentinel_exception_requests_total";
public static final String REQUESTS_LATENCY_SECONDS = "sentinel_requests_latency_seconds";
public static final String CURRENT_THREADS = "sentinel_current_threads";
public static final String DEFAULT_TAT_NAME = "resource";
private final AtomicLong CURRENT_THREAD_COUNT = new AtomicLong(0);
@Override
public void addPass(String resource, int n, Object... args) {
Metrics.counter(PASS_REQUESTS_TOTAL, DEFAULT_TAT_NAME, resource).increment(n);
}
@Override
public void addBlock(String resource, int n, String origin, BlockException ex, Object... args) {
Metrics.counter(BLOCK_REQUESTS_TOTAL, resource, ex.getClass().getSimpleName(), ex.getRuleLimitApp(), origin).increment(n);
}
@Override
public void addSuccess(String resource, int n, Object... args) {
Metrics.counter(SUCCESS_REQUESTS_TOTAL, DEFAULT_TAT_NAME, resource).increment(n);
}
@Override
public void addException(String resource, int n, Throwable throwable) {
Metrics.counter(EXCEPTION_REQUESTS_TOTAL, DEFAULT_TAT_NAME, resource).increment(n);
}
@Override
public void addRt(String resource, long rt, Object... args) {
Metrics.timer(REQUESTS_LATENCY_SECONDS, DEFAULT_TAT_NAME, resource).record(rt, TimeUnit.MICROSECONDS);
}
@Override
public void increaseThreadNum(String resource, Object... args) {
Tags tags = Tags.of(DEFAULT_TAT_NAME, resource);
Metrics.gauge(CURRENT_THREADS, tags, CURRENT_THREAD_COUNT, AtomicLong::incrementAndGet);
}
@Override
public void decreaseThreadNum(String resource, Object... args) {
Tags tags = Tags.of(DEFAULT_TAT_NAME, resource);
Metrics.gauge(CURRENT_THREADS, tags, CURRENT_THREAD_COUNT, AtomicLong::decrementAndGet);
}
}

View File

@@ -0,0 +1,273 @@
/**
* BladeX Commercial License Agreement
* Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
* <p>
* Use of this software is governed by the Commercial License Agreement
* obtained after purchasing a license from BladeX.
* <p>
* 1. This software is for development use only under a valid license
* from BladeX.
* <p>
* 2. Redistribution of this software's source code to any third party
* without a commercial license is strictly prohibited.
* <p>
* 3. Licensees may copyright their own code but cannot use segments
* from this software for such purposes. Copyright of this software
* remains with BladeX.
* <p>
* Using this software signifies agreement to this License, and the software
* must not be used for illegal purposes.
* <p>
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
* not liable for any claims arising from secondary or illegal development.
* <p>
* Author: DreamLu (596392912@qq.com)
*/
package org.springblade.core.metrics.undertow;
import io.micrometer.core.instrument.*;
import io.micrometer.core.instrument.binder.BaseUnits;
import io.undertow.Undertow;
import io.undertow.server.ConnectorStatistics;
import io.undertow.server.session.SessionManagerStatistics;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.boot.web.embedded.undertow.UndertowServletWebServer;
import org.springframework.boot.web.embedded.undertow.UndertowWebServer;
import org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext;
import org.springframework.boot.web.server.WebServer;
import org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.util.ReflectionUtils;
import org.xnio.management.XnioWorkerMXBean;
import java.lang.reflect.Field;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
/**
* Undertow Metrics
*
* @author L.cm
*/
@RequiredArgsConstructor
public class UndertowMetrics implements ApplicationListener<ApplicationStartedEvent> {
/**
* Prefix used for all Undertow metric names.
*/
public static final String UNDERTOW_METRIC_NAME_PREFIX = "undertow";
/**
* XWorker
*/
private static final String METRIC_NAME_X_WORK_WORKER_POOL_CORE_SIZE = UNDERTOW_METRIC_NAME_PREFIX + ".xwork.worker.pool.core.size";
private static final String METRIC_NAME_X_WORK_WORKER_POOL_MAX_SIZE = UNDERTOW_METRIC_NAME_PREFIX + ".xwork.worker.pool.max.size";
private static final String METRIC_NAME_X_WORK_WORKER_POOL_SIZE = UNDERTOW_METRIC_NAME_PREFIX + ".xwork.worker.pool.size";
private static final String METRIC_NAME_X_WORK_WORKER_THREAD_BUSY_COUNT = UNDERTOW_METRIC_NAME_PREFIX + ".xwork.worker.thread.busy.count";
private static final String METRIC_NAME_X_WORK_IO_THREAD_COUNT = UNDERTOW_METRIC_NAME_PREFIX + ".xwork.io.thread.count";
private static final String METRIC_NAME_X_WORK_WORKER_QUEUE_SIZE = UNDERTOW_METRIC_NAME_PREFIX + ".xwork.worker.queue.size";
/**
* connectors
*/
private static final String METRIC_NAME_CONNECTORS_REQUESTS_COUNT = UNDERTOW_METRIC_NAME_PREFIX + ".connectors.requests.count";
private static final String METRIC_NAME_CONNECTORS_REQUESTS_ERROR_COUNT = UNDERTOW_METRIC_NAME_PREFIX + ".connectors.requests.error.count";
private static final String METRIC_NAME_CONNECTORS_REQUESTS_ACTIVE = UNDERTOW_METRIC_NAME_PREFIX + ".connectors.requests.active";
private static final String METRIC_NAME_CONNECTORS_REQUESTS_ACTIVE_MAX = UNDERTOW_METRIC_NAME_PREFIX + ".connectors.requests.active.max";
private static final String METRIC_NAME_CONNECTORS_BYTES_SENT = UNDERTOW_METRIC_NAME_PREFIX + ".connectors.bytes.sent";
private static final String METRIC_NAME_CONNECTORS_BYTES_RECEIVED = UNDERTOW_METRIC_NAME_PREFIX + ".connectors.bytes.received";
private static final String METRIC_NAME_CONNECTORS_PROCESSING_TIME = UNDERTOW_METRIC_NAME_PREFIX + ".connectors.processing.time";
private static final String METRIC_NAME_CONNECTORS_PROCESSING_TIME_MAX = UNDERTOW_METRIC_NAME_PREFIX + ".connectors.processing.time.max";
private static final String METRIC_NAME_CONNECTORS_CONNECTIONS_ACTIVE = UNDERTOW_METRIC_NAME_PREFIX + ".connectors.connections.active";
private static final String METRIC_NAME_CONNECTORS_CONNECTIONS_ACTIVE_MAX = UNDERTOW_METRIC_NAME_PREFIX + ".connectors.connections.active.max";
/**
* session
*/
private static final String METRIC_NAME_SESSIONS_ACTIVE_MAX = UNDERTOW_METRIC_NAME_PREFIX + ".sessions.active.max";
private static final String METRIC_NAME_SESSIONS_ACTIVE_CURRENT = UNDERTOW_METRIC_NAME_PREFIX + ".sessions.active.current";
private static final String METRIC_NAME_SESSIONS_CREATED = UNDERTOW_METRIC_NAME_PREFIX + ".sessions.created";
private static final String METRIC_NAME_SESSIONS_EXPIRED = UNDERTOW_METRIC_NAME_PREFIX + ".sessions.expired";
private static final String METRIC_NAME_SESSIONS_REJECTED = UNDERTOW_METRIC_NAME_PREFIX + ".sessions.rejected";
private static final String METRIC_NAME_SESSIONS_ALIVE_MAX = UNDERTOW_METRIC_NAME_PREFIX + ".sessions.alive.max";
private static final Field UNDERTOW_FIELD;
private final Iterable<Tag> tags;
public UndertowMetrics() {
this.tags = Collections.emptyList();
}
@Override
public void onApplicationEvent(ApplicationStartedEvent event) {
ConfigurableApplicationContext applicationContext = event.getApplicationContext();
// find UndertowWebServer
UndertowWebServer undertowWebServer = findUndertowWebServer(applicationContext);
if (undertowWebServer == null) {
return;
}
Undertow undertow = getUndertow(undertowWebServer);
XnioWorkerMXBean xWorker = undertow.getWorker().getMXBean();
MeterRegistry registry = applicationContext.getBean(MeterRegistry.class);
// xWorker 指标
registerXWorker(registry, xWorker);
// 连接信息指标
List<Undertow.ListenerInfo> listenerInfoList = undertow.getListenerInfo();
listenerInfoList.forEach(listenerInfo -> registerConnectorStatistics(registry, listenerInfo));
// 如果是 web 监控,添加 session 指标
if (undertowWebServer instanceof UndertowServletWebServer) {
SessionManagerStatistics statistics = ((UndertowServletWebServer) undertowWebServer).getDeploymentManager()
.getDeployment()
.getSessionManager()
.getStatistics();
registerSessionStatistics(registry, statistics);
}
}
private void registerXWorker(MeterRegistry registry, XnioWorkerMXBean workerMXBean) {
Gauge.builder(METRIC_NAME_X_WORK_WORKER_POOL_CORE_SIZE, workerMXBean, XnioWorkerMXBean::getCoreWorkerPoolSize)
.description("XWork core worker pool size")
.tags(tags)
.tag("name", workerMXBean.getName())
.register(registry);
Gauge.builder(METRIC_NAME_X_WORK_WORKER_POOL_MAX_SIZE, workerMXBean, XnioWorkerMXBean::getMaxWorkerPoolSize)
.description("XWork max worker pool size")
.tags(tags)
.tag("name", workerMXBean.getName())
.register(registry);
Gauge.builder(METRIC_NAME_X_WORK_WORKER_POOL_SIZE, workerMXBean, XnioWorkerMXBean::getWorkerPoolSize)
.description("XWork worker pool size")
.tags(tags)
.tag("name", workerMXBean.getName())
.register(registry);
Gauge.builder(METRIC_NAME_X_WORK_WORKER_THREAD_BUSY_COUNT, workerMXBean, XnioWorkerMXBean::getBusyWorkerThreadCount)
.description("XWork busy worker thread count")
.tags(tags)
.tag("name", workerMXBean.getName())
.register(registry);
Gauge.builder(METRIC_NAME_X_WORK_IO_THREAD_COUNT, workerMXBean, XnioWorkerMXBean::getIoThreadCount)
.description("XWork io thread count")
.tags(tags)
.tag("name", workerMXBean.getName())
.register(registry);
Gauge.builder(METRIC_NAME_X_WORK_WORKER_QUEUE_SIZE, workerMXBean, XnioWorkerMXBean::getWorkerQueueSize)
.description("XWork worker queue size")
.tags(tags)
.tag("name", workerMXBean.getName())
.register(registry);
}
private void registerConnectorStatistics(MeterRegistry registry, Undertow.ListenerInfo listenerInfo) {
String protocol = listenerInfo.getProtcol();
ConnectorStatistics statistics = listenerInfo.getConnectorStatistics();
Gauge.builder(METRIC_NAME_CONNECTORS_REQUESTS_COUNT, statistics, ConnectorStatistics::getRequestCount)
.tags(tags)
.tag("protocol", protocol)
.register(registry);
Gauge.builder(METRIC_NAME_CONNECTORS_REQUESTS_ERROR_COUNT, statistics, ConnectorStatistics::getErrorCount)
.tags(tags)
.tag("protocol", protocol)
.register(registry);
Gauge.builder(METRIC_NAME_CONNECTORS_REQUESTS_ACTIVE, statistics, ConnectorStatistics::getActiveRequests)
.tags(tags)
.tag("protocol", protocol)
.baseUnit(BaseUnits.CONNECTIONS)
.register(registry);
Gauge.builder(METRIC_NAME_CONNECTORS_REQUESTS_ACTIVE_MAX, statistics, ConnectorStatistics::getMaxActiveRequests)
.tags(tags)
.tag("protocol", protocol)
.baseUnit(BaseUnits.CONNECTIONS)
.register(registry);
Gauge.builder(METRIC_NAME_CONNECTORS_BYTES_SENT, statistics, ConnectorStatistics::getBytesSent)
.tags(tags)
.tag("protocol", protocol)
.baseUnit(BaseUnits.BYTES)
.register(registry);
Gauge.builder(METRIC_NAME_CONNECTORS_BYTES_RECEIVED, statistics, ConnectorStatistics::getBytesReceived)
.tags(tags)
.tag("protocol", protocol)
.baseUnit(BaseUnits.BYTES)
.register(registry);
Gauge.builder(METRIC_NAME_CONNECTORS_PROCESSING_TIME, statistics, (s) -> TimeUnit.NANOSECONDS.toMillis(s.getProcessingTime()))
.tags(tags)
.tag("protocol", protocol)
.baseUnit(BaseUnits.MILLISECONDS)
.register(registry);
Gauge.builder(METRIC_NAME_CONNECTORS_PROCESSING_TIME_MAX, statistics, (s) -> TimeUnit.NANOSECONDS.toMillis(s.getMaxProcessingTime()))
.tags(tags)
.tag("protocol", protocol)
.baseUnit(BaseUnits.MILLISECONDS)
.register(registry);
Gauge.builder(METRIC_NAME_CONNECTORS_CONNECTIONS_ACTIVE, statistics, ConnectorStatistics::getActiveConnections)
.tags(tags)
.tag("protocol", protocol)
.baseUnit(BaseUnits.CONNECTIONS)
.register(registry);
Gauge.builder(METRIC_NAME_CONNECTORS_CONNECTIONS_ACTIVE_MAX, statistics, ConnectorStatistics::getMaxActiveConnections)
.tags(tags)
.tag("protocol", protocol)
.baseUnit(BaseUnits.CONNECTIONS)
.register(registry);
}
private void registerSessionStatistics(MeterRegistry registry, SessionManagerStatistics statistics) {
Gauge.builder(METRIC_NAME_SESSIONS_ACTIVE_MAX, statistics, SessionManagerStatistics::getMaxActiveSessions)
.tags(tags)
.baseUnit(BaseUnits.SESSIONS)
.register(registry);
Gauge.builder(METRIC_NAME_SESSIONS_ACTIVE_CURRENT, statistics, SessionManagerStatistics::getActiveSessionCount)
.tags(tags)
.baseUnit(BaseUnits.SESSIONS)
.register(registry);
FunctionCounter.builder(METRIC_NAME_SESSIONS_CREATED, statistics, SessionManagerStatistics::getCreatedSessionCount)
.tags(tags)
.baseUnit(BaseUnits.SESSIONS)
.register(registry);
FunctionCounter.builder(METRIC_NAME_SESSIONS_EXPIRED, statistics, SessionManagerStatistics::getExpiredSessionCount)
.tags(tags)
.baseUnit(BaseUnits.SESSIONS)
.register(registry);
FunctionCounter.builder(METRIC_NAME_SESSIONS_REJECTED, statistics, SessionManagerStatistics::getRejectedSessions)
.tags(tags)
.baseUnit(BaseUnits.SESSIONS)
.register(registry);
TimeGauge.builder(METRIC_NAME_SESSIONS_ALIVE_MAX, statistics, TimeUnit.SECONDS, SessionManagerStatistics::getHighestSessionCount)
.tags(tags)
.register(registry);
}
static {
UNDERTOW_FIELD = ReflectionUtils.findField(UndertowWebServer.class, "undertow");
Objects.requireNonNull(UNDERTOW_FIELD, "UndertowWebServer class field undertow not exist.");
ReflectionUtils.makeAccessible(UNDERTOW_FIELD);
}
private static Undertow getUndertow(UndertowWebServer undertowWebServer) {
return (Undertow) ReflectionUtils.getField(UNDERTOW_FIELD, undertowWebServer);
}
private static UndertowWebServer findUndertowWebServer(ConfigurableApplicationContext applicationContext) {
WebServer webServer;
if (applicationContext instanceof ReactiveWebServerApplicationContext) {
webServer = ((ReactiveWebServerApplicationContext) applicationContext).getWebServer();
} else if (applicationContext instanceof ServletWebServerApplicationContext) {
webServer = ((ServletWebServerApplicationContext) applicationContext).getWebServer();
} else {
return null;
}
if (webServer instanceof UndertowWebServer) {
return (UndertowWebServer) webServer;
}
return null;
}
}

View File

@@ -0,0 +1,57 @@
/**
* BladeX Commercial License Agreement
* Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
* <p>
* Use of this software is governed by the Commercial License Agreement
* obtained after purchasing a license from BladeX.
* <p>
* 1. This software is for development use only under a valid license
* from BladeX.
* <p>
* 2. Redistribution of this software's source code to any third party
* without a commercial license is strictly prohibited.
* <p>
* 3. Licensees may copyright their own code but cannot use segments
* from this software for such purposes. Copyright of this software
* remains with BladeX.
* <p>
* Using this software signifies agreement to this License, and the software
* must not be used for illegal purposes.
* <p>
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
* not liable for any claims arising from secondary or illegal development.
* <p>
* Author: DreamLu (596392912@qq.com)
*/
package org.springblade.core.metrics.undertow;
import io.undertow.Undertow;
import io.undertow.UndertowOptions;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration;
import org.springframework.boot.web.embedded.undertow.UndertowBuilderCustomizer;
import org.springframework.context.annotation.Bean;
/**
* Undertow Metrics 配置
*
* @author L.cm
*/
@AutoConfiguration(before = ServletWebServerFactoryAutoConfiguration.class)
@ConditionalOnClass(Undertow.class)
public class UndertowMetricsConfiguration {
@Bean
public UndertowMetrics undertowMetrics() {
return new UndertowMetrics();
}
@Bean
public UndertowBuilderCustomizer undertowBuilderCustomizerEnableStatistics() {
return builder -> builder.setServerOption(UndertowOptions.ENABLE_STATISTICS, true);
}
}