/
plugin.go
160 lines (133 loc) · 4.1 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
// Package remotelog is a plugin that enables log blocks being sent via UDP to a central ELK stack for debugging.
// It is disabled by default and when enabled, additionally, logger.disableEvents=false in config.json needs to be set.
// The destination can be set via logger.remotelog.serverAddress.
// All events according to logger.level in config.json are sent.
package remotelog
import (
"context"
"os"
"path/filepath"
"runtime"
"time"
"go.uber.org/dig"
"gopkg.in/src-d/go-git.v4"
"github.com/iotaledger/hive.go/core/autopeering/peer"
"github.com/iotaledger/hive.go/core/daemon"
"github.com/iotaledger/hive.go/core/generics/event"
"github.com/iotaledger/hive.go/core/logger"
"github.com/iotaledger/hive.go/core/node"
"github.com/iotaledger/hive.go/core/workerpool"
"github.com/iotaledger/goshimmer/packages/node/shutdown"
logger_plugin "github.com/iotaledger/goshimmer/plugins/logger"
)
const (
// PluginName is the name of the remote log plugin.
PluginName = "RemoteLog"
remoteLogType = "log"
levelIndex = 0
nameIndex = 1
blockIndex = 2
)
var (
// Plugin is the plugin instance of the remote plugin instance.
Plugin *node.Plugin
deps = new(dependencies)
myID string
myGitHead string
myGitConflict string
workerPool *workerpool.NonBlockingQueuedWorkerPool
)
type dependencies struct {
dig.In
Local *peer.Local
RemoteLogger *RemoteLoggerConn
}
func init() {
Plugin = node.NewPlugin(PluginName, deps, node.Enabled, configure, run)
Plugin.Events.Init.Hook(event.NewClosure(func(event *node.InitEvent) {
if err := event.Container.Provide(func() *RemoteLoggerConn {
remoteLogger, err := newRemoteLoggerConn(Parameters.RemoteLog.ServerAddress)
if err != nil {
Plugin.LogFatalAndExit(err)
return nil
}
return remoteLogger
}); err != nil {
Plugin.Panic(err)
}
}))
}
func configure(_ *node.Plugin) {
if logger_plugin.Parameters.DisableEvents {
return
}
if deps.Local != nil {
myID = deps.Local.ID().String()
}
getGitInfo()
workerPool = workerpool.NewNonBlockingQueuedWorkerPool(func(task workerpool.Task) {
deps.RemoteLogger.SendLogMsg(task.Param(levelIndex).(logger.Level), task.Param(nameIndex).(string), task.Param(blockIndex).(string))
task.Return(nil)
}, workerpool.WorkerCount(runtime.GOMAXPROCS(0)), workerpool.QueueSize(1000))
}
func run(plugin *node.Plugin) {
if logger_plugin.Parameters.DisableEvents {
return
}
logEvent := event.NewClosure(func(event *logger.LogEvent) {
workerPool.TrySubmit(event.Level, event.Name, event.Msg)
})
if err := daemon.BackgroundWorker(PluginName, func(ctx context.Context) {
logger.Events.AnyMsg.Attach(logEvent)
<-ctx.Done()
plugin.LogInfof("Stopping %s ...", PluginName)
logger.Events.AnyMsg.Detach(logEvent)
workerPool.Stop()
plugin.LogInfof("Stopping %s ... done", PluginName)
}, shutdown.PriorityRemoteLog); err != nil {
plugin.Panicf("Failed to start as daemon: %s", err)
}
}
func getGitInfo() {
r, err := git.PlainOpen(getGitDir())
if err != nil {
Plugin.LogDebug("Could not open Git repo.")
return
}
// extract git conflict and head
if h, err := r.Head(); err == nil {
myGitConflict = h.Name().String()
myGitHead = h.Hash().String()
}
}
func getGitDir() string {
var gitDir string
// this is valid when running an executable, when using "go run" this is a temp path
if ex, err := os.Executable(); err == nil {
temp := filepath.Join(filepath.Dir(ex), ".git")
if _, err := os.Stat(temp); err == nil {
gitDir = temp
}
}
// when running "go run" from the same directory
if gitDir == "" {
if wd, err := os.Getwd(); err == nil {
temp := filepath.Join(wd, ".git")
if _, err := os.Stat(temp); err == nil {
gitDir = temp
}
}
}
return gitDir
}
type logBlock struct {
Version string `json:"version"`
GitHead string `json:"gitHead,omitempty"`
GitConflict string `json:"gitConflict,omitempty"`
NodeID string `json:"nodeId"`
Level string `json:"level"`
Name string `json:"name"`
Msg string `json:"msg"`
Timestamp time.Time `json:"timestamp"`
Type string `json:"type"`
}