当前位置: 首页 > 知识库问答 >
问题:

为Spring WebClient默认指标添加标签

鲁辉
2023-03-14

我目前正在从事一个Spring webflux项目,该项目具有执行器、测微计依赖项,如下所示,

    <dependency>
        <groupId>io.micrometer</groupId>
        <artifactId>micrometer-registry-prometheus</artifactId>
    </dependency>
    <dependency>
        <groupId>io.micrometer</groupId>
        <artifactId>micrometer-core</artifactId>
    </dependency>

公开默认指标(包括Spring webClient指标)。我使用Spring WebClient调用了4个不同的endpoint。我想知道是否有一种方法可以将特定的标签添加到每个被添加到默认指标的webclient调用中。我有一些指标,比如Webclient在/actuator/prometheusendpoint公开的直方图,如下所示,

http_client_requests_seconds_bucket{clientName="my-app.com",method="GET",outcome="SUCCESS",status="200",uri="/shops",le="0.001048576",} 0.0
http_client_requests_seconds_bucket{clientName="my-app.com",method="GET",outcome="SUCCESS",status="200",uri="/shops",le="0.002088576",} 1.0

在我的代码中,我想在Webclient调用中添加一些额外的标记,以满足上述所有指标的需要。比如像这样的,

http_client_requests_seconds_bucket{clientName="my-app.com",method="GET",outcome="SUCCESS",status="200",uri="/shops",le="0.001048576",investor="A", version="v1"} 0.0
http_client_requests_seconds_bucket{clientName="my-app.com",method="GET",outcome="SUCCESS",status="200",uri="/shops",le="0.002088576",investor="A", version="v1"} 1.0

请注意我添加的2个自定义标记investor=“A”,version=“v1”。我在找一些可能是这样的代码,

@Autowire
private WebClient webclient; // Assume there is already a bean created for us

public Mono<String> getShopsList(String... extraTags) {
     return webclient.baseUrl("http://my-app.com")
         .build()
         .get()
         .uri("/shops")
         .tags(extraTags) // Some extra tags I want callers of the method to pass. Note there are only 4-5 methods that call "getShopsList()" method
         .retrieve() 
         .bodyToMono(String.class);
 }
 

共有1个答案

管峻
2023-03-14

预期的方法是引入您的自定义标签提供程序

@Component
public class CustomWebClientExchangeTagsProvider extends DefaultWebClientExchangeTagsProvider {

  public static final String VERSION_ATTRIBUTE = "custom.webclient.version";
  public static final String INVESTOR_ATTRIBUTE = "custom.webclient.investor";

  @Override
  public Iterable<Tag> tags(ClientRequest request, ClientResponse response, Throwable throwable) {
    Tag method = WebClientExchangeTags.method(request);
    Tag investor = getInvestorTag(request);
    Tag version = getVersionTag(request);
    return asList(method, investor, version, WebClientExchangeTags.status(response, throwable), WebClientExchangeTags.outcome(response));
  }

  private Tag getInvestorTag(ClientRequest request) {
    return request.attribute(INVESTOR_ATTRIBUTE)
        .map(name -> Tag.of("investor", (String) name))
        .orElse(WebClientExchangeTags.clientName(request));
  }

  private Tag getVersionTag(ClientRequest request) {
    return request.attribute(VERSION_ATTRIBUTE)
        .map(uri -> Tag.of("version", (String) uri))
        .orElse(WebClientExchangeTags.uri(request));
  }

}

您必须以这种方式检测您的自定义Web客户端:

@Bean
public WebClient webClient(MetricsWebClientCustomizer metricsCustomizer) {
    TcpClient timeoutClient = ...
    WebClient.Builder builder = WebClient.builder();
    metricsCustomizer.customize(builder);
    return ...;
}

最后,您需要像这样设置两个属性:

return webClient.get()
        .uri(filePath)
        .attribute(INVESTOR_ATTRIBUTE, "A")
        .attribute(VERSION_ATTRIBUTE, "v1")
        .retrieve()
        .bodyToMono(String.class);

示例结果:

http_client_requests_seconds_count{investor="A",method="GET",outcome="CLIENT_ERROR",status="401",version="v1",} 1.0
http_client_requests_seconds_sum{investor="A",method="GET",outcome="CLIENT_ERROR",status="401",version="v1",} 0.073818807

编辑

根据文件:

S属性(消费者

提供对迄今为止声明的每个属性的访问,并可以添加、替换或删除值。

因此,可以使用它添加多个属性。

 类似资料:
  • 我目前正在从事一个Spring boot(webflux)项目,在该项目中,我们使用Spring boot-actuator依赖关系在/actuator/prometheusendpoint上公开了我们的应用程序的指标,默认情况下,该依赖关系为我们提供了例如:http\u server\u requests\u seconds\u bucket metric。默认情况下,它有{exception=

  • 在千分尺中,我们可以创建一个新的量规,类似于 见此处代码https://github . com/micrometer-metrics/micrometer/blob/master/micrometer-core/src/main/Java/io/micrometer/core/instrument/meter registry . Java # L468 是否可以默认为我的对象包含“前缀”名称?

  • 我为普罗米修斯和Actuator添加了依赖项: 但是,如果我去endpoint /actuator/promehteuslog4j2_events_total指标是不到位的,即使我还添加了log4j2依赖从Spring Boot启动器,我错过了一些额外的配置吗?

  • 我有spring boot 2 REST应用程序,启用了Spring执行器。默认情况下,spring会在endpoint中生成大量指标(jvm、cpu、内存等)。除此之外,我还使用测微计API创建自定义指标。到目前为止,它一直运行得很好。 现在,我需要只生成自定义指标,但禁用spring提供的所有默认指标。请注意,我不想禁用endpoint,但我只想禁用默认指标。 现在直接/间接地可能吗? 谢谢你

  • 在我的JavaFX应用程序中,我使用了SceneBuilder by Gluon。场景的预览有一个Y向下的坐标系(Y向下增加)。但是,当我将fxml文件导入Java时,坐标都是翻转的。以下是相关截图。 从我广泛搜索的结果来看,JavaFX坐标系应该是Y-Down而不是y-up。我的JavaFX版本有问题吗?我在Mac OS X 10.12(El Capitan),使用JDK 1.8.0_66。

  • 我想禁用所有内置指标(jvm、cpu等),但保留自定义指标。 当我与Datadog一起启用Spring Boot执行器度量时,我最终将320个度量发送给Datadog。这些指标中的大多数来自内置核心指标(JVM指标、CPU指标、文件描述指标),其中只有5个是我的自定义指标,这些指标是我要发送给datadog的。 根据Spring Boot文档的本节: Spring Boot还配置了内置的仪器(即。