Skip to content

Commit efad183

Browse files
authored
Merge pull request lightninglabs#61 from Roasbeef/hashmail-prom
aperture: add basic gRPC prometheus scraping for hashmail server
2 parents 28e6b62 + dd485d9 commit efad183

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

aperture.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ import (
1515
"sync"
1616
"time"
1717

18+
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
19+
"github.com/prometheus/client_golang/prometheus/promhttp"
20+
1821
gateway "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
1922
flags "github.com/jessevdk/go-flags"
2023
"github.com/lightninglabs/aperture/auth"
@@ -651,6 +654,15 @@ func createProxy(cfg *Config, challenger *LndChallenger,
651654
return nil, nil, err
652655
}
653656

657+
// Ensure we spin up the necessary HTTP server to allow
658+
// promtheus to scrape us.
659+
go func() {
660+
http.Handle("/metrics", promhttp.Handler())
661+
fmt.Println(http.ListenAndServe(
662+
cfg.HashMail.PromListenAddr, nil),
663+
)
664+
}()
665+
654666
localServices = append(localServices, hashMailServices...)
655667
proxyCleanup = cleanup
656668
}
@@ -673,12 +685,26 @@ func createProxy(cfg *Config, challenger *LndChallenger,
673685
func createHashMailServer(cfg *Config) ([]proxy.LocalService, func(), error) {
674686
var localServices []proxy.LocalService
675687

688+
// Before we register both servers, we'll also ensure that the
689+
// collector will export latency metrics for the histogram.
690+
grpc_prometheus.EnableHandlingTimeHistogram()
691+
692+
var serverOpts []grpc.ServerOption
693+
serverOpts = []grpc.ServerOption{
694+
grpc.ChainUnaryInterceptor(
695+
grpc_prometheus.UnaryServerInterceptor,
696+
),
697+
grpc.ChainStreamInterceptor(
698+
grpc_prometheus.StreamServerInterceptor,
699+
),
700+
}
701+
676702
// Create a gRPC server for the hashmail server.
677703
hashMailServer := newHashMailServer(hashMailServerConfig{
678704
msgRate: cfg.HashMail.MessageRate,
679705
msgBurstAllowance: cfg.HashMail.MessageBurstAllowance,
680706
})
681-
hashMailGRPC := grpc.NewServer()
707+
hashMailGRPC := grpc.NewServer(serverOpts...)
682708
hashmailrpc.RegisterHashMailServer(hashMailGRPC, hashMailServer)
683709
localServices = append(localServices, proxy.NewLocalService(
684710
hashMailGRPC, func(r *http.Request) bool {

config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ type HashMailConfig struct {
6464
Enabled bool `long:"enabled"`
6565
MessageRate time.Duration `long:"messagerate" description:"The average minimum time that should pass between each message."`
6666
MessageBurstAllowance int `long:"messageburstallowance" description:"The burst rate we allow for messages."`
67+
68+
// PromListenAddr is the listening address that we should use to allow
69+
// the main Prometheus server to scrape our metrics.
70+
PromListenAddr string `long:"promlistenaddr" description:"the interface we should listen on for prometheus"`
6771
}
6872

6973
type TorConfig struct {

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ require (
99
github.com/btcsuite/btcwallet/wtxmgr v1.3.1-0.20210706234807-aaf03fee735a
1010
github.com/fortytw2/leaktest v1.3.0
1111
github.com/golang/protobuf v1.5.2
12+
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
1213
github.com/grpc-ecosystem/grpc-gateway/v2 v2.5.0
1314
github.com/jessevdk/go-flags v1.4.0
1415
github.com/lightninglabs/lightning-node-connect/hashmailrpc v1.0.2
1516
github.com/lightninglabs/lndclient v0.12.0-9
1617
github.com/lightningnetwork/lnd v0.13.0-beta.rc5.0.20210728112744-ebabda671786
1718
github.com/lightningnetwork/lnd/cert v1.0.3
19+
github.com/prometheus/client_golang v1.11.0
1820
github.com/stretchr/testify v1.7.0
1921
go.etcd.io/etcd/client/v3 v3.5.0
2022
go.etcd.io/etcd/server/v3 v3.5.0

0 commit comments

Comments
 (0)