ste*_*pio 5 java metrics spring-boot micrometer
最近我切换到Spring Boot 2了Micrometer. 当我获得这些闪亮的新指标时,我与我们的 DevOps 人员进行了交谈,我们开始将它们导出到Telegraf.
为了区分不同的应用程序和应用程序节点,我们决定使用标签。这对于自定义指标非常有效,但现在我开始考虑预定义。为了对默认指标实现相同的目标,我还需要能够为它们添加额外的标签。
有可能实现这一目标吗?我这样做对吗?
编辑:我尝试了下一种方法:
@Component
public class MyMetricsImpl implements MyMetrics {
@Autowired
protected MyProperties myProperties;
@Autowired
protected MeterRegistry meterRegistry;
@PostConstruct
public void initialize() {
this.meterRegistry.config()
.commonTags(commonTags());
}
@Override
public List<Tag> commonTags() {
List<Tag> tags = new ArrayList<>();
tags.add(Tag.of("application", myProperties.getApplicationName()));
tags.add(Tag.of("node", myProperties.getNodeName()));
return tags;
}
}
Run Code Online (Sandbox Code Playgroud)
问题是我的指标表现正确,甚至一些引导指标(至少http.server.requests)看到我的标签。但是jvm.*,system.*,tomcat.*和其他许多人仍然不具备所需的标签。
对于服务器:(
即@RestController)
除了 spring-boot 框架提供的默认标签之外,还为reactive-spring-boot 应用程序添加自定义指标标签。
提供一个或多个@Beans实现WebFluxTagsContributor
OR
要替换默认标记,请提供@Beanthat implements WebFluxTagsProvider。用作答案之一的
参考实现。参考:Spring-WebMVC
下面的实现是在groovy中。
import io.micrometer.core.instrument.Tag
import org.springframework.boot.actuate.metrics.web.reactive.server.WebFluxTagsContributor
import org.springframework.http.HttpMethod
import org.springframework.http.HttpStatus
import org.springframework.stereotype.Component
import org.springframework.web.server.ServerWebExchange
@Component
class CustomWebClientExchangeTagsProvider implements WebFluxTagsContributor {
final KEY = "key"
/**
* Provides tags to be associated with metrics for the given {@code exchange}.
* @param exchange the exchange
* @param ex the current exception (maybe {@code null})
* @return tags to associate with metrics for the request and response exchange
*/
@Override
Iterable<Tag> httpRequestTags(ServerWebExchange exchange, Throwable ex) {
String apiKey = exchange.request.queryParams[KEY] ?: "default_api_key"
HttpStatus status = exchange.response.statusCode ?: HttpStatus.INTERNAL_SERVER_ERROR
HttpMethod method = exchange.request.method ?: HttpMethod.OPTIONS
String group = (status.value() >= 500 ? "5XX" : (status.value() >= 400) ? "4XX" : (status.value() >= 300) ? "3XX" : "NONE")
Tag statusTag = Tag.of("status", status.value().toString())
Tag methodTag = Tag.of("method", method.toString())
Tag apiKeyTag = Tag.of(KEY, apiKey)
Tag groupTag = Tag.of("group", group)
return Arrays.asList(statusTag, methodTag, apiKeyTag, groupTag)
}
}
Run Code Online (Sandbox Code Playgroud)
指标标签:
http.client.requests.percentile或http.client.requests.import io.micrometer.core.instrument.Tag
import org.springframework.boot.actuate.metrics.web.reactive.server.WebFluxTagsContributor
import org.springframework.http.HttpMethod
import org.springframework.http.HttpStatus
import org.springframework.stereotype.Component
import org.springframework.web.server.ServerWebExchange
@Component
class CustomWebClientExchangeTagsProvider implements WebFluxTagsContributor {
final KEY = "key"
/**
* Provides tags to be associated with metrics for the given {@code exchange}.
* @param exchange the exchange
* @param ex the current exception (maybe {@code null})
* @return tags to associate with metrics for the request and response exchange
*/
@Override
Iterable<Tag> httpRequestTags(ServerWebExchange exchange, Throwable ex) {
String apiKey = exchange.request.queryParams[KEY] ?: "default_api_key"
HttpStatus status = exchange.response.statusCode ?: HttpStatus.INTERNAL_SERVER_ERROR
HttpMethod method = exchange.request.method ?: HttpMethod.OPTIONS
String group = (status.value() >= 500 ? "5XX" : (status.value() >= 400) ? "4XX" : (status.value() >= 300) ? "3XX" : "NONE")
Tag statusTag = Tag.of("status", status.value().toString())
Tag methodTag = Tag.of("method", method.toString())
Tag apiKeyTag = Tag.of(KEY, apiKey)
Tag groupTag = Tag.of("group", group)
return Arrays.asList(statusTag, methodTag, apiKeyTag, groupTag)
}
}
Run Code Online (Sandbox Code Playgroud)
http://localhost:8010/metrics/http.client.requests
Run Code Online (Sandbox Code Playgroud)
http.server.requests.percentile同样,将为指标添加标签
http://localhost:8010/metrics/http.server.requests
Run Code Online (Sandbox Code Playgroud)
或者
如果您正在使用,prometheus
可以按如下方式验证自定义标签
{
"name": "http.server.requests",
"description": null,
"base_unit": "milliseconds",
"measurements": [
{
"statistic": "COUNT",
"value": 17.0
},
{
"statistic": "TOTAL_TIME",
"value": 8832.186054
},
{
"statistic": "MAX",
"value": 6.514132
}
],
"available_tags": [
{
"tag": "exception",
"values": [
"None",
"ResponseStatusException"
]
},
{
"tag": "method",
"values": [
"GET"
]
},
{
"tag": "application",
"values": [
"myapplication"
]
},
{
"tag": "uri",
"values": [
"/myapplication/v1/abcd",
"/manage/metrics/{requiredMetricName}",
"/manage",
"/manage/metrics",
"/myapplication/v1/windows",
"/**"
]
},
{
"tag": "outcome",
"values": [
"CLIENT_ERROR",
"SERVER_ERROR",
"SUCCESS"
]
},
{
"tag": "key",
"values": [
"default_api_key",
"[abcd]"
]
},
{
"tag": "status",
"values": [
"404",
"200",
"502"
]
},
{
"tag": "group",
"values": [
"4XX",
"NONE",
"5XX"
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
客户:
http_client_requests_seconds_count{application="myapplication",clientName="myhostname.com",method="POST",outcome="CLIENT_ERROR",status="403",uri="/urlIamTryingToHit/v1/list",} 1.0
Run Code Online (Sandbox Code Playgroud)
客户端只有spring-boot提供的默认标签,没有自定义标签。
服务器:
http_server_requests_seconds_sum{application="myapplication",exception="None",group="4XX",key="default_api_key",method="GET",outcome="CLIENT_ERROR",status="404",uri="/manage/metrics/{requiredMetricName}",} 0.004154207
Run Code Online (Sandbox Code Playgroud)
我们可以观察group="4XX", key="default_api_key", method="GET",status="404"以及现有的默认标签。
对于客户端:
要自定义标签,根据您选择的客户端,您可以提供@Beanthatimplements RestTemplateExchangeTagsProvider或 WebClientExchangeTagsProvider. 和
中有方便的静态函数。参考:RestTemplateExchangeTagsWebClientExchangeTags
如果您正在寻找通用标签支持,您可以通过注册一个MeterFilter做它来实现。
随着即将到来的春天引导2.1.0.M1,您可以使用以下属性:
management.metrics.tags.*= # Common tags that are applied to every meter.
Run Code Online (Sandbox Code Playgroud)
有关详细信息,请参阅参考资料。
由于问题已更新,我使用MeterFilter基于此的方法检查了更新的问题,并确认其工作方式如下:
要求: http://localhost:8080/actuator/metrics/jvm.gc.memory.allocated
回复:
{
"name" : "jvm.gc.memory.allocated",
"measurements" : [ {
"statistic" : "COUNT",
"value" : 1.98180864E8
} ],
"availableTags" : [ {
"tag" : "stack",
"values" : [ "prod" ]
}, {
"tag" : "region",
"values" : [ "us-east-1" ]
} ]
}
Run Code Online (Sandbox Code Playgroud)
我没有检查更新问题中提供的方法,但我只会使用经过验证的MeterFilter基于方法,除非有任何理由坚持使用该方法。
我研究了这种方法,并能够用这个分支重现它。
现在应用通用标签为时已晚,@PostConstruct因为一些指标已经注册。有效的原因http.server.requests是它将在第一个请求中注册。如果您对此感兴趣,请尝试在过滤器的应用程序点上放置一个断点。
简而言之,尝试上述类似于即将推出的 Spring Boot 开箱即用支持的方法。
| 归档时间: |
|
| 查看次数: |
7555 次 |
| 最近记录: |