Skip to content

Commit 3f36447

Browse files
authored
Merge pull request lightninglabs#69 from ellemouton/mailboxMetrics
multi: mailbox metrics
2 parents 5d26f3f + 3284211 commit 3f36447

File tree

5 files changed

+77
-14
lines changed

5 files changed

+77
-14
lines changed

aperture.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
"github.com/lightningnetwork/lnd/lnrpc"
2929
"github.com/lightningnetwork/lnd/signal"
3030
"github.com/lightningnetwork/lnd/tor"
31-
"github.com/prometheus/client_golang/prometheus/promhttp"
3231
clientv3 "go.etcd.io/etcd/client/v3"
3332
"golang.org/x/crypto/acme/autocert"
3433
"golang.org/x/net/http2"
@@ -181,6 +180,12 @@ func NewAperture(cfg *Config) *Aperture {
181180
func (a *Aperture) Start(errChan chan error) error {
182181
var err error
183182

183+
// Start the prometheus exporter.
184+
if err := StartPrometheusExporter(a.cfg.Prometheus); err != nil {
185+
return fmt.Errorf("unable to start the prometheus exporter: "+
186+
"%v", err)
187+
}
188+
184189
// Initialize our etcd client.
185190
a.etcdClient, err = clientv3.New(clientv3.Config{
186191
Endpoints: []string{a.cfg.Etcd.Host},
@@ -654,15 +659,6 @@ func createProxy(cfg *Config, challenger *LndChallenger,
654659
return nil, nil, err
655660
}
656661

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-
666662
localServices = append(localServices, hashMailServices...)
667663
proxyCleanup = cleanup
668664
}
@@ -714,6 +710,9 @@ func createHashMailServer(cfg *Config) ([]proxy.LocalService, func(), error) {
714710
}),
715711
)
716712

713+
// Export the gRPC information for the public gRPC server.
714+
grpc_prometheus.Register(hashMailGRPC)
715+
717716
// And a REST proxy for it as well.
718717
// The default JSON marshaler of the REST proxy only sets OrigName to
719718
// true, which instructs it to use the same field names as specified in

config.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,6 @@ 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"`
7167
}
7268

7369
type TorConfig struct {
@@ -116,6 +112,10 @@ type Config struct {
116112
// Node Connect mailbox server.
117113
HashMail *HashMailConfig `group:"hashmail" namespace:"hashmail" description:"Configuration for the Lightning Node Connect mailbox server."`
118114

115+
// Prometheus is the config for setting up an endpoint for a Prometheus
116+
// server to scrape metrics from.
117+
Prometheus *PrometheusConfig `group:"prometheus" namespace:"prometheus" description:"Configuration setting up an endpoint that a Prometheus server can scrape."`
118+
119119
// DebugLevel is a string defining the log level for the service either
120120
// for all subsystems the same or individual level by subsystem.
121121
DebugLevel string `long:"debuglevel" description:"Debug level for the Aperture application and its subsystems."`

hashmail_server.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,8 @@ func (h *hashMailServer) InitStream(
406406

407407
h.streams[streamID] = freshStream
408408

409+
mailboxCount.Set(float64(len(h.streams)))
410+
409411
return &hashmailrpc.CipherInitResp{
410412
Resp: &hashmailrpc.CipherInitResp_Success{},
411413
}, nil
@@ -478,6 +480,8 @@ func (h *hashMailServer) TearDownStream(ctx context.Context, streamID []byte,
478480

479481
delete(h.streams, sid)
480482

483+
mailboxCount.Set(float64(len(h.streams)))
484+
481485
return nil
482486
}
483487

prometheus.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package aperture
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
7+
"github.com/prometheus/client_golang/prometheus"
8+
"github.com/prometheus/client_golang/prometheus/promhttp"
9+
)
10+
11+
var (
12+
// mailboxCount tracks the current number of active mailboxes.
13+
mailboxCount = prometheus.NewGauge(prometheus.GaugeOpts{
14+
Namespace: "hashmail",
15+
Name: "mailbox_count",
16+
})
17+
)
18+
19+
// PrometheusConfig is the set of configuration data that specifies if
20+
// Prometheus metric exporting is activated, and if so the listening address of
21+
// the Prometheus server.
22+
type PrometheusConfig struct {
23+
// Enabled, if true, then Prometheus metrics will be exported.
24+
Enabled bool `long:"enabled" description:"if true prometheus metrics will be exported"`
25+
26+
// ListenAddr is the listening address that we should use to allow the
27+
// main Prometheus server to scrape our metrics.
28+
ListenAddr string `long:"listenaddr" description:"the interface we should listen on for prometheus"`
29+
}
30+
31+
// StartPrometheusExporter registers all relevant metrics with the Prometheus
32+
// library, then launches the HTTP server that Prometheus will hit to scrape
33+
// our metrics.
34+
func StartPrometheusExporter(cfg *PrometheusConfig) error {
35+
// If we're not active, then there's nothing more to do.
36+
if !cfg.Enabled {
37+
return nil
38+
}
39+
40+
// Next, we'll register all our metrics.
41+
prometheus.MustRegister(mailboxCount)
42+
43+
// Finally, we'll launch the HTTP server that Prometheus will use to
44+
// scape our metrics.
45+
go func() {
46+
log.Infof("Prometheus metrics http endpoint being served on "+
47+
"%s", cfg.ListenAddr)
48+
49+
http.Handle("/metrics", promhttp.Handler())
50+
fmt.Println(http.ListenAndServe(cfg.ListenAddr, nil))
51+
}()
52+
53+
return nil
54+
}

sample-conf.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,9 @@ hashmail:
151151
enabled: true
152152
messagerate: 20ms
153153
messageburstallowance: 1000
154+
155+
# Enable the prometheus metrics exporter so that a prometheus server can scrape
156+
# the metrics.
157+
prometheus:
158+
enabled: true
159+
listenaddr: "localhost:9000"

0 commit comments

Comments
 (0)