Skip to content

Commit

Permalink
[ws-manager] Add metric counting totakl open ports
Browse files Browse the repository at this point in the history
  • Loading branch information
csweichel authored and ArthurSens committed Apr 23, 2021
1 parent 079af3f commit 7f5fe36
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 8 deletions.
7 changes: 4 additions & 3 deletions components/ws-manager/pkg/manager/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -664,9 +664,10 @@ func (m *Manager) createPortsService(workspaceID string, metaID string, serviceP
Name: serviceName,
Namespace: m.Config.Namespace,
Labels: map[string]string{
"workspaceID": workspaceID,
wsk8s.MetaIDLabel: metaID,
markerLabel: "true",
"workspaceID": workspaceID,
wsk8s.MetaIDLabel: metaID,
markerLabel: "true",
wsk8s.ServiceTypeLabel: "ports",
},
Annotations: annotations,
},
Expand Down
5 changes: 5 additions & 0 deletions components/ws-manager/pkg/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,11 @@ func (m *Manager) StartWorkspace(ctx context.Context, req *api.StartWorkspaceReq
// mandatory Theia service
servicePrefix := getServicePrefix(req)
theiaServiceName := getTheiaServiceName(servicePrefix)
theiaServiceLabels := make(map[string]string, len(startContext.Labels)+1)
for k, v := range startContext.Labels {
theiaServiceLabels[k] = v
}
theiaServiceLabels[wsk8s.ServiceTypeLabel] = "ide"
theiaService := corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: theiaServiceName,
Expand Down
55 changes: 50 additions & 5 deletions components/ws-manager/pkg/manager/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ package manager

import (
"context"
"math"
"strings"
"sync"
"time"

"github.com/prometheus/client_golang/prometheus"
corev1 "k8s.io/api/core/v1"

wsk8s "github.com/gitpod-io/gitpod/common-go/kubernetes"
"github.com/gitpod-io/gitpod/common-go/log"
"github.com/gitpod-io/gitpod/ws-manager/api"
)
Expand All @@ -23,8 +25,8 @@ func (m *Manager) RegisterMetrics(reg prometheus.Registerer) error {
}

const (
metricsNamespace = "gitpod_ws_manager"
metricsWorkspaceSubsystem = "workspace"
metricsNamespace = "gitpod"
metricsWorkspaceSubsystem = "ws_manager"
)

type metrics struct {
Expand All @@ -33,6 +35,7 @@ type metrics struct {
startupTimeHistVec *prometheus.HistogramVec
totalStartsCounterVec *prometheus.CounterVec
totalStopsCounterVec *prometheus.CounterVec
totalOpenPortGauge prometheus.GaugeFunc

mu sync.Mutex
phaseState map[string]api.WorkspacePhase
Expand All @@ -45,23 +48,64 @@ func newMetrics(m *Manager) *metrics {
startupTimeHistVec: prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: metricsNamespace,
Subsystem: metricsWorkspaceSubsystem,
Name: "startup_seconds",
Name: "workspace_startup_seconds",
Help: "time it took for workspace pods to reach the running phase",
// same as components/ws-manager-bridge/src/prometheus-metrics-exporter.ts#L15
Buckets: prometheus.ExponentialBuckets(2, 2, 10),
}, []string{"type"}),
totalStartsCounterVec: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: metricsNamespace,
Subsystem: metricsWorkspaceSubsystem,
Name: "starts_total",
Name: "workspace_starts_total",
Help: "total number of workspaces started",
}, []string{"type"}),
totalStopsCounterVec: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: metricsNamespace,
Subsystem: metricsWorkspaceSubsystem,
Name: "stops_total",
Name: "workspace_stops_total",
Help: "total number of workspaces stopped",
}, []string{"reason"}),
totalOpenPortGauge: prometheus.NewGaugeFunc(prometheus.GaugeOpts{
Namespace: metricsNamespace,
Subsystem: metricsWorkspaceSubsystem,
Name: "exposed_ports",
Help: "total number of currently exposed ports",
}, newTotalOpenPortGaugeHandler(m)),
}
}

func newTotalOpenPortGaugeHandler(m *Manager) func() float64 {
countExposedPorts := func(ctx context.Context) (float64, error) {
var l corev1.ServiceList
err := m.Clientset.List(ctx, &l, workspaceObjectListOptions(m.Config.Namespace))
if err != nil {
return 0, err
}
var portCount int
for _, s := range l.Items {
tpe, ok := s.Labels[wsk8s.ServiceTypeLabel]
if !ok {
continue
}
if tpe != "ports" {
continue
}
portCount += len(s.Spec.Ports)
}
return float64(portCount), nil
}

return func() float64 {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

r, err := countExposedPorts(ctx)
if err != nil {
log.WithError(err).Warn("cannot compute exposed_ports metric")
return math.NaN()
}

return r
}
}

Expand All @@ -75,6 +119,7 @@ func (m *metrics) Register(reg prometheus.Registerer) error {
newSubscriberQueueLevelVec(m.manager),
m.totalStartsCounterVec,
m.totalStopsCounterVec,
m.totalOpenPortGauge,
}
for _, c := range collectors {
err := reg.Register(c)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"labels": {
"gpwsman": "true",
"metaID": "",
"serviceType": "ports",
"workspaceID": "foobar"
},
"annotations": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"labels": {
"gpwsman": "true",
"metaID": "",
"serviceType": "ports",
"workspaceID": "foobar"
},
"annotations": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"labels": {
"gpwsman": "true",
"metaID": "",
"serviceType": "ports",
"workspaceID": "foobar"
},
"annotations": {
Expand Down

0 comments on commit 7f5fe36

Please sign in to comment.