简体   繁体   中英

Prometheus gauge with custom collector - 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM