栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Go语言

json datasource使用

Go语言 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 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
}
测试结果
  1. 建立数据源并测试

此过程,grafana通过jsonDatasource访问目标的端点服务 /

服务端输出

[GIN] 2021/09/15 - 16:43:35 | 200 |     165.478?s |      172.17.0.1 | GET      "/"
  1. 获取指标项目

此过程,grafana通过jsonDatasource访问目标的端点服务 /search,并提交{“target”:""}的json数据

服务端输出

Body: {"target":""}
[GIN] 2021/09/15 - 16:45:38 | 200 |     207.425µs |      172.17.0.1 | POST     "/search"
  1. 获取指标数据

此过程,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"
  1. 获取表格数据

总结
  1. 从以上例子可以看到,我们可以非常轻便地通过web服务端点,暴露出希望由grafana进行渲染的数据内容,增强数据的可视化
  2. grafana展示的数据大部分是以时序数据为主的,json datasource的数据可以具有时序的一列,并且是 unix timestamp in milliseconds
  3. json datasource可以展示表格数据,只要满足要求的json数据,就可以进行渲染
  4. json datasource仅支持数据展示,无法进行操作反馈
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/907804.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号