使用json通过telegraf生成metrics(摘自telegraf github文档)

本文介绍如何使用Telegraf的JSON数据格式解析JSON数据,并提取数值作为指标字段,同时指定标签键来提取JSON根级别的键作为标签。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

JSON:
The JSON data format flattens JSON into metric fields. NOTE: Only numerical values are converted to fields, and they are converted into a float. strings are ignored unless specified as a tag_key (see below).

So for example, this JSON:

{
“a”: 5,
“b”: {
“c”: 6
},
“ignored”: “I’m a string”
}
Would get translated into fields of a measurement:

myjsonmetric a=5,b_c=6
The measurement name is usually the name of the plugin, but can be overridden using the name_override config option.

JSON Configuration:
The JSON data format supports specifying “tag keys”. If specified, keys will be searched for in the root-level of the JSON blob. If the key(s) exist, they will be applied as tags to the Telegraf metrics.

For example, if you had this configuration:

[[inputs.exec]]
## Commands array
commands = [“/tmp/test.sh”, “/usr/bin/mycollector –foo=bar”]

## measurement name suffix (for separating different commands)
name_suffix = “_mycollector”

## Data format to consume.
## Each data format has its own unique set of configuration options, read
## more about them here:
## https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_INPUT.md
data_format = “json”

## List of tag names to extract from top-level of JSON server response
tag_keys = [
“my_tag_1”,
“my_tag_2”
]
with this JSON output from a command:

{
“a”: 5,
“b”: {
“c”: 6
},
“my_tag_1”: “foo”
}
Your Telegraf metrics would get tagged with “my_tag_1”

exec_mycollector,my_tag_1=foo a=5,b_c=6
If the JSON data is an array, then each element of the array is parsed with the configured settings. Each resulting metric will be output with the same timestamp.

For example, if the following configuration:

[[inputs.exec]]
## Commands array
commands = [“/usr/bin/mycollector –foo=bar”]

## measurement name suffix (for separating different commands)
name_suffix = “_mycollector”

## Data format to consume.
## Each data format has its own unique set of configuration options, read
## more about them here:
## https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_INPUT.md
data_format = “json”

## List of tag names to extract from top-level of JSON server response
tag_keys = [
“my_tag_1”,
“my_tag_2”
]
with this JSON output from a command:

[
{
“a”: 5,
“b”: {
“c”: 6
},
“my_tag_1”: “foo”,
“my_tag_2”: “baz”
},
{
“a”: 7,
“b”: {
“c”: 8
},
“my_tag_1”: “bar”,
“my_tag_2”: “baz”
}
]
Your Telegraf metrics would get tagged with “my_tag_1” and “my_tag_2”

exec_mycollector,my_tag_1=foo,my_tag_2=baz a=5,b_c=6
exec_mycollector,my_tag_1=bar,my_tag_2=baz a=7,b_c=8

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值