springboot2自定义statsd指标前缀
序
本文主要研究下springboot2自定义statsd指标前缀
背景
springboot2引入了micrometer,1.x版本的spring.metrics.export.statsd.prefix在2版本中已经被标记为废弃,但是2版本没有给出对应的配置项。
FlavorStatsdLineBuilder
micrometer-registry-statsd-1.0.1-sources.jar!/io/micrometer/statsd/internal/FlavorStatsdLineBuilder.java
/**
* A Statsd serializer for a particular {@link Meter} that formats the line in different
* ways depending on the prevailing {@link StatsdFlavor}.
*
* @author Jon Schneider
*/
public class FlavorStatsdLineBuilder implements StatsdLineBuilder {
private final Meter.Id id;
private final StatsdFlavor flavor;
private final HierarchicalNameMapper nameMapper;
private final MeterRegistry.Config config;
private final Function<NamingConvention, String> datadogTagString;
private final Function<NamingConvention, String> telegrafTagString;
public FlavorStatsdLineBuilder(Meter.Id id, StatsdFlavor flavor, HierarchicalNameMapper nameMapper, MeterRegistry.Config config) {
this.id = id;
this.flavor = flavor;
this.nameMapper = nameMapper;
this.config = config;
// service:payroll,region:us-west
this.datadogTagString = memoize(convention ->
id.getTags().iterator().hasNext() ?
id.getConventionTags(convention).stream()
.map(t -> t.getKey() + ":" + t.getValue())
.collect(Collectors.joining(","))
: null
);
// service=payroll,region=us-west
this.telegrafTagString = memoize(convention ->
id.getTags().iterator().hasNext() ?
id.getConventionTags(convention).stream()
.map(t -> t.getKey() + "=" + t.getValue())
.collect(Collectors.joining(","))
: null
);
}
@Override
public String count(long amount, Statistic stat) {
return line(Long.toString(amount), stat, "c");
}
@Override
public String gauge(double amount, Statistic stat) {
return line(DoubleFormat.decimalOrNan(amount), stat, "g");
}
@Override
public String histogram(double amount) {
return line(DoubleFormat.decimalOrNan(amount), null, "h");
}
@Override
public String timing(double timeMs) {
return line(DoubleFormat.decimalOrNan(timeMs), null, "ms");
}
private String line(String amount, @Nullable Statistic stat, String type) {
switch (flavor) {
case ETSY:
return metricName(stat) + ":" + amount + "|" + type;
case DATADOG:
return metricName(stat) + ":" + amount + "|" + type + tags(stat, datadogTagString.apply(config.namingConvention()),":", "|#");
case TELEGRAF:
default:
return metricName(stat) + tags(stat, telegrafTagString.apply(config.namingConvention()),"=", ",") + ":" + amount + "|" + type;
}
}
private String tags(@Nullable Statistic stat, String otherTags, String keyValueSeparator, String preamble) {
String tags = of(stat == null ? null : "statistic" + keyValueSeparator + stat.getTagValueRepresentation(), otherTags)
.filter(Objects::nonNull)
.collect(Collectors.joining(","));
if(!tags.isEmpty())
tags = preamble + tags;
return tags;
}
private String metricName(@Nullable Statistic stat) {
switch (flavor) {
case ETSY:
return nameMapper.toHierarchicalName(stat != null ? id.withTag(stat) : id, config.namingConvention());
case DATADOG:
case TELEGRAF:
default:
return config.namingConvention().name(id.getName(), id.getType(), id.getBaseUnit());
}
}
}
可以看到count、gauge、histogram、timing方法内部都调用了line方法,而line方法调用metricName来构造指标名称,而metricName则是调用HierarchicalNameMapper的toHierarchicalName方法(flavor为ESTY
)
HierarchicalNameMapper
micrometer-core-1.0.1-sources.jar!/io/micrometer/core/instrument/util/HierarchicalNameMapper.java
/**
* Defines the mapping between a combination of name + dimensional tags and a hierarchical name.
*
* @author Jon Schneider
*/
public interface HierarchicalNameMapper {
/**
* Sort tags alphabetically by key and append tag key values to the name with '.', e.g.
* {@code http_server_requests.response.200.method.GET}
*/
HierarchicalNameMapper DEFAULT = (id, convention) -> {
String tags = "";
if (id.getTags().iterator().hasNext()) {
tags = "." + id.getConventionTags(convention).stream()
.map(t -> t.getKey() + "." + t.getValue())
.map(nameSegment -> nameSegment.replace(" ", "_"))
.collect(Collectors.joining("."));
}
return id.getConventionName(convention) + tags;
};
String toHierarchicalName(Meter.Id id, NamingConvention convention);
}
HierarchicalNameMapper接口定义了一个DEFAULT实现,而在StatsdMetricsExportAutoConfiguration则是默认使用这个DEFAULT实现
- StatsdMetricsExportAutoConfiguration
spring-boot-actuator-autoconfigure-2.0.0.RELEASE-sources.jar!/org/springframework/boot/actuate/autoconfigure/metrics/export/statsd/StatsdMetricsExportAutoConfiguration.java
@Bean
@ConditionalOnMissingBean
public HierarchicalNameMapper hierarchicalNameMapper() {
return HierarchicalNameMapper.DEFAULT;
}
自定义
通过自定义个HierarchicalNameMapper,就可以自定义statsd指标的prefix,实例如下
@Bean
public HierarchicalNameMapper hierarchicalNameMapper() {
return new HierarchicalNameMapper(){
@Override
public String toHierarchicalName(Meter.Id id, NamingConvention convention) {
String tags = "";
if (id.getTags().iterator().hasNext()) {
tags = "." + id.getConventionTags(convention).stream()
.map(t -> t.getKey() + "." + t.getValue())
.map(nameSegment -> nameSegment.replace(" ", "_"))
.collect(Collectors.joining("."));
}
return "demo." + id.getConventionName(convention) + tags;
}
};
}
这里修改了DEFAULT方法,在return那里添加了一个demo作为prefix,这样就大功告成了。
小结
springboot2目前虽然没有通过配置文件直接支持指定statsd的prefix,但是可以通过少许代码自定义HierarchicalNameMapper来实现。
doc
code-craft
spring boot , docker and so on 欢迎关注微信公众号: geek_luandun
推荐阅读
2022年终总结
最近两年开始陷入颓废中,博客也写的越来越少了。究其原因,主要还是陷入了职业倦怠期,最近一次跳槽感觉颇为失败,但是碍于给的薪资高,为了五斗米折腰,又加上最近行情不好,想要往外跳也跳不了,就这样子一直...
codecraft阅读 706
spring boot 锁
由于当前的项目中由于多线程操作同一个实体,会出现数据覆盖的问题,后保存的实体把先保存的实体的数据给覆盖了。于是查找了锁的实现的几种方式。但写到最后发现,其实自己可以写sql 更新需要更新的字段即可,这...
weiewiyi赞 3阅读 9.2k
利用Docker部署管理LDAP及其初次使用
前言:本周主要写了gitlabWebhook转github的项目,总体上没有遇到什么大问题,这周接触到了LDAP,于是就花时间实际操作了解了一下。
李明赞 5阅读 891
记录本周问题
项目里两个地方都用到了hashmap。但是感觉自己用的时候并没有感觉非常的清晰。同时发现hashmap有线程不安全问题,而自己用的时候就是多线程来使用。于是在这里介绍一下。
weiewiyi赞 5阅读 705
记录java 在遍历中删除元素 以及 mysql5.6版本添加unique失败
遍历中删除List或Queue等数据结构中,如何一边遍历一遍删除?1. 常犯错误ArrayList可能没遇到坑过的人会用增强for循环这么写: {代码...} 但是一运行,结果却抛 java.util.ConcurrentModificationException 异常即...
weiewiyi赞 4阅读 774
Spring Security + JWT
Spring Security默认是基于session进行用户认证的,用户通过登录请求完成认证之后,认证信息在服务器端保存在session中,之后的请求发送上来后SecurityContextPersistenceFilter过滤器从session中获取认证信息、...
福赞 4阅读 1.4k
Reactive Spring实战 -- 理解Reactor的设计与实现
Reactor是Spring提供的非阻塞式响应式编程框架,实现了Reactive Streams规范。 它提供了可组合的异步序列API,例如Flux(用于[N]个元素)和Mono(用于[0 | 1]个元素)。
binecy赞 3阅读 4.7k评论 2
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。