/
plugin.go
181 lines (157 loc) · 6.17 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
// Package remotemetrics is a plugin that enables log metrics too complex for Prometheus, but still interesting in terms of analysis and debugging.
// It is enabled by default.
// The destination can be set via logger.remotelog.serverAddress.
package remotemetrics
import (
"context"
"time"
"github.com/iotaledger/hive.go/core/autopeering/peer"
"github.com/iotaledger/hive.go/core/generics/event"
"github.com/iotaledger/hive.go/core/types"
"github.com/iotaledger/goshimmer/packages/core/ledger"
"github.com/iotaledger/goshimmer/packages/core/ledger/utxo"
"github.com/iotaledger/goshimmer/packages/app/remotemetrics"
"github.com/iotaledger/goshimmer/packages/node/shutdown"
"github.com/iotaledger/goshimmer/plugins/remotelog"
"github.com/iotaledger/hive.go/core/daemon"
"github.com/iotaledger/hive.go/core/node"
"github.com/iotaledger/hive.go/core/timeutil"
"go.uber.org/dig"
"github.com/iotaledger/goshimmer/packages/core/conflictdag"
"github.com/iotaledger/goshimmer/packages/core/tangleold"
)
const (
syncUpdateTime = 500 * time.Millisecond
schedulerQueryUpdateTime = 5 * time.Second
)
const (
// Debug defines the most verbose metrics collection level.
Debug uint8 = iota
// Info defines regular metrics collection level.
Info
// Important defines the level of collection of only most important metrics.
Important
// Critical defines the level of collection of only critical metrics.
Critical
)
var (
// Plugin is the plugin instance of the remote plugin instance.
Plugin *node.Plugin
deps = new(dependencies)
)
type dependencies struct {
dig.In
Local *peer.Local
Tangle *tangleold.Tangle
RemoteLogger *remotelog.RemoteLoggerConn `optional:"true"`
ClockPlugin *node.Plugin `name:"clock" optional:"true"`
}
func init() {
Plugin = node.NewPlugin("RemoteLogMetrics", deps, node.Enabled, configure, run)
}
func configure(_ *node.Plugin) {
// if remotelog plugin is disabled, then remotemetrics should not be started either
if node.IsSkipped(remotelog.Plugin) {
Plugin.LogInfof("%s is disabled; skipping %s\n", remotelog.Plugin.Name, Plugin.Name)
return
}
measureInitialConflictCounts()
configureSyncMetrics()
configureConflictConfirmationMetrics()
configureBlockFinalizedMetrics()
configureBlockScheduledMetrics()
configureMissingBlockMetrics()
configureSchedulerQueryMetrics()
}
func run(_ *node.Plugin) {
// if remotelog plugin is disabled, then remotemetrics should not be started either
if node.IsSkipped(remotelog.Plugin) {
return
}
// create a background worker that update the metrics every second
if err := daemon.BackgroundWorker("Node State Logger Updater", func(ctx context.Context) {
// 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() { checkSynced() }, syncUpdateTime, ctx)
timeutil.NewTicker(func() { remotemetrics.Events.SchedulerQuery.Trigger(&remotemetrics.SchedulerQueryEvent{time.Now()}) }, schedulerQueryUpdateTime, ctx)
// Wait before terminating so we get correct log blocks from the daemon regarding the shutdown order.
<-ctx.Done()
}, shutdown.PriorityRemoteLog); err != nil {
Plugin.Panicf("Failed to start as daemon: %s", err)
}
}
func configureSyncMetrics() {
if Parameters.MetricsLevel > Info {
return
}
remotemetrics.Events.TangleTimeSyncChanged.Attach(event.NewClosure(func(event *remotemetrics.TangleTimeSyncChangedEvent) {
isTangleTimeSynced.Store(event.CurrentStatus)
}))
remotemetrics.Events.TangleTimeSyncChanged.Attach(event.NewClosure(func(event *remotemetrics.TangleTimeSyncChangedEvent) {
sendSyncStatusChangedEvent(event)
}))
}
func configureSchedulerQueryMetrics() {
if Parameters.MetricsLevel > Info {
return
}
remotemetrics.Events.SchedulerQuery.Attach(event.NewClosure(func(event *remotemetrics.SchedulerQueryEvent) { obtainSchedulerStats(event.Time) }))
}
func configureConflictConfirmationMetrics() {
if Parameters.MetricsLevel > Info {
return
}
deps.Tangle.Ledger.ConflictDAG.Events.ConflictAccepted.Attach(event.NewClosure(func(event *conflictdag.ConflictAcceptedEvent[utxo.TransactionID]) {
onConflictConfirmed(event.ID)
}))
deps.Tangle.Ledger.ConflictDAG.Events.ConflictCreated.Attach(event.NewClosure(func(event *conflictdag.ConflictCreatedEvent[utxo.TransactionID, utxo.OutputID]) {
activeConflictsMutex.Lock()
defer activeConflictsMutex.Unlock()
conflictID := event.ID
if _, exists := activeConflicts[conflictID]; !exists {
conflictTotalCountDB.Inc()
activeConflicts[conflictID] = types.Void
sendConflictMetrics()
}
}))
}
func configureBlockFinalizedMetrics() {
if Parameters.MetricsLevel > Info {
return
} else if Parameters.MetricsLevel == Info {
deps.Tangle.Ledger.Events.TransactionAccepted.Attach(event.NewClosure(func(event *ledger.TransactionAcceptedEvent) {
onTransactionConfirmed(event.TransactionID)
}))
} else {
deps.Tangle.ConfirmationOracle.Events().BlockAccepted.Attach(event.NewClosure(func(event *tangleold.BlockAcceptedEvent) {
onBlockFinalized(event.Block)
}))
}
}
func configureBlockScheduledMetrics() {
if Parameters.MetricsLevel > Info {
return
} else if Parameters.MetricsLevel == Info {
deps.Tangle.Scheduler.Events.BlockDiscarded.Attach(event.NewClosure(func(event *tangleold.BlockDiscardedEvent) {
sendBlockSchedulerRecord(event.BlockID, "blockDiscarded")
}))
} else {
deps.Tangle.Scheduler.Events.BlockScheduled.Attach(event.NewClosure(func(event *tangleold.BlockScheduledEvent) {
sendBlockSchedulerRecord(event.BlockID, "blockScheduled")
}))
deps.Tangle.Scheduler.Events.BlockDiscarded.Attach(event.NewClosure(func(event *tangleold.BlockDiscardedEvent) {
sendBlockSchedulerRecord(event.BlockID, "blockDiscarded")
}))
}
}
func configureMissingBlockMetrics() {
if Parameters.MetricsLevel > Info {
return
}
deps.Tangle.Solidifier.Events.BlockMissing.Attach(event.NewClosure(func(event *tangleold.BlockMissingEvent) {
sendMissingBlockRecord(event.BlockID, "missingBlock")
}))
deps.Tangle.Storage.Events.MissingBlockStored.Attach(event.NewClosure(func(event *tangleold.MissingBlockStoredEvent) {
sendMissingBlockRecord(event.BlockID, "missingBlockStored")
}))
}