Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

monitoring: export collector timeout as prometheus config #1300

Merged
merged 2 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion monitoring/asset_balances_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (a *assetBalancesCollector) Collect(ch chan<- prometheus.Metric) {
a.collectMx.Lock()
defer a.collectMx.Unlock()

ctxdb, cancel := context.WithTimeout(context.Background(), dbTimeout)
ctxdb, cancel := context.WithTimeout(context.Background(), promTimeout)
defer cancel()

assets, err := a.cfg.AssetStore.FetchAllAssets(ctxdb, false, false, nil)
Expand Down
2 changes: 1 addition & 1 deletion monitoring/asset_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (a *universeStatsCollector) Collect(ch chan<- prometheus.Metric) {
a.collectMx.Lock()
defer a.collectMx.Unlock()

ctx, cancel := context.WithTimeout(context.Background(), dbTimeout)
ctx, cancel := context.WithTimeout(context.Background(), promTimeout)
defer cancel()

universeStats, err := a.cfg.UniverseStats.AggregateSyncStats(ctx)
Expand Down
14 changes: 12 additions & 2 deletions monitoring/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package monitoring

import (
"time"

"github.com/lightninglabs/taproot-assets/tapdb"
"github.com/lightninglabs/taproot-assets/tapgarden"
"github.com/lightninglabs/taproot-assets/universe"
Expand All @@ -10,6 +12,8 @@ import (
// PrometheusConfig is the set of configuration data that specifies if
// Prometheus metric exporting is activated, and if so the listening address of
// the Prometheus server.
//
// nolint: lll
type PrometheusConfig struct {
// Active, if true, then Prometheus metrics will be exported.
Active bool `long:"active" description:"if true prometheus metrics will be exported"`
Expand All @@ -18,6 +22,11 @@ type PrometheusConfig struct {
// main Prometheus server to scrape our metrics.
ListenAddr string `long:"listenaddr" description:"the interface we should listen on for prometheus"`

// CollectorRPCTimeout is the context timeout to be used by the RPC
// calls performed during metrics collection. This should not be greater
// than the scrape interval of prometheus.
CollectorRPCTimeout time.Duration `long:"collector-rpc-timeout" description:"the default timeout to be used in the RPC calls performed during metric collection"`

// RPCServer is a pointer to the main RPC server. We use this to export
// generic RPC metrics to monitor the health of the service.
RPCServer *grpc.Server
Expand Down Expand Up @@ -45,7 +54,8 @@ type PrometheusConfig struct {
// metrics exporter.
func DefaultPrometheusConfig() PrometheusConfig {
return PrometheusConfig{
ListenAddr: "127.0.0.1:8989",
Active: false,
ListenAddr: "127.0.0.1:8989",
Active: false,
CollectorRPCTimeout: defaultTimeout,
}
}
2 changes: 1 addition & 1 deletion monitoring/db_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (a *dbCollector) Collect(ch chan<- prometheus.Metric) {
a.collectMx.Lock()
defer a.collectMx.Unlock()

ctxdb, cancel := context.WithTimeout(context.Background(), dbTimeout)
ctxdb, cancel := context.WithTimeout(context.Background(), promTimeout)
defer cancel()

// Fetch the db size.
Expand Down
16 changes: 12 additions & 4 deletions monitoring/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp"
)

const (
// defaultTimeout is the default timeout.
defaultTimeout = 25 * time.Second
)

var (
// serverMetrics is a global variable that holds the Prometheus metrics
// for the gRPC server.
serverMetrics *grpc_prometheus.ServerMetrics
)

const (
// dbTimeout is the default database timeout.
dbTimeout = 20 * time.Second
GeorgeTsagk marked this conversation as resolved.
Show resolved Hide resolved
// promTimeout is the timeout used by the prometheus collectors.
promTimeout time.Duration
GeorgeTsagk marked this conversation as resolved.
Show resolved Hide resolved
)

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

promTimeout = defaultTimeout
if p.config.CollectorRPCTimeout.Seconds() != 0 {
GeorgeTsagk marked this conversation as resolved.
Show resolved Hide resolved
promTimeout = p.config.CollectorRPCTimeout
}

// Create a custom Prometheus registry.
p.registry = prometheus.NewRegistry()
p.registry.MustRegister(collectors.NewProcessCollector(
Expand Down
3 changes: 3 additions & 0 deletions sample-tapd.conf
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,9 @@
; The interface we should listen on for prometheus
; prometheus.listenaddr=127.0.0.1:8989

; The default timeout used in prometheus collectors.
; prometheus.collector-rpc-timeout=25s

; Enable additional histogram to track gRPC call processing performance
; (latency, etc)
; prometheus.perfhistograms=false
Expand Down
Loading