日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

json datasource使用

發布時間:2023/12/20 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 json datasource使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

概述

在grafana的數據源中,有個比較輕量級的數據源,可以跨網絡進行json數據訪問,比較靈活,對于需要由grafana來渲染數據,可以通過這種方式進行暴露。

安裝

進入grafana服務器,執行以下命令進行安裝,重啟后即可使用json數據源

grafana-cli plugins install simpod-json-datasource

json數據源服務

這里,我使用了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"
  • 獲取表格數據
  • 總結

  • 從以上例子可以看到,我們可以非常輕便地通過web服務端點,暴露出希望由grafana進行渲染的數據內容,增強數據的可視化
  • grafana展示的數據大部分是以時序數據為主的,json datasource的數據可以具有時序的一列,并且是 unix timestamp in milliseconds
  • json datasource可以展示表格數據,只要滿足要求的json數據,就可以進行渲染
  • json datasource僅支持數據展示,無法進行操作反饋
  • 總結

    以上是生活随笔為你收集整理的json datasource使用的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。