8
8
"errors"
9
9
"flag"
10
10
"fmt"
11
- "log"
12
11
"net"
13
12
"net/http"
14
13
"net/netip"
@@ -25,6 +24,7 @@ import (
25
24
"github.com/google/uuid"
26
25
"golang.org/x/exp/slices"
27
26
"golang.org/x/mod/semver"
27
+ "golang.org/x/sync/errgroup"
28
28
"golang.org/x/xerrors"
29
29
"nhooyr.io/websocket"
30
30
"tailscale.com/tailcfg"
@@ -1214,42 +1214,47 @@ func (api *API) workspaceAgentReportStats(rw http.ResponseWriter, r *http.Reques
1214
1214
}
1215
1215
1216
1216
now := database .Now ()
1217
- _ , err = api .Database .InsertWorkspaceAgentStat (ctx , database.InsertWorkspaceAgentStatParams {
1218
- ID : uuid .New (),
1219
- CreatedAt : now ,
1220
- AgentID : workspaceAgent .ID ,
1221
- WorkspaceID : workspace .ID ,
1222
- UserID : workspace .OwnerID ,
1223
- TemplateID : workspace .TemplateID ,
1224
- ConnectionsByProto : payload ,
1225
- ConnectionCount : req .ConnectionCount ,
1226
- RxPackets : req .RxPackets ,
1227
- RxBytes : req .RxBytes ,
1228
- TxPackets : req .TxPackets ,
1229
- TxBytes : req .TxBytes ,
1230
- SessionCountVSCode : req .SessionCountVSCode ,
1231
- SessionCountJetBrains : req .SessionCountJetBrains ,
1232
- SessionCountReconnectingPTY : req .SessionCountReconnectingPTY ,
1233
- SessionCountSSH : req .SessionCountSSH ,
1234
- ConnectionMedianLatencyMS : req .ConnectionMedianLatencyMS ,
1235
- })
1236
- if err != nil {
1237
- httpapi .InternalServerError (rw , err )
1238
- return
1239
- }
1240
1217
1241
- if req .ConnectionCount > 0 {
1242
- err = api .Database .UpdateWorkspaceLastUsedAt (ctx , database.UpdateWorkspaceLastUsedAtParams {
1218
+ var errGroup errgroup.Group
1219
+ errGroup .Go (func () error {
1220
+ _ , err = api .Database .InsertWorkspaceAgentStat (ctx , database.InsertWorkspaceAgentStatParams {
1221
+ ID : uuid .New (),
1222
+ CreatedAt : now ,
1223
+ AgentID : workspaceAgent .ID ,
1224
+ WorkspaceID : workspace .ID ,
1225
+ UserID : workspace .OwnerID ,
1226
+ TemplateID : workspace .TemplateID ,
1227
+ ConnectionsByProto : payload ,
1228
+ ConnectionCount : req .ConnectionCount ,
1229
+ RxPackets : req .RxPackets ,
1230
+ RxBytes : req .RxBytes ,
1231
+ TxPackets : req .TxPackets ,
1232
+ TxBytes : req .TxBytes ,
1233
+ SessionCountVSCode : req .SessionCountVSCode ,
1234
+ SessionCountJetBrains : req .SessionCountJetBrains ,
1235
+ SessionCountReconnectingPTY : req .SessionCountReconnectingPTY ,
1236
+ SessionCountSSH : req .SessionCountSSH ,
1237
+ ConnectionMedianLatencyMS : req .ConnectionMedianLatencyMS ,
1238
+ })
1239
+ return err
1240
+ })
1241
+ errGroup .Go (func () error {
1242
+ return api .Database .UpdateWorkspaceLastUsedAt (ctx , database.UpdateWorkspaceLastUsedAtParams {
1243
1243
ID : workspace .ID ,
1244
1244
LastUsedAt : now ,
1245
1245
})
1246
- if err != nil {
1247
- httpapi .InternalServerError (rw , err )
1248
- return
1249
- }
1246
+ })
1247
+ if api .Options .UpdateAgentMetrics != nil {
1248
+ errGroup .Go (func () error {
1249
+ api .Options .UpdateAgentMetrics (ctx , workspace .ID , workspaceAgent .ID , req .Metrics )
1250
+ return nil
1251
+ })
1252
+ }
1253
+ err = errGroup .Wait ()
1254
+ if err != nil {
1255
+ httpapi .InternalServerError (rw , err )
1256
+ return
1250
1257
}
1251
-
1252
- log .Println ("Metrics: " , req .Metrics ) // FIXME
1253
1258
1254
1259
httpapi .Write (ctx , rw , http .StatusOK , agentsdk.StatsResponse {
1255
1260
ReportInterval : api .AgentStatsRefreshInterval ,
0 commit comments