mirror of
https://gitee.com/san-bing/JChargePointProtocol
synced 2026-05-04 09:59:55 +08:00
云快充1.5.0 初始化
This commit is contained in:
57
jcpp-infrastructure-stats/pom.xml
Normal file
57
jcpp-infrastructure-stats/pom.xml
Normal file
@@ -0,0 +1,57 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
|
||||
抖音关注:程序员三丙
|
||||
知识星球:https://t.zsxq.com/j9b21
|
||||
|
||||
-->
|
||||
<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">
|
||||
<parent>
|
||||
<groupId>sanbing</groupId>
|
||||
<artifactId>jcpp-parent</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>jcpp-infrastructure-stats</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<name>JChargePointProtocol Infrastructure Stats Module</name>
|
||||
<description>基础监控模块</description>
|
||||
|
||||
<properties>
|
||||
<main.dir>${basedir}/..</main.dir>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-tomcat</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-undertow</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.micrometer</groupId>
|
||||
<artifactId>micrometer-registry-prometheus</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,37 @@
|
||||
/**
|
||||
* 抖音关注:程序员三丙
|
||||
* 知识星球:https://t.zsxq.com/j9b21
|
||||
*/
|
||||
package sanbing.jcpp.infrastructure.stats;
|
||||
|
||||
import io.micrometer.core.instrument.Counter;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class DefaultCounter {
|
||||
private final AtomicInteger aiCounter;
|
||||
private final Counter micrometerCounter;
|
||||
|
||||
public DefaultCounter(AtomicInteger aiCounter, Counter micrometerCounter) {
|
||||
this.aiCounter = aiCounter;
|
||||
this.micrometerCounter = micrometerCounter;
|
||||
}
|
||||
|
||||
public void increment() {
|
||||
aiCounter.incrementAndGet();
|
||||
micrometerCounter.increment();
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
aiCounter.set(0);
|
||||
}
|
||||
|
||||
public int get() {
|
||||
return aiCounter.get();
|
||||
}
|
||||
|
||||
public void add(int delta){
|
||||
aiCounter.addAndGet(delta);
|
||||
micrometerCounter.increment(delta);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
/**
|
||||
* 抖音关注:程序员三丙
|
||||
* 知识星球:https://t.zsxq.com/j9b21
|
||||
*/
|
||||
package sanbing.jcpp.infrastructure.stats;
|
||||
|
||||
public class DefaultMessagesStats implements MessagesStats {
|
||||
private final StatsCounter totalCounter;
|
||||
private final StatsCounter successfulCounter;
|
||||
private final StatsCounter failedCounter;
|
||||
|
||||
public DefaultMessagesStats(StatsCounter totalCounter, StatsCounter successfulCounter, StatsCounter failedCounter) {
|
||||
this.totalCounter = totalCounter;
|
||||
this.successfulCounter = successfulCounter;
|
||||
this.failedCounter = failedCounter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void incrementTotal(int amount) {
|
||||
totalCounter.add(amount);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void incrementSuccessful(int amount) {
|
||||
successfulCounter.add(amount);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void incrementFailed(int amount) {
|
||||
failedCounter.add(amount);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getTotal() {
|
||||
return totalCounter.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSuccessful() {
|
||||
return successfulCounter.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getFailed() {
|
||||
return failedCounter.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reset() {
|
||||
totalCounter.clear();
|
||||
successfulCounter.clear();
|
||||
failedCounter.clear();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
/**
|
||||
* 抖音关注:程序员三丙
|
||||
* 知识星球:https://t.zsxq.com/j9b21
|
||||
*/
|
||||
package sanbing.jcpp.infrastructure.stats;
|
||||
|
||||
import io.micrometer.common.util.StringUtils;
|
||||
import io.micrometer.core.instrument.Counter;
|
||||
import io.micrometer.core.instrument.MeterRegistry;
|
||||
import io.micrometer.core.instrument.Tags;
|
||||
import io.micrometer.core.instrument.Timer;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
@Service
|
||||
public class DefaultStatsFactory implements StatsFactory {
|
||||
private static final String TOTAL_MSGS = "totalMsgs";
|
||||
private static final String SUCCESSFUL_MSGS = "successfulMsgs";
|
||||
private static final String FAILED_MSGS = "failedMsgs";
|
||||
|
||||
private static final String STATS_NAME_TAG = "statsName";
|
||||
|
||||
private static final Counter STUB_COUNTER = new StubCounter();
|
||||
|
||||
@Resource
|
||||
private MeterRegistry meterRegistry;
|
||||
|
||||
@Value("${metrics.enabled:true}")
|
||||
private Boolean metricsEnabled;
|
||||
|
||||
@Value("${metrics.timer.percentiles:1.0}")
|
||||
private String timerPercentilesStr;
|
||||
|
||||
private double[] timerPercentiles;
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
if (StringUtils.isNotEmpty(timerPercentilesStr)) {
|
||||
String[] split = timerPercentilesStr.split(",");
|
||||
timerPercentiles = new double[split.length];
|
||||
for (int i = 0; i < split.length; i++) {
|
||||
timerPercentiles[i] = Double.parseDouble(split[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public StatsCounter createStatsCounter(String key, String statsName, String... otherTags) {
|
||||
String[] tags = getTags(statsName, otherTags);
|
||||
return new StatsCounter(
|
||||
new AtomicInteger(0),
|
||||
metricsEnabled ? meterRegistry.counter(key, tags) : STUB_COUNTER,
|
||||
statsName
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public DefaultCounter createDefaultCounter(String key, String... tags) {
|
||||
return new DefaultCounter(
|
||||
new AtomicInteger(0),
|
||||
metricsEnabled ?
|
||||
meterRegistry.counter(key, tags)
|
||||
: STUB_COUNTER
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MessagesStats createMessagesStats(String key, String... tags) {
|
||||
StatsCounter totalCounter = createStatsCounter(key, TOTAL_MSGS, tags);
|
||||
StatsCounter successfulCounter = createStatsCounter(key, SUCCESSFUL_MSGS, tags);
|
||||
StatsCounter failedCounter = createStatsCounter(key, FAILED_MSGS, tags);
|
||||
return new DefaultMessagesStats(totalCounter, successfulCounter, failedCounter);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Timer createTimer(String key, String... tags) {
|
||||
Timer.Builder timerBuilder = Timer.builder(key)
|
||||
.tags(tags)
|
||||
.publishPercentiles();
|
||||
if (timerPercentiles != null && timerPercentiles.length > 0) {
|
||||
timerBuilder.publishPercentiles(timerPercentiles);
|
||||
}
|
||||
return timerBuilder.register(meterRegistry);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T extends Number> T createGauge(String key, T number, String... tags) {
|
||||
return meterRegistry.gauge(key, Tags.of(tags), number);
|
||||
}
|
||||
|
||||
private static String[] getTags(String statsName, String[] otherTags) {
|
||||
String[] tags = new String[]{STATS_NAME_TAG, statsName};
|
||||
if (otherTags.length > 0) {
|
||||
if (otherTags.length % 2 != 0) {
|
||||
throw new IllegalArgumentException("Invalid tags array size");
|
||||
}
|
||||
tags = ArrayUtils.addAll(tags, otherTags);
|
||||
}
|
||||
return tags;
|
||||
}
|
||||
|
||||
|
||||
private static class StubCounter implements Counter {
|
||||
@Override
|
||||
public void increment(double amount) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public double count() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Id getId() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
/**
|
||||
* 抖音关注:程序员三丙
|
||||
* 知识星球:https://t.zsxq.com/j9b21
|
||||
*/
|
||||
package sanbing.jcpp.infrastructure.stats;
|
||||
|
||||
public interface MessagesStats {
|
||||
default void incrementTotal() {
|
||||
incrementTotal(1);
|
||||
}
|
||||
|
||||
void incrementTotal(int amount);
|
||||
|
||||
default void incrementSuccessful() {
|
||||
incrementSuccessful(1);
|
||||
}
|
||||
|
||||
void incrementSuccessful(int amount);
|
||||
|
||||
default void incrementFailed() {
|
||||
incrementFailed(1);
|
||||
}
|
||||
|
||||
void incrementFailed(int amount);
|
||||
|
||||
int getTotal();
|
||||
|
||||
int getSuccessful();
|
||||
|
||||
int getFailed();
|
||||
|
||||
void reset();
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
/**
|
||||
* 抖音关注:程序员三丙
|
||||
* 知识星球:https://t.zsxq.com/j9b21
|
||||
*/
|
||||
package sanbing.jcpp.infrastructure.stats;
|
||||
|
||||
import io.micrometer.core.instrument.Counter;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class StatsCounter extends DefaultCounter {
|
||||
private final String name;
|
||||
|
||||
public StatsCounter(AtomicInteger aiCounter, Counter micrometerCounter, String name) {
|
||||
super(aiCounter, micrometerCounter);
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
/**
|
||||
* 抖音关注:程序员三丙
|
||||
* 知识星球:https://t.zsxq.com/j9b21
|
||||
*/
|
||||
package sanbing.jcpp.infrastructure.stats;
|
||||
|
||||
import io.micrometer.core.instrument.Timer;
|
||||
|
||||
public interface StatsFactory {
|
||||
|
||||
/**
|
||||
* 创建状态计数器,默认带一个statsName的Tag,并可以自定义扩展其他Tag
|
||||
*
|
||||
* @param key 指标名
|
||||
* @param statsName statsName的标签值
|
||||
* @param otherTags 其他Tag键值对,参数个数需要是偶数
|
||||
* @return
|
||||
*/
|
||||
StatsCounter createStatsCounter(String key, String statsName, String... otherTags);
|
||||
|
||||
/**
|
||||
* 创建计数器,可自定义Tag
|
||||
*
|
||||
* @param key 指标名
|
||||
* @param tags 自定义Tag键值对,参数个数需要是偶数
|
||||
* @return
|
||||
*/
|
||||
DefaultCounter createDefaultCounter(String key, String... tags);
|
||||
|
||||
/**
|
||||
* 创建消息计数器,消息计数器默认包含三种状态(总数、成功数、失败数)
|
||||
*
|
||||
* @param key 指标名
|
||||
* @param tags 自定义Tag键值对,参数个数需要是偶数
|
||||
* @return
|
||||
*/
|
||||
MessagesStats createMessagesStats(String key, String... tags);
|
||||
|
||||
/**
|
||||
* 创建计时器
|
||||
*
|
||||
* @param key 指标名
|
||||
* @param tags 自定义Tag键值对,参数个数需要是偶数
|
||||
* @return
|
||||
*/
|
||||
Timer createTimer(String key, String... tags);
|
||||
|
||||
/**
|
||||
* 创建计量器,用于记录某个值的当前状态,可以是瞬时数值
|
||||
*
|
||||
* @param key 指标名
|
||||
* @param number 初始值
|
||||
* @param tags 自定义Tag键值对,参数个数需要是偶数
|
||||
* @return
|
||||
* @param <T>
|
||||
*/
|
||||
<T extends Number> T createGauge(String key, T number, String... tags);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* 抖音关注:程序员三丙
|
||||
* 知识星球:https://t.zsxq.com/j9b21
|
||||
*/
|
||||
package sanbing.jcpp.infrastructure.stats;
|
||||
|
||||
import io.micrometer.core.instrument.Timer;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class StatsTimer {
|
||||
|
||||
@Getter
|
||||
private final String name;
|
||||
private final Timer timer;
|
||||
|
||||
private int count;
|
||||
private long totalTime;
|
||||
|
||||
public StatsTimer(String name, Timer micrometerTimer) {
|
||||
this.name = name;
|
||||
this.timer = micrometerTimer;
|
||||
}
|
||||
|
||||
public void record(long timeMs) {
|
||||
count++;
|
||||
totalTime += timeMs;
|
||||
timer.record(timeMs, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
public double getAvg() {
|
||||
if (count == 0) {
|
||||
return 0.0;
|
||||
}
|
||||
return (double) totalTime / count;
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
count = 0;
|
||||
totalTime = 0;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user