/
plugin.go
237 lines (204 loc) · 8.76 KB
/
plugin.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
package metrics
import (
"sync"
"time"
"github.com/iotaledger/hive.go/daemon"
"github.com/iotaledger/hive.go/events"
"github.com/iotaledger/hive.go/logger"
"github.com/iotaledger/hive.go/node"
"github.com/iotaledger/hive.go/timeutil"
gossippkg "github.com/iotaledger/goshimmer/packages/gossip"
"github.com/iotaledger/goshimmer/packages/mana"
"github.com/iotaledger/goshimmer/packages/metrics"
"github.com/iotaledger/goshimmer/packages/shutdown"
"github.com/iotaledger/goshimmer/packages/tangle"
"github.com/iotaledger/goshimmer/packages/vote"
"github.com/iotaledger/goshimmer/plugins/analysis/server"
"github.com/iotaledger/goshimmer/plugins/autopeering"
"github.com/iotaledger/goshimmer/plugins/gossip"
"github.com/iotaledger/goshimmer/plugins/messagelayer"
)
// PluginName is the name of the metrics plugin.
const PluginName = "Metrics"
var (
// plugin is the plugin instance of the metrics plugin.
plugin *node.Plugin
once sync.Once
log *logger.Logger
)
// Plugin gets the plugin instance.
func Plugin() *node.Plugin {
once.Do(func() {
plugin = node.NewPlugin(PluginName, node.Enabled, configure, run)
})
return plugin
}
func configure(_ *node.Plugin) {
log = logger.NewLogger(PluginName)
}
func run(_ *node.Plugin) {
log.Infof("Starting %s ...", PluginName)
if Parameters.Local {
// initial measurement, since we have to know how many messages are there in the db
measureInitialDBStats()
registerLocalMetrics()
}
// Events from analysis server
if Parameters.Global {
server.Events.MetricHeartbeat.Attach(onMetricHeartbeatReceived)
}
// create a background worker that update the metrics every second
if err := daemon.BackgroundWorker("Metrics Updater", func(shutdownSignal <-chan struct{}) {
if Parameters.Local {
// Do not block until the Ticker is shutdown because we might want to start multiple Tickers and we can
// safely ignore the last execution when shutting down.
timeutil.NewTicker(func() {
measureCPUUsage()
measureMemUsage()
measureSynced()
measureMessageTips()
measureReceivedMPS()
measureRequestQueueSize()
measureGossipTraffic()
measurePerComponentCounter()
}, 1*time.Second, shutdownSignal)
}
if Parameters.Global {
// Do not block until the Ticker is shutdown because we might want to start multiple Tickers and we can
// safely ignore the last execution when shutting down.
timeutil.NewTicker(calculateNetworkDiameter, 1*time.Minute, shutdownSignal)
}
// Wait before terminating so we get correct log messages from the daemon regarding the shutdown order.
<-shutdownSignal
}, shutdown.PriorityMetrics); err != nil {
log.Panicf("Failed to start as daemon: %s", err)
}
// create a background worker that updates the mana metrics
if err := daemon.BackgroundWorker("Metrics Mana Updater", func(shutdownSignal <-chan struct{}) {
defer log.Infof("Stopping Metrics Mana Updater ... done")
timeutil.NewTicker(func() {
measureMana()
}, time.Second*time.Duration(Parameters.ManaUpdateInterval), shutdownSignal)
// Wait before terminating so we get correct log messages from the daemon regarding the shutdown order.
<-shutdownSignal
log.Infof("Stopping Metrics Mana Updater ...")
}, shutdown.PriorityMetrics); err != nil {
log.Panicf("Failed to start as daemon: %s", err)
}
if Parameters.ManaResearch {
// create a background worker that updates the research mana metrics
if err := daemon.BackgroundWorker("Metrics Research Mana Updater", func(shutdownSignal <-chan struct{}) {
defer log.Infof("Stopping Metrics Research Mana Updater ... done")
timeutil.NewTicker(func() {
measureAccessResearchMana()
measureConsensusResearchMana()
}, time.Second*time.Duration(Parameters.ManaUpdateInterval), shutdownSignal)
// Wait before terminating so we get correct log messages from the daemon regarding the shutdown order.
<-shutdownSignal
log.Infof("Stopping Metrics Research Mana Updater ...")
}, shutdown.PriorityMetrics); err != nil {
log.Panicf("Failed to start as daemon: %s", err)
}
}
}
func registerLocalMetrics() {
//// Events declared in other packages which we want to listen to here ////
// increase received MPS counter whenever we attached a message
messagelayer.Tangle().Storage.Events.MessageStored.Attach(events.NewClosure(func(messageID tangle.MessageID) {
messagelayer.Tangle().Storage.Message(messageID).Consume(func(message *tangle.Message) {
increaseReceivedMPSCounter()
increasePerPayloadCounter(message.Payload().Type())
// MessageStored is triggered in storeMessageWorker that saves the msg to database
messageTotalCountDB.Inc()
})
increasePerComponentCounter(Store)
}))
messagelayer.Tangle().Storage.Events.MessageRemoved.Attach(events.NewClosure(func(messageId tangle.MessageID) {
// MessageRemoved triggered when the message gets removed from database.
messageTotalCountDB.Dec()
}))
// messages can only become solid once, then they stay like that, hence no .Dec() part
messagelayer.Tangle().Solidifier.Events.MessageSolid.Attach(events.NewClosure(func(messageID tangle.MessageID) {
increasePerComponentCounter(Solidifier)
solidTimeMutex.Lock()
defer solidTimeMutex.Unlock()
// Consume should release cachedMessageMetadata
messagelayer.Tangle().Storage.MessageMetadata(messageID).Consume(func(msgMetaData *tangle.MessageMetadata) {
if msgMetaData.IsSolid() {
messageSolidCountDBInc.Inc()
sumSolidificationTime += msgMetaData.SolidificationTime().Sub(msgMetaData.ReceivedTime())
}
})
}))
// fired when a message gets added to missing message storage
messagelayer.Tangle().Solidifier.Events.MessageMissing.Attach(events.NewClosure(func(messageId tangle.MessageID) {
missingMessageCountDB.Inc()
}))
// fired when a missing message was received and removed from missing message storage
messagelayer.Tangle().Storage.Events.MissingMessageStored.Attach(events.NewClosure(func(tangle.MessageID) {
missingMessageCountDB.Dec()
}))
messagelayer.Tangle().Scheduler.Events.MessageScheduled.Attach(events.NewClosure(func(messageID tangle.MessageID) {
increasePerComponentCounter(Scheduler)
}))
messagelayer.Tangle().FIFOScheduler.Events.MessageScheduled.Attach(events.NewClosure(func(messageID tangle.MessageID) {
increasePerComponentCounter(Scheduler)
}))
messagelayer.Tangle().Booker.Events.MessageBooked.Attach(events.NewClosure(func(message tangle.MessageID) {
increasePerComponentCounter(Booker)
}))
// // Value payload attached
// valuetransfers.Tangle().Events.PayloadAttached.Attach(events.NewClosure(func(cachedPayloadEvent *valuetangle.CachedPayloadEvent) {
// cachedPayloadEvent.Payload.Release()
// cachedPayloadEvent.PayloadMetadata.Release()
// valueTransactionCounter.Inc()
// }))
// FPC round executed
messagelayer.Voter().Events().RoundExecuted.Attach(events.NewClosure(func(roundStats *vote.RoundStats) {
processRoundStats(roundStats)
}))
// a conflict has been finalized
messagelayer.Voter().Events().Finalized.Attach(events.NewClosure(func(ev *vote.OpinionEvent) {
processFinalized(ev.Ctx)
}))
// consensus failure in conflict resolution
messagelayer.Voter().Events().Failed.Attach(events.NewClosure(func(ev *vote.OpinionEvent) {
processFailed(ev.Ctx)
}))
//// Events coming from metrics package ////
metrics.Events().FPCInboundBytes.Attach(events.NewClosure(func(amountBytes uint64) {
_FPCInboundBytes.Add(amountBytes)
}))
metrics.Events().FPCOutboundBytes.Attach(events.NewClosure(func(amountBytes uint64) {
_FPCOutboundBytes.Add(amountBytes)
}))
metrics.Events().AnalysisOutboundBytes.Attach(events.NewClosure(func(amountBytes uint64) {
analysisOutboundBytes.Add(amountBytes)
}))
metrics.Events().CPUUsage.Attach(events.NewClosure(func(cpuPercent float64) {
cpuUsage.Store(cpuPercent)
}))
metrics.Events().MemUsage.Attach(events.NewClosure(func(memAllocBytes uint64) {
memUsageBytes.Store(memAllocBytes)
}))
metrics.Events().TangleTimeSynced.Attach(events.NewClosure(func(synced bool) {
isTangleTimeSynced.Store(synced)
}))
gossip.Manager().NeighborsEvents(gossippkg.NeighborsGroupAuto).NeighborRemoved.Attach(onNeighborRemoved)
gossip.Manager().NeighborsEvents(gossippkg.NeighborsGroupAuto).NeighborAdded.Attach(onNeighborAdded)
autopeering.Selection().Events().IncomingPeering.Attach(onAutopeeringSelection)
autopeering.Selection().Events().OutgoingPeering.Attach(onAutopeeringSelection)
metrics.Events().MessageTips.Attach(events.NewClosure(func(tipsCount uint64) {
messageTips.Store(tipsCount)
}))
metrics.Events().QueryReceived.Attach(events.NewClosure(func(ev *metrics.QueryReceivedEvent) {
processQueryReceived(ev)
}))
metrics.Events().QueryReplyError.Attach(events.NewClosure(func(ev *metrics.QueryReplyErrorEvent) {
processQueryReplyError(ev)
}))
// mana pledge events
mana.Events().Pledged.Attach(events.NewClosure(func(ev *mana.PledgedEvent) {
addPledge(ev)
}))
}