forked from 31z4/ethereum-prometheus-exporter
/
main.go
74 lines (59 loc) · 1.65 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"github.com/0xVox/ethereum-prometheus-exporter/internal/collector"
"github.com/ethereum/go-ethereum/rpc"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var version = "undefined"
func main() {
flag.Usage = func() {
const (
usage = "Usage: ethereum_exporter [option] [arg]\n\n" +
"Prometheus exporter for Ethereum client metrics\n\n" +
"Options and arguments:\n"
)
fmt.Fprint(flag.CommandLine.Output(), usage)
flag.PrintDefaults()
os.Exit(2)
}
url := flag.String("url", "http://localhost:8545", "Ethereum JSON-RPC URL")
addr := flag.String("addr", ":9368", "listen address")
ver := flag.Bool("v", false, "print version number and exit")
flag.Parse()
if len(flag.Args()) > 0 {
flag.Usage()
}
if *ver {
fmt.Println(version)
os.Exit(0)
}
rpc, err := rpc.Dial(*url)
if err != nil {
log.Fatal(err)
}
registry := prometheus.NewPedanticRegistry()
registry.MustRegister(
collector.NewNetPeerCount(rpc),
collector.NewEthBlockNumber(rpc),
collector.NewEthGasPrice(rpc),
collector.NewEthEarliestBlockTransactions(rpc),
collector.NewEthLatestBlockTransactions(rpc),
collector.NewEthPendingBlockTransactions(rpc),
collector.NewEthHashrate(rpc),
collector.NewEthSyncing(rpc),
collector.NewParityNetPeers(rpc),
collector.NewEthHeightDiff(rpc),
)
handler := promhttp.HandlerFor(registry, promhttp.HandlerOpts{
ErrorLog: log.New(os.Stderr, log.Prefix(), log.Flags()),
ErrorHandling: promhttp.ContinueOnError,
})
http.Handle("/metrics", handler)
log.Fatal(http.ListenAndServe(*addr, nil))
}