Telegraf Wavefront 输出插件指南:通过 API 直连或 Proxy 代理上报指标的完整实战
2026/9/14 17:27:10 网站建设 项目流程

Telegraf Wavefront 输出插件指南:通过 API 直连或 Proxy 代理上报指标的完整实战

【免费下载链接】telegrafAgent for collecting, processing, aggregating, and writing metrics, logs, and other arbitrary data.项目地址: https://gitcode.com/GitHub_Trending/te/telegraf

本篇技术指南围绕 Telegraf 的 Wavefront Output 插件(plugins/outputs/wavefront)展开,介绍如何将 Telegraf 采集到的指标写入 VMware 的 Wavefront 监控平台(通过 Wavefront API 直连或 Wavefront Proxy 转发,支持 HTTP/HTTPS)。读完本文你将掌握完整的配置项语义、三种直连认证方式(API Token、CSP API Token、CSP Client Credentials)、指标名与标签的清洗规则、source 覆盖策略以及数据格式与合法值约束,并能在真实环境中正确落地这一输出插件。

插件概览与定位

Wavefront Output 插件是 Telegraf 众多 outputs 之一,注册名称为wavefront。它依赖 wavefront-sdk-go 的senders包实现底层发送,支持两种目标形态:

  • Wavefront API(Direct Ingestion 直连采集):插件直接通过 HTTP/HTTPS 把数据点推送到 Wavefront 云服务,此时必须配置认证信息
  • Wavefront Proxy(代理转发):插件把数据点发送给本机或内网的 Wavefront Proxy(默认端口2878),由 Proxy 代为转发,无需认证

从 README 的元数据(plugins/outputs/wavefront/README.md)可知,该插件于Telegraf v1.5.0引入,归属分类标签applicationscloud,支持全平台(all),符合 Wavefront 数据格式(text 格式)的要求。

插件的核心发送逻辑位于 plugins/outputs/wavefront/wavefront.go,主要职责为:

  1. 解析并校验连接 URL(parseConnectionURL);
  2. 按认证方式构造 Sender 选项(makeAuthOptionscreateSender);
  3. 将 Telegraf 指标批量转换为 Wavefront 的MetricPointbuildMetricsbuildTagsbuildValue);
  4. 通过 SDK 的SendMetric发送,并在 SDK 内部缓冲溢出时强制Flush兜底(Write)。

完整配置示例与参数解析

以下为插件自带的完整配置(对应仓库中的 plugins/outputs/wavefront/sample.conf,与 README 中@sample.conf块一致):

