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 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集