Commit 9aee57f7 authored by Tomasz Maczukin's avatar Tomasz Maczukin

Add a gitlab_runner_jobs_total metric

parent a75097c9
Pipeline #29860171 passed with stages
in 26 minutes and 21 seconds
......@@ -62,6 +62,8 @@ type buildsHelper struct {
counters map[string]*runnerCounter
builds []*common.Build
lock sync.Mutex
jobsTotal *prometheus.CounterVec
}
func (b *buildsHelper) getRunnerCounter(runner *common.RunnerConfig) *runnerCounter {
......@@ -183,6 +185,8 @@ func (b *buildsHelper) addBuild(build *common.Build) {
}
b.builds = append(b.builds, build)
b.jobsTotal.WithLabelValues(build.Runner.ShortDescription()).Inc()
return
}
......@@ -239,6 +243,8 @@ func (b *buildsHelper) Describe(ch chan<- *prometheus.Desc) {
ch <- numBuildsDesc
ch <- requestConcurrencyDesc
ch <- requestConcurrencyExceededDesc
b.jobsTotal.Describe(ch)
}
// Collect implements prometheus.Collector.
......@@ -272,6 +278,8 @@ func (b *buildsHelper) Collect(ch chan<- prometheus.Metric) {
runner,
)
}
b.jobsTotal.Collect(ch)
}
func (b *buildsHelper) ListJobsHandler(w http.ResponseWriter, r *http.Request) {
......@@ -329,3 +337,15 @@ func CreateJobURL(projectURL string, jobID int) string {
return fmt.Sprintf("%s/-/jobs/%d", URL, jobID)
}
func newBuildsHelper() buildsHelper {
return buildsHelper{
jobsTotal: prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "gitlab_runner_jobs_total",
Help: "Total number of handled jobs",
},
[]string{"runner"},
),
}
}
......@@ -24,7 +24,7 @@ var fakeRunner = &common.RunnerConfig{
func TestBuildsHelperCollect(t *testing.T) {
ch := make(chan prometheus.Metric, 50)
b := &buildsHelper{}
b := newBuildsHelper()
b.builds = append(b.builds, &common.Build{
CurrentState: common.BuildRunStatePending,
CurrentStage: common.BuildStagePrepare,
......@@ -39,7 +39,7 @@ func TestBuildsHelperAcquireRequestWithLimit(t *testing.T) {
RequestConcurrency: 2,
}
b := &buildsHelper{}
b := newBuildsHelper()
result := b.acquireRequest(&runner)
require.True(t, result)
......@@ -64,7 +64,7 @@ func TestBuildsHelperAcquireRequestWithDefault(t *testing.T) {
RequestConcurrency: 0,
}
b := &buildsHelper{}
b := newBuildsHelper()
result := b.acquireRequest(&runner)
require.True(t, result)
......@@ -92,7 +92,7 @@ func TestBuildsHelperAcquireBuildWithLimit(t *testing.T) {
Limit: 1,
}
b := &buildsHelper{}
b := newBuildsHelper()
result := b.acquireBuild(&runner)
require.True(t, result)
......@@ -111,7 +111,7 @@ func TestBuildsHelperAcquireBuildUnlimited(t *testing.T) {
Limit: 0,
}
b := &buildsHelper{}
b := newBuildsHelper()
result := b.acquireBuild(&runner)
require.True(t, result)
......@@ -130,9 +130,14 @@ func TestBuildsHelperFindSessionByURL(t *testing.T) {
require.NoError(t, err)
build := common.Build{
Session: sess,
Runner: &common.RunnerConfig{
RunnerCredentials: common.RunnerCredentials{
Token: "abcd1234",
},
},
}
h := &buildsHelper{}
h := newBuildsHelper()
h.addBuild(&build)
foundSession := h.findSessionByURL(sess.Endpoint + "/action")
......@@ -173,7 +178,7 @@ func TestBuildsHelper_ListJobsHandlerVersioning(t *testing.T) {
},
}
b := &buildsHelper{}
b := newBuildsHelper()
mux := http.NewServeMux()
mux.HandleFunc(baseURL, b.ListJobsHandler)
......@@ -286,7 +291,7 @@ func TestBuildsHelper_ListJobsHandler(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, URL, nil)
require.NoError(t, err)
b := &buildsHelper{}
b := newBuildsHelper()
b.addBuild(test.build)
b.ListJobsHandler(writer, req)
......
......@@ -628,5 +628,6 @@ func init() {
networkRequestStatusesCollector: requestStatusesCollector,
prometheusLogHook: prometheus_helper.NewLogHook(),
failuresCollector: prometheus_helper.NewFailuresCollector(),
buildsHelper: newBuildsHelper(),
})
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment