Skip to content

Commit

Permalink
plugin: improve info report and rename rpc method into lnmetrics-info
Browse files Browse the repository at this point in the history
Fixes #68

Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
  • Loading branch information
vincenzopalazzo committed Dec 4, 2021
1 parent 0b0a968 commit cd246e4
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ After running the plugin you will have the possibility to run the following rpc
- `metric_one start end`: RPC command that give you the possibility to query the internal db and make access to the metric data collected by the plugin. The start and end need to be a string that is the timestamp or you can use the following query to make some particular query:
- `metric_one start="now"`: Give you the possibility to query the metric data that the plugin have in memory;
- `metric_one start="last"`: Give you the possibility to query the metric data that the plugin committed to the server last time.
- `lnmetrics-reporter`: RPC command that give you access to the plugin information, like version, go version and architecture this will be useful when there is some bug
- `lnmetrics-info`: RPC command that give you access to the plugin information, like version, go version and architecture this will be useful when there is some bug
report or just consult the version of the plugin that the user is running.

## How to Contribute
Expand Down
3 changes: 2 additions & 1 deletion cmd/go-lnmetrics.reporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ func onInit(plugin *glightning.Plugin,
log.GetInstance().Error(fmt.Sprintf("Error received %s", err))
panic(err)
}

// FIXME: After on init event c-lightning should be ready to accept request
// from any plugin.
metricsPlugin.RegisterOneTimeEvt("10s")
}

Expand Down
3 changes: 3 additions & 0 deletions internal/db/db_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,7 @@ type PluginDatabase interface {

// Erase database and all the data are lost forever
EraseDatabase() error

// Get location of the DB
GetDBPath() string
}
11 changes: 11 additions & 0 deletions internal/db/leveldb_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,22 @@ import (
)

type LevelDB struct {

// Global database version to know basically
// how the key are stored.
dbVersion int

// Global Key in the database to get the data model version
dbKeyDb string

// dictionary with the a diction that contains a mapping
// to get the correct version by the following info:
// - metric_name
// - dbVersion
metricsDbKeys map[string]string

// Database path
path string
}

func NewLevelDB(path string) (PluginDatabase, error) {
Expand Down Expand Up @@ -51,6 +57,7 @@ func NewLevelDB(path string) (PluginDatabase, error) {
return &LevelDB{
dbVersion: dataVersionConv,
dbKeyDb: dbKey,
path: strings.Join([]string{path, "db"}, "/"),
metricsDbKeys: map[string]string{
"metric_one/1": "metric_one",
"metric_one/2": "metric_one",
Expand All @@ -75,6 +82,10 @@ func (instance *LevelDB) IsReady() bool {
return db.GetInstance().Ready()
}

func (instance *LevelDB) GetDBPath() string {
return instance.path
}

func (instance *LevelDB) StoreMetricOneSnapshot(timestamp int, payload *string) error {
key := strings.Join([]string{"metric_one", fmt.Sprint(timestamp)}, "/")
if err := instance.PutValue(key, payload); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (plugin *MetricsPlugin) RegisterMethods() error {
return err
}

infoMethod := NewPluginRpcMethod()
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\"\""
Expand Down
35 changes: 28 additions & 7 deletions internal/plugin/plugin_rpc.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package plugin

import (
sysinfo "github.com/elastic/go-sysinfo"
"github.com/vincenzopalazzo/glightning/jrpc2"
)

type PluginRpcMethod struct{}
type PluginRpcMethod struct {
metricsPlugin *MetricsPlugin `json:"-"`
}

// 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
Expand All @@ -14,20 +17,38 @@ type info struct {
Version string
LangVersion string
Architecture string
MaxProcs int
StoragePath string
Metrics []string
}

func (instance PluginRpcMethod) Name() string {
return "lnmetrics-reporter"
return "lnmetrics-info"
}

func NewPluginRpcMethod() *PluginRpcMethod {
return &PluginRpcMethod{}
func NewPluginRpcMethod(pluginMetrics *MetricsPlugin) *PluginRpcMethod {
return &PluginRpcMethod{
metricsPlugin: pluginMetrics,
}
}

func (instance PluginRpcMethod) New() interface{} {
return NewPluginRpcMethod()
func (instance *PluginRpcMethod) New() interface{} {
return instance
}

func (instance *PluginRpcMethod) Call() (jrpc2.Result, error) {
return info{Name: "go-lnmetrics-reporter", Version: "0.1", LangVersion: "Go lang 1.15.8", Architecture: "amd64"}, nil
metricsSupp := make([]string, 0)
for key := range instance.metricsPlugin.Metrics {
metricsSupp = append(metricsSupp, MetricsSupported[key])
}
goInfo := sysinfo.Go()
return info{
Name: "go-lnmetrics.reporter",
Version: "v0.0.4-rc4",
LangVersion: goInfo.Version,
Architecture: goInfo.Arch,
MaxProcs: goInfo.MaxProcs,
StoragePath: instance.metricsPlugin.Storage.GetDBPath(),
Metrics: metricsSupp,
}, nil
}

0 comments on commit cd246e4

Please sign in to comment.