拓冰建站拓冰建站
首页 / 资讯中心 / 正文

Telegraf webhooks 输入插件:构建统一的多源 Webhook 事件采集服务

Telegraf webhooks 输入插件构建统一的多源 Webhook 事件采集服务【免费下载链接】telegrafAgent for collecting, processing, aggregating, and writing metrics, logs, and other arbitrary data.项目地址: https://gitcode.com/GitHub_Trending/te/telegrafTelegraf 的inputs.webhooks是一个 service input 插件它在 Telegraf 进程内启动一个 HTTP 服务并在同一端口上注册多个独立的 Webhook 监听器把来自 GitHub、Artifactory、Rollbar、Mandrill、Papertrail、Particle、Filestack 等平台的事件推送转换成 Telegraf 指标。读完本文你将掌握该插件的完整配置方式含超时与鉴权参数、单个 HTTP 服务承载多路由的注册机制以及每种 Webhook 的校验逻辑、事件到指标measurement/tags/fields的映射关系和源码级实现依据。一、插件定位一个 HTTP 服务多个 Webhook 监听器根据 插件文档该插件提供一个 HTTP 服务器并为多个 webhook 监听器进行注册。它的核心特征单一监听地址所有已启用的 webhook 共用service_address指定的一个 TCP 端口默认:1619彼此通过 URL 路径path区分按需启用只有配置文件中声明了对应子表如[inputs.webhooks.github]的 webhook 才会注册路由未配置的不占用任何资源service input 语义与普通输入插件不同service 插件启动一个服务来监听等待事件因此全局或插件级的interval设置对它不生效--test、--test-wait、--once等 CLI 选项可能不会产出任何输出见 service_input.md 与 CONFIGURATION.md。源码结构上插件入口是 webhooks.go七个 webhook 各自成包位于plugins/inputs/webhooks/下artifactory/、filestack/、github/、mandrill/、papertrail/、particle/、rollbar/。插件支持 Telegraf v1.0.0 及以上版本适用所有平台。二、完整配置示例与参数说明以下配置完整继承自插件文档与 sample.conf 一致# A Webhooks Event collector [[inputs.webhooks]] ## Address and port to host Webhook listener on service_address :1619 ## Maximum duration before timing out read of the request # read_timeout 10s ## Maximum duration before timing out write of the response # write_timeout 10s [inputs.webhooks.filestack] path /filestack ## HTTP basic auth #username #password [inputs.webhooks.github] path /github # secret ## HTTP basic auth #username #password [inputs.webhooks.mandrill] path /mandrill ## HTTP basic auth #username #password [inputs.webhooks.rollbar] path /rollbar ## HTTP basic auth #username #password [inputs.webhooks.papertrail] path /papertrail ## HTTP basic auth #username #password [inputs.webhooks.particle] path /particle ## HTTP basic auth #username #password [inputs.webhooks.artifactory] path /artifactory顶层参数参数说明默认值service_addressWebhook 监听服务的地址和端口无必须配置read_timeout读取请求的最大超时时间10swrite_timeout写回响应的最大超时时间10s关于超时默认值webhooks.go 定义了defaultReadTimeout和defaultWriteTimeout两个常量均为 10 秒并且在Start()中只有当配置值小于 1 秒即未设置或设置过小时才会回填默认值见 webhooks.go。每个 webhook 子表的参数参数说明适用范围path该 webhook 的 URL 路径例如/github事件方将推送请求发到http://host:1619 path全部username/passwordHTTP Basic 认证配置后未携带正确凭据的请求将被拒绝返回 401全部secret用于校验请求签名的密钥仅github三、启动流程与路由注册机制源码剖析3.1 Start() 的完整调用链插件生命周期由 webhooks.go 的Start()方法驱动流程如下超时归一化检查ReadTimeout/WriteTimeout不足 1 秒时回填 10 秒默认值创建路由mux.NewRouter()创建一个 gorilla/mux 路由器注册所有可用 webhook遍历wb.availableWebhooks()逐个调用webhook.Register(r, acc, wb.Log)把各自的 handler 挂到路由器上构造 http.Server把 router 作为 Handler并把读/写超时设置为 Server 级超时监听端口net.Listen(tcp, wb.ServiceAddress)失败时直接返回错误如端口被占用Telegraf 启动会报error starting server异步 Serve在 goroutine 中srv.Serve(ln)监听异常除正常关闭的http.ErrServerClosed会通过acc.AddError上报为插件错误记录日志Started the webhooks service on address。停止时Stop()调用wb.srv.Close()关闭服务。Gather()为空实现——该插件完全由外部事件驱动不主动采集。3.2 反射式的可用 webhook发现值得关注的实现细节在 availableWebhooks()它用反射遍历Webhooks结构体的所有导出字段凡是实现了如下接口的字段都会被注册// Webhook is an interface that all webhooks must implement type Webhook interface { // Register registers the webhook with the provided router Register(router *mux.Router, acc telegraf.Accumulator, log telegraf.Logger) }判断逻辑有两层过滤字段必须可导出且实现了Webhook接口同时不能为 nilreflect.ValueOf(wbPlugin).IsNil()检查。由于 TOML 配置中未声明子表的字段会被解码为 nil 指针这就天然实现了配置了才启用。测试用例 webhooks_test.go 的TestAvailableWebhooks精确验证了这一点newWebhooks()初始返回空列表每设置一个非 nil 的 webhook 字段如wb.Artifactory artifactory.Webhook{Path: /artifactory}后该 webhook 就出现在返回列表中。从源码结构看这种接口 反射的设计意味着扩展新 webhook 只需实现Register方法并在Webhooks结构体中增加对应字段无需改动启动逻辑。3.3 插件注册入口webhooks.go 的init()通过inputs.Add(webhooks, ...)将插件注册进 Telegraf 输入插件注册表因此配置文件中写作[[inputs.webhooks]]。四、各 Webhook 的实现细节与指标映射插件文档列出的可用 webhook 共 7 种Artifactory、Filestack、Github、Mandrill、Papertrail、Particle、Rollbar。它们的路由注册方式一致——在Register中向 router 注册自己的path并限定 HTTP 方法绝大多数只接受POST但事件解析与校验逻辑各不相同。4.1 GitHub实现见 github_webhooks.go处理流程Basic Auth 校验配置了username/password时从请求头X-Github-Event读取事件类型签名校验若配置了secret用sha1HMAC-SHA1hmac.New(sha1.New, secret)对请求体计算摘要与请求头X-Hub-Signature做hmac.Equal比较不匹配则记录错误日志并返回 400。源码注释说明 SHA1 是 GitHub Webhook 协议本身要求的摘要算法按事件类型反序列化并生成指标写入固定 measurementgithub_webhooks。newEvent()支持的事件类型包括commit_comment、create、delete、deployment、deployment_status、fork、gollum、issue_comment、issues、member、membership、page_build、ping、public、pull_request、pull_request_review_comment、push、release、repository、status、team_add、watch、workflow_job、workflow_run。其中ping事件GitHub 在创建 webhook 时的探测请求不产生任何指标直接返回 200。以push事件为例其指标映射格式为详见 github/README.md# TAGS * event headers[X-Github-Event] string * repository event.repository.full_name string * private event.repository.private bool * user event.sender.login string * admin event.sender.site_admin bool # FIELDS * stars event.repository.stargazers_count int * forks event.repository.forks_count int * issues event.repository.open_issues_count int * ref event.ref string * before event.before string * after event.after string需要说明的一点github 子包的 README 提到可通过measurement_name自定义 measurement 名称但从 github_webhooks.go 的当前代码看写入的是硬编码的github_webhooks文档描述与代码存在偏差实际使用以代码为准。使用方式来自 github/README.md在 GitHub 组织的Settings Webhooks Add webhook中将Payload URL设为http://my_ip:1619/githubContent type选application/json事件选择 Send me everything并可填写与secret相同的密钥用于请求签名校验。4.2 Artifactory实现见 artifactory_webhook.go。与 GitHub 类似支持secret但签名放在请求头x-jfrog-event-auth中同样是sha1HMAC-SHA1 格式。事件路由依据请求体中的domain字段domain识别的 event_typeartifactdeployed/deleted部署或删除、moved/copied移动或复制artifact_property任意属性变更docker任意Docker 事件build任意构建事件release_bundle任意发布包事件distribution任意分发事件destination任意目标仓库事件指标写入固定 measurementartifactory_webhooks签名或事件类型校验失败时返回 400。4.3 Rollbar实现见 rollbar_webhooks.go。支持 Basic Auth先反序列化一个哑事件读取event_name再按事件名二次解析支持new_item新错误、occurrence错误发生、deploy部署三类事件指标写入rollbar_webhooks。注意遇到未知事件类型时它返回 200而不是 400避免 Rollbar 服务端不断重发。4.4 Papertrail实现见 papertrail_webhooks.go是 7 种中请求格式最特殊的一个要求Content-Type为application/x-www-form-urlencoded否则返回415 Unsupported Media Type事件 JSON 放在表单字段payload中支持两种载荷事件型events数组逐条生成指标含source_ip、severity、facility、message、url等字段时间戳取事件的ReceivedAt和计数型counts时间序列按时间点生成count字段Basic Auth 校验失败返回 401载荷缺失或无法解析返回 400指标写入 measurementpapertrailtags 为host主机名/源名称与event保存的搜索名。4.5 Particle实现见 particle_webhooks.go。它是唯一自由格式的 webhook请求体直接声明event事件名、data.tags、data.values字段、published_at时间戳以及可选的measurement。若measurement为空则回退使用event名作为 measurement因此它可以承载任意自定义遥测数据。published_at解析失败时回退为当前时间。4.6 Mandrill实现见 mandrill_webhooks.go。两个特殊点额外注册了一个HEAD路由并固定返回 200returnOK用于 Mandrill 控制台保存 Webhook URL 时的连通性探测请求体是表单编码url.ParseQuery邮件事件数组以 JSON 字符串放在mandrill_events字段中解析后逐条写入mandrill_webhooks时间戳取每个事件的TimeStamp。4.7 Filestack实现见 filestack_webhooks.go。标准 JSON 请求体Basic Auth 校验后按事件类型解析字段写入filestack_webhooks时间戳取事件中的TimeStamp。测试用例中的 testdata 覆盖了dialog_open、upload、video_conversion等典型事件。五、统一的行为约定综合 7 个子包源码可以总结出该插件的统一响应约定情形响应码正常处理含不识别事件但请求本身合法如 GitHubping、Rollbar 未知事件200Basic Auth 凭据错误401请求体读取/解析失败、签名校验失败、事件类型不匹配400Papertrail 内容类型不符415所有 handler 都以defer r.Body.Close()开头确保请求体释放事件成功转换后通过acc.AddFields(...)写入累积器遵循 Telegraf 全局的namepass/tagpass等过滤与插件顺序配置见 CONFIGURATION.md。各 webhook 的解析逻辑均有对应的单元测试与 mock JSON 数据例如 github_webhooks_mock_json_test.go、artifactory_webhook_mock_json_test.go、rollbar_webhooks_events_json_test.go可用于核对各事件的字段映射。六、指标与输出正如插件文档所述The produced metrics depend on the configured webhook.——插件本身不产生固定指标各 webhook 的 measurement 与字段定义互不相同。汇总如下Webhookmeasurement说明GitHubgithub_webhooks按事件类型映射见 github/README.mdArtifactoryartifactory_webhooks按 domain/event_type 映射Rollbarrollbar_webhooksnew_item / occurrence / deployFilestackfilestack_webhooks按 Filestack 事件类型映射Mandrillmandrill_webhooks邮件事件send、bounce 等Papertrailpapertrail事件型与计数型载荷Particle事件自定measurement或event名自由格式七、部署与验证建议最小配置只启用需要的子表即可例如仅采集 GitHub 时配置[[inputs.webhooks]]加[inputs.webhooks.github]pathsecret网络可达性事件源必须能访问service_address对应端口默认:1619。该插件是纯 HTTP 服务生产环境通常需要在前置反向代理上终结 TLS 后转发验证监听成功启动后日志出现Started the webhooks service on address以及各 webhook 的Started the webhooks_github on /github等注册日志验证事件解析Mandrill 路由支持HEAD探测GitHub 创建 webhook 后会自动收到ping事件插件以 200 应答但不产生指标注意 service input 限制interval不生效telegraf --test单轮模式下看不到本插件输出这属于预期行为超时调优默认读写超时各 10 秒对大体积事件体如包含大量文件的 push 载荷可视情况调大read_timeout。八、相关源码索引内容路径插件主文档plugins/inputs/webhooks/README.md样例配置plugins/inputs/webhooks/sample.conf服务启动/路由注册plugins/inputs/webhooks/webhooks.goGitHub 事件解析plugins/inputs/webhooks/github/github_webhooks.goArtifactory 事件解析plugins/inputs/webhooks/artifactory/artifactory_webhook.goRollbar 事件解析plugins/inputs/webhooks/rollbar/rollbar_webhooks.goPapertrail 事件解析plugins/inputs/webhooks/papertrail/papertrail_webhooks.goParticle 事件解析plugins/inputs/webhooks/particle/particle_webhooks.goMandrill 事件解析plugins/inputs/webhooks/mandrill/mandrill_webhooks.goFilestack 事件解析plugins/inputs/webhooks/filestack/filestack_webhooks.go反射注册测试plugins/inputs/webhooks/webhooks_test.go【免费下载链接】telegrafAgent for collecting, processing, aggregating, and writing metrics, logs, and other arbitrary data.项目地址: https://gitcode.com/GitHub_Trending/te/telegraf创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
分享:

看完干货,该让你的企业上线了

免费需求沟通 · 48 小时内出具建站方案 · 河南本地可上门