@@ -6,13 +6,15 @@ package manager
6
6
7
7
import (
8
8
"context"
9
+ "math"
9
10
"strings"
10
11
"sync"
11
12
"time"
12
13
13
14
"github.com/prometheus/client_golang/prometheus"
14
15
corev1 "k8s.io/api/core/v1"
15
16
17
+ wsk8s "github.com/gitpod-io/gitpod/common-go/kubernetes"
16
18
"github.com/gitpod-io/gitpod/common-go/log"
17
19
"github.com/gitpod-io/gitpod/ws-manager/api"
18
20
)
@@ -23,8 +25,8 @@ func (m *Manager) RegisterMetrics(reg prometheus.Registerer) error {
23
25
}
24
26
25
27
const (
26
- metricsNamespace = "gitpod_ws_manager "
27
- metricsWorkspaceSubsystem = "workspace "
28
+ metricsNamespace = "gitpod "
29
+ metricsWorkspaceSubsystem = "ws_manager "
28
30
)
29
31
30
32
type metrics struct {
@@ -33,6 +35,7 @@ type metrics struct {
33
35
startupTimeHistVec * prometheus.HistogramVec
34
36
totalStartsCounterVec * prometheus.CounterVec
35
37
totalStopsCounterVec * prometheus.CounterVec
38
+ totalOpenPortGauge prometheus.GaugeFunc
36
39
37
40
mu sync.Mutex
38
41
phaseState map [string ]api.WorkspacePhase
@@ -45,23 +48,64 @@ func newMetrics(m *Manager) *metrics {
45
48
startupTimeHistVec : prometheus .NewHistogramVec (prometheus.HistogramOpts {
46
49
Namespace : metricsNamespace ,
47
50
Subsystem : metricsWorkspaceSubsystem ,
48
- Name : "startup_seconds " ,
51
+ Name : "workspace_startup_seconds " ,
49
52
Help : "time it took for workspace pods to reach the running phase" ,
50
53
// same as components/ws-manager-bridge/src/prometheus-metrics-exporter.ts#L15
51
54
Buckets : prometheus .ExponentialBuckets (2 , 2 , 10 ),
52
55
}, []string {"type" }),
53
56
totalStartsCounterVec : prometheus .NewCounterVec (prometheus.CounterOpts {
54
57
Namespace : metricsNamespace ,
55
58
Subsystem : metricsWorkspaceSubsystem ,
56
- Name : "starts_total " ,
59
+ Name : "workspace_starts_total " ,
57
60
Help : "total number of workspaces started" ,
58
61
}, []string {"type" }),
59
62
totalStopsCounterVec : prometheus .NewCounterVec (prometheus.CounterOpts {
60
63
Namespace : metricsNamespace ,
61
64
Subsystem : metricsWorkspaceSubsystem ,
62
- Name : "stops_total " ,
65
+ Name : "workspace_stops_total " ,
63
66
Help : "total number of workspaces stopped" ,
64
67
}, []string {"reason" }),
68
+ totalOpenPortGauge : prometheus .NewGaugeFunc (prometheus.GaugeOpts {
69
+ Namespace : metricsNamespace ,
70
+ Subsystem : metricsWorkspaceSubsystem ,
71
+ Name : "exposed_ports" ,
72
+ Help : "total number of currently exposed ports" ,
73
+ }, newTotalOpenPortGaugeHandler (m )),
74
+ }
75
+ }
76
+
77
+ func newTotalOpenPortGaugeHandler (m * Manager ) func () float64 {
78
+ countExposedPorts := func (ctx context.Context ) (float64 , error ) {
79
+ var l corev1.ServiceList
80
+ err := m .Clientset .List (ctx , & l , workspaceObjectListOptions (m .Config .Namespace ))
81
+ if err != nil {
82
+ return 0 , err
83
+ }
84
+ var portCount int
85
+ for _ , s := range l .Items {
86
+ tpe , ok := s .Labels [wsk8s .ServiceTypeLabel ]
87
+ if ! ok {
88
+ continue
89
+ }
90
+ if tpe != "ports" {
91
+ continue
92
+ }
93
+ portCount += len (s .Spec .Ports )
94
+ }
95
+ return float64 (portCount ), nil
96
+ }
97
+
98
+ return func () float64 {
99
+ ctx , cancel := context .WithTimeout (context .Background (), 30 * time .Second )
100
+ defer cancel ()
101
+
102
+ r , err := countExposedPorts (ctx )
103
+ if err != nil {
104
+ log .WithError (err ).Warn ("cannot compute exposed_ports metric" )
105
+ return math .NaN ()
106
+ }
107
+
108
+ return r
65
109
}
66
110
}
67
111
@@ -75,6 +119,7 @@ func (m *metrics) Register(reg prometheus.Registerer) error {
75
119
newSubscriberQueueLevelVec (m .manager ),
76
120
m .totalStartsCounterVec ,
77
121
m .totalStopsCounterVec ,
122
+ m .totalOpenPortGauge ,
78
123
}
79
124
for _ , c := range collectors {
80
125
err := reg .Register (c )
0 commit comments