-
Notifications
You must be signed in to change notification settings - Fork 5
/
metrics_one.go
284 lines (249 loc) · 8.76 KB
/
metrics_one.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package plugin
import (
"encoding/json"
"fmt"
"time"
"github.com/OpenLNMetrics/go-metrics-reported/pkg/db"
"github.com/OpenLNMetrics/go-metrics-reported/pkg/log"
"github.com/niftynei/glightning/glightning"
)
// Information about the Payment forward by the node
type PaymentInfo struct {
// the payment is received by the channel or is sent to the channel
Direction string `json:"direction"`
// the status of the channels is completed, failed or pending
Status string `json:"status"`
}
// Only a wrapper to pass collected information about the channel
type ChannelInfo struct {
NodeId string
Alias string
Color string
Direction string
Forwards []*PaymentInfo
}
// Wrap all useful information about the own node
type status struct {
//node_id string `json:node_id`
Event string `json:"event"`
// how many channels the node have
Channels int `json:"channels"`
// how many payments it forwords
Forwords int `json:"forwords"`
// unix time where the check is made.
Timestamp int64 `json:"timestamp"`
}
// Wrap all the information about the node that the own node
// has some channel open.
type statusChannel struct {
// node id
NodeId string `json:"node_id"`
// label of the node
NodeAlias string `json:"node_alias"`
// the capacity of the channel
Capacity uint64 `json:"capacity"`
// how payment the channel forwords
Forwords []*PaymentInfo `json:"forwords"`
// The node answer from the ping operation
Timestamp int64 `json:"timestamp"`
// the node is ready to receive payment to share?
Online bool `json:"online"`
// last message (channel_update) received from the gossip
LastUpdate uint `json:"last_update"`
// the channel is public?
Public bool `json:"public"`
// information about the direction of the channel: out, in, mutual.
Direction string `json:"direction"`
// Status of the channel
Status string `json:"status"`
// Color of the node
Color string `json:"color"`
}
type MetricOne struct {
// Internal id to identify the metric
id int `json:"-"`
// Name of the metrics
Name string `json:"metric_name"`
NodeId string `json:"node_id"`
Color string `json:"color"`
// architecture of the system
Architecture string `json:"architecture"`
// array of the up_time
UpTime []*status `json:"up_time"`
// array of channel information
ChannelsInfo []*statusChannel `json:"channels_info"`
}
// FIXME: Move this in a separate file like a generic metrics file
var MetricsSupported map[int]string
// FIXME: Move this in a utils file to give the possibility also to other metrics to access to it
// 0 = outcoming
// 1 = incoming
// 2 = mutual.
var ChannelDirections map[int]string
func init() {
log.GetInstance().Debug("Init metrics map with all the name")
MetricsSupported = make(map[int]string)
MetricsSupported[1] = "metric_one"
ChannelDirections = make(map[int]string)
ChannelDirections[0] = "OUTCOMING"
ChannelDirections[1] = "INCOOMING"
ChannelDirections[2] = "MUTUAL"
}
// This method is required by the
func NewMetricOne(nodeId string, architecture string) *MetricOne {
return &MetricOne{id: 1, Name: MetricsSupported[1], NodeId: nodeId,
Architecture: architecture, UpTime: make([]*status, 0),
ChannelsInfo: make([]*statusChannel, 0), Color: ""}
}
func (instance *MetricOne) OnInit(lightning *glightning.Lightning) error {
getInfo, err := lightning.GetInfo()
if err != nil {
log.GetInstance().Error(fmt.Sprintf("Error during the OnInit method; %s", err))
return err
}
instance.NodeId = getInfo.Id
instance.Color = getInfo.Color
log.GetInstance().Debug("Init event on metrics on called")
listFunds, err := lightning.ListFunds()
log.GetInstance().Debug(fmt.Sprintf("%s", listFunds))
if err != nil {
log.GetInstance().Error(fmt.Sprintf("Error: %s", err))
return err
}
instance.collectInfoChannels(lightning, listFunds.Channels)
listForwards, err := lightning.ListForwards()
if err != nil {
log.GetInstance().Error(fmt.Sprintf("Error: %s", err))
return err
}
instance.UpTime = append(instance.UpTime,
&status{Event: "on_start",
Timestamp: time.Now().Unix(),
Channels: len(listFunds.Channels),
Forwords: len(listForwards)})
return instance.MakePersistent()
return nil
}
func (instance *MetricOne) Update(lightning *glightning.Lightning) error {
log.GetInstance().Debug("Update event on metrics on called")
listFunds, err := lightning.ListFunds()
log.GetInstance().Debug(fmt.Sprintf("%s", listFunds))
if err != nil {
log.GetInstance().Error(fmt.Sprintf("Error: %s", err))
return err
}
instance.collectInfoChannels(lightning, listFunds.Channels)
listForwards, err := lightning.ListForwards()
if err != nil {
log.GetInstance().Error(fmt.Sprintf("Error: %s", err))
return err
}
instance.UpTime = append(instance.UpTime,
&status{Event: "on_update",
Timestamp: time.Now().Unix(),
Channels: len(listFunds.Channels),
Forwords: len(listForwards)})
return instance.MakePersistent()
}
func (metric *MetricOne) UpdateWithMsg(message *Msg,
lightning *glightning.Lightning) error {
return nil
}
func (instance *MetricOne) MakePersistent() error {
log.GetInstance().Debug(fmt.Sprintf("%s", instance))
json, err := instance.ToJSON()
if err != nil {
log.GetInstance().Error(fmt.Sprintf("JSON error %s", err))
return err
}
return db.GetInstance().PutValue(instance.Name, json)
}
// here the message is not useful, but we keep it only for future evolution
// or we will remove it from here.
func (instance *MetricOne) OnClose(msg *Msg, lightning *glightning.Lightning) error {
log.GetInstance().Debug("On close event on metrics called")
lastValue := 0
sizeForwords := 0
if len(instance.UpTime) > 0 {
lastValue = instance.UpTime[len(instance.UpTime)-1].Channels
sizeForwords = instance.UpTime[len(instance.UpTime)-1].Forwords
}
instance.UpTime = append(instance.UpTime,
&status{Event: "on_close",
Timestamp: time.Now().Unix(),
Channels: lastValue, Forwords: sizeForwords})
return instance.MakePersistent()
}
func (instance *MetricOne) ToJSON() (string, error) {
json, err := json.Marshal(&instance)
if err != nil {
log.GetInstance().Error(err)
return "", err
}
return string(json), nil
}
// private method of the module
func (instance *MetricOne) collectInfoChannels(lightning *glightning.Lightning, channels []*glightning.FundingChannel) error {
for _, channel := range channels {
err := instance.collectInfoChannel(lightning, channel)
if err != nil {
// void returning error here? We can continue to make the analysis over the channels
log.GetInstance().Error(fmt.Sprintf("Error: %s", err))
return err
}
err = instance.collectInfoChannel(lightning, channel)
if err != nil {
log.GetInstance().Error(fmt.Sprintf("Error: %s", err))
return nil
}
}
return nil
}
func (instance *MetricOne) collectInfoChannel(lightning *glightning.Lightning, channel *glightning.FundingChannel) error {
var timestamp int64 = 0
if instance.pingNode(lightning, channel.Id) {
timestamp = time.Now().Unix()
}
info, err := instance.getChannelInfo(lightning, channel)
if err != nil {
log.GetInstance().Error(fmt.Sprintf("Error during get the information about the channel: %s", err))
return err
}
// TODO: Could be good to have a information about the direction of the channel
infoChannel := statusChannel{NodeId: info.NodeId, NodeAlias: info.Alias, Color: info.Color,
Capacity: channel.ChannelSatoshi, Forwords: info.Forwards,
Timestamp: timestamp, Online: channel.Connected,
Direction: info.Direction, Status: channel.State}
instance.ChannelsInfo = append(instance.ChannelsInfo, &infoChannel)
return nil
}
func (instance *MetricOne) pingNode(lightning *glightning.Lightning, nodeId string) bool {
_, err := lightning.Ping(nodeId)
if err != nil {
log.GetInstance().Error(fmt.Sprintf("Error during pinging node: %s", err))
return false
}
return true
}
func (instance *MetricOne) getChannelInfo(lightning *glightning.Lightning, channel *glightning.FundingChannel) (*ChannelInfo, error) {
nodeInfo, err := lightning.GetNode(channel.Id)
if err != nil {
log.GetInstance().Error(fmt.Sprintf("Error during the call listNodes: %s", err))
return nil, err
}
channelInfo := ChannelInfo{NodeId: channel.Id, Alias: nodeInfo.Alias, Color: nodeInfo.Color, Direction: "unknown"}
listForwards, err := lightning.ListForwards()
if err != nil {
log.GetInstance().Error(fmt.Sprintf("Error during the listForwards call: %s", err))
return nil, err
}
for _, forward := range listForwards {
if channel.ShortChannelId == forward.InChannel {
channelInfo.Forwards = append(channelInfo.Forwards, &PaymentInfo{Direction: ChannelDirections[1], Status: forward.Status})
} else if channel.ShortChannelId == forward.OutChannel {
channelInfo.Forwards = append(channelInfo.Forwards, &PaymentInfo{Direction: ChannelDirections[0], Status: forward.Status})
}
}
//TODO Adding support for the dual founding channels.
return &channelInfo, nil
}