我一直在使用 spring-boot 中的指标,但对 spring-cloud 似乎如何改变行为感到有些困惑。
一个简单的最小 spring-boot 1.3.3.RELEASE 应用程序,带有执行器和单个 Controller :
@RestController
public class HelloController {
@Autowired
private CounterService counterService;
@RequestMapping("/hello")
public String sayHello(@RequestParam("name") String name) {
counterService.increment("counter.calls.hello");
return "Hello, " + name;
}
}
此应用程序的指标端点具有
...
gauge.response.hello: 5,
gauge.response.metrics: 69,
gauge.response.star-star.favicon.ico: 2,
counter.status.200.star-star.favicon.ico: 4,
counter.status.200.metrics: 1,
counter.calls.hello: 5,
counter.status.200.hello: 5
如 spring-boot 文档中所述。我的自定义计数器 (counter.calls.hello) 按预期用作计数器。
现在,如果我将 spring-cloud-starter-eureka (Brixton.RC1) 添加到我的 pom 并且不更改任何其他内容,则指标端点具有
...
gauge.servo.counter.calls.hello: 1,
normalized.servo.rest.totaltime: 0,
normalized.servo.rest.count: 0,
gauge.servo.rest.min: 0,
gauge.servo.rest.max: 0,
gauge.servo.response.hello: 7,
gauge.servo.response.star-star.favicon.ico: 2,
正常的 MVC 指标消失了。响应代码信息在哪里?我的自定义计数器被报告为gauge.servo.counter.calls.hello,但它不再作为计数器工作,即使我对HelloController 进行了5 次调用,它似乎也只显示值1。经过一些调试和搜索,我将计数器指标名称更改为 meter.calls.hello,这会在指标响应中生成 counter.servo.calls.hello,并恢复计数器功能。
进一步阅读表明 spring-cloud 默认为指标伺服,并表明如果我使用 java 8,我应该使用旁观者。将 spring-cloud-starter-spectator 添加到我的 pom,并返回“counter.calls.hello”作为计数器指标名称,指标端点有
...
counter.calls.hello(): 3,
response.hello(): 7,
response.metrics(): 9,
response.star-star.favicon.ico(): 2,
这关于其余端点的内置信息更少,但我的自定义计数器似乎确实起作用。
关于指标的 Spring-cloud 文档似乎表明我应该使用 ServoRegistry,无论是使用伺服器还是旁观者。观众指标部分中的术语是不同的。计数器不能按我预期的方式工作。当我按照文档中的描述使用 ServoRegistry 发布一个简单的命中计数器时,我会在指标端点中获得某种速率。
与 spring-boot 提供的内容相比,伺服器和/或旁观者是否有一些附加值(value)? Spring-cloud 文档表明有更多信息被记录到默认 Controller 指标中,但它们没有显示在/metrics 中。我应该排除 ServoMetricsAutoConfiguration 并使用 spring-boot 实现吗?
最佳答案
根据Spring Cloud Brixton documentation Servo/Spectator 指标的优势在于它们是有标签的(也就是维度),而常规的 Spring Boot 指标是分层的。
Spring Boot
"counter.status.200.root": 20
"counter.status.400.root": 3
网飞
"root(method=GET,status=200,statistic=count)": 20
"root(method=GET,status=400,statistic=count)": 3
维度指标允许更多的查询灵活性,默认情况下,MVC 请求标记有 HTTP 方法、HTTP 状态、URI 和异常(如果请求处理程序抛出异常)。
不幸的是,Netflix 维度指标在
/metrics
中显示时似乎不能很好地转换。执行器端点,因此如果您只需要 Spring Boot 在指标端点中提供的默认请求计数器,您最好禁用伺服配置:spring.metrics.servo.enabled=false
关于spring-boot 指标与 spring-cloud 指标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36417045/