JSON v2 输入数据格式
使用 json_v2
输入数据格式将 JSON 对象或对象数组解析为 Telegraf 指标。
该解析器支持 GJSON 路径语法 以查询 JSON。
要测试您的 GJSON 路径,请使用 GJSON Playground。
您可以在 Telegraf 仓库的 此处 找到多个示例。
配置
通过描述您想要的指标来配置此解析器,方法是从输入中定义字段和标签。配置分为名为 field
、tag
和 object
的配置子表。在下面的示例中,您可以看到可以为每个配置表定义的所有可能的配置键。在接下来的章节中,将更详细地定义这些配置键。
[[inputs.file]]
urls = []
data_format = "json_v2"
[[inputs.file.json_v2]]
measurement_name = "" # A string that will become the new measurement name
measurement_name_path = "" # A string with valid GJSON path syntax, will override measurement_name
timestamp_path = "" # A string with valid GJSON path syntax to a valid timestamp (single value)
timestamp_format = "" # A string with a valid timestamp format (see below for possible values)
timestamp_timezone = "" # A string with with a valid timezone (see below for possible values)
[[inputs.file.json_v2.field]]
path = "" # A string with valid GJSON path syntax
rename = "new name" # A string with a new name for the tag key
type = "int" # A string specifying the type (int,uint,float,string,bool)
optional = false # true: suppress errors if configured path does not exist
[[inputs.file.json_v2.tag]]
path = "" # A string with valid GJSON path syntax
rename = "new name" # A string with a new name for the tag key
type = "float" # A string specifying the type (int,uint,float,string,bool)
optional = false # true: suppress errors if configured path does not exist
[[inputs.file.json_v2.object]]
path = "" # A string with valid GJSON path syntax
timestamp_key = "" # A JSON key (for a nested key, prepend the parent keys with underscores) to a valid timestamp
timestamp_format = "" # A string with a valid timestamp format (see below for possible values)
timestamp_timezone = "" # A string with with a valid timezone (see below for possible values)
disable_prepend_keys = false (or true, just not both)
included_keys = [] # List of JSON keys (for a nested key, prepend the parent keys with underscores) that should be only included in result
excluded_keys = [] # List of JSON keys (for a nested key, prepend the parent keys with underscores) that shouldn't be included in result
tags = [] # List of JSON keys (for a nested key, prepend the parent keys with underscores) to be a tag instead of a field
optional = false # true: suppress errors if configured path does not exist
[inputs.file.json_v2.object.renames] # A map of JSON keys (for a nested key, prepend the parent keys with underscores) with a new name for the tag key
key = "new name"
[inputs.file.json_v2.object.fields] # A map of JSON keys (for a nested key, prepend the parent keys with underscores) with a type (int,uint,float,string,bool)
key = "int"
根配置选项
- measurement_name (可选):将测量名称设置为提供的字符串。
- measurement_name_path (可选):您可以使用 GJSON 路径语法 定义查询,以从 JSON 输入设置测量名称。查询必须返回单个数据值,否则将使用默认测量名称。这优先于
measurement_name
。 - timestamp_path (可选):您可以使用 GJSON 路径语法 定义查询,以从 JSON 输入设置时间戳。查询必须返回单个数据值,否则将默认为当前时间。
- timestamp_format (可选,但当定义 timestamp_path 时为必需):必须设置为
unix
、unix_ms
、unix_us
、unix_ns
或 Go “参考时间”,其定义为特定时间:Mon Jan 2 15:04:05 MST 2006
- timestamp_timezone (可选,但需要 timestamp_path):此选项应设置为 Unix TZ 值,例如
America/New_York
、Local
以使用系统时区,或UTC
。默认为UTC
数组和对象
以下描述了解析数组和对象时的高级方法
- 数组:数组中的每个元素都被视为单独的指标
- 对象:对象中的每个键值对都被视为单个指标
当处理嵌套数组和对象时,上述规则在解析器创建指标时继续适用。当一个对象有多个数组作为值时,这些数组将成为单独的指标,仅包含对象中的非数组值。在下面,您可以看到此行为的示例,其中输入 JSON 包含一个书籍对象数组,该数组具有字符的嵌套数组。
JSON 示例
{
"book": {
"title": "The Lord Of The Rings",
"chapters": [
"A Long-expected Party",
"The Shadow of the Past"
],
"author": "Tolkien",
"characters": [
{
"name": "Bilbo",
"species": "hobbit"
},
{
"name": "Frodo",
"species": "hobbit"
}
],
"random": [
1,
2
]
}
}
配置示例
[[inputs.file]]
files = ["./testdata/multiple_arrays_in_object/input.json"]
data_format = "json_v2"
[[inputs.file.json_v2]]
[[inputs.file.json_v2.object]]
path = "book"
tags = ["title"]
disable_prepend_keys = true
预期指标
file,title=The\ Lord\ Of\ The\ Rings author="Tolkien",chapters="A Long-expected Party"
file,title=The\ Lord\ Of\ The\ Rings author="Tolkien",chapters="The Shadow of the Past"
file,title=The\ Lord\ Of\ The\ Rings author="Tolkien",name="Bilbo",species="hobbit"
file,title=The\ Lord\ Of\ The\ Rings author="Tolkien",name="Frodo",species="hobbit"
file,title=The\ Lord\ Of\ The\ Rings author="Tolkien",random=1
file,title=The\ Lord\ Of\ The\ Rings author="Tolkien",random=2
您可以在 telegraf 仓库的 [testdata
][] 文件夹下找到更复杂的示例。
类型
对于每个字段,您可以选择为每个指标定义类型。以下规则适用于此配置
- 如果显式定义了类型,则解析器将强制执行此类型,并在可能的情况下将数据转换为定义的类型。如果类型无法转换,则解析器将失败。
- 如果未定义类型,则解析器将使用 JSON 中定义的默认类型(int、float、string)。
您可以设置的类型值
int
、bool、floats 或字符串(带有有效数字)可以转换为 int。uint
、bool、floats 或字符串(带有有效数字)可以转换为 uint。string
,任何数据都可以格式化为字符串。float
,字符串值(带有有效数字)或整数可以转换为 float。bool
,字符串值“true”或“false”(不区分大小写)或整数值0
或1
可以转换为 bool。
此页是否对您有帮助?
感谢您的反馈!