对于服务器:(
即@RestController
)
除了 spring-boot 框架提供的默认标签之外,为响应式spring-boot 应用程序添加自定义指标标签。
提供一个或多个@Beans
实现WebFluxTagsContributor
OR
要替换默认标签,请提供一个@Bean
that implements WebFluxTagsProvider
。用作答案之一的
参考实现。参考:Spring-WebMVC
以下实施是常规的。
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)
}
}
指标标签:
验证客户端:公制http.client.requests.percentile
或http.client.requests
.
http://localhost:8010/metrics/http.client.requests
验证服务器:
http://localhost:8010/metrics/http.server.requests
同样,将为http.server.requests.percentile
指标添加标签
{
"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"
]
}
]
}
或者
如果您正在使用prometheus
,您可以验证自定义标签如下
http://localhost:8010/prometheus
客户:
http_client_requests_seconds_count{application="myapplication",clientName="myhostname.com",method="POST",outcome="CLIENT_ERROR",status="403",uri="/urlIamTryingToHit/v1/list",} 1.0
没有为客户端添加,因此没有自定义标签,只有 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
我们可以观察group="4XX"
, key="default_api_key"
, method="GET"
,status="404"
以及现有的默认标签。
对于客户端:
要自定义标签,根据您选择的客户端,您可以提供@Bean
thatimplements RestTemplateExchangeTagsProvider
或WebClientExchangeTagsProvider
. 和
中有方便的静态函数。参考:RestTemplateExchangeTags
WebClientExchangeTags