Browse Source

:gear: Expose metrics to API

mudler 3 years ago
parent
commit
a3a4af00a5
5 changed files with 47 additions and 5 deletions
  1. 23 1
      api/api.go
  2. 1 1
      api/api_test.go
  3. 7 1
      cmd/api.go
  4. 9 1
      cmd/main.go
  5. 7 1
      cmd/proxy.go

+ 23 - 1
api/api.go

@@ -23,11 +23,15 @@ import (
 	"net"
 	"net"
 	"net/http"
 	"net/http"
 	_ "net/http/pprof"
 	_ "net/http/pprof"
+	"path/filepath"
 	"strings"
 	"strings"
 	"time"
 	"time"
 
 
+	"github.com/libp2p/go-libp2p-core/metrics"
 	"github.com/libp2p/go-libp2p-core/network"
 	"github.com/libp2p/go-libp2p-core/network"
 	"github.com/libp2p/go-libp2p-core/peer"
 	"github.com/libp2p/go-libp2p-core/peer"
+	p2pprotocol "github.com/libp2p/go-libp2p-core/protocol"
+
 	"github.com/miekg/dns"
 	"github.com/miekg/dns"
 	apiTypes "github.com/mudler/edgevpn/api/types"
 	apiTypes "github.com/mudler/edgevpn/api/types"
 
 
@@ -60,10 +64,11 @@ const (
 	FileURL       = "/api/files"
 	FileURL       = "/api/files"
 	NodesURL      = "/api/nodes"
 	NodesURL      = "/api/nodes"
 	DNSURL        = "/api/dns"
 	DNSURL        = "/api/dns"
+	MetricsURL    = "/api/metrics"
 	PeerstoreURL  = "/api/peerstore"
 	PeerstoreURL  = "/api/peerstore"
 )
 )
 
 
-func API(ctx context.Context, l string, defaultInterval, timeout time.Duration, e *node.Node, debugMode bool) error {
+func API(ctx context.Context, l string, defaultInterval, timeout time.Duration, e *node.Node, bwc metrics.Reporter, debugMode bool) error {
 
 
 	ledger, _ := e.Ledger()
 	ledger, _ := e.Ledger()
 
 
@@ -82,6 +87,23 @@ func API(ctx context.Context, l string, defaultInterval, timeout time.Duration,
 		ec.GET("/debug/pprof/*", echo.WrapHandler(http.DefaultServeMux))
 		ec.GET("/debug/pprof/*", echo.WrapHandler(http.DefaultServeMux))
 	}
 	}
 
 
+	if bwc != nil {
+		ec.GET(MetricsURL, func(c echo.Context) error {
+			return c.JSON(http.StatusOK, bwc.GetBandwidthTotals())
+		})
+		ec.GET(filepath.Join(MetricsURL, "protocol"), func(c echo.Context) error {
+			return c.JSON(http.StatusOK, bwc.GetBandwidthByProtocol())
+		})
+		ec.GET(filepath.Join(MetricsURL, "peer"), func(c echo.Context) error {
+			return c.JSON(http.StatusOK, bwc.GetBandwidthByPeer())
+		})
+		ec.GET(filepath.Join(MetricsURL, "peer", ":peer"), func(c echo.Context) error {
+			return c.JSON(http.StatusOK, bwc.GetBandwidthForPeer(peer.ID(c.Param("peer"))))
+		})
+		ec.GET(filepath.Join(MetricsURL, "protocol", ":protocol"), func(c echo.Context) error {
+			return c.JSON(http.StatusOK, bwc.GetBandwidthForProtocol(p2pprotocol.ID(c.Param("protocol"))))
+		})
+	}
 	// Get data from ledger
 	// Get data from ledger
 	ec.GET(FileURL, func(c echo.Context) error {
 	ec.GET(FileURL, func(c echo.Context) error {
 		list := []*types.File{}
 		list := []*types.File{}

+ 1 - 1
api/api_test.go

@@ -54,7 +54,7 @@ var _ = Describe("API", func() {
 			e.Start(ctx)
 			e.Start(ctx)
 
 
 			go func() {
 			go func() {
-				err := API(ctx, fmt.Sprintf("unix://%s", socket), 10*time.Second, 20*time.Second, e, false)
+				err := API(ctx, fmt.Sprintf("unix://%s", socket), 10*time.Second, 20*time.Second, e, nil, false)
 				Expect(err).ToNot(HaveOccurred())
 				Expect(err).ToNot(HaveOccurred())
 			}()
 			}()
 
 

+ 7 - 1
cmd/api.go

@@ -19,6 +19,8 @@ import (
 	"context"
 	"context"
 	"time"
 	"time"
 
 
+	"github.com/libp2p/go-libp2p"
+	"github.com/libp2p/go-libp2p-core/metrics"
 	"github.com/mudler/edgevpn/api"
 	"github.com/mudler/edgevpn/api"
 	"github.com/mudler/edgevpn/pkg/node"
 	"github.com/mudler/edgevpn/pkg/node"
 	"github.com/urfave/cli"
 	"github.com/urfave/cli"
@@ -43,6 +45,10 @@ A simple UI interface is available to display network data.`,
 		),
 		),
 		Action: func(c *cli.Context) error {
 		Action: func(c *cli.Context) error {
 			o, _, ll := cliToOpts(c)
 			o, _, ll := cliToOpts(c)
+
+			bwc := metrics.NewBandwidthCounter()
+			o = append(o, node.WithLibp2pAdditionalOptions(libp2p.BandwidthReporter(bwc)))
+
 			e, err := node.New(o...)
 			e, err := node.New(o...)
 			if err != nil {
 			if err != nil {
 				return err
 				return err
@@ -56,7 +62,7 @@ A simple UI interface is available to display network data.`,
 				return err
 				return err
 			}
 			}
 
 
-			return api.API(ctx, c.String("listen"), 5*time.Second, 20*time.Second, e, c.Bool("debug"))
+			return api.API(ctx, c.String("listen"), 5*time.Second, 20*time.Second, e, bwc, c.Bool("debug"))
 		},
 		},
 	}
 	}
 }
 }

