Skip to content

Commit

Permalink
rpc: adding new rpc command lnmetrics-cache
Browse files Browse the repository at this point in the history
Changelog-Added: adding new rpc command `lnmetrics-cache`

Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
  • Loading branch information
vincenzopalazzo committed Apr 1, 2022
1 parent e2cd456 commit 27aac0f
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 28 deletions.
Binary file removed go-lnmetrics-linux-amd64
Binary file not shown.
2 changes: 1 addition & 1 deletion internal/plugin/diagnostic.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type MetricOneRpcMethod struct {
plugin *MetricsPlugin `json:"-"`
}

func (rpc *MetricOneRpcMethod) Name() string {
func (instance *MetricOneRpcMethod) Name() string {
return "metric_one"
}

Expand Down
49 changes: 28 additions & 21 deletions internal/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,72 +54,79 @@ 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 {
log.GetInstance().Error(fmt.Sprintf("Error during registering recurrent event: %s", err))
}
}

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 {
Expand All @@ -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 {
Expand All @@ -141,7 +148,7 @@ func (instance *MetricsPlugin) RegisterOneTimeEvt(after string) {
log.GetInstance().Error(fmt.Sprintf("Error: %s", err))
}

}(instance, metric)
}(plugin, metric)
}
})
}
48 changes: 42 additions & 6 deletions internal/plugin/plugin_rpc.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand All @@ -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
Expand All @@ -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
}
Expand All @@ -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,
Expand All @@ -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
}

0 comments on commit 27aac0f

Please sign in to comment.