Skip to content

Commit

Permalink
More comprehensive ScPrime changes
Browse files Browse the repository at this point in the history
Numerous internal changes to further differentiate from Sia.
  • Loading branch information
MasterHW authored Dec 15, 2019
1 parent 4c7750d commit 9479617
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 54 deletions.
56 changes: 28 additions & 28 deletions collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/prometheus/client_golang/prometheus/promauto"
"gitlab.com/SiaPrime/SiaPrime/modules"
"gitlab.com/SiaPrime/SiaPrime/node/api"
sia "gitlab.com/SiaPrime/SiaPrime/node/api/client"
scp "gitlab.com/SiaPrime/SiaPrime/node/api/client"
"gitlab.com/SiaPrime/errors"
)

Expand All @@ -19,11 +19,11 @@ var (
renterModuleLoaded = promauto.NewGauge(prometheus.GaugeOpts{
Name: "renter_module_loaded", Help: "Is the renter module loaded. 0=not loaded. 1=loaded"})
renterAggregateNumFiles = promauto.NewGauge(prometheus.GaugeOpts{
Name: "renter_aggregate_num_files", Help: "Shows the number of files uploaded to Sia by the renter"})
Name: "renter_aggregate_num_files", Help: "Shows the number of files uploaded to ScPrime by the renter"})
renterAggregateNumStuckChunks = promauto.NewGauge(prometheus.GaugeOpts{
Name: "renter_aggregate_num_stuck_chunks", Help: "The aggregate number of stuck chunks"})
renterAggregateSize = promauto.NewGauge(prometheus.GaugeOpts{
Name: "renter_aggregate_size", Help: "The aggregate size of data stored on Sia"})
Name: "renter_aggregate_size", Help: "The aggregate size of data stored on ScPrime"})
renterMaxHealth = promauto.NewGauge(prometheus.GaugeOpts{
Name: "renter_max_health", Help: "The max health"})
renterMinRedundancy = promauto.NewGauge(prometheus.GaugeOpts{
Expand All @@ -47,29 +47,29 @@ var (
Name: "renter_num_expired_refreshed_contracts", Help: "Number of expired refreshed contracts"})
// Allowance
renterAllowanceAmount = promauto.NewGauge(prometheus.GaugeOpts{
Name: "renter_allowance_amount", Help: "Renter allowance Amount (siacoins)"})
Name: "renter_allowance_amount", Help: "Renter allowance Amount (SCP)"})
renterAllowancePeriod = promauto.NewGauge(prometheus.GaugeOpts{
Name: "renter_allowance_period", Help: "Renter allowance period length (blocks)"})
renterAllowanceRenewWindow = promauto.NewGauge(prometheus.GaugeOpts{
Name: "renter_allowance_renew_window", Help: "Renter allowance renew window (blocks)"})
renterAllowanceHosts = promauto.NewGauge(prometheus.GaugeOpts{
Name: "renter_allowance_hosts", Help: "Renter allowance hosts"})
renterAllowanceCurrentSpent = promauto.NewGauge(prometheus.GaugeOpts{
Name: "renter_allowance_current_spent", Help: "Amount of allowance in Siacoins spent in the current period"})
Name: "renter_allowance_current_spent", Help: "Amount of allowance in SCP spent in the current period"})
renterAllowanceCurrentUnspent = promauto.NewGauge(prometheus.GaugeOpts{
Name: "renter_allowance_current_unspent", Help: "Unspent amount of allowance in Siacoins in the current period"})
Name: "renter_allowance_current_unspent", Help: "Unspent amount of allowance in SCP in the current period"})
renterAllowanceCurrentStorage = promauto.NewGauge(prometheus.GaugeOpts{
Name: "renter_allowance_current_storage", Help: "Amount of allowance in Siacoins spent in the current period on storage"})
Name: "renter_allowance_current_storage", Help: "Amount of allowance in SCP spent in the current period on storage"})
renterAllowanceCurrentUpload = promauto.NewGauge(prometheus.GaugeOpts{
Name: "renter_allowance_current_upload", Help: "Amount of allowance in Siacoins spent in the current period on upload bandwidth"})
Name: "renter_allowance_current_upload", Help: "Amount of allowance in SCP spent in the current period on upload bandwidth"})
renterAllowanceCurrentDownload = promauto.NewGauge(prometheus.GaugeOpts{
Name: "renter_allowance_current_download", Help: "Amount of allowance in Siacoins spent in the current period on download bandwidth"})
Name: "renter_allowance_current_download", Help: "Amount of allowance in SCP spent in the current period on download bandwidth"})
renterAllowanceCurrentFees = promauto.NewGauge(prometheus.GaugeOpts{
Name: "renter_allowance_current_fees", Help: "Amount of allowance in Siacoins spent in the current period on fees"})
Name: "renter_allowance_current_fees", Help: "Amount of allowance in SCP spent in the current period on fees"})
renterAllowanceCurrentUnspentAllocated = promauto.NewGauge(prometheus.GaugeOpts{
Name: "renter_allowance_current_unspent_allocated", Help: "Amount of allocated unspent allowance in Siacoins"})
Name: "renter_allowance_current_unspent_allocated", Help: "Amount of allocated unspent allowance in SCP"})
renterAllowanceCurrentUnspentUnallocated = promauto.NewGauge(prometheus.GaugeOpts{
Name: "renter_allowance_current_unspent_unallocated", Help: "Amount of unallocated unspent allowance in Siacoins"})
Name: "renter_allowance_current_unspent_unallocated", Help: "Amount of unallocated unspent allowance in SCP"})

