Path: blob/main/components/usage/pkg/scheduler/reporter.go
2498 views
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package scheduler56import (7"fmt"8"github.com/prometheus/client_golang/prometheus"9"time"10)1112const (13namespace = "gitpod"14subsystem = "usage"15)1617var (18jobStartedSeconds = prometheus.NewCounterVec(prometheus.CounterOpts{19Namespace: namespace,20Subsystem: subsystem,21Name: "scheduler_job_started_total",22Help: "Number of jobs started",23}, []string{"job"})2425jobCompletedSeconds = prometheus.NewHistogramVec(prometheus.HistogramOpts{26Namespace: namespace,27Subsystem: subsystem,28Name: "scheduler_job_completed_seconds",29Help: "Histogram of job duration",30Buckets: prometheus.LinearBuckets(30, 30, 10), // every 30 secs, starting at 30secs31}, []string{"job", "outcome"})3233ledgerLastCompletedTime = prometheus.NewGaugeVec(prometheus.GaugeOpts{34Namespace: namespace,35Subsystem: subsystem,36Name: "ledger_last_completed_time",37Help: "The last time the ledger scheduled job completed, by outcome",38ConstLabels: nil,39}, []string{"outcome"})4041stoppedWithoutStoppingTime = prometheus.NewGauge(prometheus.GaugeOpts{42Namespace: namespace,43Subsystem: subsystem,44Name: "job_stopped_instances_without_stopping_time_count",45Help: "Gauge of usage records where workpsace instance is stopped but doesn't have a stopping time",46})47)4849func RegisterMetrics(reg *prometheus.Registry) error {50metrics := []prometheus.Collector{51jobStartedSeconds,52jobCompletedSeconds,53stoppedWithoutStoppingTime,54ledgerLastCompletedTime,55}56for _, metric := range metrics {57err := reg.Register(metric)58if err != nil {59return fmt.Errorf("failed to register metric: %w", err)60}61}6263return nil64}6566func reportJobStarted(id string) {67jobStartedSeconds.WithLabelValues(id).Inc()68}6970func reportJobCompleted(id string, duration time.Duration, err error) {71jobCompletedSeconds.WithLabelValues(id, outcomeFromErr(err)).Observe(duration.Seconds())72}7374func reportLedgerCompleted(err error) {75ledgerLastCompletedTime.WithLabelValues(outcomeFromErr(err)).SetToCurrentTime()76}7778func outcomeFromErr(err error) string {79out := "success"80if err != nil {81out = "error"82}83return out84}858687