1
0
Fork 0

Compare commits

..

No commits in common. "04bb2b923230fd9f81e6c7f4447dbf1cee5bef83" and "0f7fc9d443c40c81c1fbae2e07a8b9e329413c13" have entirely different histories.

2 changed files with 9 additions and 132 deletions

View File

@ -1,11 +1,9 @@
package main package main
import ( import (
"flag"
"fmt" "fmt"
"log" "log"
"net/http" "net/http"
"strings"
"github.com/helium-blockchain-exporter/heliumapi" "github.com/helium-blockchain-exporter/heliumapi"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
@ -20,7 +18,6 @@ type metricInfo struct {
// Exporter collect metrics from the helium blockchain api and exports them as prometheus metrics. // Exporter collect metrics from the helium blockchain api and exports them as prometheus metrics.
type Exporter struct { type Exporter struct {
Accounts []string
} }
const ( const (
@ -28,10 +25,6 @@ const (
) )
var ( var (
// lables
commonAccountLabels = []string{"account"}
commonHotspotLabels = append(commonAccountLabels, "hotspot", "hotspot_name")
// exporter metrics // exporter metrics
// helium oracle metrics // helium oracle metrics
@ -48,7 +41,7 @@ var (
statsValidators = metricInfo{ statsValidators = metricInfo{
prometheus.NewDesc( prometheus.NewDesc(
prometheus.BuildFQName(namespace, "stats", "validators"), prometheus.BuildFQName(namespace, "stats", "validators"),
"The number of validators.", "The number of hotspots.",
nil, nil, nil, nil,
), ),
prometheus.GaugeValue, prometheus.GaugeValue,
@ -127,46 +120,6 @@ var (
} }
// helium account metrics // helium account metrics
accountBalanceHnt = metricInfo{
prometheus.NewDesc(
prometheus.BuildFQName(namespace, "account", "balance_hnt"),
"The number of HNT token owned by the account.",
commonAccountLabels, nil,
),
prometheus.GaugeValue,
}
accountActivity = metricInfo{
prometheus.NewDesc(
prometheus.BuildFQName(namespace, "account", "activity_total"),
"The total number of time an activity occurred in an account.",
append(commonAccountLabels, "type"), nil,
),
prometheus.CounterValue,
}
accountRewardsHnt = metricInfo{
prometheus.NewDesc(
prometheus.BuildFQName(namespace, "account", "reward_hnt"),
"The number of HNT token rewarded to this account.",
commonAccountLabels, nil,
),
prometheus.GaugeValue,
}
accountDepositsHnt = metricInfo{
prometheus.NewDesc(
prometheus.BuildFQName(namespace, "account", "deposits_hnt_total"),
"The number of HNT token deposited to this account.",
commonAccountLabels, nil,
),
prometheus.CounterValue,
}
accountWithdrawalsHnt = metricInfo{
prometheus.NewDesc(
prometheus.BuildFQName(namespace, "account", "withdrawals_hnt_total"),
"The number of HNT token withdrawn from this account.",
commonAccountLabels, nil,
),
prometheus.CounterValue,
}
// helium hotspot metrics // helium hotspot metrics
) )
@ -186,12 +139,6 @@ func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
ch <- statsCountries.Desc ch <- statsCountries.Desc
ch <- statsHotspots.Desc ch <- statsHotspots.Desc
ch <- statsTokenSupply.Desc ch <- statsTokenSupply.Desc
ch <- accountBalanceHnt.Desc
ch <- accountActivity.Desc
ch <- accountRewardsHnt.Desc
ch <- accountDepositsHnt.Desc
ch <- accountWithdrawalsHnt.Desc
} }
// Collect fetches the data from the helium blockchain api and delivers them as Prometheus metrics. // Collect fetches the data from the helium blockchain api and delivers them as Prometheus metrics.
@ -199,12 +146,8 @@ func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
func (e *Exporter) Collect(ch chan<- prometheus.Metric) { func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
e.collectOracleMetrics(ch) e.collectOracleMetrics(ch)
e.collectStatsMetrics(ch) e.collectStatsMetrics(ch)
for _, account := range e.Accounts {
e.collectAccountMetrics(ch, account)
}
} }
// collectOracleMetrics collect metrics in the oracle group from the helium api
func (e *Exporter) collectOracleMetrics(ch chan<- prometheus.Metric) { func (e *Exporter) collectOracleMetrics(ch chan<- prometheus.Metric) {
currentOraclePrice, err := heliumapi.GetCurrentOraclePrice() currentOraclePrice, err := heliumapi.GetCurrentOraclePrice()
if err != nil { if err != nil {
@ -217,7 +160,6 @@ func (e *Exporter) collectOracleMetrics(ch chan<- prometheus.Metric) {
) )
} }
// collectStatsMetrics collect metrics in the stats group from the helium api
func (e *Exporter) collectStatsMetrics(ch chan<- prometheus.Metric) { func (e *Exporter) collectStatsMetrics(ch chan<- prometheus.Metric) {
blockchainStats, err := heliumapi.GetBlockchainStats() blockchainStats, err := heliumapi.GetBlockchainStats()
if err != nil { if err != nil {
@ -257,41 +199,15 @@ 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) {
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 // NewExporter returns an initialized Exporter
func NewExporter(accounts []string) (*Exporter, error) { func NewExporter() (*Exporter, error) {
return &Exporter{ return &Exporter{}, nil
Accounts: accounts,
}, nil
} }
func main() { func main() {
fHeliumAccounts := flag.String("accounts", "", "A comma-delimited list of helium accounts to scrape.") metricsPath := "/metrics"
fMetricsPath := flag.String("metricpath", "/metrics", "The metrics path")
fListenAddress := flag.String("listenAddress", "0.0.0.0", "The http server listen address")
fListenPort := flag.String("listenPort", "9111", "The http server listen port")
flag.Parse()
heliumAccounts := make([]string, 0) e, err := NewExporter()
if *fHeliumAccounts != "" {
heliumAccounts = strings.Split(*fHeliumAccounts, ",")
}
serverAddr := *fListenAddress + ":" + *fListenPort
e, err := NewExporter(heliumAccounts)
if err != nil { if err != nil {
log.Fatalf("failed to start exporter: %s", err.Error()) log.Fatalf("failed to start exporter: %s", err.Error())
} }
@ -305,12 +221,11 @@ func main() {
<head><title>Helium blockchain exporter</title></head> <head><title>Helium blockchain exporter</title></head>
<body> <body>
<h1>Helium blockchain exporter</h1> <h1>Helium blockchain exporter</h1>
<p><a href='` + *fMetricsPath + `'>Metrics</a></p> <p><a href='` + metricsPath + `'>Metrics</a></p>
</body> </body>
</html>`)) </html>`))
}) })
http.Handle(*fMetricsPath, promhttp.HandlerFor(r, promhttp.HandlerOpts{})) http.Handle(metricsPath, promhttp.HandlerFor(r, promhttp.HandlerOpts{}))
fmt.Printf("exporter listening on %v\n", serverAddr) http.ListenAndServe(":9111", nil)
http.ListenAndServe(serverAddr, nil)
} }

View File

@ -1,38 +0,0 @@
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
}