Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/dev/loadgen/pkg/observer/stats.go
2499 views
1
// Copyright (c) 2020 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package observer
6
7
import (
8
"time"
9
10
"github.com/gitpod-io/gitpod/loadgen/pkg/loadgen"
11
"github.com/gitpod-io/gitpod/ws-manager/api"
12
)
13
14
const (
15
defaultCapacity = 200
16
)
17
18
// Stats is stats across a whole session
19
type Stats struct {
20
Total int `json:"total"`
21
Failed int `json:"failed"`
22
Running int `json:"running"`
23
Samples []StatsSample `json:"samples"`
24
}
25
26
// StatsSample is a single workspace sample
27
type StatsSample struct {
28
InstanceID string
29
Start, Running time.Time
30
Phase api.WorkspacePhase
31
Failed bool
32
StartDuration time.Duration
33
CloneURL string
34
WorkspaceImage string
35
WorkspaceName string
36
}
37
38
// NewStatsObserver produces a stats collecting observer
39
func NewStatsObserver(cb func(*Stats)) chan<- *loadgen.SessionEvent {
40
res := make(chan *loadgen.SessionEvent, defaultCapacity)
41
42
publishStats := func(status map[string]*StatsSample) {
43
var s Stats
44
s.Samples = make([]StatsSample, 0, len(status))
45
for _, ws := range status {
46
s.Total++
47
if ws.Failed {
48
s.Failed++
49
}
50
if ws.Phase == api.WorkspacePhase_RUNNING {
51
s.Running++
52
}
53
s.Samples = append(s.Samples, *ws)
54
}
55
56
cb(&s)
57
}
58
59
go func() {
60
status := make(map[string]*StatsSample)
61
for evt := range res {
62
switch evt.Kind {
63
case loadgen.SessionStart:
64
status = make(map[string]*StatsSample)
65
case loadgen.SessionWorkspaceStart:
66
status[evt.WorkspaceStart.Spec.Id] = &StatsSample{
67
InstanceID: evt.WorkspaceStart.Spec.Id,
68
Start: evt.WorkspaceStart.Time,
69
Failed: false,
70
Phase: api.WorkspacePhase_UNKNOWN,
71
StartDuration: evt.WorkspaceStart.CallDuration,
72
CloneURL: evt.WorkspaceStart.Spec.Metadata.Annotations["context-url"],
73
WorkspaceImage: evt.WorkspaceStart.Spec.Spec.WorkspaceImage,
74
WorkspaceName: evt.WorkspaceStart.Spec.Metadata.MetaId,
75
}
76
case loadgen.SessionWorkspaceUpdate:
77
up := evt.WorkspaceUpdate.Update
78
ws, ok := status[up.InstanceID]
79
if !ok {
80
continue
81
}
82
ws.Phase = up.Phase
83
ws.Failed = up.Failed
84
ws.Running = evt.WorkspaceUpdate.Time
85
publishStats(status)
86
case loadgen.SessionDone:
87
publishStats(status)
88
}
89
}
90
}()
91
return res
92
}
93
94