Skip to content
Snippets Groups Projects
Unverified Commit a31747e6 authored by Ching-Hua (Vivian) Lin's avatar Ching-Hua (Vivian) Lin Committed by GitHub
Browse files

feat: Get instances of singletons via functions instead of global variables (#804)

* feat: Get instances of singletons via functions instead of global variables

* fix: Fix comments
parent 248d503a
No related branches found
No related tags found
No related merge requests found
......@@ -36,7 +36,7 @@ var Core = node.Plugins(
profiling.Plugin(),
database.Plugin(),
autopeering.Plugin(),
pow.Plugin,
pow.Plugin(),
messagelayer.Plugin(),
gossip.Plugin(),
issuer.Plugin(),
......
package pow
import (
"sync"
"github.com/iotaledger/goshimmer/packages/tangle"
"github.com/iotaledger/goshimmer/plugins/messagelayer"
"github.com/iotaledger/hive.go/logger"
......@@ -12,9 +14,18 @@ const PluginName = "PoW"
var (
// Plugin is the plugin instance of the PoW plugin.
Plugin = node.NewPlugin(PluginName, node.Enabled, run)
plugin *node.Plugin
once sync.Once
)
// Plugin gets the plugin instance.
func Plugin() *node.Plugin {
once.Do(func() {
plugin = node.NewPlugin(PluginName, node.Enabled, run)
})
return plugin
}
func run(*node.Plugin) {
// assure that the logger is available
log := logger.NewLogger(PluginName)
......
......@@ -8,9 +8,9 @@ import (
)
var (
infoApp *prometheus.GaugeVec
sync prometheus.Gauge
nodeID string
infoApp *prometheus.GaugeVec
syncStatus prometheus.Gauge
nodeID string
)
func registerInfoMetrics() {
......@@ -26,19 +26,19 @@ func registerInfoMetrics() {
}
infoApp.WithLabelValues(banner.AppName, banner.AppVersion, nodeID).Set(1)
sync = prometheus.NewGauge(prometheus.GaugeOpts{
syncStatus = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "sync",
Help: "Node sync status.",
})
registry.MustRegister(infoApp)
registry.MustRegister(sync)
registry.MustRegister(syncStatus)
addCollect(collectInfoMetrics)
}
func collectInfoMetrics() {
sync.Set(func() float64 {
syncStatus.Set(func() float64 {
if metrics.Synced() {
return 1.0
}
......
......@@ -3,6 +3,7 @@ package prometheus
import (
"context"
"net/http"
"sync"
"time"
"github.com/gin-gonic/gin"
......@@ -16,9 +17,13 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp"
)
// PluginName is the name of the prometheus plugin.
const PluginName = "Prometheus"
// Plugin Prometheus
var (
Plugin = node.NewPlugin("Prometheus", node.Disabled, configure, run)
plugin *node.Plugin
once sync.Once
log *logger.Logger
server *http.Server
......@@ -26,6 +31,14 @@ var (
collects []func()
)
// Plugin gets the plugin instance.
func Plugin() *node.Plugin {
once.Do(func() {
plugin = node.NewPlugin(PluginName, node.Disabled, configure, run)
})
return plugin
}
func configure(plugin *node.Plugin) {
log = logger.NewLogger(plugin.Name)
......
......@@ -16,6 +16,6 @@ var Research = node.Plugins(
analysisserver.Plugin(),
analysisclient.Plugin(),
analysisdashboard.Plugin(),
prometheus.Plugin,
prometheus.Plugin(),
networkdelay.App(),
)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment