Skip to content

Commit

Permalink
fix: init node id w/ val status
Browse files Browse the repository at this point in the history
  • Loading branch information
murakamikaze committed Nov 9, 2024
1 parent c072a0d commit 2df3b67
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 23 deletions.
15 changes: 11 additions & 4 deletions cmd/hl-exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/validaoxyz/hyperliquid-exporter/internal/exporter"
"github.com/validaoxyz/hyperliquid-exporter/internal/logger"
"github.com/validaoxyz/hyperliquid-exporter/internal/metrics"
"github.com/validaoxyz/hyperliquid-exporter/internal/monitors"
)

func main() {
Expand Down Expand Up @@ -76,19 +77,25 @@ func main() {
}
}

// Initialize metrics
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()

// After loading config, before metrics initialization
validatorAddress, isValidator := monitors.GetValidatorStatus(cfg.NodeHome)

// Initialize metrics configuration
metricsConfig := metrics.MetricsConfig{
EnablePrometheus: !*disableProm && *enableProm,
EnableOTLP: *enableOTLP,
OTLPEndpoint: *otlpEndpoint,
OTLPInsecure: *otlpInsecure,
Alias: *alias,
Chain: *chain,
NodeHome: cfg.NodeHome,
ValidatorAddress: validatorAddress,
IsValidator: isValidator,
}

ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()

if err := metrics.InitMetrics(ctx, metricsConfig); err != nil {
logger.Error("Failed to initialize metrics: %v", err)
os.Exit(1)
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ module github.com/validaoxyz/hyperliquid-exporter

go 1.22


require (
github.com/joho/godotenv v1.5.1
github.com/prometheus/client_golang v1.20.4
Expand Down
3 changes: 3 additions & 0 deletions internal/metrics/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ type MetricsConfig struct {
OTLPInsecure bool
Alias string
Chain string
NodeHome string
ValidatorAddress string
IsValidator bool
}
2 changes: 1 addition & 1 deletion internal/metrics/getters.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package metrics
func IsValidator() bool {
metricsMutex.Lock()
defer metricsMutex.Unlock()
return nodeAlias.IsValidator
return nodeIdentity.IsValidator
}
12 changes: 7 additions & 5 deletions internal/metrics/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func getPublicIP() (string, error) {
return string(ip), nil
}

func InitializeNodeAlias(cfg MetricsConfig) error {
func InitializeNodeIdentity(cfg MetricsConfig) error {
ip, err := getPublicIP()
if err != nil {
return fmt.Errorf("failed to get public IP: %w", err)
Expand All @@ -30,10 +30,12 @@ func InitializeNodeAlias(cfg MetricsConfig) error {
metricsMutex.Lock()
defer metricsMutex.Unlock()

nodeAlias = NodeAlias{
ServerIP: ip,
Alias: cfg.Alias,
Chain: cfg.Chain,
nodeIdentity = NodeIdentity{
ServerIP: ip,
Alias: cfg.Alias,
Chain: cfg.Chain,
ValidatorAddress: cfg.ValidatorAddress,
IsValidator: cfg.IsValidator,
}

return nil
Expand Down
14 changes: 7 additions & 7 deletions internal/metrics/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ import (

// InitMetrics initializes the metrics system with the given configuration
func InitMetrics(ctx context.Context, cfg MetricsConfig) error {
if err := InitializeNodeAlias(cfg); err != nil {
return fmt.Errorf("failed to initialize node alias: %w", err)
// Initialize node identity with values from config
if err := InitializeNodeIdentity(cfg); err != nil {
return fmt.Errorf("failed to initialize node identity: %w", err)
}

// Initialize the provider
if err := InitProvider(ctx, cfg); err != nil {
return fmt.Errorf("failed to initialize provider: %w", err)
}
Expand All @@ -29,8 +31,6 @@ func InitMetrics(ctx context.Context, cfg MetricsConfig) error {
return fmt.Errorf("failed to initialize instruments: %w", err)
}

// SetNodeInfo()

if cfg.EnablePrometheus {
if err := StartPrometheusServer(ctx, 8086); err != nil {
return fmt.Errorf("failed to start Prometheus server: %w", err)
Expand All @@ -57,9 +57,9 @@ func sanitizeEndpoint(endpoint string) string {

func InitProvider(ctx context.Context, cfg MetricsConfig) error {
metricsMutex.RLock()
serverIP := nodeAlias.ServerIP
isValidator := nodeAlias.IsValidator
validatorAddress := nodeAlias.ValidatorAddress
serverIP := nodeIdentity.ServerIP
isValidator := nodeIdentity.IsValidator
validatorAddress := nodeIdentity.ValidatorAddress
metricsMutex.RUnlock()

res := resource.NewWithAttributes(
Expand Down
4 changes: 2 additions & 2 deletions internal/metrics/setters.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,13 @@ func IncrementEVMTransactionsCounter() {
func SetIsValidator(isValidator bool) {
metricsMutex.Lock()
defer metricsMutex.Unlock()
nodeAlias.IsValidator = isValidator
nodeIdentity.IsValidator = isValidator
}

func SetValidatorAddress(address string) {
metricsMutex.Lock()
defer metricsMutex.Unlock()
nodeAlias.ValidatorAddress = address
nodeIdentity.ValidatorAddress = address
}

func SetActiveStake(stake float64) {
Expand Down
6 changes: 3 additions & 3 deletions internal/metrics/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type labeledValue struct {
labels []attribute.KeyValue
}

type NodeAlias struct {
type NodeIdentity struct {
ValidatorAddress string
ServerIP string
Alias string
Expand All @@ -22,11 +22,11 @@ type NodeAlias struct {

// Global variables for metric state management
var (
nodeIdentity NodeIdentity
metricsMutex sync.RWMutex
currentValues = make(map[api.Observable]interface{})
labeledValues = make(map[api.Observable]map[string]labeledValue)
metricsMutex sync.RWMutex
callbacks []api.Registration
nodeAlias NodeAlias
)

// TODO CommonLabels holds the common labels to be added to all metrics
Expand Down
47 changes: 47 additions & 0 deletions internal/monitors/validator_status_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,50 @@ func ReadLastLine(filePath string) (string, error) {
}
return lastLine, nil
}

func GetValidatorStatus(nodeHome string) (string, bool) {
statusDir := filepath.Join(nodeHome, "data/node_logs/status/hourly")
latestFile, err := utils.GetLatestFile(statusDir)
if err != nil {
logger.Warning("Error finding latest status file: %v", err)
return "", false
}

fileInfo, err := os.Stat(latestFile)
if err != nil {
logger.Warning("Error getting status file info: %v", err)
return "", false
}

if time.Since(fileInfo.ModTime()) > 24*time.Hour {
return "", false
}

lastLine, err := ReadLastLine(latestFile)
if err != nil {
logger.Warning("Error reading last line of status file: %v", err)
return "", false
}

var rawData []json.RawMessage
if err := json.Unmarshal([]byte(lastLine), &rawData); err != nil {
logger.Warning("Failed to parse status array: %v", err)
return "", false
}

if len(rawData) != 2 {
logger.Warning("Unexpected validator status data format")
return "", false
}

var data struct {
HomeValidator string `json:"home_validator"`
}

if err := json.Unmarshal(rawData[1], &data); err != nil {
logger.Warning("Failed to parse validator data: %v", err)
return "", false
}

return data.HomeValidator, data.HomeValidator != ""
}

0 comments on commit 2df3b67

Please sign in to comment.