Skip to content

Commit

Permalink
aperture: add basic gRPC prometheus scraping for hashmail server
Browse files Browse the repository at this point in the history
  • Loading branch information
Roasbeef committed Nov 30, 2021
1 parent 28e6b62 commit dd485d9
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
28 changes: 27 additions & 1 deletion aperture.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit dd485d9

Please sign in to comment.