Path: blob/main/components/ee/agent-smith/pkg/agent/metrics.go
2501 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 agent56import (7"sync"89"github.com/gitpod-io/gitpod/agent-smith/pkg/detector"10"github.com/prometheus/client_golang/prometheus"11)1213type metrics struct {14penaltyAttempts *prometheus.CounterVec15penaltyFailures *prometheus.CounterVec16classificationBackpressureInCount prometheus.GaugeFunc17classificationBackpressureOutCount prometheus.GaugeFunc18classificationBackpressureInDrop prometheus.Counter1920mu sync.RWMutex21cl []prometheus.Collector22}2324func newAgentMetrics() *metrics {25m := &metrics{}2627m.penaltyAttempts = prometheus.NewCounterVec(28prometheus.CounterOpts{29Namespace: "gitpod",30Subsystem: "agent_smith",31Name: "penalty_attempts_total",32Help: "The total amount of attempts that agent-smith is trying to apply a penalty.",33}, []string{"penalty"},34)35m.penaltyFailures = prometheus.NewCounterVec(36prometheus.CounterOpts{37Namespace: "gitpod",38Subsystem: "agent_smith",39Name: "penalty_attempts_failed_total",40Help: "The total amount of failed attempts that agent-smith is trying to apply a penalty.",41}, []string{"penalty", "reason"},42)43m.classificationBackpressureInDrop = prometheus.NewCounter(prometheus.CounterOpts{44Namespace: "gitpod",45Subsystem: "agent_smith",46Name: "classification_backpressure_in_drop_total",47Help: "total count of processes that went unclassified because of backpressure",48})49m.cl = []prometheus.Collector{50m.penaltyAttempts,51m.penaltyFailures,52m.classificationBackpressureInDrop,53}54return m55}5657func (m *metrics) RegisterClassificationQueues(in chan detector.Process, out chan classifiedProcess) {58m.mu.Lock()59defer m.mu.Unlock()6061m.classificationBackpressureInCount = prometheus.NewGaugeFunc(prometheus.GaugeOpts{62Namespace: "gitpod",63Subsystem: "agent_smith",64Name: "classification_backpressure_in_count",65Help: "processes queued for classification",66}, func() float64 { return float64(len(in)) })67m.classificationBackpressureOutCount = prometheus.NewGaugeFunc(prometheus.GaugeOpts{68Namespace: "gitpod",69Subsystem: "agent_smith",70Name: "classification_backpressure_out_count",71Help: "processes coming out of classification",72}, func() float64 { return float64(len(out)) })73}7475func (m *metrics) Describe(d chan<- *prometheus.Desc) {76m.mu.RLock()77defer m.mu.RUnlock()78if m.classificationBackpressureInCount != nil {79m.classificationBackpressureInCount.Describe(d)80}81if m.classificationBackpressureOutCount != nil {82m.classificationBackpressureOutCount.Describe(d)83}84for _, c := range m.cl {85c.Describe(d)86}87}8889func (m *metrics) Collect(d chan<- prometheus.Metric) {90m.mu.RLock()91defer m.mu.RUnlock()92if m.classificationBackpressureInCount != nil {93m.classificationBackpressureInCount.Collect(d)94}95if m.classificationBackpressureOutCount != nil {96m.classificationBackpressureOutCount.Collect(d)97}98for _, c := range m.cl {99c.Collect(d)100}101}102103104