Skip to content

Commit 5574603

Browse files
committed
Refactor PBM metrics collection to utilize current node information
- Updated `pbmBackupsMetrics` and `pbmAgentMetrics` functions to accept `currentNode` as a parameter, reducing redundant calls to retrieve node information. - Enhanced error handling for node info retrieval, ensuring metrics are only collected if the current node information is successfully obtained. - Improved overall structure for clarity and maintainability.
1 parent 708bfa8 commit 5574603

File tree

1 file changed

+11
-16
lines changed

1 file changed

+11
-16
lines changed

exporter/pbm_collector.go

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/prometheus/client_golang/prometheus"
2727
"go.mongodb.org/mongo-driver/mongo"
2828

29+
"github.com/percona/mongodb_exporter/internal/proto"
2930
"github.com/percona/mongodb_exporter/internal/util"
3031
)
3132

@@ -113,8 +114,14 @@ func (p *pbmCollector) collect(ch chan<- prometheus.Metric) {
113114
"PBM PITR backups are enabled for the cluster",
114115
float64(pitrEnabledMetric), nil))
115116

116-
metrics = append(metrics, p.pbmBackupsMetrics(p.ctx, pbmClient, logger)...)
117-
metrics = append(metrics, p.pbmAgentMetrics(p.ctx, pbmClient, logger)...)
117+
// Get current node info once for both agent and backup metrics
118+
currentNode, err := util.MyRole(p.ctx, p.base.client)
119+
if err != nil {
120+
logger.Error("failed to get current node info", "error", err.Error())
121+
} else {
122+
metrics = append(metrics, p.pbmBackupsMetrics(p.ctx, pbmClient, logger, currentNode)...)
123+
metrics = append(metrics, p.pbmAgentMetrics(p.ctx, pbmClient, logger, currentNode)...)
124+
}
118125
}
119126

120127
metrics = append(metrics, createPBMMetric("cluster_backup_configured",
@@ -126,13 +133,7 @@ func (p *pbmCollector) collect(ch chan<- prometheus.Metric) {
126133
}
127134
}
128135

129-
func (p *pbmCollector) pbmAgentMetrics(ctx context.Context, pbmClient *sdk.Client, l *slog.Logger) []prometheus.Metric {
130-
currentNode, err := util.MyRole(ctx, p.base.client)
131-
if err != nil {
132-
l.Error("failed to get current node info", "error", err.Error())
133-
return nil
134-
}
135-
136+
func (p *pbmCollector) pbmAgentMetrics(ctx context.Context, pbmClient *sdk.Client, l *slog.Logger, currentNode *proto.HelloResponse) []prometheus.Metric {
136137
clusterStatus, err := cli.ClusterStatus(ctx, pbmClient, cli.RSConfGetter(p.mongoURI))
137138
if err != nil {
138139
l.Error("failed to get cluster status", "error", err.Error())
@@ -174,13 +175,7 @@ func (p *pbmCollector) pbmAgentMetrics(ctx context.Context, pbmClient *sdk.Clien
174175
return metrics
175176
}
176177

177-
func (p *pbmCollector) pbmBackupsMetrics(ctx context.Context, pbmClient *sdk.Client, l *slog.Logger) []prometheus.Metric {
178-
currentNode, err := util.MyRole(ctx, p.base.client)
179-
if err != nil {
180-
l.Error("failed to get current node info", "error", err.Error())
181-
return nil
182-
}
183-
178+
func (p *pbmCollector) pbmBackupsMetrics(ctx context.Context, pbmClient *sdk.Client, l *slog.Logger, currentNode *proto.HelloResponse) []prometheus.Metric {
184179
backupsList, err := pbmClient.GetAllBackups(ctx)
185180
if err != nil {
186181
l.Error("failed to get PBM backup list", "error", err.Error())

0 commit comments

Comments
 (0)