我想创建一个新的端点来扩展现有的jhimetrics
端点(或扩展现有jhimetrics
的结果(。该应用程序是使用JHipster生成的。所以我所做的是:
- 在
application.yml
文件中将新端点添加到数组中,具体如下:
management:
endpoints:
web:
base-path: /management
exposure:
include: [ ..., "health", "info", "jhimetrics", "roxhens"]
- 创建了具有以下内容的
ExtendedMetricsEndpoint.java
:
// imports, etc...
@Endpoint(id = "roxhens")
public class ExtendedMetricsEndpoint {
private final JHipsterMetricsEndpoint delegate;
private final SimpUserRegistry simpUserRegistry;
public ExtendedMetricsEndpoint(
JHipsterMetricsEndpoint delegate,
SimpUserRegistry simpUserRegistry
) {
this.delegate = delegate;
this.simpUserRegistry = simpUserRegistry;
}
@ReadOperation
public Map<String, Map> getMetrics() {
Map<String, Map> metrics = this.delegate.allMetrics();
HashMap<String, Integer> activeUsers = new HashMap<>();
activeUsers.put("activeUsers", this.simpUserRegistry.getUserCount());
metrics.put("customMetrics", new HashMap(activeUsers));
return metrics;
}
}
- 为此端点创建了配置文件:
// imports etc...
@Configuration
@ConditionalOnClass(Timed.class)
@AutoConfigureAfter(JHipsterMetricsEndpointConfiguration.class)
public class ExtendedMetricsEndpointConfiguration {
@Bean
@ConditionalOnBean({JHipsterMetricsEndpoint.class, SimpUserRegistry.class})
@ConditionalOnMissingBean
@ConditionalOnAvailableEndpoint
public ExtendedMetricsEndpoint extendedMetricsEndpoint(JHipsterMetricsEndpoint jHipsterMetricsEndpoint, SimpUserRegistry simpUserRegistry) {
return new ExtendedMetricsEndpoint(jHipsterMetricsEndpoint, simpUserRegistry);
}
}
我错过了什么步骤,或者我做错了什么?
我遇到了同样的问题,经过两天的努力,我找到了一个适合我的解决方案:
@Component
@WebEndpoint(id = "xxxmetrics")
public class XXXMetricsEndpoint {
private final MeterRegistry meterRegistry;
public SrnMetricsEndpoint(MeterRegistry meterRegistry) {
this.meterRegistry = meterRegistry;
}
@ReadOperation
public Map<String, Map> allMetrics() {
Map<String, Map> stringMapMap = new LinkedHashMap<>();
return stringMapMap;
}
}
应用yml:
management:
endpoints:
web:
base-path: /management
exposure:
include: [... , 'health', 'info', 'jhimetrics', 'xxxmetrics' ,'metrics', 'logfile']
请求:/management/xxxxmetrics就是这样工作的。
弹簧文档:https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-features.html#production-就绪端点自定义
编辑:弹簧版本:5.1.10,弹簧套执行器:2.1.9