// Consensus Metrics
consensusModuleLoaded = promauto.NewGauge(prometheus.GaugeOpts{
Expand All @@ -95,15 +95,15 @@ var (
walletLocked = promauto.NewGauge(prometheus.GaugeOpts{
Name: "wallet_locked", Help: "Is the wallet locked. 0=not locked. 1=locked"})
walletConfirmedSiacoinBalanceHastings = promauto.NewGauge(prometheus.GaugeOpts{
Name: "wallet_confirmed_siacoin_balance_hastings", Help: "Wallet confirmed Siacoin balance (Hastings)"})
Name: "wallet_confirmed_siacoin_balance_hastings", Help: "Wallet confirmed SCP balance (Hastings)"})
walletConfirmedSiacoinBalance = promauto.NewGauge(prometheus.GaugeOpts{
Name: "wallet_confirmed_siacoin_balance", Help: "Wallet confirmed Siacoin balance (Siacoins)"})
Name: "wallet_confirmed_siacoin_balance", Help: "Wallet confirmed SCP balance"})
walletSiafundBalance = promauto.NewGauge(prometheus.GaugeOpts{
Name: "wallet_siafund_balance", Help: "Wallet Siafund balance"})
walletSiafundClaimBalance = promauto.NewGauge(prometheus.GaugeOpts{
Name: "wallet_siafund_claim_balance", Help: "Wallet Siafund claim balance"})
walletNumAddresses = promauto.NewGauge(prometheus.GaugeOpts{
Name: "wallet_num_addresses", Help: "Number of wallet addresses being tracked by Sia"})
Name: "wallet_num_addresses", Help: "Number of wallet addresses being tracked by ScPrime"})