[[outputs.wavefront]] ## URL for Wavefront API or Wavefront proxy instance ## Direct Ingestion via Wavefront API requires authentication. See below. url = "https://metrics.wavefront.com" ## Maximum number of metrics to send per HTTP request. This value should be ## higher than the `metric_batch_size`. Values higher than 40,000 are not ## recommended. # http_maximum_batch_size = 10000 ## Prefix for metrics keys # prefix = "my.specific.prefix." ## Use "value" for name of simple fields # simple_fields = false ## character to use between metric and field name # metric_separator = "." ## Convert metric name paths to use metricSeparator character ## When true will convert all _ (underscore) characters in final metric name. # convert_paths = true ## Use Strict rules to sanitize metric and tag names from invalid characters ## When enabled forward slash (/) and comma (,) will be accepted # use_strict = false ## Use Regex to sanitize metric and tag names from invalid characters ## Regex is more thorough, but significantly slower. # use_regex = false ## Tags to use as the source name for Wavefront ("host" if none is found) # source_override = ["hostname", "address", "agent_host", "node_host"] ## Convert boolean values to numeric values, with false -> 0.0 and true -> 1.0 # convert_bool = true ## Truncate metric tags to a total of 254 characters for the tag name value ## Wavefront will reject any data point exceeding this limit if not truncated ## Defaults to 'false' to provide backwards compatibility. # truncate_tags = false ## Flush the internal buffers after each batch. This effectively bypasses the ## background sending of metrics normally done by the Wavefront SDK. This can ## be used if you are experiencing buffer overruns. The sending of metrics ## will block for a longer time, but this will be handled gracefully by ## internal buffering in Telegraf. # immediate_flush = true ## Send internal metrics (starting with `~sdk.go`) for valid, invalid, and ## dropped metrics # send_internal_metrics = true ## Optional TLS Config ## Set to true/false to enforce TLS being enabled/disabled. If not set, ## enable TLS only if any of the other options are specified. # tls_enable = ## Trusted root certificates for server # tls_ca = "/path/to/cafile" ## Used for TLS client certificate authentication # tls_cert = "/path/to/certfile" ## Used for TLS client certificate authentication # tls_key = "/path/to/keyfile" ## Send the specified TLS server name via SNI # tls_server_name = "kubernetes.example.com" ## Use TLS but skip chain & host verification # insecure_skip_verify = false ## HTTP Timeout # timeout="10s" ## MaxIdleConns controls the maximum number of idle (keep-alive) connections ## across all hosts. Zero means unlimited. # max_idle_conn = 0 ## MaxIdleConnsPerHost, if non-zero, controls the maximum idle (keep-alive) ## connections to keep per-host. If zero, DefaultMaxIdleConnsPerHost is used. # max_idle_conn_per_host = 2 ## Idle (keep-alive) connection timeout # idle_conn_timeout = 0 ## Authentication for Direct Ingestion. ## Direct Ingestion requires one of: `token`,`auth_csp_api_token`, or ## `auth_csp_client_credentials` ## to learn more about using CSP credentials with Wavefront. ## Not required if using a Wavefront proxy. ## Wavefront API Token Authentication, ignored if using a Wavefront proxy ## 1. Click the gear icon at the top right in the Wavefront UI. ## 2. Click your account name (usually your email) ## 3. Click *API access*. # token = "YOUR_TOKEN" ## Base URL used for authentication, ignored if using a Wavefront proxy or a ## Wavefront API token. # auth_csp_base_url=https://console.cloud.vmware.com ## CSP API Token Authentication, ignored if using a Wavefront proxy # auth_csp_api_token=CSP_API_TOKEN_HERE ## CSP Client Credentials Authentication Information, ignored if using a ## Wavefront proxy. # [outputs.wavefront.auth_csp_client_credentials] # app_id=CSP_APP_ID_HERE # app_secret=CSP_APP_SECRET_HERE # org_id=CSP_ORG_ID_HERE

核心参数语义速览

参数默认值作用
urlhttps://metrics.wavefront.comWavefront API 或 Proxy 的地址,直连时必须可解析且配置认证
http_maximum_batch_size10000每次 HTTP 请求携带的最大指标数,应高于 agent 的metric_batch_size,不建议超过 40000
prefix指标名的统一前缀,例如"my.specific.prefix."
simple_fieldsfalsetrue时,名为value的字段直接使用指标名,不再拼接字段名
metric_separator.指标名与字段名之间的分隔字符
convert_pathstruetrue时把指标/字段名中的_全部转换为metric_separator
use_strictfalse开启更宽松的严格清洗规则,接受/,字符
use_regexfalse用正则做全量非法字符清洗,更彻底但显著更慢
source_override["hostname","address","agent_host","node_host"]指定哪些标签的值作为 Wavefront source
convert_booltrue布尔值转数值:false -> 0.0true -> 1.0
truncate_tagsfalse标签 key+value 总长超过 254 字符时截断/丢弃,避免被 Wavefront 拒绝
immediate_flushtrue每批数据立即刷新 SDK 内部缓冲,绕过后台异步发送
send_internal_metricstrue是否上报~sdk.go开头的内部指标(合法、非法、丢弃三类)

以上默认值均由 plugins/outputs/wavefront/wavefront.go 中的init()注册函数设定,且有测试 wavefront_test.go 中的TestDefaults校验(如HTTPMaximumBatchSize == 10000Timeout == 10s)。

全局配置与 Secret Store

