Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial prometheus metrics support #42

Merged
merged 1 commit into from
Jul 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -106,9 +109,13 @@ func runAction(c *cli.Context) error {
return err
}

// Instantiate grpc server.
grpcServer := grpc.NewServer()
// Instantiate grpc server and enable reflection and Prometheus metrics.
joostjager marked this conversation as resolved.
Show resolved Hide resolved
grpcServer := grpc.NewServer(
grpc.StreamInterceptor(grpc_prometheus.StreamServerInterceptor),
grpc.UnaryInterceptor(grpc_prometheus.UnaryServerInterceptor),
)
reflection.Register(grpcServer)
grpc_prometheus.Register(grpcServer)
joostjager marked this conversation as resolved.
Show resolved Hide resolved

server := newServer(creator, registry)

Expand All @@ -126,6 +133,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,
joostjager marked this conversation as resolved.
Show resolved Hide resolved
}

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

// Run multiplexer.
Expand Down Expand Up @@ -159,6 +184,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