json datasource使用
概述
在grafana的數據源中,有個比較輕量級的數據源,可以跨網絡進行json數據訪問,比較靈活,對于需要由grafana來渲染數據,可以通過這種方式進行暴露。
安裝
進入grafana服務器,執行以下命令進行安裝,重啟后即可使用json數據源
grafana-cli plugins install simpod-json-datasourcejson數據源服務
這里,我使用了gin框架,實現了json數據源至少需要的3個訪問點,1個用于測試連通性,其余2個為指標數據返回。
json數據源的使用官方參考鏈接:https://grafana.com/grafana/plugins/simpod-json-datasource/#development-setup
To work with this datasource the backend needs to implement 4 endpoints:
- GET / with 200 status code response. Used for “Test connection” on the datasource config page.
- POST /search to return available metrics.
- POST /query to return panel data or annotations.
Those two urls are optional:
- POST /tag-keys returning tag keys for ad hoc filters.
- POST /tag-values returning tag values for ad hoc filters.
實現代碼如下
package mainimport ("fmt""github.com/gin-gonic/gin""net/http" )type Metric struct {Target string `json:"target"`Datapoints [][]interface{} `json:"datapoints"` }func newMetric(target string, data [][]interface{}) *Metric {return &Metric{Target: target,Datapoints: data,} }func main() {r := gin.Default()//對于json datasource,需要實現3個訪問點//1.GET / with 200 status code response. Used for "Test connection" on the datasource config page.r.GET("/", func(c *gin.Context) {c.JSON(200, gin.H{"message": "pong",})})//2.POST /search to return available metrics.r.POST("/search", func(context *gin.Context) {//查看grafana提交過來的body內容body, _ := context.GetRawData()fmt.Println("Body:", string(body))context.JSON(http.StatusOK, []interface{}{"demo", 22, "test", "share"})})//3.POST /query to return panel data or annotations.m1 := newMetric("pps in", [][]interface{}{{"ok", 1450754160000},{"error", 1450754220000},})m2 := newMetric("pps out", [][]interface{}{{861, 1450754160000},{767, 1450754220000},})m3 := newMetric("errors out", [][]interface{}{{861, 1450754160000},{767, 1450754220000},})m4 := newMetric("errors in", [][]interface{}{{861, 1450754160000},{767, 1450754220000},})result := []*Metric{m1, m2, m3, m4,}r.POST("/query", func(context *gin.Context) {//查看grafana提交過來的body內容body, _ := context.GetRawData()fmt.Println("Body:", string(body))context.JSON(http.StatusOK, result)})r.Run("0.0.0.0:58080") // listen and serve on 0.0.0.0:8080 }測試結果
此過程,grafana通過jsonDatasource訪問目標的端點服務 /
服務端輸出
[GIN] 2021/09/15 - 16:43:35 | 200 | 165.478?s | 172.17.0.1 | GET "/"此過程,grafana通過jsonDatasource訪問目標的端點服務 /search,并提交{“target”:""}的json數據
服務端輸出
Body: {"target":""} [GIN] 2021/09/15 - 16:45:38 | 200 | 207.425μs | 172.17.0.1 | POST "/search"此過程,grafana通過jsonDatasource訪問目標的端點服務 /query,并提交了json數據
服務端輸出
Body: {"app":"dashboard","requestId":"Q174","timezone":"browser","panelId":123125,"dashboardId":null,"range":{"from":"2021-09-15T03:58:50.983Z","to":"2021-09-15T09:58:50.983Z","raw":{"from":"now-6h","to":"now"}},"timeInfo":"","interval":"15s","intervalMs":15000,"targets":[{"refId":"A","payload":"","target":"時序性數據","datasource":"JSON"}],"maxDataPoints":1318,"scopedVars":{"__interval":{"text":"15s","value":"15s"},"__interval_ms":{"text":"15000","value":15000}},"startTime":1631699930983,"rangeRaw":{"from":"now-6h","to":"now"},"adhocFilters":[]} [GIN] 2021/09/15 - 16:50:26 | 200 | 265.951μs | 172.17.0.1 | POST "/query"從獲取的json數據可以看到,我們可以通過json數據要求,傳送對應的時序數據給grafana進行數據展示,如果默認的數據請求不夠,可以通過payload進行內容限定。
Body: {"app":"dashboard","requestId":"Q156","timezone":"browser","panelId":123125,"dashboardId":null,"range":{"from":"2021-09-15T02:50:51.665Z","to":"2021-09-15T08:50:51.665Z","raw":{"from":"now-6h","to":"now"}},"timeInfo":"","interval":"30s","intervalMs":30000,"targets":[{"refId":"A","payload":{"require":"demo"},"target":22,"datasource":"JSON"}],"maxDataPoints":581,"scopedVars":{"__interval":{"text":"30s","value":"30s"},"__interval_ms":{"text":"30000","value":30000}},"startTime":1631696138287,"rangeRaw":{"from":"now-6h","to":"now"},"adhocFilters":[]} [GIN] 2021/09/15 - 16:55:39 | 200 | 205.214μs | 172.17.0.1 | POST "/query"總結
總結
以上是生活随笔為你收集整理的json datasource使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据库字典生成器和使用方式
- 下一篇: 宁晋实验初中之行