Files
node/service/metrics/client.go
T
zeekayandHanzo Dev 51a304804c chore: migrate luxd HTTP routes /ext -> /v1
Drop the Avalanche-heritage /ext prefix; /v1 is the single canonical route
surface (one way, no backward compat). The node's baseURL is the source of
truth; clients, SDKs, CLI, indexer, maker, genesis, netrunner, and the
k8s/compose/gateway/explorer configs are updated to match.

Co-authored-by: Hanzo Dev <dev@hanzo.ai>
2026-07-01 11:40:13 -07:00

75 lines
1.7 KiB
Go

// Copyright (C) 2019-2025, Lux Industries Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package metrics
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"net/url"
"github.com/luxfi/metric"
)
// cleanlyCloseBody drains and closes an HTTP response body to prevent
// HTTP/2 GOAWAY errors caused by closing bodies with unread data.
func cleanlyCloseBody(body io.ReadCloser) error {
if body == nil {
return nil
}
_, _ = io.Copy(io.Discard, body)
return body.Close()
}
// Client for requesting metrics from a remote Lux Node instance
type Client struct {
uri string
}
// NewClient returns a new Metrics API Client
func NewClient(uri string) *Client {
return &Client{
uri: uri + "/v1/metrics",
}
}
// GetMetrics returns the metrics from the connected node. The metrics are
// returned as a map of metric family name to the metric family.
func (c *Client) GetMetrics(ctx context.Context) (map[string]*metric.MetricFamily, error) {
uri, err := url.Parse(c.uri)
if err != nil {
return nil, err
}
request, err := http.NewRequestWithContext(
ctx,
http.MethodGet,
uri.String(),
bytes.NewReader(nil),
)
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
resp, err := http.DefaultClient.Do(request)
if err != nil {
return nil, fmt.Errorf("failed to issue request: %w", err)
}
defer cleanlyCloseBody(resp.Body)
// Return an error for any non successful status code
if resp.StatusCode < 200 || resp.StatusCode > 299 {
return nil, fmt.Errorf("received status code: %d", resp.StatusCode)
}
var parser metric.TextParser
metrics, err := parser.TextToMetricFamilies(resp.Body)
if err != nil {
return nil, err
}
return metrics, nil
}