@@ -243,7 +243,7 @@ type sshNetworkStats struct {
243
243
}
244
244
245
245
func collectNetworkStats (ctx context.Context , agentConn * codersdk.WorkspaceAgentConn , start , end time.Time , counts map [netlogtype.Connection ]netlogtype.Counts ) (* sshNetworkStats , error ) {
246
- latency , p2p , _ , err := agentConn .Ping (ctx )
246
+ latency , p2p , pingResult , err := agentConn .Ping (ctx )
247
247
if err != nil {
248
248
return nil , err
249
249
}
@@ -284,10 +284,26 @@ func collectNetworkStats(ctx context.Context, agentConn *codersdk.WorkspaceAgent
284
284
uploadSecs := float64 (totalTx ) / dur .Seconds ()
285
285
downloadSecs := float64 (totalRx ) / dur .Seconds ()
286
286
287
+ // Sometimes the preferred DERP doesn't match the one we're actually
288
+ // connected with. Perhaps because the agent prefers a different DERP and
289
+ // we're using that server instead.
290
+ preferredDerpID := node .PreferredDERP
291
+ if pingResult .DERPRegionID != 0 {
292
+ preferredDerpID = pingResult .DERPRegionID
293
+ }
294
+ preferredDerp , ok := derpMap .Regions [preferredDerpID ]
295
+ preferredDerpName := fmt .Sprintf ("Unnamed %d" , preferredDerpID )
296
+ if ok {
297
+ preferredDerpName = preferredDerp .RegionName
298
+ }
299
+ if _ , ok := derpLatency [preferredDerpName ]; ! ok {
300
+ derpLatency [preferredDerpName ] = 0
301
+ }
302
+
287
303
return & sshNetworkStats {
288
304
P2P : p2p ,
289
305
Latency : float64 (latency .Microseconds ()) / 1000 ,
290
- PreferredDERP : derpMap . Regions [ node . PreferredDERP ]. RegionName ,
306
+ PreferredDERP : preferredDerpName ,
291
307
DERPLatency : derpLatency ,
292
308
UploadBytesSec : int64 (uploadSecs ),
293
309
DownloadBytesSec : int64 (downloadSecs ),
0 commit comments