This repository has been archived by the owner on May 17, 2024. It is now read-only.
forked from ethereum-optimism/optimism
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
123 lines (106 loc) · 3.51 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"context"
"math/big"
"net/http"
"os"
"time"
"github.com/ethereum-optimism/optimism/l2geth-exporter/l1contracts"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/log"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
l1TimeoutSeconds = 5
)
func main() {
listenAddress := os.Getenv("LISTEN_ADDRESS")
if listenAddress == "" {
listenAddress = ":9100"
}
log.Root().SetHandler(log.CallerFileHandler(log.StdoutHandler))
l1Url := os.Getenv("L1_URL")
if l1Url == "" {
log.Error("L1_URL environmental variable is required")
os.Exit(1)
}
ctcAddress := os.Getenv("OVM_CTC_ADDRESS")
if ctcAddress == "" {
log.Error("OVM_CTC_ADDRESS environmental variable is required")
os.Exit(1)
}
sccAddress := os.Getenv("OVM_SCC_ADDRESS")
if sccAddress == "" {
log.Error("OVM_SCC_ADDRESS environmental variable is required")
os.Exit(1)
}
client, err := ethclient.Dial(l1Url)
if err != nil {
log.Error("Problem connecting to L1: %s", err)
}
http.Handle("/metrics", promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>L2geth Exporter</title></head>
<body>
<h1>L2geth Exporter</h1>
<p><a href="/metrics">Metrics</a></p>
</body>
</html>`))
})
go getCTCTotalElements(ctcAddress, "ctc", client)
go getSCCTotalElements(sccAddress, "scc", client)
log.Info("Program starting", "listenAddress", listenAddress, "GETH_URL", l1Url, "CTC_ADDRESS", ctcAddress)
if err := http.ListenAndServe(listenAddress, nil); err != nil {
log.Error("Can't start http server", "error", err)
}
}
func getSCCTotalElements(address string, addressLabel string, client *ethclient.Client) {
scc := l1contracts.SCC{
Address: common.HexToAddress(address),
Client: client,
}
ticker := time.NewTicker(30 * time.Second)
defer ticker.Stop()
for {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(l1TimeoutSeconds))
totalElements, err := scc.GetTotalElements(ctx)
if err != nil {
addressTotalElementsCallStatus.WithLabelValues("error", addressLabel).Inc()
log.Error("Error calling GetTotalElements", "address", addressLabel, "error", err)
cancel()
continue
}
addressTotalElementsCallStatus.WithLabelValues("success", addressLabel).Inc()
totalElementsFloat, _ := new(big.Float).SetInt(totalElements).Float64()
addressTotalElements.WithLabelValues("latest", addressLabel).Set(totalElementsFloat)
log.Info(addressLabel, "TotalElements", totalElementsFloat)
cancel()
<-ticker.C
}
}
func getCTCTotalElements(address string, addressLabel string, client *ethclient.Client) {
ctc := l1contracts.CTC{
Address: common.HexToAddress(address),
Client: client,
}
ticker := time.NewTicker(30 * time.Second)
defer ticker.Stop()
for {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(l1TimeoutSeconds))
totalElements, err := ctc.GetTotalElements(ctx)
if err != nil {
addressTotalElementsCallStatus.WithLabelValues("error", addressLabel).Inc()
log.Error("Error calling GetTotalElements", "address", addressLabel, "error", err)
cancel()
continue
}
addressTotalElementsCallStatus.WithLabelValues("success", addressLabel).Inc()
totalElementsFloat, _ := new(big.Float).SetInt(totalElements).Float64()
addressTotalElements.WithLabelValues("latest", addressLabel).Set(totalElementsFloat)
log.Info(addressLabel, "TotalElements", totalElementsFloat)
cancel()
<-ticker.C
}
}