@@ -16,16 +16,26 @@ import (
16
16
dw "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
17
17
"github.com/go-logr/logr"
18
18
"github.com/prometheus/client_golang/prometheus"
19
+
20
+ "github.com/devfile/devworkspace-operator/pkg/conditions"
19
21
)
20
22
23
+ // WorkspaceStarted updates metrics for workspaces entering the 'Starting' phase, given a workspace. If an error is
24
+ // encountered, the provided logger is used to log the error.
21
25
func WorkspaceStarted (wksp * dw.DevWorkspace , log logr.Logger ) {
22
26
incrementMetricForWorkspace (workspaceTotal , wksp , log )
23
27
}
24
28
29
+ // WorkspaceRunning updates metrics for workspaces entering the 'Running' phase, given a workspace. If an error is
30
+ // encountered, the provided logger is used to log the error. This function assumes the provided workspace has
31
+ // fully-synced conditions (i.e. the WorkspaceReady condition is present).
25
32
func WorkspaceRunning (wksp * dw.DevWorkspace , log logr.Logger ) {
26
33
incrementMetricForWorkspace (workspaceStarts , wksp , log )
34
+ incrementStartTimeBucketForWorkspace (wksp , log )
27
35
}
28
36
37
+ // WorkspaceFailed updates metrics for workspace entering the 'Failed' phase. If an error is encountered, the provided
38
+ // logger is used to log the error.
29
39
func WorkspaceFailed (wksp * dw.DevWorkspace , log logr.Logger ) {
30
40
incrementMetricForWorkspace (workspaceFailures , wksp , log )
31
41
}
@@ -34,7 +44,27 @@ func incrementMetricForWorkspace(metric *prometheus.CounterVec, wksp *dw.DevWork
34
44
sourceLabel := wksp .Labels [workspaceSourceLabel ]
35
45
ctr , err := metric .GetMetricWith (map [string ]string {metricSourceLabel : sourceLabel })
36
46
if err != nil {
37
- log .Info ( "Failed to increment metric: %s" , err )
47
+ log .Error ( err , "Failed to increment metric" )
38
48
}
39
49
ctr .Inc ()
40
50
}
51
+
52
+ func incrementStartTimeBucketForWorkspace (wksp * dw.DevWorkspace , log logr.Logger ) {
53
+ sourceLabel := wksp .Labels [workspaceSourceLabel ]
54
+ hist , err := workspaceStartupTimesHist .GetMetricWith (map [string ]string {metricSourceLabel : sourceLabel })
55
+ if err != nil {
56
+ log .Error (err , "Failed to update metric" )
57
+ }
58
+ readyCondition := conditions .GetConditionByType (wksp .Status .Conditions , dw .DevWorkspaceReady )
59
+ if readyCondition == nil {
60
+ return
61
+ }
62
+ startedCondition := conditions .GetConditionByType (wksp .Status .Conditions , conditions .Started )
63
+ if startedCondition == nil {
64
+ return
65
+ }
66
+ readyTime := readyCondition .LastTransitionTime
67
+ startTime := startedCondition .LastTransitionTime
68
+ startDuration := readyTime .Sub (startTime .Time )
69
+ hist .Observe (startDuration .Seconds ())
70
+ }
0 commit comments