Skip to content

Commit f340f7f

Browse files
authored
Merge pull request #1300 from GeorgeTsagk/prometheus-bump-timeout
monitoring: export collector timeout as prometheus config
2 parents 64b91f8 + 65679a0 commit f340f7f

File tree

6 files changed

+30
-9
lines changed

6 files changed

+30
-9
lines changed

monitoring/asset_balances_collector.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func (a *assetBalancesCollector) Collect(ch chan<- prometheus.Metric) {
7272
a.collectMx.Lock()
7373
defer a.collectMx.Unlock()
7474

75-
ctxdb, cancel := context.WithTimeout(context.Background(), dbTimeout)
75+
ctxdb, cancel := context.WithTimeout(context.Background(), promTimeout)
7676
defer cancel()
7777

7878
assets, err := a.cfg.AssetStore.FetchAllAssets(ctxdb, false, false, nil)

monitoring/asset_collector.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func (a *universeStatsCollector) Collect(ch chan<- prometheus.Metric) {
9797
a.collectMx.Lock()
9898
defer a.collectMx.Unlock()
9999

100-
ctx, cancel := context.WithTimeout(context.Background(), dbTimeout)
100+
ctx, cancel := context.WithTimeout(context.Background(), promTimeout)
101101
defer cancel()
102102

103103
universeStats, err := a.cfg.UniverseStats.AggregateSyncStats(ctx)

monitoring/config.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package monitoring
22

33
import (
4+
"time"
5+
46
"github.com/lightninglabs/taproot-assets/tapdb"
57
"github.com/lightninglabs/taproot-assets/tapgarden"
68
"github.com/lightninglabs/taproot-assets/universe"
@@ -10,6 +12,8 @@ import (
1012
// PrometheusConfig is the set of configuration data that specifies if
1113
// Prometheus metric exporting is activated, and if so the listening address of
1214
// the Prometheus server.
15+
//
16+
// nolint: lll
1317
type PrometheusConfig struct {
1418
// Active, if true, then Prometheus metrics will be exported.
1519
Active bool `long:"active" description:"if true prometheus metrics will be exported"`
@@ -18,6 +22,11 @@ type PrometheusConfig struct {
1822
// main Prometheus server to scrape our metrics.
1923
ListenAddr string `long:"listenaddr" description:"the interface we should listen on for prometheus"`
2024

25+
// CollectorRPCTimeout is the context timeout to be used by the RPC
26+
// calls performed during metrics collection. This should not be greater
27+
// than the scrape interval of prometheus.
28+
CollectorRPCTimeout time.Duration `long:"collector-rpc-timeout" description:"the default timeout to be used in the RPC calls performed during metric collection"`
29+
2130
// RPCServer is a pointer to the main RPC server. We use this to export
2231
// generic RPC metrics to monitor the health of the service.
2332
RPCServer *grpc.Server
@@ -45,7 +54,8 @@ type PrometheusConfig struct {
4554
// metrics exporter.
4655
func DefaultPrometheusConfig() PrometheusConfig {
4756
return PrometheusConfig{
48-
ListenAddr: "127.0.0.1:8989",
49-
Active: false,
57+
ListenAddr: "127.0.0.1:8989",
58+
Active: false,
59+
CollectorRPCTimeout: defaultTimeout,
5060
}
5161
}

monitoring/db_collector.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func (a *dbCollector) Collect(ch chan<- prometheus.Metric) {
8484
a.collectMx.Lock()
8585
defer a.collectMx.Unlock()
8686

87-
ctxdb, cancel := context.WithTimeout(context.Background(), dbTimeout)
87+
ctxdb, cancel := context.WithTimeout(context.Background(), promTimeout)
8888
defer cancel()
8989

9090
// Fetch the db size.

monitoring/prometheus.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,18 @@ import (
1212
"github.com/prometheus/client_golang/prometheus/promhttp"
1313
)
1414

15+
const (
16+
// defaultTimeout is the default timeout.
17+
defaultTimeout = 25 * time.Second
18+
)
19+
1520
var (
1621
// serverMetrics is a global variable that holds the Prometheus metrics
1722
// for the gRPC server.
1823
serverMetrics *grpc_prometheus.ServerMetrics
19-
)
2024

21-
const (
22-
// dbTimeout is the default database timeout.
23-
dbTimeout = 20 * time.Second
25+
// promTimeout is the timeout used by the prometheus collectors.
26+
promTimeout time.Duration
2427
)
2528

2629
// PrometheusExporter is a metric exporter that uses Prometheus directly. The
@@ -46,6 +49,11 @@ func (p *PrometheusExporter) Start() error {
4649
return fmt.Errorf("server metrics not set")
4750
}
4851

52+
promTimeout = defaultTimeout
53+
if p.config.CollectorRPCTimeout.Seconds() != 0 {
54+
promTimeout = p.config.CollectorRPCTimeout
55+
}
56+
4957
// Create a custom Prometheus registry.
5058
p.registry = prometheus.NewRegistry()
5159
p.registry.MustRegister(collectors.NewProcessCollector(

sample-tapd.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,9 @@
346346
; The interface we should listen on for prometheus
347347
; prometheus.listenaddr=127.0.0.1:8989
348348

349+
; The default timeout used in prometheus collectors.
350+
; prometheus.collector-rpc-timeout=25s
351+
349352
; Enable additional histogram to track gRPC call processing performance
350353
; (latency, etc)
351354
; prometheus.perfhistograms=false

0 commit comments

Comments
 (0)