与所有插件一样,[[outputs.wavefront]]也支持 Telegraf 的全局配置选项,例如指标/标签修改、别名、插件执行顺序等,详见 docs/CONFIGURATION.md。此外,token选项支持从 secret store 读取密钥,避免明文写在配置文件中,例如:

[[outputs.wavefront]] url = "https://metrics.wavefront.com" token = "${SECRETSTORE:wavefront_token}"

从源码看,TokenAuthCSPAPITokenAppIDAppSecret等字段均以config.Secret类型定义(见 wavefront.go),使用后通过Destroy()清理内存中的明文,这正是其支持 secret store 的底层原因。

Convert Path 与 Metric Separator:路径转换

convert_paths = true时,指标名与字段名中的_会被全部转换为metric_separator的值。默认情况下,为了便于在 Wavefront UI 中浏览指标,convert_pathstruemetric_separator.(点号)。Wavefront 官方默认集成期望这两个值保持默认;但如果你是从其他平台迁移而来,可能希望修改它们以保留原有指标路径习惯。

举例:某指标名为disk_used_percent、字段名为total_bytes,默认配置下最终上报的指标名为:

disk.used.percent.total.bytes

源码实现中有两个细节值得注意(wavefront.go 的Connect()):

  • 如果ConvertPaths == trueMetricSeparator == "_",插件会自动关闭路径转换(因为转换后结果不变);
  • 转换通过strings.NewReplacer("_", w.MetricSeparator)完成,在Connect()时重建替换器。

对应测试用例TestBuildMetrics也验证了这一点:输入指标testing_just*a%metric:float会被清洗、转换后输出为testing.just-a-metric-float(见 wavefront_test.go)。

指标名与标签清洗:Sanitize、Use Strict 与 Use Regex

Wavefront 对指标名、标签名有严格的字符集要求。插件提供两套清洗机制:

默认清洗(Sanitize)

绝大多数非法字符会被自动替换为-。字符替换表定义在 plugins/serializers/wavefront/replacers.go 的sanitizedChars,覆盖! @ # $ % ^ & * ( ) + \' " [ ] { } : ; < > , ? / \ | 空格 =` 等字符。

use_strict(宽松严格模式)

use_strict = true时使用strictSanitizedChars替换表:该表不再替换/,,也就是说正斜杠和逗号被保留接受。测试TestBuildMetricsStrict验证了这一点:指标testing_just/another,metric:float在 strict 模式下输出为testing.just/another,metric-float,标签tag/1tag,2也被原样保留(见 wavefront_test.go)。

use_regex(正则全量清洗)

默认的Sanitize可能漏掉部分特殊字符,use_regex = true时改用正则[^a-zA-Z\d_.-](定义于 wavefront.go 的sanitizedRegex)把每个非法字符替换为-。由于正则匹配显著慢于基于strings.Replacer的字符表替换,README 明确提示Use Regex 更彻底但会带来性能损耗,仅在确有特殊字符未清洗干净时再开启。插件测试中还附带了BenchmarkReplaceAllStringBenchmarkReplacer基准用例,用于量化两者性能差距。

标签值中的*字符会被替换为-,双引号"在序列化阶段会被转义为\"(见 replacers.go 的tagValueReplacer)。

Source Override:指定数据源身份

Wavefront 的每条数据点都必须带有source(数据源)标识。默认情况下插件使用host标签的值作为 source。但在采集第三方系统指标时,往往希望用被监控的目标系统作为 source,而不是运行 Telegraf 的主机。很多 Telegraf 输入插件会用不同的标签名标识目标(例如 SNMP 插件常用snmp_host)。

source_override正是为解决标签名不一致而设计:

  1. 插件按列表中标签名的先后顺序依次查找;
  2. 找到第一个匹配的标签后,即用其值作为 source,不再继续检查后续标签
  3. 若全部未命中,则回退使用默认的host标签值作为 source。

源码中的buildTags(wavefront.go)还会做以下处理:

  • 若指标自带source标签,优先使用它并删除该标签;
  • 当通过source_override命中某个标签时,原host标签的值会被改存为telegraf_host标签,以保留原始主机信息;
  • 无论何种路径,最终的host标签都会被删除,source 值中的*会被替换为-

