Spring Boot Actuator 的 logback_events_total 指标值

总结

    • Spring Boot Actuator の prometheus エンドポイントが出力する logback_events_total についてまとめる

 

    Spring Boot Actuator と Micrometer Registry Prometheus で logback_events_total を出力する

logback_events_total 是什么意思?

    • Spring Boot Actuator が出力するメトリクス値

 

    • 該当するログレベルのログ出力回数を表す

 

    Prometheus の Counter タイプ

Counter 类型是表示累积指标值的单调递增值,它会增加但不会减少,并在重新启动时被重置为零。

指标类型 | Prometheus

一个计数器是一个累积指标,表示一个单调递增的计数器,其值在重新启动时只能增加或重置为零。例如,可以使用计数器来表示已服务的请求数量、完成的任务数量或错误数量。

不要使用计数器来暴露可能会减少的值。例如,不要使用计数器来表示当前正在运行的进程数量;而应该使用一个测量器。

logback_events_total 的输出示例

数值代表了该日志级别的日志输出次数。

# HELP logback_events_total Number of error level events that made it to the logs
# TYPE logback_events_total counter
logback_events_total{level="warn",} 2.0
logback_events_total{level="debug",} 0.0
logback_events_total{level="error",} 2.0
logback_events_total{level="trace",} 0.0
logback_events_total{level="info",} 0.0

样本代码

使用Java 11 + Spring Boot 2.2.7 + Micrometer Registry Prometheus 1.3.9 + Gradle 6.4进行设置。

文件列表

├── build.gradle
├── settings.gradle
└── src
    └── main
        ├── java
        │   └── com
        │       └── example
        │           └── MyApplication.java
        └── resources
            └── application.properties

构建.gradle文件

为了使用Spring Boot Actuator,需要指定Spring Boot Actuator Starter。
为了输出给Prometheus,需要指定Micrometer Registry Prometheus。

plugins {
  id 'org.springframework.boot' version '2.2.7.RELEASE'
  id 'io.spring.dependency-management' version '1.0.9.RELEASE'
  id 'java'
}

group = 'com.example'
version = '0.0.1'
sourceCompatibility = '11'

repositories {
  mavenCentral()
}

dependencies {
  // Spring Boot Web Starter
  implementation 'org.springframework.boot:spring-boot-starter-web'
  // Spring Boot Actuator Starter
  implementation 'org.springframework.boot:spring-boot-starter-actuator'
  // Micrometer Registry Prometheus
  implementation 'io.micrometer:micrometer-registry-prometheus:1.3.9'
}

使用默认配置来设置日志库。

Spring Boot的功能 – 官方文档的日文翻译

默认情况下,如果使用”Starter”,则会使用Logback进行日志记录。为确保Java Util Logging、Commons Logging、Log4J或SLF4J等依赖库都能正常运行,还包含适当的Logback路由。

设置.gradle

rootProject.name = 'my-app'

src/main/resources/application.properties 可在此目录下找到的文件为 application.properties。

# ログ出力レベルを指定
logging.level.root=warn
logging.level.org.springframework.web=warn
logging.level.org.hibernate=error

# サンプルアプリケーションは warn レベル以上のログを出力する
logging.level.com.example=warn

# Spring Boot Actuator のすべてのエンドポイントをいったん無効にする設定
management.endpoints.enabled-by-default=false

# Prometheus 用のエンドポイントを有効にする設定
management.endpoint.prometheus.enabled=true
management.endpoints.web.exposure.include=prometheus

src/main/java/com/example/我的应用程序.java

package com.example;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@SpringBootApplication
@RestController
public class MyApplication {

  // SLF4J Logger
  private static final Logger logger = LoggerFactory.getLogger(MyApplication.class);

  public static void main(String[] args) {
    SpringApplication.run(MyApplication.class, args);
  }

  @GetMapping("/")
  public Map index() {
    // アクセスが来たらそれぞれのログレベルでログを出力する
    logger.trace("Sample TRACE Message");
    logger.debug("Sample DEBUG Message");
    logger.info ("Sample INFO  Message");
    logger.warn ("Sample WARN  Message");
    logger.error("Sample ERROR Message");
    return Map.of("message", "Hello, world.");
  }
}

样本执行示例

使用Gradle的bootRun任务启动Spring Boot应用程序。

$ gradle bootRun

> Task :bootRun

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.7.RELEASE)

可以通过另一个终端,用 curl 命令访问 Spring Boot Actuator 的 HTTP 端点 /actuator/prometheus。
由于所有的日志级别都还没有任何日志条目,所以该值为 “0.0”。

$ curl --silent http://localhost:8080/actuator/prometheus | grep logback
# HELP logback_events_total Number of error level events that made it to the logs
# TYPE logback_events_total counter
logback_events_total{level="warn",} 0.0
logback_events_total{level="debug",} 0.0
logback_events_total{level="error",} 0.0
logback_events_total{level="trace",} 0.0
logback_events_total{level="info",} 0.0

用curl访问首页。

$ curl --silent http://localhost:8080/
{"message":"Hello, world."}

由于设置了application.properties文件中的warn级别,Spring Boot将输出日志,其中包括warn和error级别的日志。

2020-05-11 20:51:43.225  WARN 70007 --- [nio-8080-exec-4] com.example.MyApplication                : Sample WARN  Message
2020-05-11 20:51:43.233 ERROR 70007 --- [nio-8080-exec-4] com.example.MyApplication                : Sample ERROR Message

使用curl命令访问/actuator/prometheus。
当输出一条warn日志和一条error日志时,与相应日志级别相关的数据项的值将成为“1.0”。

$ curl --silent http://localhost:8080/actuator/prometheus | grep logback
# HELP logback_events_total Number of error level events that made it to the logs
# TYPE logback_events_total counter
logback_events_total{level="warn",} 1.0
logback_events_total{level="debug",} 0.0
logback_events_total{level="error",} 1.0
logback_events_total{level="trace",} 0.0
logback_events_total{level="info",} 0.0

用 curl 工具再次访问首页。

$ curl --silent http://localhost:8080/
{"message":"Hello, world."}

Spring Boot 输出日志。

2020-05-11 20:52:09.924  WARN 70007 --- [nio-8080-exec-8] com.example.MyApplication                : Sample WARN  Message
2020-05-11 20:52:09.924 ERROR 70007 --- [nio-8080-exec-8] com.example.MyApplication                : Sample ERROR Message

使用curl工具访问/actuator/prometheus。
由于记录了一条warn和一条error日志,所以相关日志级别的值为”2.0″。

$ curl --silent http://localhost:8080/actuator/prometheus | grep logback
# HELP logback_events_total Number of error level events that made it to the logs
# TYPE logback_events_total counter
logback_events_total{level="warn",} 2.0
logback_events_total{level="debug",} 0.0
logback_events_total{level="error",} 2.0
logback_events_total{level="trace",} 0.0
logback_events_total{level="info",} 0.0

请查阅相关资料

    • Spring Boot Actuator: 本番対応機能 – ドキュメント

 

    • Micrometer Application Monitoring

 

    • Spring Boot の機能 4. ロギング – 公式ドキュメントの日本語訳

 

    Spring でのロギング
bannerAds