如果您的目标是高頻更新發送,那麼您可能需要使用 Telegraf 的 WebSocket 輸出插件(在 Telegraf v1.19.0 中引入)而不是使用 HTTP 輸出插件。WebSocket 避免在來自 Telegraf 的每個請求上運作所有 Grafana HTTP 中間件,進而顯着降低 Grafana 後端 CPU 使用率。
在grafana中生成 api key(使用admin role)
eyJrIjoiS3UyOEhVcW1oUmFPVlNkYktkT2swTWJHUEhJUnhDSWUiLCJuIjoibGl2ZSIsImlkIjoxfQ==
macOs安裝telegraf
cd "$(brew --repo)"
git remote set-url origin https://mirrors.aliyun.com/homebrew/brew.git
brew update
brew install telegraf
使用http方式
配置telegraf_http.conf檔案
[agent]
interval = "1s"
flush_interval = "1s"
[[inputs.cpu]]
percpu = false
totalcpu = true
[[outputs.http]]
url = "http://localhost:3000/api/live/push/custom_stream_id"
data_format = "influx"
[outputs.http.headers]
Authorization = "Bearer eyJrIjoiS3UyOEhVcW1oUmFPVlNkYktkT2swTWJHUEhJUnhDSWUiLCJuIjoibGl2ZSIsImlkIjoxfQ=="
啟動
telegraf -config telegraf_http.conf
展示結果
效果是從左到右移動的資料展示
使用WebSocket方式
配置telegraf_websocket.conf檔案
[agent]
interval = "500ms"
flush_interval = "500ms"
[[inputs.cpu]]
percpu = false
totalcpu = true
[[outputs.websocket]]
url = "ws://localhost:3000/api/live/push/custom_stream_id222"
data_format = "influx"
[outputs.websocket.headers]
Authorization = "Bearer eyJrIjoiS3UyOEhVcW1oUmFPVlNkYktkT2swTWJHUEhJUnhDSWUiLCJuIjoibGl2ZSIsImlkIjoxfQ=="
啟動
telegraf -config telegraf_websocket.conf
展示結果與http一緻。
其他
Grafana live 可以通過redis實作HA。
參考
Stream metrics from Telegraf to Grafana
https://grafana.com/blog/2021/08/12/streaming-real-time-sensor-data-to-grafana-using-mqtt-and-grafana-live/
Grafana Live 是 Grafana v8.0 中引入的實時消息傳遞引擎。
使用 Grafana Live,您可以在事件發生後立即将事件資料推送到前端。
這可能是有關儀表闆更改、呈現資料的新架構等的通知。 實時功能可以幫助消除許多地方的頁面重新加載或輪詢,它可以将物聯網 (IOT) 傳感器或任何其他實時資料流式傳輸到面闆。