donglvhe7591 2019-09-18 13:26
浏览 89
已采纳

带自定义收集器的普罗米修斯量规-Go

i have problem on "reseting" the values for gauges because "life stops" as soon http servervice is started or when i start looping my "runJob" then is server not started..

the way how i am trying to establish this:

i load all the Job-s from YAML array. i generate gauges from that, then i run loop to get some values for them. Then i register them.

And after that i start http service for prometheus.

All works perfect, until next cycle - next cycle is just not starting.

I tryed to move functions inside functions etc..

so thats my main function:

        //gets poll time from yaml (60s)
    timerCh := time.Tick(time.Duration(appConf.PollTimeSec) * time.Second)

    //loop after given time
    for range timerCh {
        runJobs()

    }

    //start new muxServer
    server := http.NewServeMux()
    log.Println("DEBUG: Starting server")
    server.Handle(appConf.HostPath, promhttp.Handler())

    http.ListenAndServe(":"+appConf.ListenerPort, server)

and my runJobs function basicly gets Http response codes and ads them to prometheus gauge values. -- everything is OK with that and it works very well on starting, but after i try to start it wheet sleep (as shown in main go) it just gets stuck -

Server is up and values do not change.

So i have (my optinion) two possible ways of fixing it:

  1. My "runJobs" is infinitive loop what runs after every minute

    • thats why is server not started.
    • But when i add there a if statement that on first run(cycle) should server be started, then it still gets stuck when server gets started (next loop cycle just woun't get started)
  2. And the other part, when i start the server first, then it never gets to the part where it starts runJobs()

Prefered outcome should be that:

server is started with first values, and after every minute it runs "runJobs" again.

  • 写回答

1条回答 默认 最新

  • duanhu7400 2019-09-18 13:36
    关注

    The way it is written, your program will not go beyond that for loop. Try this instead:

    go func() {
     //loop after given time
        for range timerCh {
            runJobs()
    
        }
    }()
    

    This runs the for loop in its own goroutine, and runJobs executes every so often.

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

报告相同问题?

悬赏问题

  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式