Skip to content
Snippets Groups Projects
Commit baed1524 authored by Tomasz Maczukin's avatar Tomasz Maczukin
Browse files

Merge branch 'improvement/list-handled-jobs' into 'master'

Add '/debug/jobs/list' endpoint that lists all handled jobs

See merge request !564
parents e2124600 e7e24fd0
No related branches found
No related tags found
No related merge requests found
package commands
import (
"fmt"
"net/http"
"strings"
"sync"
"gitlab.com/gitlab-org/gitlab-ci-multi-runner/common"
......@@ -194,3 +197,19 @@ func (b *buildsHelper) Collect(ch chan<- prometheus.Metric) {
)
}
}
func (b *buildsHelper) ListJobsHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "text/plain")
var jobs []string
for _, job := range b.builds {
jobDescription := fmt.Sprintf(
"id=%d url=%s state=%s stage=%s executor_stage=%s",
job.ID, job.RepoCleanURL(),
job.CurrentState, job.CurrentStage, job.CurrentExecutorStage(),
)
jobs = append(jobs, jobDescription)
}
w.Write([]byte(strings.Join(jobs, "\n")))
}
......@@ -342,14 +342,7 @@ func (mr *RunCommand) runWait() {
mr.stopSignal = <-mr.stopSignals
}
func (mr *RunCommand) serveMetrics() error {
// We separate out the listener creation here so that we can return an error if
// the provided address is invalid or there is some other listener error.
listener, err := net.Listen("tcp", mr.metricsServerAddress())
if err != nil {
return err
}
func (mr *RunCommand) serveMetrics() {
registry := prometheus.NewRegistry()
// Metrics about the runner's business logic.
registry.MustRegister(&mr.buildsHelper)
......@@ -370,22 +363,37 @@ func (mr *RunCommand) serveMetrics() error {
}
http.Handle("/metrics", promhttp.HandlerFor(registry, promhttp.HandlerOpts{}))
}
func (mr *RunCommand) serveDebugData() {
http.Handle("/debug/jobs/list", http.HandlerFunc(mr.buildsHelper.ListJobsHandler))
}
func (mr *RunCommand) setupMetricsAndDebugServer() {
if mr.metricsServerAddress() == "" {
log.Infoln("Metrics server disabled")
return
}
// We separate out the listener creation here so that we can return an error if
// the provided address is invalid or there is some other listener error.
listener, err := net.Listen("tcp", mr.metricsServerAddress())
if err != nil {
log.Fatalln(err)
}
go func() {
log.Fatalln(http.Serve(listener, nil))
}()
return nil
mr.serveMetrics()
mr.serveDebugData()
log.Infoln("Metrics server listening at", mr.metricsServerAddress())
}
func (mr *RunCommand) Run() {
if mr.metricsServerAddress() != "" {
if err := mr.serveMetrics(); err != nil {
log.Fatalln(err)
}
log.Infoln("Metrics server listening at", mr.metricsServerAddress())
} else {
log.Infoln("Metrics server disabled")
}
mr.setupMetricsAndDebugServer()
runners := make(chan *common.RunnerConfig)
go mr.feedRunners(runners)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment