Skip to content

Commit

Permalink
initial prometheus metrics support
Browse files Browse the repository at this point in the history
  • Loading branch information
aakselrod committed Jul 26, 2022
1 parent 1d601ba commit 81e58c0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
8 changes: 7 additions & 1 deletion cmd/lnmuxd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import (
"gopkg.in/yaml.v2"
)

var DefaultListenAddress = "localhost:19090"
var (
DefaultListenAddress = "localhost:19090"
DefaultPrometheusAddress = "localhost:2112"
)

type Config struct {
// Lnd contains the configuration of the nodes.
Expand All @@ -27,6 +30,9 @@ type Config struct {

// ListenAddress is the network address that we listen on.
ListenAddress string `yaml:"listenAddress"`

// PrometheusAddress is the network address that Prometheus listens on.
PrometheusAddress string `yaml:"prometheusAddress"`
}

func (c *Config) GetIdentityKey() ([32]byte, error) {
Expand Down
33 changes: 32 additions & 1 deletion cmd/lnmuxd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"net"
"net/http"
"os"
"os/signal"
"syscall"
Expand All @@ -17,8 +18,10 @@ import (
"github.com/bottlepay/lnmux/persistence"
"github.com/btcsuite/btcd/chaincfg"
"github.com/go-pg/pg/v10"
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
"github.com/lightningnetwork/lnd/clock"
"github.com/lightningnetwork/lnd/keychain"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/urfave/cli/v2"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc"
Expand Down Expand Up @@ -106,9 +109,10 @@ func runAction(c *cli.Context) error {
return err
}

// Instantiate grpc server.
// Instantiate grpc server and enable reflection and Prometheus metrics.
grpcServer := grpc.NewServer()
reflection.Register(grpcServer)
grpc_prometheus.Register(grpcServer)

server := newServer(creator, registry)

Expand All @@ -126,6 +130,18 @@ func runAction(c *cli.Context) error {
return err
}

promAddress := cfg.PrometheusAddress
if promAddress == "" {
promAddress = DefaultPrometheusAddress
}

// Instantiate a new HTTP server and mux.
metrics := &http.Server{
Addr: promAddress,
Handler: promhttp.Handler(),
ReadHeaderTimeout: 10 * time.Second,
}

group, ctx := errgroup.WithContext(context.Background())

// Run multiplexer.
Expand Down Expand Up @@ -159,6 +175,21 @@ func runAction(c *cli.Context) error {
return nil
})

group.Go(func() error {
log.Infow("Prometheus server starting", "promAddress", promAddress)

return metrics.ListenAndServe()
})

group.Go(func() error {
<-ctx.Done()

// Stop Prometheus server
log.Infow("Prometheus server stopping")

return metrics.Close()
})

// Run ctrl-c handler.
group.Go(func() error {
log.Infof("Press ctrl-c to exit")
Expand Down

0 comments on commit 81e58c0

Please sign in to comment.