-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
We needed a possibility to execute some jobs / queries synchronously every time /metrics is requested. So I added the infrastructure to execute jobs with an interval <= 0 synchronously when the http-handler is called. interval: '0s' # an interval <= 0 will make the queries synchronous Some typos fixed by Felix Hillingshaeuser, thanks!
- Loading branch information
1 parent
0001728
commit be16a1f
Showing
5 changed files
with
70 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
"net/http" | ||
) | ||
|
||
// handlerFunc can be used as handler for http.HandleFunc() | ||
// all synchronous jobs will be triggered and waited for, | ||
// then the promhttp handler is executed | ||
func (ex *Exporter) handlerFunc(w http.ResponseWriter, req *http.Request) { | ||
// pull all triggers on jobs with interval 0 | ||
for _, job := range ex.jobs { | ||
// if job is nil or is async then continue to next job | ||
if job == nil || job.Interval > 0 { | ||
continue | ||
} | ||
job.Trigger <- true | ||
} | ||
|
||
// wait for all sync jobs to finish | ||
for _, job := range ex.jobs { | ||
if job == nil || job.Interval > 0 { | ||
continue | ||
} | ||
<-job.Done | ||
} | ||
|
||
// get the prometheus handler | ||
handler := promhttp.HandlerFor(prometheus.DefaultGatherer, promhttp.HandlerOpts{}) | ||
|
||
// execute the ServeHTTP function | ||
handler.ServeHTTP(w, req) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters