grafana 通过自定义API获取数据

一、安装插件

安装infinity插件

二、配置数据源


三、配置图表

1、数据

这边提供一个go的demo

golang 复制代码
package main

import (
	"math/rand"
	"net/http"
	"time"

	"github.com/gin-gonic/gin"
)

func main() {
	router := gin.Default()

	rand.Seed(time.Now().Unix())
	router.GET("/api/data", func(ctx *gin.Context) {
		var data []Metric
		for i := 0; i < 10; i++ {
			data = append(data, Metric{Entity: "m1", Value: rand.Float64() * 10.0, Time: time.Now().Add(time.Duration(-i) * time.Minute).UnixMilli()})
			data = append(data, Metric{Entity: "m2", Value: rand.Float64() * 10.0, Time: time.Now().Add(time.Duration(-i) * time.Minute).UnixMilli()})
			data = append(data, Metric{Entity: "m3", Value: rand.Float64() * 10.0, Time: time.Now().Add(time.Duration(-i) * time.Minute).UnixMilli()})
		}

		ctx.JSON(http.StatusOK, data)
	})

	router.Run(":8080")
}

type Metric struct {
	Entity string      `json:"entity"`
	Value  interface{} `json:"value"`
	Time   int64       `json:"time"`
}

2、图表

相关推荐
飞奔的瓜牛9 小时前
Loki + Promtail + Prometheus + Grafana 实现日志关键字监控及告警
grafana·prometheus·日志监控告警
飞奔的瓜牛13 小时前
Loki + Grafana + Alloy 实现日志检索:完整部署步骤
grafana
JasonChen@6664 天前
Prometheus 存储实战:本地 TSDB 配置与 VictoriaMetrics 远端存储对接(18)
grafana·prometheus·victoria
JasonChen@6665 天前
Grafana 权限管理实战:用户授权与 Team 团队管理(17)
grafana·prometheus
JasonChen@6665 天前
pushgateway组件实现自定义监控数据采集(16)
grafana·prometheus·pushgateway
W_son8 天前
14.Grafana 监控看板实战:新建、调优、变量配置全流程
grafana·prometheus
BullSmall10 天前
Prometheus监控栈安全加固全攻略
安全·grafana·prometheus
JasonChen@66612 天前
prometheus静态监控static_config案例四(10)
grafana·prometheus
JasonChen@66613 天前
prometheus静态监控static_config案例二(8)
grafana·prometheus
IT古董20 天前
Grafana 企业级可观测性平台:从零基础到生产级实战完整教程
grafana