Skip to content

Commit

Permalink
Merge pull request #42 from aakselrod/prometheus
Browse files Browse the repository at this point in the history
Initial prometheus metrics support
  • Loading branch information
aakselrod-nydig authored Jul 27, 2022
2 parents 3a28d23 + 0ad70db commit 9378f50
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
9 changes: 8 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"
DefaultInstrumentationAddress = "localhost:2112"
)

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

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

// InstrumentationAddress is the network address that Prometheus
// and eventually pprof and/or other instrumentation listen on.
InstrumentationAddress string `yaml:"instrumentationAddress"`
}

func (c *Config) GetIdentityKey() ([32]byte, error) {
Expand Down
45 changes: 43 additions & 2 deletions 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 @@ -115,9 +118,13 @@ func runAction(c *cli.Context) error {
return err
}

// Instantiate grpc server.
grpcServer := grpc.NewServer()
// Instantiate grpc server and enable reflection and Prometheus metrics.
grpcServer := grpc.NewServer(
grpc.StreamInterceptor(grpc_prometheus.StreamServerInterceptor),
grpc.UnaryInterceptor(grpc_prometheus.UnaryServerInterceptor),
)
reflection.Register(grpcServer)
grpc_prometheus.Register(grpcServer)

server := newServer(creator, registry, settledHandler)

Expand All @@ -135,6 +142,24 @@ func runAction(c *cli.Context) error {
return err
}

instAddress := cfg.InstrumentationAddress
if instAddress == "" {
instAddress = DefaultInstrumentationAddress
}

// Instantiate a new HTTP server and mux.
instMux := http.NewServeMux()
instMux.Handle("/metrics", promhttp.Handler())

instServer := &http.Server{
Addr: instAddress,
Handler: instMux,

// Even though this server should only be exposed to trusted
// clients, this mitigates slowloris-like DoS attacks.
ReadHeaderTimeout: 10 * time.Second,
}

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

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

group.Go(func() error {
log.Infow("Instrumentation HTTP server starting",
"instrumentationAddress", instAddress)

return instServer.ListenAndServe()
})

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

// Stop instrumentation server
log.Infow("Instrumentation server stopping")

return instServer.Close()
})

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

0 comments on commit 9378f50

Please sign in to comment.