json datasource使用

json datasource使用,第1张

概述

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 main

import (
   "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 millisecondsjson datasource可以展示表格数据,只要满足要求的json数据,就可以进行渲染json datasource仅支持数据展示,无法进行 *** 作反馈

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/995409.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存