@@ -2,6 +2,9 @@ package webrpc
2
2
3
3
import (
4
4
"context"
5
+ "fmt"
6
+ "io"
7
+ "net/http"
5
8
"strings"
6
9
"time"
7
10
@@ -484,3 +487,39 @@ func (a *WebRPC) AbortRestart(ctx context.Context, id int64) error {
484
487
}
485
488
return nil
486
489
}
490
+
491
+ func (a * WebRPC ) ClusterNodeMetrics (ctx context.Context , id int64 ) (string , error ) {
492
+ var hostPort string
493
+ err := a .deps .DB .QueryRow (ctx , `SELECT host_and_port FROM harmony_machines WHERE id = $1` , id ).Scan (& hostPort )
494
+ if err != nil {
495
+ return "" , xerrors .Errorf ("failed to get host_and_port for machine %d: %w" , id , err )
496
+ }
497
+
498
+ url := fmt .Sprintf ("http://%s/debug/metrics" , hostPort )
499
+
500
+ timeoutCtx , cancel := context .WithTimeout (ctx , 5 * time .Second )
501
+ defer cancel ()
502
+
503
+ req , err := http .NewRequestWithContext (timeoutCtx , "GET" , url , nil )
504
+ if err != nil {
505
+ return "" , xerrors .Errorf ("failed to create request: %w" , err )
506
+ }
507
+
508
+ client := & http.Client {}
509
+ resp , err := client .Do (req )
510
+ if err != nil {
511
+ return "" , xerrors .Errorf ("failed to fetch metrics from %s: %w" , url , err )
512
+ }
513
+ defer resp .Body .Close ()
514
+
515
+ if resp .StatusCode != http .StatusOK {
516
+ return "" , xerrors .Errorf ("metrics endpoint returned status %d" , resp .StatusCode )
517
+ }
518
+
519
+ body , err := io .ReadAll (resp .Body )
520
+ if err != nil {
521
+ return "" , xerrors .Errorf ("failed to read metrics response: %w" , err )
522
+ }
523
+
524
+ return string (body ), nil
525
+ }
0 commit comments