// Gateway Metrics
gatewayModuleLoaded = promauto.NewGauge(prometheus.GaugeOpts{
Expand Down Expand Up @@ -137,9 +137,9 @@ var (
hostWindowSize = promauto.NewGauge(prometheus.GaugeOpts{
Name: "host_window_size", Help: "Window Size in hours"})
hostCollateral = promauto.NewGauge(prometheus.GaugeOpts{
Name: "host_collateral", Help: "Host Collateral in Siacoins"})
Name: "host_collateral", Help: "Host Collateral in SCP"})
hostCollateralBudget = promauto.NewGauge(prometheus.GaugeOpts{
Name: "host_collateral_budget", Help: "Host Collateral budget in Siacoins"})
Name: "host_collateral_budget", Help: "Host Collateral budget in SCP"})
hostMaxCollateral = promauto.NewGauge(prometheus.GaugeOpts{
Name: "host_max_collateral", Help: "Max collateral per contract"})
hostContractCount = promauto.NewGauge(prometheus.GaugeOpts{
Expand All @@ -150,7 +150,7 @@ const (
moduleNotReadyStatus = "Module not loaded or still starting up"
)

func hostMetrics(sc *sia.Client) {
func hostMetrics(sc *scp.Client) {
hg, err := sc.HostGet()
if errors.Contains(err, ErrAPICallNotRecognized) {
// Assume module is not loaded if status command is not recognized.
Expand Down Expand Up @@ -206,7 +206,7 @@ func hostMetrics(sc *sia.Client) {

}

func renterMetrics(sc *sia.Client) {
func renterMetrics(sc *scp.Client) {

// Renter Get Dir Metrics
rg, err := sc.RenterGetDir(modules.RootSiaPath())
Expand Down Expand Up @@ -281,7 +281,7 @@ func renterMetrics(sc *sia.Client) {

// consensuMetrics retrieves and sets the Prometheus metrics related to the
// consensus module
func consensusMetrics(sc *sia.Client) {
func consensusMetrics(sc *scp.Client) {
cs, err := sc.ConsensusGet()
if errors.Contains(err, ErrAPICallNotRecognized) {
log.Info("Consensus module is not loaded")
Expand All @@ -300,8 +300,8 @@ func consensusMetrics(sc *sia.Client) {
}

// daemonMetrics retrieves and sets the Prometheus metrics related to the
// Sia daemon
func daemonMetrics(sc *sia.Client) {
// ScPrime daemon
func daemonMetrics(sc *scp.Client) {
//al, err := sc.DaemonAlertsGet()
//if err != nil {
// log.Info("Could not get Daemon metrics")
Expand All @@ -321,8 +321,8 @@ func daemonMetrics(sc *sia.Client) {
}

// walletMetrics retrieves and sets the Prometheus metrics related to the
// Sia wallet
func walletMetrics(sc *sia.Client) {
// ScPrime wallet
func walletMetrics(sc *scp.Client) {
status, err := sc.WalletGet()
if errors.Contains(err, ErrAPICallNotRecognized) {
log.Info("Wallet module is not loaded")
Expand Down Expand Up @@ -356,8 +356,8 @@ func walletMetrics(sc *sia.Client) {
}

// gatewayMetrics retrieves and sets the Prometheus metrics related to the
// Sia gateway
func gatewayMetrics(sc *sia.Client) {
// ScPrime gateway
func gatewayMetrics(sc *scp.Client) {
gateway, err := sc.GatewayGet()
if errors.Contains(err, ErrAPICallNotRecognized) {
log.Info("Gateway module is not loaded")
Expand All @@ -375,8 +375,8 @@ func gatewayMetrics(sc *sia.Client) {
}

// hostdbMetrics retrieves and sets the Prometheus metrics related to the
// Sia hostdb
func hostdbMetrics(sc *sia.Client) {
// ScPrime hostdb
func hostdbMetrics(sc *scp.Client) {
hostdb, err := sc.HostDbAllGet()
if errors.Contains(err, ErrAPICallNotRecognized) {
log.Info("HostDB module is not loaded")
Expand Down
51 changes: 25 additions & 26 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/sirupsen/logrus"
"gitlab.com/SiaPrime/SiaPrime/build"
sia "gitlab.com/SiaPrime/SiaPrime/node/api/client"
scprime "gitlab.com/SiaPrime/SiaPrime/node/api/client"
)

var (
Expand All @@ -22,8 +22,7 @@ var (
log *logrus.Logger
)

//initSiaClient sets the values of the client so we can communicate with the
//Sia Daemon.
//Sets the values of the client so we can communicate with the ScPrime Daemon.
func findPassword() string {
// Check environment variables
apiPassword := os.Getenv("SIA_API_PASSWORD")
Expand All @@ -33,8 +32,8 @@ func findPassword() string {
}

// Check .apipassword file
var siaDir = build.DefaultSiaDir()
pw, err := ioutil.ReadFile(build.APIPasswordFile(siaDir))
var scprimeDir = build.DefaultSiaDir()
pw, err := ioutil.ReadFile(build.APIPasswordFile(scprimeDir))
if err != nil {
log.Info("Could not read API password file:", err)
return ""
Expand Down Expand Up @@ -66,46 +65,46 @@ func boolToFloat64(b bool) float64 {
return float64(0)
}

// startMonitor refreshes the Sia metrics periodically as defined by refreshRate
func startMonitor(refreshRate time.Duration, sc *sia.Client) {
// startMonitor refreshes the ScPrime metrics periodically as defined by refreshRate
func startMonitor(refreshRate time.Duration, scp *scprime.Client) {
for range time.Tick(time.Minute * refreshRate) {
updateMetrics(sc)
updateMetrics(scp)
}
}

// updateMetrics calls the various metric collection functions
func updateMetrics(sc *sia.Client) {
func updateMetrics(scp *scprime.Client) {

log.Debug("Updating metrics for modules:", module)

log.Debug("Updating Daemon Metrics")
daemonMetrics(sc)
daemonMetrics(scp)

if strings.Contains(module, "r") {
log.Debug("Updating Renter Metrics")
renterMetrics(sc)
renterMetrics(scp)
log.Debug("Updating hostdb Metrics")
hostdbMetrics(sc)
hostdbMetrics(scp)
}

if strings.Contains(module, "c") {
log.Debug("Updating Consensus Metrics")
consensusMetrics(sc)
consensusMetrics(scp)
}

if strings.Contains(module, "w") {
log.Debug("Updating Wallet Metrics")
walletMetrics(sc)
walletMetrics(scp)
}

if strings.Contains(module, "g") {
log.Debug("Updating Gateway Metrics")
gatewayMetrics(sc)
gatewayMetrics(scp)
}

if strings.Contains(module, "h") {
log.Debug("Updating Host Metrics")
hostMetrics(sc)
hostMetrics(scp)
}

if strings.Contains(module, "m") {
Expand All @@ -122,27 +121,27 @@ func main() {

// Flags
flag.BoolVar(&debug, "debug", false, "Enable debug mode. Warning: generates a lot of output.")
address := flag.String("address", "127.0.0.1:4280", "Sia's API address")
agent := flag.String("agent", "SiaPrime-Agent", "SiaPrime agent")
refresh := flag.Int("refresh", 5, "Frequency to get Metrics from Sia (minutes)")
address := flag.String("address", "127.0.0.1:4280", "ScPrime's API address")
agent := flag.String("agent", "SiaPrime-Agent", "ScPrime agent")
refresh := flag.Int("refresh", 5, "Frequency to get Metrics from ScPrime (minutes)")
port := flag.Int("port", 4283, "Port to serve Prometheus Metrics on")
flag.StringVar(&module, "modules", "cghmrtw", "Sia Modules to monitor")
flag.StringVar(&module, "modules", "cghmrtw", "ScPrime Modules to monitor")
flag.Parse()

// Initialize the logger
initLogger(debug)

// Set the Sia Client connection information
sc := sia.New(*address)
sc.UserAgent = *agent
sc.Password = findPassword()
// Set the ScPrime Client connection information
scp := scprime.New(*address)
scp.UserAgent = *agent
scp.Password = findPassword()

// Set the metrics initially before starting the monitor and HTTP server
// If you don't do this all the metrics start with a "0" until they are set
updateMetrics(sc)
updateMetrics(scp)

// start the metrics collector
go startMonitor(time.Duration(*refresh), sc)
go startMonitor(time.Duration(*refresh), scp)

// This section will start the HTTP server and expose
// any metrics on the /metrics endpoint.
Expand Down

0 comments on commit 9479617

Please sign in to comment.