-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Node Balance Monitoring for STOM (#414)
- Loading branch information
1 parent
8cf7b7f
commit 79d5810
Showing
20 changed files
with
722 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package monitoring | ||
|
||
import ( | ||
"context" | ||
"math/big" | ||
"sync" | ||
|
||
commonMonitoring "github.com/smartcontractkit/chainlink-common/pkg/monitoring" | ||
) | ||
|
||
func NewNodeBalancesExporterFactory(log commonMonitoring.Logger, metrics Metrics) commonMonitoring.ExporterFactory { | ||
return &nodeBalancesExporterFactory{ | ||
log, | ||
metrics, | ||
} | ||
} | ||
|
||
type nodeBalancesExporterFactory struct { | ||
log commonMonitoring.Logger | ||
metrics Metrics | ||
} | ||
|
||
func (f *nodeBalancesExporterFactory) NewExporter(params commonMonitoring.ExporterParams) (commonMonitoring.Exporter, error) { | ||
return &nodeBalancesExporter{ | ||
log: f.log, | ||
metrics: f.metrics, | ||
chainConfig: params.ChainConfig, | ||
}, nil | ||
} | ||
|
||
type nodeBalancesExporter struct { | ||
log commonMonitoring.Logger | ||
metrics Metrics | ||
chainConfig commonMonitoring.ChainConfig | ||
addrsSet []ContractAddressWithBalance | ||
addrsMu sync.Mutex | ||
} | ||
|
||
func (e *nodeBalancesExporter) Export(ctx context.Context, data interface{}) { | ||
balanceEnvelope, isBalanceEnvelope := data.(BalanceEnvelope) | ||
if !isBalanceEnvelope { | ||
return | ||
} | ||
|
||
decimals := balanceEnvelope.Decimals | ||
divisor := new(big.Int).Exp(new(big.Int).SetUint64(10), decimals, nil) // 10^(decimals) | ||
|
||
for _, c := range balanceEnvelope.Contracts { | ||
balanceAns := new(big.Int).Div(c.Balance, divisor) | ||
|
||
e.metrics.SetBalance( | ||
toFloat64(balanceAns), | ||
c.Address.String(), | ||
c.Name, | ||
e.chainConfig.GetNetworkID(), | ||
e.chainConfig.GetNetworkName(), | ||
e.chainConfig.GetChainID()) | ||
} | ||
|
||
e.addrsMu.Lock() | ||
defer e.addrsMu.Unlock() | ||
|
||
e.addrsSet = balanceEnvelope.Contracts | ||
|
||
} | ||
|
||
func (e *nodeBalancesExporter) Cleanup(_ context.Context) { | ||
e.addrsMu.Lock() | ||
defer e.addrsMu.Unlock() | ||
|
||
for _, c := range e.addrsSet { | ||
e.metrics.CleanupBalance(c.Address.String(), c.Name, e.chainConfig.GetNetworkID(), e.chainConfig.GetNetworkName(), e.chainConfig.GetChainID()) | ||
} | ||
} | ||
|
||
func toFloat64(bignum *big.Int) float64 { | ||
val, _ := new(big.Float).SetInt(bignum).Float64() | ||
return val | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.