diff --git a/go-lnmetrics-linux-amd64 b/go-lnmetrics-linux-amd64 deleted file mode 100755 index bbc4fd0..0000000 Binary files a/go-lnmetrics-linux-amd64 and /dev/null differ diff --git a/internal/plugin/diagnostic.go b/internal/plugin/diagnostic.go index 63964dc..0314d88 100644 --- a/internal/plugin/diagnostic.go +++ b/internal/plugin/diagnostic.go @@ -15,7 +15,7 @@ type MetricOneRpcMethod struct { plugin *MetricsPlugin `json:"-"` } -func (rpc *MetricOneRpcMethod) Name() string { +func (instance *MetricOneRpcMethod) Name() string { return "metric_one" } diff --git a/internal/plugin/plugin.go b/internal/plugin/plugin.go index 410302d..4c30a6c 100644 --- a/internal/plugin/plugin.go +++ b/internal/plugin/plugin.go @@ -54,64 +54,71 @@ func (plugin *MetricsPlugin) RegisterMetrics(id int, metric Metric) error { func (plugin *MetricsPlugin) RegisterMethods() error { method := NewMetricPlugin(plugin) rpcMethod := glightning.NewRpcMethod(method, "Show diagnostic node") - rpcMethod.LongDesc = "Show the diagnostic data of the lightning network node" - rpcMethod.Category = "metrics" + rpcMethod.LongDesc = "Show the metric one data of the lightning network node. An example metric_one start=last" + rpcMethod.Category = "lnmetrics" if err := plugin.Plugin.RegisterMethod(rpcMethod); err != nil { return err } infoMethod := NewPluginRpcMethod(plugin) infoRpcMethod := glightning.NewRpcMethod(infoMethod, "Show go-lnmetrics.reporter info") - infoRpcMethod.Category = "metrics" - infoRpcMethod.LongDesc = "Return a map where the key is the id of the method and the value is the payload of the metric. The metrics_id is a string that conatins the id divided by a comma. An example is \"diagnostic \"1,2,3\"\"" + infoRpcMethod.Category = "lnmetrics" + infoRpcMethod.LongDesc = "Return the info od the env where the plugin is running. An example is \"lnmetrics-info" if err := plugin.Plugin.RegisterMethod(infoRpcMethod); err != nil { return err } + cacheMethod := NewCleanCacheRPC(plugin) + cacheRPCMethod := glightning.NewRpcMethod(cacheMethod, "Clean all the lnmetrics cache") + cacheRPCMethod.Category = "lnmetrics" + cacheRPCMethod.LongDesc = "Clean the cache made by the plugin during the time. An example is \"lnmetrics-cache clean" + if err := plugin.Plugin.RegisterMethod(cacheRPCMethod); err != nil { + return err + } return nil } //nolint -func (instance *MetricsPlugin) callUpdateOnMetric(metric Metric, msg *Msg) { - if err := metric.UpdateWithMsg(msg, instance.Rpc); err != nil { +func (plugin *MetricsPlugin) callUpdateOnMetric(metric Metric, msg *Msg) { + if err := metric.UpdateWithMsg(msg, plugin.Rpc); err != nil { log.GetInstance().Error(fmt.Sprintf("Error during update metrics event: %s", err)) } } // Call on stop operation on the node when the caller are shoutdown it self. -func (instance *MetricsPlugin) callOnStopOnMetrics(metric Metric, msg *Msg) { - err := metric.OnClose(msg, instance.Rpc) +func (plugin *MetricsPlugin) callOnStopOnMetrics(metric Metric, msg *Msg) { + err := metric.OnStop(msg, plugin.Rpc) if err != nil { log.GetInstance().Error(err) } } // Update the metrics without any information received by the caller -func (instance *MetricsPlugin) callUpdateOnMetricNoMsg(metric Metric) { +func (plugin *MetricsPlugin) callUpdateOnMetricNoMsg(metric Metric) { log.GetInstance().Debug("Calling Update on metrics") - err := metric.Update(instance.Rpc) + err := metric.Update(plugin.Rpc) if err != nil { log.GetInstance().Error(fmt.Sprintf("Error %s", err)) } } -func (instance *MetricsPlugin) updateAndUploadMetric(metric Metric) { +func (plugin *MetricsPlugin) updateAndUploadMetric(metric Metric) { log.GetInstance().Info("Calling update and upload metric") - instance.callUpdateOnMetricNoMsg(metric) - if err := metric.UploadOnRepo(instance.Server, instance.Rpc); err != nil { + plugin.callUpdateOnMetricNoMsg(metric) + if err := metric.UploadOnRepo(plugin.Server, plugin.Rpc); err != nil { log.GetInstance().Error(fmt.Sprintf("Error %s", err)) } } // Register internal recurrent methods -func (instance *MetricsPlugin) RegisterRecurrentEvt(after string) { +func (plugin *MetricsPlugin) RegisterRecurrentEvt(after string) { log.GetInstance().Info(fmt.Sprintf("Register recurrent event each %s", after)) - instance.Cron = cron.New() + plugin.Cron = cron.New() // FIXME: Discover what is the first value - _, err := instance.Cron.AddFunc(after, func() { + _, err := plugin.Cron.AddFunc(after, func() { log.GetInstance().Info("Update and Uploading metrics") - for _, metric := range instance.Metrics { - go instance.updateAndUploadMetric(metric) + for _, metric := range plugin.Metrics { + go plugin.updateAndUploadMetric(metric) } }) if err != nil { @@ -119,7 +126,7 @@ func (instance *MetricsPlugin) RegisterRecurrentEvt(after string) { } } -func (instance *MetricsPlugin) RegisterOneTimeEvt(after string) { +func (plugin *MetricsPlugin) RegisterOneTimeEvt(after string) { log.GetInstance().Info(fmt.Sprintf("Register one time event after %s", after)) duration, err := time.ParseDuration(after) if err != nil { @@ -129,7 +136,7 @@ func (instance *MetricsPlugin) RegisterOneTimeEvt(after string) { time.AfterFunc(duration, func() { log.GetInstance().Debug("Calling on time function function") // TODO: Should C-Lightning send a on init event like notification? - for _, metric := range instance.Metrics { + for _, metric := range plugin.Metrics { go func(instance *MetricsPlugin, metric Metric) { err := metric.OnInit(instance.Rpc) if err != nil { @@ -141,7 +148,7 @@ func (instance *MetricsPlugin) RegisterOneTimeEvt(after string) { log.GetInstance().Error(fmt.Sprintf("Error: %s", err)) } - }(instance, metric) + }(plugin, metric) } }) } diff --git a/internal/plugin/plugin_rpc.go b/internal/plugin/plugin_rpc.go index 52323b7..ac570aa 100644 --- a/internal/plugin/plugin_rpc.go +++ b/internal/plugin/plugin_rpc.go @@ -1,6 +1,9 @@ package plugin import ( + "fmt" + "github.com/LNOpenMetrics/go-lnmetrics.reporter/internal/cache" + "github.com/LNOpenMetrics/lnmetrics.utils/log" sysinfo "github.com/elastic/go-sysinfo" "github.com/vincenzopalazzo/glightning/jrpc2" ) @@ -10,7 +13,7 @@ type PluginRpcMethod struct { } // Would be cool if it is possible auto-generate a structure from a -// file, so this will be inside the binary and we can avoid the hard coded +// file, so this will be inside the binary, and we can avoid the hard coded // file. type info struct { Name string @@ -23,16 +26,16 @@ type info struct { ProxyEnabled bool } -func (instance PluginRpcMethod) Name() string { - return "lnmetrics-info" -} - func NewPluginRpcMethod(pluginMetrics *MetricsPlugin) *PluginRpcMethod { return &PluginRpcMethod{ metricsPlugin: pluginMetrics, } } +func (instance PluginRpcMethod) Name() string { + return "lnmetrics-info" +} + func (instance *PluginRpcMethod) New() interface{} { return instance } @@ -45,7 +48,7 @@ func (instance *PluginRpcMethod) Call() (jrpc2.Result, error) { goInfo := sysinfo.Go() return info{ Name: "go-lnmetrics.reporter", - Version: "v0.0.4-rc7", + Version: "v0.0.5-rc1", LangVersion: goInfo.Version, Architecture: goInfo.Arch, MaxProcs: goInfo.MaxProcs, @@ -54,3 +57,36 @@ func (instance *PluginRpcMethod) Call() (jrpc2.Result, error) { ProxyEnabled: instance.metricsPlugin.WithProxy, }, nil } + +// CleanCacheRPC RPC call to clean up the plugin cache. +type CleanCacheRPC struct { + Cmd string +} + +func NewCleanCacheRPC(plugin *MetricsPlugin) *CleanCacheRPC { + return &CleanCacheRPC{} +} + +func (instance *CleanCacheRPC) New() interface{} { + return instance +} + +func (instance CleanCacheRPC) Name() string { + return "lnmetrics-cache" +} + +func (instance *CleanCacheRPC) Call() (jrpc2.Result, error) { + + if instance.Cmd != "clean" { + return nil, fmt.Errorf("command %s node found", instance.Cmd) + } + + if err := cache.GetInstance().CleanCache(); err != nil { + log.GetInstance().Errorf("CleanCacheRPC rpc call return the following error during the %s cmd: %s", instance.Cmd, err) + return nil, fmt.Errorf("clean cache operation return the following error: %s", err) + } + response := struct { + Result string `json:"result"` + }{Result: "cleaning operation succeeded"} + return response, nil +}