Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/usage/pkg/scheduler/reporter.go
2498 views
1
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package scheduler
6
7
import (
8
"fmt"
9
"github.com/prometheus/client_golang/prometheus"
10
"time"
11
)
12
13
const (
14
namespace = "gitpod"
15
subsystem = "usage"
16
)
17
18
var (
19
jobStartedSeconds = prometheus.NewCounterVec(prometheus.CounterOpts{
20
Namespace: namespace,
21
Subsystem: subsystem,
22
Name: "scheduler_job_started_total",
23
Help: "Number of jobs started",
24
}, []string{"job"})
25
26
jobCompletedSeconds = prometheus.NewHistogramVec(prometheus.HistogramOpts{
27
Namespace: namespace,
28
Subsystem: subsystem,
29
Name: "scheduler_job_completed_seconds",
30
Help: "Histogram of job duration",
31
Buckets: prometheus.LinearBuckets(30, 30, 10), // every 30 secs, starting at 30secs
32
}, []string{"job", "outcome"})
33
34
ledgerLastCompletedTime = prometheus.NewGaugeVec(prometheus.GaugeOpts{
35
Namespace: namespace,
36
Subsystem: subsystem,
37
Name: "ledger_last_completed_time",
38
Help: "The last time the ledger scheduled job completed, by outcome",
39
ConstLabels: nil,
40
}, []string{"outcome"})
41
42
stoppedWithoutStoppingTime = prometheus.NewGauge(prometheus.GaugeOpts{
43
Namespace: namespace,
44
Subsystem: subsystem,
45
Name: "job_stopped_instances_without_stopping_time_count",
46
Help: "Gauge of usage records where workpsace instance is stopped but doesn't have a stopping time",
47
})
48
)
49
50
func RegisterMetrics(reg *prometheus.Registry) error {
51
metrics := []prometheus.Collector{
52
jobStartedSeconds,
53
jobCompletedSeconds,
54
stoppedWithoutStoppingTime,
55
ledgerLastCompletedTime,
56
}
57
for _, metric := range metrics {
58
err := reg.Register(metric)
59
if err != nil {
60
return fmt.Errorf("failed to register metric: %w", err)
61
}
62
}
63
64
return nil
65
}
66
67
func reportJobStarted(id string) {
68
jobStartedSeconds.WithLabelValues(id).Inc()
69
}
70
71
func reportJobCompleted(id string, duration time.Duration, err error) {
72
jobCompletedSeconds.WithLabelValues(id, outcomeFromErr(err)).Observe(duration.Seconds())
73
}
74
75
func reportLedgerCompleted(err error) {
76
ledgerLastCompletedTime.WithLabelValues(outcomeFromErr(err)).SetToCurrentTime()
77
}
78
79
func outcomeFromErr(err error) string {
80
out := "success"
81
if err != nil {
82
out = "error"
83
}
84
return out
85
}
86
87