From 04bb2b923230fd9f81e6c7f4447dbf1cee5bef83 Mon Sep 17 00:00:00 2001 From: Massaki Archambault Date: Mon, 13 Sep 2021 23:51:47 -0400 Subject: [PATCH] add account balance metric --- helium-blockchain-exporter.go | 15 +++++++++++--- heliumapi/accounts.go | 38 +++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 heliumapi/accounts.go diff --git a/helium-blockchain-exporter.go b/helium-blockchain-exporter.go index 3dc6b2a..4056ef3 100644 --- a/helium-blockchain-exporter.go +++ b/helium-blockchain-exporter.go @@ -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 diff --git a/heliumapi/accounts.go b/heliumapi/accounts.go new file mode 100644 index 0000000..d2487f3 --- /dev/null +++ b/heliumapi/accounts.go @@ -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 +}