Skip to content

Commit

Permalink
refactor: sync code from metrics-exporter
Browse files Browse the repository at this point in the history
  • Loading branch information
pcfreak30 committed Dec 28, 2024
1 parent 6da24ec commit 37862fd
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
27 changes: 25 additions & 2 deletions pkg/registrar/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package registrar
import (
"context"
"fmt"
"go.lumeweb.com/akash-metrics-registrar/pkg/build"
"go.lumeweb.com/akash-metrics-registrar/pkg/logger"
"go.lumeweb.com/akash-metrics-registrar/pkg/util"
"os"
"strings"
etcdregistry "go.lumeweb.com/etcd-registry"
"go.lumeweb.com/etcd-registry/types"
"golang.org/x/time/rate"
Expand Down Expand Up @@ -175,10 +178,25 @@ func (a *App) performInitialRegistration() error {
return fmt.Errorf("rate limit exceeded: %w", err)
}

// Get Akash identifiers
ingressHost := os.Getenv("AKASH_INGRESS_HOST")
identifiers := util.GetNodeIdentifiers(ingressHost)

// Add standard Akash and version labels
labels := make(map[string]string)
for k, v := range a.currentInfo.Labels {
labels[k] = v
}
labels["version"] = build.Version
labels["git_commit"] = build.GitCommit
labels["deployment_id"] = identifiers.DeploymentID
labels["hash_id"] = identifiers.HashID
labels["ingress_host"] = ingressHost

node := types.Node{
ID: a.currentInfo.ID,
ExporterType: "metrics_exporter",
Labels: a.currentInfo.Labels,
Labels: labels,
Status: string(a.currentInfo.Status),
LastSeen: time.Now(),
}
Expand Down Expand Up @@ -245,7 +263,12 @@ func (a *App) Shutdown(ctx context.Context) error {

// Close etcd registry
if err := a.registry.Close(); err != nil {
return fmt.Errorf("failed to close etcd registry: %w", err)
if strings.Contains(err.Error(), "requested lease not found") {
logger.Log.Debug("Ignoring lease not found error during shutdown")
} else {
logger.Log.Errorf("Error closing etcd registry: %v", err)
return fmt.Errorf("failed to close etcd registry: %w", err)
}
}

return nil
Expand Down
32 changes: 32 additions & 0 deletions pkg/util/akash.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package util

import (
"os"
"strings"
)

// NodeIdentifiers contains Akash deployment identification info
type NodeIdentifiers struct {
HashID string // From ingress hostname
DeploymentID string // From AKASH_DEPLOYMENT_SEQUENCE
}

// GetNodeIdentifiers extracts node identifiers from Akash environment
func GetNodeIdentifiers(host string) NodeIdentifiers {
hashID := extractHashFromHost(host)
deploymentID := os.Getenv("AKASH_DEPLOYMENT_SEQUENCE")

return NodeIdentifiers{
HashID: hashID,
DeploymentID: deploymentID,
}
}

// ExtractHashFromHost gets the hash ID from an Akash hostname
func extractHashFromHost(host string) string {
parts := strings.Split(host, ".")
if len(parts) > 0 {
return parts[0]
}
return host
}

0 comments on commit 37862fd

Please sign in to comment.