Skip to content

Commit

Permalink
Merge pull request #45 from OpenLNMetrics/dev
Browse files Browse the repository at this point in the history
🍣 Adding logic to report the metric to the server.
  • Loading branch information
vincenzopalazzo committed Oct 21, 2021
2 parents c0fa0cc + 615b79f commit c592ff8
Show file tree
Hide file tree
Showing 14 changed files with 189 additions and 29 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ ARCH=386
ARM=

default: fmt lint
$(CC) build -o $(NAME) cmd/go-metrics-reported/main.go
$(CC) build -o $(NAME) cmd/go-lnmetrics.reporter/main.go

fmt:
$(CC) fmt ./...
Expand All @@ -19,4 +19,4 @@ check:
$(CC) test -v ./...

build:
env GOOS=$(OS) GOARCH=$(ARCH) GOARM=$(ARM) $(CC) build -o $(NAME)-$(OS)-$(ARCH) cmd/go-metrics-reported/main.go
env GOOS=$(OS) GOARCH=$(ARCH) GOARM=$(ARM) $(CC) build -o $(NAME)-$(OS)-$(ARCH) cmd/go-lnmetrics.reporter/main.go
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# go-metrics-reported
# go-lnmetrics.reporter

Reference implementation of C-lightning plugin to collect and report of the lightning node metrics

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
"os"
"strings"

maker "github.com/OpenLNMetrics/go-metrics-reported/init/persistence"
metrics "github.com/OpenLNMetrics/go-metrics-reported/internal/plugin"
"github.com/OpenLNMetrics/go-metrics-reported/pkg/db"
"github.com/OpenLNMetrics/go-metrics-reported/pkg/graphql"
"github.com/OpenLNMetrics/go-metrics-reported/pkg/log"
maker "github.com/OpenLNMetrics/go-lnmetrics.reporter/init/persistence"
metrics "github.com/OpenLNMetrics/go-lnmetrics.reporter/internal/plugin"
"github.com/OpenLNMetrics/go-lnmetrics.reporter/pkg/graphql"
"github.com/OpenLNMetrics/go-lnmetrics.reporter/pkg/log"
"github.com/OpenLNMetrics/lnmetrics.utils/db/leveldb"

