我是靠谱客的博主 畅快飞机,最近开发中收集的这篇文章主要介绍基于Go实现自定义Prometheus监控指标,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

使用go自定义prometheus监控指标

package main

import (
	"fmt"
	"net/http"

	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/client_golang/prometheus/promhttp"
	"log"
	"math/rand"
	"time"
)

var (
	counter = prometheus.NewCounter(
		prometheus.CounterOpts{
			Namespace: "golang",
			Name:      "my_counter",
			Help:      "This is my counter",
		})

	gauge = prometheus.NewGauge(
		prometheus.GaugeOpts{
			Namespace: "golang",
			Name:      "my_gauge",
			Help:      "This is my gauge",
			ConstLabels: map[string]string{
				"path":"/api/test",
			},
		})
	gauge2 = prometheus.NewGauge(
		prometheus.GaugeOpts{
			Namespace: "golang",
			Name:      "my_gauge",
			Help:      "This is my gauge",
			ConstLabels: map[string]string{
				"path":"/api/test1",
			},
		})
	gauge3 = prometheus.NewGauge(
		prometheus.GaugeOpts{
			Namespace: "golang",
			Name:      "my_gauge",
			Help:      "This is my gauge",
			ConstLabels: map[string]string{
				"path":"/api/test2",
			},
		})
	gauge4 = prometheus.NewGauge(
		prometheus.GaugeOpts{
			Namespace: "golang",
			Name:      "my_gauge",
			Help:      "This is my gauge",
			ConstLabels: map[string]string{
				"path":"/api/test3",
			},
		})
	histogram = prometheus.NewHistogram(
		prometheus.HistogramOpts{
			Namespace: "golang",
			Name:      "my_histogram",
			Help:      "This is my histogram",
		})

	summary = prometheus.NewSummary(
		prometheus.SummaryOpts{
			Namespace: "golang",
			Name:      "my_summary",
			Help:      "This is my summary",
		})
)

func main() {
	rand.Seed(time.Now().Unix())

	http.Handle("/metrics", promhttp.Handler())

	prometheus.MustRegister(counter)
	prometheus.MustRegister(gauge)
	prometheus.MustRegister(gauge2)
	prometheus.MustRegister(gauge3)
	prometheus.MustRegister(gauge4)
	prometheus.MustRegister(histogram)
	prometheus.MustRegister(summary)

	go func() {
		for {
			counter.Add(rand.Float64() * 5)
			gauge.Set(rand.Float64()*15 )
			gauge2.Set(rand.Float64()*15 )
			gauge3.Set(rand.Float64()*15 )
			gauge4.Set(rand.Float64()*15 )
			histogram.Observe(rand.Float64() * 10)
			summary.Observe(rand.Float64() * 10)

			time.Sleep(time.Second)
		}
	}()

	fmt.Println("Starting.....")
	log.Fatal(http.ListenAndServe(":8080", nil))
}

在这里插入图片描述

最后

以上就是畅快飞机为你收集整理的基于Go实现自定义Prometheus监控指标的全部内容,希望文章能够帮你解决基于Go实现自定义Prometheus监控指标所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(37)

评论列表共有 0 条评论

立即
投稿
返回
顶部