diff --git a/aperture.go b/aperture.go index c475c1a..566bbea 100644 --- a/aperture.go +++ b/aperture.go @@ -15,6 +15,9 @@ import ( "sync" "time" + grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus" + "github.com/prometheus/client_golang/prometheus/promhttp" + gateway "github.com/grpc-ecosystem/grpc-gateway/v2/runtime" flags "github.com/jessevdk/go-flags" "github.com/lightninglabs/aperture/auth" @@ -651,6 +654,15 @@ func createProxy(cfg *Config, challenger *LndChallenger, return nil, nil, err } + // Ensure we spin up the necessary HTTP server to allow + // promtheus to scrape us. + go func() { + http.Handle("/metrics", promhttp.Handler()) + fmt.Println(http.ListenAndServe( + cfg.HashMail.PromListenAddr, nil), + ) + }() + localServices = append(localServices, hashMailServices...) proxyCleanup = cleanup } @@ -673,12 +685,26 @@ func createProxy(cfg *Config, challenger *LndChallenger, func createHashMailServer(cfg *Config) ([]proxy.LocalService, func(), error) { var localServices []proxy.LocalService + // Before we register both servers, we'll also ensure that the + // collector will export latency metrics for the histogram. + grpc_prometheus.EnableHandlingTimeHistogram() + + var serverOpts []grpc.ServerOption + serverOpts = []grpc.ServerOption{ + grpc.ChainUnaryInterceptor( + grpc_prometheus.UnaryServerInterceptor, + ), + grpc.ChainStreamInterceptor( + grpc_prometheus.StreamServerInterceptor, + ), + } + // Create a gRPC server for the hashmail server. hashMailServer := newHashMailServer(hashMailServerConfig{ msgRate: cfg.HashMail.MessageRate, msgBurstAllowance: cfg.HashMail.MessageBurstAllowance, }) - hashMailGRPC := grpc.NewServer() + hashMailGRPC := grpc.NewServer(serverOpts...) hashmailrpc.RegisterHashMailServer(hashMailGRPC, hashMailServer) localServices = append(localServices, proxy.NewLocalService( hashMailGRPC, func(r *http.Request) bool { diff --git a/config.go b/config.go index 5dd5de1..b27f9c3 100644 --- a/config.go +++ b/config.go @@ -64,6 +64,10 @@ type HashMailConfig struct { Enabled bool `long:"enabled"` MessageRate time.Duration `long:"messagerate" description:"The average minimum time that should pass between each message."` MessageBurstAllowance int `long:"messageburstallowance" description:"The burst rate we allow for messages."` + + // PromListenAddr is the listening address that we should use to allow + // the main Prometheus server to scrape our metrics. + PromListenAddr string `long:"promlistenaddr" description:"the interface we should listen on for prometheus"` } type TorConfig struct { diff --git a/go.mod b/go.mod index 2c3e38e..50b8aed 100644 --- a/go.mod +++ b/go.mod @@ -9,12 +9,14 @@ require ( github.com/btcsuite/btcwallet/wtxmgr v1.3.1-0.20210706234807-aaf03fee735a github.com/fortytw2/leaktest v1.3.0 github.com/golang/protobuf v1.5.2 + github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 github.com/grpc-ecosystem/grpc-gateway/v2 v2.5.0 github.com/jessevdk/go-flags v1.4.0 github.com/lightninglabs/lightning-node-connect/hashmailrpc v1.0.2 github.com/lightninglabs/lndclient v0.12.0-9 github.com/lightningnetwork/lnd v0.13.0-beta.rc5.0.20210728112744-ebabda671786 github.com/lightningnetwork/lnd/cert v1.0.3 + github.com/prometheus/client_golang v1.11.0 github.com/stretchr/testify v1.7.0 go.etcd.io/etcd/client/v3 v3.5.0 go.etcd.io/etcd/server/v3 v3.5.0