sysinfo "github.com/elastic/go-sysinfo"
"github.com/vincenzopalazzo/glightning/glightning"
Expand All @@ -24,6 +24,11 @@ func main() {
metricsPlugin = metrics.MetricsPlugin{Plugin: plugin,
Metrics: make(map[int]metrics.Metric), Rpc: nil}

if err := plugin.RegisterNewOption("lnmetrics-urls", "URLs of remote servers", ""); err != nil {
log.GetInstance().Error(fmt.Sprintf("Error: %s", err))
panic(err)
}

hook := &glightning.Hooks{RpcCommand: OnRpcCommand}
if err := plugin.RegisterHooks(hook); err != nil {
log.GetInstance().Error(fmt.Sprintf("Error: %s", err))
Expand All @@ -36,7 +41,7 @@ func main() {

// To set the time the following doc is followed
// https://pkg.go.dev/github.com/robfig/cron?utm_source=godoc
metricsPlugin.RegisterRecurrentEvt("@every 30m")
metricsPlugin.RegisterRecurrentEvt("@every 10s")

metricsPlugin.Cron.Start()

Expand Down
10 changes: 9 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
module github.com/OpenLNMetrics/go-metrics-reported
module github.com/OpenLNMetrics/go-lnmetrics.reporter

go 1.15

require (
github.com/Khan/genqlient v0.3.0 // indirect
github.com/OpenLNMetrics/lnmetrics.utils v0.0.0-20211018052622-a9fb45a6236b
github.com/agnivade/levenshtein v1.1.1 // indirect
github.com/alexflint/go-scalar v1.1.0 // indirect
github.com/elastic/go-sysinfo v1.7.0
github.com/kinbiko/jsonassert v1.0.1
github.com/niftynei/glightning v0.8.2 // indirect
github.com/robfig/cron/v3 v3.0.1
github.com/sirupsen/logrus v1.8.1
github.com/stretchr/testify v1.7.0 // indirect
github.com/syndtr/goleveldb v1.0.0
github.com/vektah/gqlparser/v2 v2.2.0 // indirect
github.com/vincenzopalazzo/cpstl/go v0.0.0-20210802220150-6acfbf585502 // indirect
github.com/vincenzopalazzo/glightning v0.8.3-0.20211003145319-87ad168ff299
github.com/zcalusic/sysinfo v0.0.0-20210609180555-aff387a52b3a // indirect
golang.org/x/mod v0.5.1 // indirect
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 // indirect
golang.org/x/tools v0.1.7 // indirect
)
104 changes: 104 additions & 0 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion init/persistence/maker_dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"errors"
"os"

log "github.com/OpenLNMetrics/go-metrics-reported/pkg/log"
log "github.com/OpenLNMetrics/go-lnmetrics.reporter/pkg/log"
)

func PrepareHomeDirectory(lightningPath string) (*string, error) {
Expand Down
4 changes: 2 additions & 2 deletions internal/plugin/diagnostic.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"strconv"
"strings"

"github.com/OpenLNMetrics/go-metrics-reported/pkg/db"
"github.com/OpenLNMetrics/go-metrics-reported/pkg/log"
db "github.com/OpenLNMetrics/lnmetrics.utils/db/leveldb"
"github.com/OpenLNMetrics/lnmetrics.utils/log"
)

type DiagnosticRpcMethod struct {
Expand Down
5 changes: 5 additions & 0 deletions internal/plugin/metric_interface.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package plugin

import (
"github.com/OpenLNMetrics/go-lnmetrics.reporter/pkg/graphql"

"github.com/vincenzopalazzo/glightning/glightning"
)

Expand All @@ -21,6 +23,9 @@ type Metric interface {
OnClose(msg *Msg, lightning *glightning.Lightning) error
// Call this method to make the status of the metrics persistent
MakePersistent() error
// Method to store the run a callback to upload the content on the server.
// TODO: Use an interface to generalize the client, it can be also a rest api.
Upload(client *graphql.Client) error
// Call this method when you want update all the metrics without
// some particular event throw from c-lightning
Update(lightning *glightning.Lightning) error
Expand Down
2 changes: 1 addition & 1 deletion internal/plugin/metric_one_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"
"testing"

"github.com/OpenLNMetrics/go-metrics-reported/pkg/db"
"github.com/OpenLNMetrics/lnmetrics.utils/db/leveldb"

sysinfo "github.com/elastic/go-sysinfo"
"github.com/kinbiko/jsonassert"
Expand Down
20 changes: 18 additions & 2 deletions internal/plugin/metrics_one.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import (
"reflect"
"time"

"github.com/OpenLNMetrics/go-metrics-reported/pkg/db"
"github.com/OpenLNMetrics/go-metrics-reported/pkg/log"
"github.com/OpenLNMetrics/go-lnmetrics.reporter/pkg/graphql"
"github.com/OpenLNMetrics/go-lnmetrics.reporter/pkg/log"
"github.com/OpenLNMetrics/lnmetrics.utils/db/leveldb"

sysinfo "github.com/elastic/go-sysinfo/types"
"github.com/vincenzopalazzo/glightning/glightning"
Expand Down Expand Up @@ -370,6 +371,21 @@ func (instance *MetricOne) ToJSON() (string, error) {
return string(json), nil
}

func (instance *MetricOne) Upload(client *graphql.Client) error {
payload, err := instance.ToJSON()
if err != nil {
return err
}
if err := client.UploadMetrics(instance.NodeId, &payload); err != nil {
log.GetInstance().Error(fmt.Sprintf("Error %s: ", err))
return err
}
// Refactored this method in a utils functions
t := time.Now()
log.GetInstance().Info(fmt.Sprintf("Metric One Upload at %s", t.Format(time.RFC850)))
return nil
}

// Make a summary of all the channels information that the node have a channels with.
func (instance *MetricOne) makeChannelsSummary(lightning *glightning.Lightning, channels []*glightning.FundingChannel) (*ChannelsSummary, error) {
channelsSummary := &ChannelsSummary{TotChannels: 0, Summary: make([]*ChannelSummary, 0)}
Expand Down
21 changes: 16 additions & 5 deletions internal/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"github.com/robfig/cron/v3"
"github.com/vincenzopalazzo/glightning/glightning"

"github.com/OpenLNMetrics/go-metrics-reported/pkg/graphql"
"github.com/OpenLNMetrics/go-metrics-reported/pkg/log"
"github.com/OpenLNMetrics/go-lnmetrics.reporter/pkg/graphql"
"github.com/OpenLNMetrics/go-lnmetrics.reporter/pkg/log"
)

type MetricsPlugin struct {
Expand Down Expand Up @@ -75,13 +75,15 @@ func (instance *MetricsPlugin) callUpdateOnMetric(metric Metric, msg *Msg) {
}
}

// 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)
if err != nil {
log.GetInstance().Error(err)
}
}

// Update the metrics without any information received by the caller
func (instance *MetricsPlugin) callUpdateOnMetricNoMsg(metric Metric) {
log.GetInstance().Debug("Calling Update on metrics")
err := metric.Update(instance.Rpc)
Expand All @@ -90,13 +92,22 @@ func (instance *MetricsPlugin) callUpdateOnMetricNoMsg(metric Metric) {
}
}

func (instance *MetricsPlugin) updateAndUploadMetric(metric Metric) {
log.GetInstance().Info("Calling update and upload metric")
instance.callUpdateOnMetricNoMsg(metric)
if err := metric.Upload(instance.Server); err != nil {
log.GetInstance().Error(fmt.Sprintf("Error %s", err))
}
}

// Register internal recurrent methods
func (instance *MetricsPlugin) RegisterRecurrentEvt(after string) {
instance.Cron = cron.New()
// FIXME: Discover what is the fist value
// FIXME: Discover what is the first value
_, err := instance.Cron.AddFunc(after, func() {
log.GetInstance().Debug("Calling recurrent")
log.GetInstance().Info("Update and Uploading metrics")
for _, metric := range instance.Metrics {
go instance.callUpdateOnMetricNoMsg(metric)
go instance.updateAndUploadMetric(metric)
}
})
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/db/database_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"github.com/syndtr/goleveldb/leveldb"

log "github.com/OpenLNMetrics/go-metrics-reported/pkg/log"
log "github.com/OpenLNMetrics/go-lnmetrics.reporter/pkg/log"
)

type database struct {
Expand Down
23 changes: 17 additions & 6 deletions pkg/graphql/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"

"github.com/OpenLNMetrics/go-metrics-reported/pkg/log"
"github.com/OpenLNMetrics/lnmetrics.utils/log"
)

type Client struct {
Expand All @@ -34,15 +35,15 @@ func (instance *Client) MakeRequest(query map[string]string) error {
}

failure := 0
log.GetInstance().Debug(fmt.Sprintf("Push payload on server(s): %s", jsonValue))
for _, url := range instance.BaseUrl {
log.GetInstance().Debug(fmt.Sprintf("Request to URL %s", url))
log.GetInstance().Info(fmt.Sprintf("Request to URL %s", url))
request, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonValue))
if err != nil {
failure++
log.GetInstance().Error(fmt.Sprintf("Error with the message \"%s\" during the request to endpoint %s", err, url))
continue
}
request.Header.Set("Content-Type", "application/json")
response, err := instance.Client.Do(request)
defer func() {
if err := response.Body.Close(); err != nil {
Expand Down Expand Up @@ -77,10 +78,20 @@ func (instance *Client) MakeQuery(payload string) map[string]string {

// This method is a util function to help the node to push the mertics over the servers.
// the payload is a JSON string of the payloads.
func (instance *Client) UploadMetrics(nodeId string, payloads []*string) error {
func (instance *Client) UploadMetrics(nodeId string, body *string) error {
//TODO: generalize this method
payload := fmt.Sprintf("mutation { addNodeMetrics(input: {node_id: %s, payload_metric_one: %s) { node_id }}", nodeId, *payloads[0])
log.GetInstance().Info(fmt.Sprintf("Query payload is: %s", payload))
// mutation {
// addNodeMetrics(input: { node_id: "%s", payload_metric_one: "{}"} ){
// node_id
// }
// }
cleanBody := strings.ReplaceAll(*body, `"`, `\"`)
payload := fmt.Sprintf(`mutation {
addNodeMetrics( input: { node_id: "%s", payload_metric_one: "%s" } ) {
node_id
}
}`, nodeId, cleanBody)
_ = ioutil.WriteFile("/home/vincent/metrics_debug.json", []byte(payload), 0644)
query := instance.MakeQuery(payload)
return instance.MakeRequest(query)
}
2 changes: 1 addition & 1 deletion pkg/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func init() {
if err != nil {
panic(err)
}
instance.Log.SetLevel(logrus.DebugLevel)
instance.Log.SetLevel(logrus.InfoLevel)
file, err := os.OpenFile(dirname+"/metrics.log",
os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err == nil {
Expand Down

0 comments on commit c592ff8

Please sign in to comment.