文档文档

文件路径处理器插件

此插件允许使用路径(例如,basename 提取最后一个路径元素)来转换标签和字段值。值可以就地修改,也可以存储在另一个键中。

引入版本: Telegraf v1.15.0 标签: transformation 操作系统支持: all

全局配置选项

插件支持其他全局和插件配置设置,用于修改指标、标签和字段,创建别名以及配置插件顺序等任务。更多详情请参阅 CONFIGURATION.md

配置

# Performs file path manipulations on tags and fields
[[processors.filepath]]
  ## Treat the tag value as a path and convert it to its last element, storing the result in a new tag
  # [[processors.filepath.basename]]
  #   tag = "path"
  #   dest = "basepath"

  ## Treat the field value as a path and keep all but the last element of path, typically the path's directory
  # [[processors.filepath.dirname]]
  #   field = "path"

  ## Treat the tag value as a path, converting it to its the last element without its suffix
  # [[processors.filepath.stem]]
  #   tag = "path"

  ## Treat the tag value as a path, converting it to the shortest path name equivalent
  ## to path by purely lexical processing
  # [[processors.filepath.clean]]
  #   tag = "path"

  ## Treat the tag value as a path, converting it to a relative path that is lexically
  ## equivalent to the source path when joined to 'base_path'
  # [[processors.filepath.rel]]
  #   tag = "path"
  #   base_path = "/var/log"

  ## Treat the tag value as a path, replacing each separator character in path with a '/' character. Has only
  ## effect on Windows
  # [[processors.filepath.toslash]]
  #   tag = "path"

注意事项

处理顺序

此插件按照配置中出现的顺序处理指定的函数。其中一个例外是stem部分,它会首先应用。

如果您计划对同一个tag/field应用多个转换,请牢记上述处理顺序。

clean 自动调用

尽管clean被提供为一个独立的函数,但在使用reldirname函数时也会调用它,所以无需与它们一起使用。

[[processors.filepath]]
  [[processors.filepath.dir]]
    tag = "path"
  [[processors.filepath.clean]]
    tag = "path"

等同于

[[processors.filepath]]
  [[processors.filepath.dir]]
    tag = "path"

ToSlash 平台特定行为

此函数的效果仅在 Windows 平台上 noticeable,因为底层 Golang 实现的原因。

示例

Basename

[[processors.filepath]]
  [[processors.filepath.basename]]
    tag = "path"
- my_metric,path="/var/log/batch/ajob.log" duration_seconds=134 1587920425000000000
+ my_metric,path="ajob.log" duration_seconds=134 1587920425000000000

Dirname

[[processors.filepath]]
  [[processors.filepath.dirname]]
    field = "path"
    dest = "folder"
- my_metric path="/var/log/batch/ajob.log",duration_seconds=134 1587920425000000000
+ my_metric path="/var/log/batch/ajob.log",folder="/var/log/batch",duration_seconds=134 1587920425000000000

Stem

[[processors.filepath]]
  [[processors.filepath.stem]]
    tag = "path"
- my_metric,path="/var/log/batch/ajob.log" duration_seconds=134 1587920425000000000
+ my_metric,path="ajob" duration_seconds=134 1587920425000000000

Clean

[[processors.filepath]]
  [[processors.filepath.clean]]
    tag = "path"
- my_metric,path="/var/log/dummy/../batch//ajob.log" duration_seconds=134 1587920425000000000
+ my_metric,path="/var/log/batch/ajob.log" duration_seconds=134 1587920425000000000

Rel

[[processors.filepath]]
  [[processors.filepath.rel]]
    tag = "path"
    base_path = "/var/log"
- my_metric,path="/var/log/batch/ajob.log" duration_seconds=134 1587920425000000000
+ my_metric,path="batch/ajob.log" duration_seconds=134 1587920425000000000

ToSlash

[[processors.filepath]]
  [[processors.filepath.rel]]
    tag = "path"
- my_metric,path="\var\log\batch\ajob.log" duration_seconds=134 1587920425000000000
+ my_metric,path="/var/log/batch/ajob.log" duration_seconds=134 1587920425000000000

从 tail 插件处理路径

此插件可以与 tail 输入插件一起使用,对为每个文件注入的path标签进行修改。

场景

  • 一个日志文件/var/log/myjobs/mysql_backup.log,其中包含作业执行的日志。每当作业结束时,一行日志就会按照以下格式写入日志文件:2020-04-05 11:45:21 total time execution: 70 seconds
  • 我们希望生成一个度量,将脚本的持续时间捕获为字段,并将path作为标签。
    • 我们只对没有扩展名的文件名感兴趣,因为这可能足以在仪表板中绘制执行时间。
    • 以防万一,我们不想覆盖原始路径(如果因为某种原因我们最终出现重复,我们可能需要这些信息)。

为此,我们将使用tail输入插件、grok解析器插件和filepath处理器。

# Performs file path manipulations on tags and fields
[[inputs.tail]]
  files = ["/var/log/myjobs/**.log"]
  data_format = "grok"
  grok_patterns = ['%{TIMESTAMP_ISO8601:timestamp:ts-"2006-01-02 15:04:05"} total time execution: %{NUMBER:duration_seconds:int}']
  name_override = "myjobs"

[[processors.filepath]]
   [[processors.filepath.stem]]
     tag = "path"
     dest = "stempath"

对于所提到的日志文件,一个耗时 70 秒的作业产生的输出如下:

myjobs_duration_seconds,host="my-host",path="/var/log/myjobs/mysql_backup.log",stempath="mysql_backup" 70 1587920425000000000

此页面是否有帮助?

感谢您的反馈!


InfluxDB 3.8 新特性

InfluxDB 3.8 和 InfluxDB 3 Explorer 1.6 的主要增强功能。

查看博客文章

InfluxDB 3.8 现已适用于 Core 和 Enterprise 版本,同时发布了 InfluxDB 3 Explorer UI 的 1.6 版本。本次发布着重于操作成熟度,以及如何更轻松地部署、管理和可靠地运行 InfluxDB。

更多信息,请查看

InfluxDB Docker 的 latest 标签将指向 InfluxDB 3 Core

在 **2026 年 2 月 3 日**,InfluxDB Docker 镜像的 latest 标签将指向 InfluxDB 3 Core。为避免意外升级,请在您的 Docker 部署中使用特定的版本标签。

如果使用 Docker 来安装和运行 InfluxDB,latest 标签将指向 InfluxDB 3 Core。为避免意外升级,请在您的 Docker 部署中使用特定的版本标签。例如,如果使用 Docker 运行 InfluxDB v2,请将 latest 版本标签替换为 Docker pull 命令中的特定版本标签 — 例如

docker pull influxdb:2