-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
147 lines (125 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"context"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"unsafe"
"github.com/pelletier/go-toml"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/stellar/go/network"
"github.com/stellar/go/support/datastore"
)
type Config struct {
StartLedger uint32
EndLedger uint32
EnableCSV bool
CSVPath string
ConfigPath string
MetricsPort int
}
// parseFlags parses command line arguments into a Config struct
func parseFlags() *Config {
cfg := &Config{}
flag.UintVar((*uint)(unsafe.Pointer(&cfg.StartLedger)), "start-ledger", 0, "Starting ledger sequence number (0 means latest)")
flag.UintVar((*uint)(unsafe.Pointer(&cfg.EndLedger)), "end-ledger", 0, "Ending ledger sequence number (0 means unbounded)")
flag.BoolVar(&cfg.EnableCSV, "enable-csv", false, "Enable CSV output")
flag.StringVar(&cfg.CSVPath, "csv-path", "ledger_data.csv", "Path to CSV output file")
flag.StringVar(&cfg.ConfigPath, "config", "config.toml", "Path to TOML configuration file")
flag.IntVar(&cfg.MetricsPort, "metrics-port", 8080, "Port for Prometheus metrics")
flag.Parse()
return cfg
}
type Message struct {
Payload interface{}
}
type OutboundAdapter interface {
Write(ctx context.Context, msg Message) error
Close()
}
var registry = prometheus.NewRegistry()
var namespace = "validator_bias"
var OperationsTotal = prometheus.NewSummaryVec(
prometheus.SummaryOpts{
Namespace: namespace,
Name: "total_operations",
},
[]string{"name", "node_id"},
)
var OperationsByCategory = prometheus.NewSummaryVec(
prometheus.SummaryOpts{
Namespace: namespace,
Name: "operations_by_category",
},
[]string{"name", "node_id", "category"},
)
var LedgerClosed = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "latest_ledger_closed",
},
[]string{"name", "node_id"},
)
func init() {
registry.MustRegister(LedgerClosed)
registry.MustRegister(OperationsTotal)
registry.MustRegister(OperationsByCategory)
fmt.Println("Prometheus metrics registered")
}
func main() {
cmdFlags := parseFlags()
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill)
defer stop()
go func() {
metricsAddr := fmt.Sprintf(":%d", cmdFlags.MetricsPort)
http.Handle("/metrics", promhttp.HandlerFor(registry, promhttp.HandlerOpts{}))
http.ListenAndServe(metricsAddr, nil)
}()
cfg, err := toml.LoadFile(cmdFlags.ConfigPath)
if err != nil {
fmt.Printf("%v\n", err)
return
}
datastoreConfig := datastore.DataStoreConfig{}
if err = cfg.Unmarshal(&datastoreConfig); err != nil {
fmt.Printf("error unmarshalling TOML config: %v\n", err)
return
}
// Create and register outbound adapters
var outboundAdapters []OutboundAdapter
zeroMQOutboundAdapter, err := NewZeroMQOutboundAdapter()
if err != nil {
log.Printf("%v\n", err)
return
}
outboundAdapters = append(outboundAdapters, zeroMQOutboundAdapter)
if cmdFlags.EnableCSV {
writer, err := NewCSVWriter(cmdFlags.CSVPath)
if err != nil {
log.Printf("%v\n", err)
return
}
outboundAdapters = append(outboundAdapters, writer)
}
processors := []Processor{&processor{
outboundAdapters: outboundAdapters,
}}
reader, err := NewLedgerMetadataReader(
&datastoreConfig,
network.PublicNetworkhistoryArchiveURLs,
processors,
cmdFlags.StartLedger,
cmdFlags.EndLedger,
)
if err != nil {
fmt.Println(err)
return
}
log.Printf("ingestion pipeline ended %v\n", reader.Run(ctx))
for _, adapter := range outboundAdapters {
adapter.Close()
}
}