Skip to content

Commit e87225e

Browse files
strahervagg
authored andcommitted
Add cluster node metrics endpoint (#621)
1 parent d59eda0 commit e87225e

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

web/api/webrpc/cluster.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ package webrpc
22

33
import (
44
"context"
5+
"fmt"
6+
"io"
7+
"net/http"
58
"strings"
69
"time"
710

@@ -484,3 +487,39 @@ func (a *WebRPC) AbortRestart(ctx context.Context, id int64) error {
484487
}
485488
return nil
486489
}
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

Comments
 (0)