Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tests/fixture/tmpnet/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ func (n *Network) Stop(ctx context.Context) error {

// Initiate stop on all nodes
for _, node := range nodes {
if err := node.InitiateStop(); err != nil {
if err := node.InitiateStop(ctx); err != nil {
errs = append(errs, fmt.Errorf("failed to stop node %s: %w", node.NodeID, err))
}
}
Expand Down
31 changes: 29 additions & 2 deletions tests/fixture/tmpnet/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"errors"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -141,7 +142,10 @@ func (n *Node) Start(w io.Writer) error {
return n.getRuntime().Start(w)
}

func (n *Node) InitiateStop() error {
func (n *Node) InitiateStop(ctx context.Context) error {
if err := n.SaveMetricsSnapshot(ctx); err != nil {
return err
}
return n.getRuntime().InitiateStop()
}

Expand All @@ -157,9 +161,32 @@ func (n *Node) getDataDir() string {
return cast.ToString(n.Flags[config.DataDirKey])
}

// Writes the current state of the metrics endpoint to disk
func (n *Node) SaveMetricsSnapshot(ctx context.Context) error {
if len(n.URI) == 0 {
// No URI to request metrics from
return nil
}
uri := n.URI + "/ext/metrics"
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
return n.writeMetricsSnapshot(body)
}

// Initiates node shutdown and waits for the node to stop.
func (n *Node) Stop(ctx context.Context) error {
if err := n.InitiateStop(); err != nil {
if err := n.InitiateStop(ctx); err != nil {
return err
}
return n.WaitForStopped(ctx)
Expand Down
14 changes: 14 additions & 0 deletions tests/fixture/tmpnet/node_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
"time"

"github.com/ava-labs/avalanchego/utils/perms"
)
Expand Down Expand Up @@ -98,3 +100,15 @@ func (n *Node) Write() error {
}
return n.writeConfig()
}

func (n *Node) writeMetricsSnapshot(data []byte) error {
metricsDir := filepath.Join(n.getDataDir(), "metrics")
if err := os.MkdirAll(metricsDir, perms.ReadWriteExecute); err != nil {
return fmt.Errorf("failed to create metrics dir: %w", err)
}
// Create a compatible filesystem from the current timestamp
ts := time.Now().UTC().Format(time.RFC3339)
ts = strings.ReplaceAll(strings.ReplaceAll(ts, ":", ""), "-", "")
metricsPath := filepath.Join(metricsDir, ts)
return os.WriteFile(metricsPath, data, perms.ReadWrite)
}