+ 9 - 1
cmd/main.go

@@ -23,9 +23,12 @@ import (
 	"path/filepath"
 	"path/filepath"
 	"time"
 	"time"
 
 
+	"github.com/libp2p/go-libp2p"
+	"github.com/libp2p/go-libp2p-core/metrics"
 	"github.com/libp2p/go-libp2p-core/network"
 	"github.com/libp2p/go-libp2p-core/network"
 
 
 	"github.com/mudler/edgevpn/api"
 	"github.com/mudler/edgevpn/api"
+	"github.com/mudler/edgevpn/pkg/node"
 	edgevpn "github.com/mudler/edgevpn/pkg/node"
 	edgevpn "github.com/mudler/edgevpn/pkg/node"
 	"github.com/mudler/edgevpn/pkg/services"
 	"github.com/mudler/edgevpn/pkg/services"
 	"github.com/mudler/edgevpn/pkg/vpn"
 	"github.com/mudler/edgevpn/pkg/vpn"
@@ -208,6 +211,11 @@ func Main() func(c *cli.Context) error {
 				)...)
 				)...)
 		}
 		}
 
 
+		bwc := metrics.NewBandwidthCounter()
+		if c.Bool("api") {
+			o = append(o, node.WithLibp2pAdditionalOptions(libp2p.BandwidthReporter(bwc)))
+		}
+
 		opts, err := vpn.Register(vpnOpts...)
 		opts, err := vpn.Register(vpnOpts...)
 		if err != nil {
 		if err != nil {
 			return err
 			return err
@@ -227,7 +235,7 @@ func Main() func(c *cli.Context) error {
 		}
 		}
 
 
 		if c.Bool("api") {
 		if c.Bool("api") {
-			go api.API(ctx, c.String("api-listen"), 5*time.Second, 20*time.Second, e, c.Bool("debug"))
+			go api.API(ctx, c.String("api-listen"), 5*time.Second, 20*time.Second, e, bwc, c.Bool("debug"))
 		}
 		}
 
 
 		return e.Start(ctx)
 		return e.Start(ctx)

+ 7 - 1
cmd/proxy.go

@@ -19,6 +19,8 @@ import (
 	"context"
 	"context"
 	"time"
 	"time"
 
 
+	"github.com/libp2p/go-libp2p"
+	"github.com/libp2p/go-libp2p-core/metrics"
 	"github.com/mudler/edgevpn/api"
 	"github.com/mudler/edgevpn/api"
 	"github.com/mudler/edgevpn/pkg/node"
 	"github.com/mudler/edgevpn/pkg/node"
 	"github.com/mudler/edgevpn/pkg/services"
 	"github.com/mudler/edgevpn/pkg/services"
@@ -61,6 +63,10 @@ func Proxy() cli.Command {
 				time.Duration(c.Int("interval"))*time.Second,
 				time.Duration(c.Int("interval"))*time.Second,
 				time.Duration(c.Int("dead-interval"))*time.Second,
 				time.Duration(c.Int("dead-interval"))*time.Second,
 				c.String("listen"))...)
 				c.String("listen"))...)
+
+			bwc := metrics.NewBandwidthCounter()
+			o = append(o, node.WithLibp2pAdditionalOptions(libp2p.BandwidthReporter(bwc)))
+
 			e, err := node.New(o...)
 			e, err := node.New(o...)
 			if err != nil {
 			if err != nil {
 				return err
 				return err
@@ -74,7 +80,7 @@ func Proxy() cli.Command {
 				return err
 				return err
 			}
 			}
 
 
-			return api.API(ctx, c.String("listen"), 5*time.Second, 20*time.Second, e, c.Bool("debug"))
+			return api.API(ctx, c.String("listen"), 5*time.Second, 20*time.Second, e, bwc, c.Bool("debug"))
 		},
 		},
 	}
 	}
 }
 }