From 8c784a95a2a400dc1c48ba8d50abd9976c2d6b86 Mon Sep 17 00:00:00 2001 From: Wangjunqi123 Date: Wed, 1 Nov 2023 15:36:23 +0800 Subject: [PATCH] define the functionality of get prometheus metric list --- server/dao/prometheus.go | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/server/dao/prometheus.go b/server/dao/prometheus.go index 4f1bda1..b51c4f0 100644 --- a/server/dao/prometheus.go +++ b/server/dao/prometheus.go @@ -1,9 +1,11 @@ package dao import ( + "context" + + "github.com/pkg/errors" "github.com/prometheus/client_golang/api" v1 "github.com/prometheus/client_golang/api/prometheus/v1" - "github.com/pkg/errors" ) var Prome *Prometheus @@ -11,11 +13,13 @@ var Prome *Prometheus type Prometheus struct { addr string Api v1.API + Ctx context.Context } func CreatePrometheus(url string) *Prometheus { return &Prometheus{ addr: url, + Ctx: context.Background(), } } @@ -31,3 +35,19 @@ func (p *Prometheus) CreateAPI() error { return nil } + +func (p *Prometheus) GetMetricList() ([]string, error) { + metrics := make([]string, 0) + + result, err := p.Api.Metadata(p.Ctx, "", "") + if err != nil { + err = errors.Errorf("failed to get prometheus metric list: %s **2", err.Error()) + return nil, err + } + + for m := range result { + metrics = append(metrics, m) + } + + return metrics, nil +} -- Gitee