0

来自 Spring Boot 应用程序的代码示例,它正在生成普罗米修斯指标。

我正在尝试使用http://localhost:8080/myApi/ABC访问它。

但是在普罗米修斯指标中,它使用"uri=/myApi/{refNum}"生成指标。

Promotheus 指标:

my_example_api_seconds{application="my-application",exception="None", method="GET", outcome="SUCCESS",status="200",uri="/myApi/{refNum}",quantile="0.3",} 14.495514624

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;    
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.spring.autoconfigure.MeterRegistryCustomizer;

@Configuration
public class BeansConfiguration {
@Bean
    MeterRegistryCustomizer<MeterRegistry> meterRegistryMeterRegistryCustomizer(MeterRegistry meterRegistry) {
        return meterRegistry1 -> {
            meterRegistry.config().commonTags("application","my-application");
        };
    }
}

控制器:

import io.micrometer.core.annotation.Timed;
import io.micrometer.core.instrument.MeterRegistry;

   @Timed(
            value="my.example.api",
            percentiles = {0.30,0.50,0.95,0.99}
    )
    @GetMapping(value = "myApi/{refNum}")
    public ResponseEntity<String> fun(@NotNull @PathVariable("refNum") String refNum) {
     ...
    }

Pom.xml

<properties>
  <micrometer.version>1.2.0</micrometer.version>
  <prometheus.registry.version>1.2.0</prometheus.registry.version>
</properties>
<dependencies>
  <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-spring-legacy</artifactId>
            <version>${micrometer.version}</version>
  </dependency>

  <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-registry-prometheus</artifactId>
            <version>${prometheus.registry.version}</version>
  </dependency>
  <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-registry-prometheus</artifactId>
            <version>${prometheus.registry.version}</version>
  </dependency>
  <dependency>
            <groupId>io.prometheus</groupId>
            <artifactId>simpleclient_pushgateway</artifactId>
            <version>0.8.0</version>
  </dependency>
</dependencies>
4

1 回答 1

0

它按设计工作。

假设您有一个用户端点@GetMapping(value = "user/{userId}")

如果您要调用user/1, user/2, 一直到user/1000000, 您会希望指标系统记录 1 个指标以及该单个端点的所有调用的聚合时间。

要记录单个呼叫,您需要添加日志消息,而不是指标框架。

于 2020-05-06T11:57:51.353 回答