ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

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

Telegraf Wavefront 输出插件指南:通过 API 直连或 Proxy 代理上报指标的完整实战 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 APIDirect Ingestion 直连采集插件直接通过 HTTP/HTTPS 把数据点推送到 Wavefront 云服务此时必须配置认证信息Wavefront Proxy代理转发插件把数据点发送给本机或内网的 Wavefront Proxy默认端口2878由 Proxy 代为转发无需认证。从 README 的元数据plugins/outputs/wavefront/README.md可知该插件于Telegraf v1.5.0引入归属分类标签applications、cloud支持全平台all符合 Wavefront 数据格式text 格式的要求。插件的核心发送逻辑位于 plugins/outputs/wavefront/wavefront.go主要职责为解析并校验连接 URLparseConnectionURL按认证方式构造 Sender 选项makeAuthOptions、createSender将 Telegraf 指标批量转换为 Wavefront 的MetricPointbuildMetrics、buildTags、buildValue通过 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 # timeout10s ## 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_urlhttps://console.cloud.vmware.com ## CSP API Token Authentication, ignored if using a Wavefront proxy # auth_csp_api_tokenCSP_API_TOKEN_HERE ## CSP Client Credentials Authentication Information, ignored if using a ## Wavefront proxy. # [outputs.wavefront.auth_csp_client_credentials] # app_idCSP_APP_ID_HERE # app_secretCSP_APP_SECRET_HERE # org_idCSP_ORG_ID_HERE核心参数语义速览参数默认值作用urlhttps://metrics.wavefront.comWavefront API 或 Proxy 的地址直连时必须可解析且配置认证http_maximum_batch_size10000每次 HTTP 请求携带的最大指标数应高于 agent 的metric_batch_size不建议超过 40000prefix空指标名的统一前缀例如my.specific.prefix.simple_fieldsfalse为true时名为value的字段直接使用指标名不再拼接字段名metric_separator.指标名与字段名之间的分隔字符convert_pathstrue为true时把指标/字段名中的_全部转换为metric_separatoruse_strictfalse开启更宽松的严格清洗规则接受/与,字符use_regexfalse用正则做全量非法字符清洗更彻底但显著更慢source_override[hostname,address,agent_host,node_host]指定哪些标签的值作为 Wavefront sourceconvert_booltrue布尔值转数值false - 0.0true - 1.0truncate_tagsfalse标签 keyvalue 总长超过 254 字符时截断/丢弃避免被 Wavefront 拒绝immediate_flushtrue每批数据立即刷新 SDK 内部缓冲绕过后台异步发送send_internal_metricstrue是否上报~sdk.go开头的内部指标合法、非法、丢弃三类以上默认值均由 plugins/outputs/wavefront/wavefront.go 中的init()注册函数设定且有测试 wavefront_test.go 中的TestDefaults校验如HTTPMaximumBatchSize 10000、Timeout 10s。全局配置与 Secret Store与所有插件一样[[outputs.wavefront]]也支持 Telegraf 的全局配置选项例如指标/标签修改、别名、插件执行顺序等详见 docs/CONFIGURATION.md。此外token选项支持从 secret store 读取密钥避免明文写在配置文件中例如[[outputs.wavefront]] url https://metrics.wavefront.com token ${SECRETSTORE:wavefront_token}从源码看Token、AuthCSPAPIToken、AppID、AppSecret等字段均以config.Secret类型定义见 wavefront.go使用后通过Destroy()清理内存中的明文这正是其支持 secret store 的底层原因。Convert Path 与 Metric Separator路径转换当convert_paths true时指标名与字段名中的_会被全部转换为metric_separator的值。默认情况下为了便于在 Wavefront UI 中浏览指标convert_paths为true、metric_separator为.点号。Wavefront 官方默认集成期望这两个值保持默认但如果你是从其他平台迁移而来可能希望修改它们以保留原有指标路径习惯。举例某指标名为disk_used_percent、字段名为total_bytes默认配置下最终上报的指标名为disk.used.percent.total.bytes源码实现中有两个细节值得注意wavefront.go 的Connect()如果ConvertPaths true但MetricSeparator _插件会自动关闭路径转换因为转换后结果不变转换通过strings.NewReplacer(_, w.MetricSeparator)完成在Connect()时重建替换器。对应测试用例TestBuildMetrics也验证了这一点输入指标testing_just*a%metric:float会被清洗、转换后输出为testing.just-a-metric-float见 wavefront_test.go。指标名与标签清洗Sanitize、Use Strict 与 Use RegexWavefront 对指标名、标签名有严格的字符集要求。插件提供两套清洗机制默认清洗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/1、tag,2也被原样保留见 wavefront_test.go。use_regex正则全量清洗默认的Sanitize可能漏掉部分特殊字符use_regex true时改用正则[^a-zA-Z\d_.-]定义于 wavefront.go 的sanitizedRegex把每个非法字符替换为-。由于正则匹配显著慢于基于strings.Replacer的字符表替换README 明确提示Use Regex 更彻底但会带来性能损耗仅在确有特殊字符未清洗干净时再开启。插件测试中还附带了BenchmarkReplaceAllString与BenchmarkReplacer基准用例用于量化两者性能差距。标签值中的*字符会被替换为-双引号在序列化阶段会被转义为\见 replacers.go 的tagValueReplacer。Source Override指定数据源身份Wavefront 的每条数据点都必须带有source数据源标识。默认情况下插件使用host标签的值作为 source。但在采集第三方系统指标时往往希望用被监控的目标系统作为 source而不是运行 Telegraf 的主机。很多 Telegraf 输入插件会用不同的标签名标识目标例如 SNMP 插件常用snmp_host。source_override正是为解决标签名不一致而设计插件按列表中标签名的先后顺序依次查找找到第一个匹配的标签后即用其值作为 source不再继续检查后续标签若全部未命中则回退使用默认的host标签值作为 source。源码中的buildTagswavefront.go还会做以下处理若指标自带source标签优先使用它并删除该标签当通过source_override命中某个标签时原host标签的值会被改存为telegraf_host标签以保留原始主机信息无论何种路径最终的host标签都会被删除source 值中的*会被替换为-。测试TestBuildTagsWithSource覆盖了这些场景例如{snmp_host: realHost, host: origHost}输出sourcerealHost且保留telegraf_hostorigHost见 wavefront_test.go。直连认证方式Token 与 CSP 凭据使用 Wavefront API 直连采集时三种认证方式三选一若通过 Wavefront Proxy 转发则无需任何认证。方式一Wavefront API Tokentoken在 Wavefront UI 右上角点击齿轮图标 → 点击你的账户名通常是邮箱→ 点击API access即可获取 API Token。配置为token YOUR_TOKEN方式二CSP API Tokenauth_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 Credentialsauth_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_HEREauth_csp_base_url默认值为https://console.cloud.vmware.com仅在 CSP 认证时生效使用 API Token 或 Proxy 时该配置被忽略。认证选项的实际装配发生在makeAuthOptions()wavefront.go其优先级为token→auth_csp_api_token→auth_csp_client_credentials最终分别映射为 SDK 的APIToken、CSPAPIToken与CSPClientCredentials选项三者均未配置时返回空选项此时直连请求会因缺少凭据被拒绝。TestMakeAuthOptions对四种情况均有覆盖见 wavefront_test.go。Wavefront 数据格式Wavefront 采用 line文本协议格式的变体插件的预期输入格式为metric value [timestamp] source|hostsourceTagValue [tagk1tagv1 ...tagkNtagvN]序列化实现位于 plugins/serializers/wavefront/wavefront.go 的formatMetricPoint实际输出形如metric.name 42.000000 1700000000 sourcemy-source tag1value1注意序列化输出中指标名、source 值、标签 key/value 均用双引号包裹数值以 6 位小数的浮点格式写出strconv.AppendFloat(..., f, 6, 64)时间戳为 Unix 秒m.Time().Unix()。合法值约束Wavefront 仅接受**整数integers与浮点数floats**作为数据点值。插件的buildValuewavefront.go支持的类型为int64、uint64、float64直接转换为float64bool在convert_bool true默认时映射为数值false - 0.0、true - 1.0若关闭该选项布尔值将无法发送其他类型如string返回错误并跳过该字段日志记录为 Debug 级别。如果确实需要把字符串映射为数值README 建议配合enum处理器插件使用plugins/processors/enum。TestBuildValue覆盖了上述类型转换与非法类型报错的用例。标签长度限制与截断Wavefront 会拒绝标签名值总长度超过 254 字符的数据点。插件默认truncate_tags false不截断以保持向后兼容当truncate_tags true时常量maxTagLength 254标签 key 本身超过 254 字符直接跳过该标签并输出 Warn 日志keyvalue 总长超过 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、跳过校验等timeoutHTTP 超时默认10smax_idle_conn全局最大空闲 keep-alive 连接数0 表示不限制max_idle_conn_per_host每主机最大空闲连接数0 使用 Go 默认值默认2idle_conn_timeout空闲连接超时0 表示不限制。最小可用配置实战场景一直连 Wavefront APIToken 认证[[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.go、plugins/outputs/wavefront/wavefront_test.go、plugins/outputs/wavefront/sample.conf序列化器实现plugins/serializers/wavefront/wavefront.go、plugins/serializers/wavefront/replacers.go插件全局配置与 Secret Storedocs/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),仅供参考
返回列表