dongluojiao6322 2018-08-16 11:17
浏览 196
已采纳

Prometheus自定义注册表不起作用

I'm still learning Prometheus so maybe I'm not sure about the problem correctly.

All I need is a custom registry where I can only collect my metrics. Since I'm learning Prometheus I really not interested in default metric provide by Prometheus namely all the go metrics like go_gc_duration_seconds, go_gc_duration_seconds_count, go_threads, promhttp_metric_handler_requests_in_flight etc

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()
}

But when I run the following code I don't see my i.e jobs_in_queue metric in /metrics endpoint of 8080.

How am I suppose to get this work.

  • 写回答

1条回答 默认 最新

  • duandai7601 2018-08-16 16:26
    关注

    promhttp.Handler() creates a handler for the default registry. You need to use promhttp.HandlerFor(registry, HandlerOpts{})

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误