Go 普罗米修斯自定义注册表不工作
我还在学习普罗米修斯,所以可能我对这个问题没有把握 我所需要的只是一个自定义注册表,在这里我只能收集我的指标。因为我在学习普罗米修斯,所以我对普罗米修斯提供的默认度量并不感兴趣,即所有的go度量,如Go 普罗米修斯自定义注册表不工作,go,prometheus,Go,Prometheus,我还在学习普罗米修斯,所以可能我对这个问题没有把握 我所需要的只是一个自定义注册表,在这里我只能收集我的指标。因为我在学习普罗米修斯,所以我对普罗米修斯提供的默认度量并不感兴趣,即所有的go度量,如go\u gc\u duration\u seconds,go\u threads,promhttp\u metric\u handler\u请求飞行中的时间等 package main import ( "fmt" "log" "math/rand" "net/ht
go\u gc\u duration\u seconds
,go\u threads
,promhttp\u metric\u handler\u请求飞行中的时间
等
package main
import (
"fmt"
"log"
"math/rand"
"net/http"
"sync"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var Types = [2]string{"Random", "Simple"}
type Queue struct {
mutex sync.Mutex
jobs []Job
}
func (q *Queue) Add(job Job) {
q.mutex.Lock()
q.jobs = append(q.jobs, job)
q.mutex.Unlock()
}
func (q *Queue) Dequeue() Job {
q.mutex.Lock()
job := q.jobs[0]
q.jobs = q.jobs[1:]
q.mutex.Unlock()
return job
}
type Job struct {
message string
Type string
}
func (j *Job) Run() {
fmt.Println(j.message)
}
var jobsInQueue = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "jobs_in_queue",
Help: "Current number of jobs in the queue",
},
[]string{"job_type"},
)
var register = prometheus.NewRegistry()
var queue = &Queue{}
func init() {
rand.Seed(2)
// prometheus.MustRegister(jobsInQueue)
// register the collector..
register.MustRegister(jobsInQueue)
queue.jobs = make([]Job, 0)
}
func main() {
go func() {
i := 0
for {
job := Job{}
num := rand.Intn(2)
type_d := Types[num]
job.Type = type_d
job.message = fmt.Sprintf("[%s] job %d", type_d, i)
enqueueJob(job)
fmt.Println(i)
i++
time.Sleep(1 * time.Second)
}
}()
// sleep so that we do not read from a empty queue
time.Sleep(2 * time.Millisecond)
go func() {
for {
runNextJob()
time.Sleep(2 * time.Second)
}
}()
http.Handle("/metrics", promhttp.Handler())
log.Fatal(http.ListenAndServe(":8080", nil))
}
func enqueueJob(job Job) {
queue.Add(job)
jobsInQueue.WithLabelValues(job.Type).Inc()
}
func runNextJob() {
job := queue.Dequeue()
jobsInQueue.WithLabelValues(job.Type).Dec()
job.Run()
}
但是当我运行下面的代码时,我在8080
的/metrics
端点的队列中看不到我的I.ejobs\u
度量
我该如何完成这项工作。promhttp.Handler()为默认注册表创建一个处理程序。您需要使用promhttp.HandlerFor(注册表,HandlerOpts{})