Micrometer Influx
InfluxData 工具套件支持实时流处理和存储时间序列数据。它支持降采样、自动过期和删除不需要的数据,以及备份和恢复功能。
InfluxMeterRegistry
支持 1.x 版本的 InfluxDB API 以及 v2 API。
1. 安装与配置
Micrometer 支持直接将指标发送到 InfluxDB,或通过 Telegraf 通过 StatsD 注册表发送指标。
建议使用由 Micrometer(或您的框架,如果有的话)提供的 BOM,您可以在此处查看如何配置它 here。以下示例假设您正在使用 BOM。
1.1. 直接写入 InfluxDB
1.1.1. Gradle
在 配置 好 BOM 之后,添加以下依赖:
implementation 'io.micrometer:micrometer-registry-influx'
此依赖项不需要指定版本,因为它由 BOM 定义。
1.1.2. Maven
在 配置 好 BOM 之后,添加以下依赖项:
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-influx</artifactId>
</dependency>
由于此依赖项的版本由 BOM 定义,因此不需要指定版本。
指标会按照速率进行聚合,并定期推送到 InfluxDB。注册表执行的速率聚合生成的数据集与 Telegraf 生成的数据集非常相似。以下示例配置了一个用于 InfluxDB 的计量器注册表:
InfluxConfig config = new InfluxConfig() {
@Override
public Duration step() {
return Duration.ofSeconds(10);
}
@Override
public String db() {
return "mydb";
}
@Override
public String get(String k) {
return null; // accept the rest of the defaults
}
};
MeterRegistry registry = new InfluxMeterRegistry(config, Clock.SYSTEM);
要将指标发送到 InfluxDB 2.x,请确保配置要写入指标的 org
和 bucket
,以及身份验证的 token
:
InfluxConfig config = new InfluxConfig() {
@Override
public String org() {
return "myorg";
}
@Override
public String bucket() {
return "app-metrics";
}
@Override
public String token() {
return "auth_token_here"; // FIXME: This should be securely bound rather than hard-coded, of course.
}
@Override
public String get(String k) {
return null; // accept the rest of the defaults
}
};
MeterRegistry registry = new InfluxMeterRegistry(config, Clock.SYSTEM);
InfluxConfig
是一个带有一组默认方法的接口。如果在 get(String k)
的实现中,你不返回 null
,而是将其绑定到一个属性源,那么你可以覆盖默认配置。例如,Micrometer 的 Spring Boot 支持将前缀为 management.metrics.export.influx
的属性直接绑定到 InfluxConfig
:
management.metrics.export.influx:
api-version: v2 # API version of InfluxDB to use. Defaults to 'v1' unless an org is configured. If an org is configured, defaults to 'v2'.
auto-create-db: true # Whether to create the InfluxDB database if it does not exist before attempting to publish metrics to it. InfluxDB v1 only. (Default: true)
batch-size: 10000 # Number of measurements per request to use for this backend. If more measurements are found, then multiple requests will be made. (Default: 10000)
bucket: mybucket # Bucket for metrics. Use either the bucket name or ID. Defaults to the value of the db property if not set. InfluxDB v2 only.
compressed: true # Whether to enable GZIP compression of metrics batches published to InfluxDB. (Default: true)
connect-timeout: 1s # Connection timeout for requests to this backend. (Default: 1s)
consistency: one # Write consistency for each point. (Default: one)
db: mydb # Database to send metrics to. InfluxDB v1 only. (Default: mydb)
enabled: true # Whether exporting of metrics to this backend is enabled. (Default: true)
num-threads: 2 # Number of threads to use with the metrics publishing scheduler. (Default: 2)
org: myorg # Org to write metrics to. InfluxDB v2 only.
password: mysecret # Login password of the InfluxDB server. InfluxDB v1 only.
read-timeout: 10s # Read timeout for requests to this backend. (Default: 10s)
retention-policy: my_rp # Retention policy to use (InfluxDB writes to the DEFAULT retention policy if one is not specified). InfluxDB v1 only.
step: 1m # Step size (i.e. reporting frequency) to use. (Default: 1m)
token: AUTH_TOKEN_HERE # Authentication token to use with calls to the InfluxDB backend. For InfluxDB v1, the Bearer scheme is used. For v2, the Token scheme is used.
uri: http://localhost:8086 # URI of the InfluxDB server. (Default: http://localhost:8086)
user-name: myusername # Login user of the InfluxDB server. InfluxDB v1 only.
1.2. 通过 Telegraf
Telegraf 是一个 StatsD 代理,它期望使用一种修改版的 StatsD 行协议。
1.2.1. Gradle
在 配置 完 BOM 之后,添加以下依赖:
implementation 'io.micrometer:micrometer-registry-statsd'
该依赖项的版本号不需要指定,因为它由 BOM 定义。
1.2.2. Maven
在 配置 了 BOM 之后,添加以下依赖项:
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-statsd</artifactId>
</dependency>
由于此依赖项的版本由 BOM 定义,因此不需要指定版本。
指标立即通过 UDP 使用 Telegraf 的 StatsD 线路协议发送到 Telegraf:
StatsdConfig config = new StatsdConfig() {
@Override
public String get(String k) {
return null;
}
@Override
public StatsdFlavor flavor() {
return StatsdFlavor.Telegraf;
}
};
MeterRegistry registry = new StatsdMeterRegistry(config, Clock.SYSTEM);