测试TestBuildTagsWithSource覆盖了这些场景,例如{"snmp_host": "realHost", "host": "origHost"}输出source=realHost且保留telegraf_host=origHost(见 wavefront_test.go)。

直连认证方式:Token 与 CSP 凭据

使用 Wavefront API 直连采集时,三种认证方式三选一;若通过 Wavefront Proxy 转发则无需任何认证。

方式一:Wavefront API Token(token

在 Wavefront UI 右上角点击齿轮图标 → 点击你的账户名(通常是邮箱)→ 点击API access即可获取 API Token。配置为:

token = "YOUR_TOKEN"

方式二:CSP API Token(auth_csp_api_token

使用 VMware Cloud Services Platform 生成的 API Token 认证:

auth_csp_base_url = "https://console.cloud.vmware.com" auth_csp_api_token = "CSP_API_TOKEN_HERE"

方式三:CSP Client Credentials(auth_csp_client_credentials

适用于服务到服务(server-to-server)应用场景,使用 App ID、App Secret 与可选的 Org ID 换取令牌:

[outputs.wavefront.auth_csp_client_credentials] app_id = "CSP_APP_ID_HERE" app_secret = "CSP_APP_SECRET_HERE" org_id = "CSP_ORG_ID_HERE"

auth_csp_base_url默认值为https://console.cloud.vmware.com,仅在 CSP 认证时生效;使用 API Token 或 Proxy 时该配置被忽略。

认证选项的实际装配发生在makeAuthOptions()(wavefront.go),其优先级为:tokenauth_csp_api_tokenauth_csp_client_credentials,最终分别映射为 SDK 的APITokenCSPAPITokenCSPClientCredentials选项;三者均未配置时返回空选项(此时直连请求会因缺少凭据被拒绝)。TestMakeAuthOptions对四种情况均有覆盖(见 wavefront_test.go)。

Wavefront 数据格式

Wavefront 采用 line(文本)协议格式的变体,插件的预期输入格式为:

<metric> <value> [<timestamp>] <source|host>=<sourceTagValue> [tagk1=tagv1 ...tagkN=tagvN]

序列化实现位于 plugins/serializers/wavefront/wavefront.go 的formatMetricPoint,实际输出形如:

"metric.name" 42.000000 1700000000 source="my-source" "tag1"="value1"

注意序列化输出中:

  • 指标名、source 值、标签 key/value 均用双引号包裹;
  • 数值以 6 位小数的浮点格式写出(strconv.AppendFloat(..., 'f', 6, 64));
  • 时间戳为 Unix 秒(m.Time().Unix())。

合法值约束

Wavefront 仅接受**整数(integers)与浮点数(floats)**作为数据点值。插件的buildValue(wavefront.go)支持的类型为:

  • int64uint64float64:直接转换为float64
  • bool:在convert_bool = true(默认)时映射为数值,false -> 0.0true -> 1.0;若关闭该选项,布尔值将无法发送;
  • 其他类型(如string)返回错误并跳过该字段(日志记录为 Debug 级别)。

如果确实需要把字符串映射为数值,README 建议配合enum处理器插件使用(plugins/processors/enum)。TestBuildValue覆盖了上述类型转换与非法类型报错的用例。

标签长度限制与截断

Wavefront 会拒绝标签名+值总长度超过 254 字符的数据点。插件默认(truncate_tags = false)不截断,以保持向后兼容;当truncate_tags = true时(常量maxTagLength = 254):

  • 标签 key 本身超过 254 字符:直接跳过该标签并输出 Warn 日志;
  • key+value 总长超过 254:对 value 进行截断,保留前254 - len(key)个字符。

TestTagLimits详细验证了"全跳过 / 截断 value / 不截断 / 关闭截断"四种场景(见 wavefront_test.go)。

发送流程、刷新策略与缓冲兜底

发送主流程

Write()(wavefront.go)对每一条 Telegraf 指标调用buildMetrics展开为多个MetricPoint(每个字段一个点),随后逐个调用sender.SendMetric。连接建立时通过createSender构造 SDK Sender,传入的关键选项包括:

  • BatchSize(w.HTTPMaximumBatchSize):控制每批发送的指标数;
  • FlushIntervalSeconds(flushSeconds):SDK 后台刷新间隔;
  • HTTPClient(client):复用 Telegraf 的 HTTP 客户端(含 TLS、超时、连接池配置);
  • SendInternalMetrics(w.SendInternalMetrics):是否上报 SDK 内部指标。

immediate_flush 与缓冲溢出兜底

immediate_flush = true(默认)时,Connect()会把 SDK 的刷新间隔设为 86400 秒(一个极长的值),从而绕过 SDK 的后台异步发送,改为每批数据在Write()末尾显式调用Flush()。这适合遇到 SDK 内部缓冲溢出(buffer overrun)的场景——虽然单次发送会阻塞更长时间,但 Telegraf 自身的内部缓冲会优雅地消化这一阻塞。

即使不开启immediate_flushWrite()也内置了溢出兜底逻辑:当SendMetric返回错误且判定为可重试错误(isRetryable)时,说明 SDK 内部缓冲已满,插件会先强制Flush()再重试一次。唯一的非重试错误是"empty metric name"(空指标名,重试只会反复得到同一错误),源码注释也说明目前以"白名单式"区分可重试/不可重试错误,且已向 Wavefront 提交了提供规范判定接口的支持请求。

TLS 与 HTTP 连接池

插件继承 Telegraf 的通用 HTTP 客户端配置(common_http.HTTPClientConfig),支持:

  • tls_enable/tls_ca/tls_cert/tls_key/tls_server_name/insecure_skip_verify:完整 TLS 配置(客户端证书、SNI、跳过校验等);
  • timeout:HTTP 超时(默认10s);
  • max_idle_conn:全局最大空闲 keep-alive 连接数,0 表示不限制;
  • max_idle_conn_per_host:每主机最大空闲连接数,0 使用 Go 默认值(默认2);
  • idle_conn_timeout:空闲连接超时,0 表示不限制。

最小可用配置实战

场景一:直连 Wavefront API(Token 认证)

[[outputs.wavefront]] url = "https://metrics.wavefront.com" token = "YOUR_WAVEFRONT_API_TOKEN" prefix = "prod.dc1." http_maximum_batch_size = 10000 source_override = ["hostname", "address", "agent_host", "node_host"]

场景二:内网 Wavefront Proxy 转发(无需认证)

[[outputs.wavefront]] url = "http://wavefront-proxy.local:2878" convert_paths = true metric_separator = "." truncate_tags = true

场景三:CSP Client Credentials 直连

[[outputs.wavefront]] url = "https://metrics.wavefront.com" auth_csp_base_url = "https://console.cloud.vmware.com" [outputs.wavefront.auth_csp_client_credentials] app_id = "CSP_APP_ID_HERE" app_secret = "CSP_APP_SECRET_HERE" org_id = "CSP_ORG_ID_HERE"

配置完成后,通过telegraf --config telegraf.conf --test可校验插件能否正常加载与连通;正式运行后可通过send_internal_metrics上报的~sdk.go内部指标观察合法、非法与丢弃的数据点数量,用于排查清洗规则或认证配置是否到位。

深入阅读

  • 插件源码与实现:plugins/outputs/wavefront/wavefront.goplugins/outputs/wavefront/wavefront_test.goplugins/outputs/wavefront/sample.conf
  • 序列化器实现:plugins/serializers/wavefront/wavefront.goplugins/serializers/wavefront/replacers.go
  • 插件全局配置与 Secret Store:docs/CONFIGURATION.md
  • 字符串映射为数值的辅助方案:plugins/processors/enum

【免费下载链接】telegrafAgent for collecting, processing, aggregating, and writing metrics, logs, and other arbitrary data.项目地址: https://gitcode.com/GitHub_Trending/te/telegraf

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询