Skip to content

[ws-manager-mk2] Add metrics #16109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions components/ws-daemon/pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type WorkspaceController struct {
NodeName string
opts *WorkspaceControllerOpts
operations WorkspaceOperations
metrics *workspaceMetrics
}

func NewWorkspaceController(c client.Client, opts WorkspaceControllerOpts) (*WorkspaceController, error) {
Expand All @@ -71,6 +72,9 @@ func NewWorkspaceController(c client.Client, opts WorkspaceControllerOpts) (*Wor
return nil, err
}

metrics := newWorkspaceMetrics()
opts.MetricsRegistry.Register(metrics)

ops, err := NewWorkspaceOperations(opts.ContentConfig, store, opts.MetricsRegistry)
if err != nil {
return nil, err
Expand All @@ -81,6 +85,7 @@ func NewWorkspaceController(c client.Client, opts WorkspaceControllerOpts) (*Wor
NodeName: opts.NodeName,
opts: &opts,
operations: *ops,
metrics: metrics,
}, nil
}

Expand Down Expand Up @@ -155,6 +160,7 @@ func (wsc *WorkspaceController) handleWorkspaceInit(ctx context.Context, ws *wor
return ctrl.Result{}, err
}

initStart := time.Now()
alreadyInit, failure, err := wsc.operations.InitWorkspaceContent(ctx, InitContentOptions{
Meta: WorkspaceMeta{
Owner: ws.Spec.Ownership.Owner,
Expand Down Expand Up @@ -198,6 +204,10 @@ func (wsc *WorkspaceController) handleWorkspaceInit(ctx context.Context, ws *wor
return wsc.Status().Update(ctx, ws)
})

if err == nil {
wsc.metrics.recordInitializeTime(time.Since(initStart).Seconds(), ws)
}

return ctrl.Result{}, err
}

Expand All @@ -220,6 +230,7 @@ func (wsc *WorkspaceController) handleWorkspaceStop(ctx context.Context, ws *wor
return ctrl.Result{}, nil
}

disposeStart := time.Now()
alreadyDisposing, gitStatus, disposeErr := wsc.operations.DisposeWorkspace(ctx, DisposeOptions{
Meta: WorkspaceMeta{
Owner: ws.Spec.Ownership.Owner,
Expand Down Expand Up @@ -266,6 +277,10 @@ func (wsc *WorkspaceController) handleWorkspaceStop(ctx context.Context, ws *wor
return wsc.Status().Update(ctx, ws)
})

if err == nil {
wsc.metrics.recordFinalizeTime(time.Since(disposeStart).Seconds(), ws)
}

return ctrl.Result{}, err
}

Expand All @@ -285,3 +300,63 @@ func toWorkspaceGitStatus(status *csapi.GitStatus) *workspacev1.GitStatus {
TotalUnpushedCommits: status.TotalUnpushedCommits,
}
}

type workspaceMetrics struct {
initializeTimeHistVec *prometheus.HistogramVec
finalizeTimeHistVec *prometheus.HistogramVec
}

func newWorkspaceMetrics() *workspaceMetrics {
return &workspaceMetrics{
initializeTimeHistVec: prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: "gitpod",
Subsystem: "ws_daemon",
Name: "workspace_initialize_seconds",
Help: "time it took to initialize workspace",
Buckets: prometheus.ExponentialBuckets(2, 2, 10),
}, []string{"type", "class"}),
finalizeTimeHistVec: prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: "gitpod",
Subsystem: "ws_daemon",
Name: "workspace_finalize_seconds",
Help: "time it took to finalize workspace",
Buckets: prometheus.ExponentialBuckets(2, 2, 10),
}, []string{"type", "class"}),
}
}

func (m *workspaceMetrics) recordInitializeTime(duration float64, ws *workspacev1.Workspace) {
tpe := string(ws.Spec.Type)
class := ws.Spec.Class

hist, err := m.initializeTimeHistVec.GetMetricWithLabelValues(tpe, class)
if err != nil {
glog.WithError(err).WithField("type", tpe).WithField("class", class).Infof("could not retrieve initialize metric")
}

hist.Observe(duration)
}

func (m *workspaceMetrics) recordFinalizeTime(duration float64, ws *workspacev1.Workspace) {
tpe := string(ws.Spec.Type)
class := ws.Spec.Class

hist, err := m.finalizeTimeHistVec.GetMetricWithLabelValues(tpe, class)
if err != nil {
glog.WithError(err).WithField("type", tpe).WithField("class", class).Infof("could not retrieve finalize metric")
}

hist.Observe(duration)
}

// Describe implements Collector. It will send exactly one Desc to the provided channel.
func (m *workspaceMetrics) Describe(ch chan<- *prometheus.Desc) {
m.initializeTimeHistVec.Describe(ch)
m.finalizeTimeHistVec.Describe(ch)
}

// Collect implements Collector.
func (m *workspaceMetrics) Collect(ch chan<- prometheus.Metric) {
m.initializeTimeHistVec.Collect(ch)
m.finalizeTimeHistVec.Collect(ch)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Copyright (c) 2022 Gitpod GmbH. All rights reserved.
# Copyright (c) 2023 Gitpod GmbH. All rights reserved.
# Licensed under the GNU Affero General Public License (AGPL).
# See License-AGPL.txt in the project root for license information.
# See License.AGPL.txt in the project root for license information.

---
apiVersion: apiextensions.k8s.io/v1
Expand Down
4 changes: 2 additions & 2 deletions components/ws-manager-mk2/config/webhook/manifests.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Copyright (c) 2022 Gitpod GmbH. All rights reserved.
# Copyright (c) 2023 Gitpod GmbH. All rights reserved.
# Licensed under the GNU Affero General Public License (AGPL).
# See License-AGPL.txt in the project root for license information.
# See License.AGPL.txt in the project root for license information.

---
apiVersion: admissionregistration.k8s.io/v1
Expand Down
Loading