1
0
Fork 0

add account balance metric

This commit is contained in:
Massaki Archambault 2021-09-13 23:51:47 -04:00
parent 3850678993
commit 04bb2b9232
2 changed files with 50 additions and 3 deletions

View File

@ -30,7 +30,7 @@ const (
var (
// lables
commonAccountLabels = []string{"account"}
commonHotspotLabels = append([]string{"hotspot"}, commonAccountLabels...)
commonHotspotLabels = append(commonAccountLabels, "hotspot", "hotspot_name")
// exporter metrics
@ -138,7 +138,7 @@ var (
accountActivity = metricInfo{
prometheus.NewDesc(
prometheus.BuildFQName(namespace, "account", "activity_total"),
"The total of time an activity occurred in an account.",
"The total number of time an activity occurred in an account.",
append(commonAccountLabels, "type"), nil,
),
prometheus.CounterValue,
@ -259,7 +259,16 @@ func (e *Exporter) collectStatsMetrics(ch chan<- prometheus.Metric) {
// collectStatsMetrics collect metrics in the account group from the helium api
func (e *Exporter) collectAccountMetrics(ch chan<- prometheus.Metric, account string) {
fmt.Println(account)
accountForAddress, err := heliumapi.GetAccountForAddress(account)
if err != nil {
fmt.Println(err)
return
}
ch <- prometheus.MustNewConstMetric(
accountBalanceHnt.Desc, accountBalanceHnt.Type, float64(accountForAddress.Data.Balance),
account,
)
}
// NewExporter returns an initialized Exporter

38
heliumapi/accounts.go Normal file
View File

@ -0,0 +1,38 @@
package heliumapi
import (
"encoding/json"
"fmt"
)
type Account struct {
Data struct {
Address string `json:"address"`
Balance int `json:"balance"`
Block int `json:"block"`
DCBalance int `json:"dc_balance"`
DCNonce int `json:"dc_nonce"`
SECBalance int `json:"sec_balance"`
SECNonce int `json:"sec_nonce"`
SpeculativeNonce int `json:"speculative_nonce"`
} `json:"data"`
}
func GetAccountForAddress(account string) (*Account, error) {
path := "/v1/accounts/" + account
// query the api
respBody, err := getHeliumApi(path, nil)
if err != nil {
return nil, err
}
// unmarshal the response
respobject := Account{}
err = json.Unmarshal(respBody, &respobject)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal response from path %v: %v", path, err)
}
return &respobject, nil
}