Skip to content

Commit 1b81c71

Browse files
committed
Refactor PBM backup metrics collection to streamline node information retrieval
- Removed unnecessary cluster status retrieval and integrated replset iteration directly from backup metadata. - Updated metrics to focus on node-specific details, enhancing clarity and performance. - Improved overall structure for better maintainability.
1 parent 77ca094 commit 1b81c71

File tree

1 file changed

+40
-53
lines changed

1 file changed

+40
-53
lines changed

exporter/pbm_collector.go

Lines changed: 40 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,6 @@ func (p *pbmCollector) pbmBackupsMetrics(ctx context.Context, pbmClient *sdk.Cli
181181
return nil
182182
}
183183

184-
clusterStatus, err := cli.ClusterStatus(ctx, pbmClient, cli.RSConfGetter(p.mongoURI))
185-
if err != nil {
186-
l.Error("failed to get cluster status", "error", err.Error())
187-
return nil
188-
}
189-
190184
backupsList, err := pbmClient.GetAllBackups(ctx)
191185
if err != nil {
192186
l.Error("failed to get PBM backup list", "error", err.Error())
@@ -196,54 +190,47 @@ func (p *pbmCollector) pbmBackupsMetrics(ctx context.Context, pbmClient *sdk.Cli
196190
metrics := make([]prometheus.Metric, 0, len(backupsList))
197191

198192
for _, backup := range backupsList {
199-
// For each backup, iterate through all nodes in the cluster
200-
for replsetName, nodes := range clusterStatus {
201-
for _, node := range nodes {
202-
// Determine role
203-
role := string(node.Role)
204-
205-
// Determine if this is the current node
206-
self := "0"
207-
if node.Host == currentNode.Me {
208-
self = "1"
209-
}
210-
211-
baseLabels := map[string]string{
212-
"opid": backup.OPID,
213-
"status": string(backup.Status),
214-
"name": backup.Name,
215-
"type": string(backup.Type),
216-
"host": node.Host,
217-
"replica_set": replsetName,
218-
"role": role,
219-
"self": self,
220-
}
221-
222-
metrics = append(metrics, createPBMMetric("backup_size_bytes",
223-
"Size of PBM backup",
224-
float64(backup.Size), baseLabels),
225-
)
226-
227-
// Add backup_last_transition_ts metric
228-
metrics = append(metrics, createPBMMetric("backup_last_transition_ts",
229-
"Last transition timestamp of PBM backup (seconds since epoch)",
230-
float64(backup.LastTransitionTS), baseLabels),
231-
)
232-
233-
var endTime int64
234-
switch pbmAgentStatus(backup.Status) {
235-
case statusDone, statusCancelled, statusError, statusDown:
236-
endTime = backup.LastTransitionTS
237-
default:
238-
endTime = time.Now().Unix()
239-
}
240-
241-
duration := time.Unix(endTime-backup.StartTS, 0).Unix()
242-
metrics = append(metrics, createPBMMetric("backup_duration_seconds",
243-
"Duration of PBM backup",
244-
float64(duration), baseLabels),
245-
)
193+
// Iterate through replsets in the backup metadata
194+
for _, replset := range backup.Replsets {
195+
// Determine if this is the current node
196+
self := "0"
197+
if replset.Node == currentNode.Me {
198+
self = "1"
199+
}
200+
201+
labels := map[string]string{
202+
"opid": backup.OPID,
203+
"status": string(backup.Status),
204+
"name": backup.Name,
205+
"host": replset.Node,
206+
"replica_set": replset.Name,
207+
"self": self,
246208
}
209+
210+
metrics = append(metrics, createPBMMetric("backup_size_bytes",
211+
"Size of PBM backup",
212+
float64(backup.Size), labels),
213+
)
214+
215+
// Add backup_last_transition_ts metric
216+
metrics = append(metrics, createPBMMetric("backup_last_transition_ts",
217+
"Last transition timestamp of PBM backup (seconds since epoch)",
218+
float64(backup.LastTransitionTS), labels),
219+
)
220+
221+
var endTime int64
222+
switch pbmAgentStatus(backup.Status) {
223+
case statusDone, statusCancelled, statusError, statusDown:
224+
endTime = backup.LastTransitionTS
225+
default:
226+
endTime = time.Now().Unix()
227+
}
228+
229+
duration := time.Unix(endTime-backup.StartTS, 0).Unix()
230+
metrics = append(metrics, createPBMMetric("backup_duration_seconds",
231+
"Duration of PBM backup",
232+
float64(duration), labels),
233+
)
247234
}
248235
}
249236
return metrics

0 commit comments

Comments
 (0)