@@ -344,87 +344,62 @@ func Agents(ctx context.Context, logger slog.Logger, registerer prometheus.Regis
344344timer := prometheus .NewTimer (metricsCollectorAgents )
345345derpMap := derpMapFn ()
346346
347- workspaceRows ,err := db .GetWorkspacesForAgentMetrics (ctx ,false )
347+ workspaceAgents ,err := db .GetWorkspaceAgentsForMetrics (ctx ,false )
348348if err != nil {
349- logger .Error (ctx ,"can't get workspacerows " ,slog .Error (err ))
349+ logger .Error (ctx ,"can't get workspaceagents " ,slog .Error (err ))
350350goto done
351351}
352352
353- for _ ,workspace := range workspaceRows {
354- templateName := workspace .TemplateName
355- templateVersionName := workspace .TemplateVersionName .String
356- if ! workspace .TemplateVersionName .Valid {
357- templateVersionName = "unknown"
358- }
353+ for _ ,agent := range workspaceAgents {
354+ // Collect information about agents
355+ agentsGauge .WithLabelValues (VectorOperationAdd ,1 ,agent .OwnerUsername ,agent .WorkspaceName ,agent .TemplateName ,agent .TemplateVersionName .String )
359356
360- agents ,err := db .GetWorkspaceAgentsByWorkspaceAndBuildNumber (ctx ,
361- database.GetWorkspaceAgentsByWorkspaceAndBuildNumberParams {
362- WorkspaceID :workspace .ID ,
363- BuildNumber :workspace .BuildNumber ,
364- })
365- if err != nil {
366- logger .Error (ctx ,"can't get workspace agents" ,slog .F ("workspace_id" ,workspace .ID ),slog .Error (err ))
367- agentsGauge .WithLabelValues (VectorOperationAdd ,0 ,workspace .OwnerUsername ,workspace .Name ,templateName ,templateVersionName )
368- continue
369- }
357+ connectionStatus := agent .WorkspaceAgent .Status (agentInactiveDisconnectTimeout )
358+ node := (* coordinator .Load ()).Node (agent .WorkspaceAgent .ID )
370359
371- if len (agents )== 0 {
372- logger .Debug (ctx ,"workspace agents are unavailable" ,slog .F ("workspace_id" ,workspace .ID ))
373- agentsGauge .WithLabelValues (VectorOperationAdd ,0 ,workspace .OwnerUsername ,workspace .Name ,templateName ,templateVersionName )
374- continue
360+ tailnetNode := "unknown"
361+ if node != nil {
362+ tailnetNode = node .ID .String ()
375363}
376364
377- for _ ,agent := range agents {
378- // Collect information about agents
379- agentsGauge .WithLabelValues (VectorOperationAdd ,1 ,workspace .OwnerUsername ,workspace .Name ,templateName ,templateVersionName )
380-
381- connectionStatus := agent .Status (agentInactiveDisconnectTimeout )
382- node := (* coordinator .Load ()).Node (agent .ID )
383-
384- tailnetNode := "unknown"
385- if node != nil {
386- tailnetNode = node .ID .String ()
387- }
388-
389- agentsConnectionsGauge .WithLabelValues (VectorOperationSet ,1 ,agent .Name ,workspace .OwnerUsername ,workspace .Name ,string (connectionStatus .Status ),string (agent .LifecycleState ),tailnetNode )
390-
391- if node == nil {
392- logger .Debug (ctx ,"can't read in-memory node for agent" ,slog .F ("agent_id" ,agent .ID ))
393- }else {
394- // Collect information about connection latencies
395- for rawRegion ,latency := range node .DERPLatency {
396- regionParts := strings .SplitN (rawRegion ,"-" ,2 )
397- regionID ,err := strconv .Atoi (regionParts [0 ])
398- if err != nil {
399- logger .Error (ctx ,"can't convert DERP region" ,slog .F ("agent_id" ,agent .ID ),slog .F ("raw_region" ,rawRegion ),slog .Error (err ))
400- continue
401- }
365+ agentsConnectionsGauge .WithLabelValues (VectorOperationSet ,1 ,agent .WorkspaceAgent .Name ,agent .OwnerUsername ,agent .WorkspaceName ,string (connectionStatus .Status ),string (agent .WorkspaceAgent .LifecycleState ),tailnetNode )
366+
367+ if node == nil {
368+ logger .Debug (ctx ,"can't read in-memory node for agent" ,slog .F ("agent_id" ,agent .WorkspaceAgent .ID ))
369+ }else {
370+ // Collect information about connection latencies
371+ for rawRegion ,latency := range node .DERPLatency {
372+ regionParts := strings .SplitN (rawRegion ,"-" ,2 )
373+ regionID ,err := strconv .Atoi (regionParts [0 ])
374+ if err != nil {
375+ logger .Error (ctx ,"can't convert DERP region" ,slog .F ("agent_id" ,agent .WorkspaceAgent .ID ),slog .F ("raw_region" ,rawRegion ),slog .Error (err ))
376+ continue
377+ }
402378
403- region ,found := derpMap .Regions [regionID ]
404- if ! found {
405- // It's possible that a workspace agent is using an old DERPMap
406- // and reports regions that do not exist. If that's the case,
407- // report the region as unknown!
408- region = & tailcfg.DERPRegion {
409- RegionID :regionID ,
410- RegionName :fmt .Sprintf ("Unnamed %d" ,regionID ),
411- }
379+ region ,found := derpMap .Regions [regionID ]
380+ if ! found {
381+ // It's possible that a workspace agent is using an old DERPMap
382+ // and reports regions that do not exist. If that's the case,
383+ // report the region as unknown!
384+ region = & tailcfg.DERPRegion {
385+ RegionID :regionID ,
386+ RegionName :fmt .Sprintf ("Unnamed %d" ,regionID ),
412387}
413-
414- agentsConnectionLatenciesGauge .WithLabelValues (VectorOperationSet ,latency ,agent .Name ,workspace .OwnerUsername ,workspace .Name ,region .RegionName ,fmt .Sprintf ("%v" ,node .PreferredDERP == regionID ))
415388}
416- }
417389
418- // Collect information about registered applications
419- apps ,err := db .GetWorkspaceAppsByAgentID (ctx ,agent .ID )
420- if err != nil && ! errors .Is (err ,sql .ErrNoRows ) {
421- logger .Error (ctx ,"can't get workspace apps" ,slog .F ("agent_id" ,agent .ID ),slog .Error (err ))
422- continue
390+ agentsConnectionLatenciesGauge .WithLabelValues (VectorOperationSet ,latency ,agent .WorkspaceAgent .Name ,agent .OwnerUsername ,agent .WorkspaceName ,region .RegionName ,fmt .Sprintf ("%v" ,node .PreferredDERP == regionID ))
423391}
392+ }
424393
425- for _ ,app := range apps {
426- agentsAppsGauge .WithLabelValues (VectorOperationAdd ,1 ,agent .Name ,workspace .OwnerUsername ,workspace .Name ,app .DisplayName ,string (app .Health ))
427- }
394+ // Collect information about registered applications
395+ apps ,err := db .GetWorkspaceAppsByAgentID (ctx ,agent .WorkspaceAgent .ID )
396+ if err != nil && ! errors .Is (err ,sql .ErrNoRows ) {
397+ logger .Error (ctx ,"can't get workspace apps" ,slog .F ("agent_id" ,agent .WorkspaceAgent .ID ),slog .Error (err ))
398+ continue
399+ }
400+
401+ for _ ,app := range apps {
402+ agentsAppsGauge .WithLabelValues (VectorOperationAdd ,1 ,agent .WorkspaceAgent .Name ,agent .OwnerUsername ,agent .WorkspaceName ,app .DisplayName ,string (app .Health ))
428403}
429404}
430405