1
0
Fork 0

add flags

This commit is contained in:
Massaki Archambault 2021-09-13 23:36:48 -04:00
parent 0f7fc9d443
commit 3850678993
1 changed files with 85 additions and 9 deletions

View File

@ -1,9 +1,11 @@
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"
@ -18,6 +20,7 @@ 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 (
@ -25,6 +28,10 @@ const (
) )
var ( var (
// lables
commonAccountLabels = []string{"account"}
commonHotspotLabels = append([]string{"hotspot"}, commonAccountLabels...)
// exporter metrics // exporter metrics
// helium oracle metrics // helium oracle metrics
@ -41,7 +48,7 @@ var (
statsValidators = metricInfo{ statsValidators = metricInfo{
prometheus.NewDesc( prometheus.NewDesc(
prometheus.BuildFQName(namespace, "stats", "validators"), prometheus.BuildFQName(namespace, "stats", "validators"),
"The number of hotspots.", "The number of validators.",
nil, nil, nil, nil,
), ),
prometheus.GaugeValue, prometheus.GaugeValue,
@ -119,7 +126,47 @@ var (
prometheus.GaugeValue, prometheus.GaugeValue,
} }
// 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 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
) )
@ -139,6 +186,12 @@ 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.
@ -146,8 +199,12 @@ 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 {
@ -160,6 +217,7 @@ 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 {
@ -199,15 +257,32 @@ 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)
}
// NewExporter returns an initialized Exporter // NewExporter returns an initialized Exporter
func NewExporter() (*Exporter, error) { func NewExporter(accounts []string) (*Exporter, error) {
return &Exporter{}, nil return &Exporter{
Accounts: accounts,
}, nil
} }
func main() { func main() {
metricsPath := "/metrics" fHeliumAccounts := flag.String("accounts", "", "A comma-delimited list of helium accounts to scrape.")
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()
e, err := NewExporter() heliumAccounts := make([]string, 0)
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())
} }
@ -221,11 +296,12 @@ 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='` + metricsPath + `'>Metrics</a></p> <p><a href='` + *fMetricsPath + `'>Metrics</a></p>
</body> </body>
</html>`)) </html>`))
}) })
http.Handle(metricsPath, promhttp.HandlerFor(r, promhttp.HandlerOpts{})) http.Handle(*fMetricsPath, promhttp.HandlerFor(r, promhttp.HandlerOpts{}))
http.ListenAndServe(":9111", nil) fmt.Printf("exporter listening on %v\n", serverAddr)
http.ListenAndServe(serverAddr, nil)
} }