@@ -106,3 +106,78 @@ func Workspaces(ctx context.Context, registerer prometheus.Registerer, db databa
106
106
}()
107
107
return cancelFunc , nil
108
108
}
109
+
110
+ // Agents tracks the total number of workspaces with labels on status.
111
+ func Agents (ctx context.Context , registerer prometheus.Registerer , db database.Store , duration time.Duration ) (context.CancelFunc , error ) {
112
+ if duration == 0 {
113
+ duration = 15 * time .Second // TODO 5 * time.Minute
114
+ }
115
+
116
+ agentsConnectionGauge := prometheus .NewGaugeVec (prometheus.GaugeOpts {
117
+ Namespace : "coderd" ,
118
+ Subsystem : "agents" ,
119
+ Name : "connection" ,
120
+ Help : "The agent connection with a status." ,
121
+ }, []string {"agent_name" , "workspace_name" , "status" })
122
+ err := registerer .Register (agentsConnectionGauge )
123
+ if err != nil {
124
+ return nil , err
125
+ }
126
+
127
+ agentsUserLatenciesHistogram := prometheus .NewHistogramVec (prometheus.HistogramOpts {
128
+ Namespace : "coderd" ,
129
+ Subsystem : "agents" ,
130
+ Name : "user_latencies_seconds" ,
131
+ Help : "The user's agent latency in seconds." ,
132
+ Buckets : []float64 {0.001 , 0.005 , 0.010 , 0.025 , 0.050 , 0.100 , 0.500 , 1 , 5 , 10 , 30 },
133
+ }, []string {"agent_id" , "workspace" , "connection_type" , "ide" })
134
+ err = registerer .Register (agentsUserLatenciesHistogram )
135
+ if err != nil {
136
+ return nil , err
137
+ }
138
+
139
+ ctx , cancelFunc := context .WithCancel (ctx )
140
+ ticker := time .NewTicker (duration )
141
+ go func () {
142
+ defer ticker .Stop ()
143
+ for {
144
+ select {
145
+ case <- ctx .Done ():
146
+ return
147
+ case <- ticker .C :
148
+ }
149
+
150
+ // FIXME Optimize this routine: SQL db calls
151
+
152
+ builds , err := db .GetLatestWorkspaceBuilds (ctx )
153
+ if err != nil {
154
+ continue
155
+ }
156
+
157
+ agentsConnectionGauge .Reset ()
158
+ for _ , build := range builds {
159
+ workspace , err := db .GetWorkspaceByID (ctx , build .WorkspaceID )
160
+ if err != nil {
161
+ continue
162
+ }
163
+
164
+ agents , err := db .GetWorkspaceAgentsInLatestBuildByWorkspaceID (ctx , build .WorkspaceID )
165
+ if err != nil {
166
+ continue
167
+ }
168
+
169
+ if len (agents ) == 0 {
170
+ continue
171
+ }
172
+
173
+ for _ , agent := range agents {
174
+ connectionStatus := agent .Status (6 * time .Second )
175
+
176
+ // FIXME AgentInactiveDisconnectTimeout
177
+ agentsConnectionGauge .WithLabelValues (agent .Name , workspace .Name , string (connectionStatus .Status )).Set (1 )
178
+ }
179
+ }
180
+ }
181
+ }()
182
+ return cancelFunc , nil
183
+ }
0 commit comments