Skip to content

Conversation

@hangliuc
Copy link

@hangliuc hangliuc commented Jan 8, 2026

Fixes #36

  • 解决aws route 53一条解析有多个记录时,会使用重复的record_id,导致Prometheus采集报错
  • 如果发生label 一致 Prometheus会丢弃指标,采集时增加防御逻辑

Summary by CodeRabbit

发布说明

  • Bug Fixes
    • 增强了导出与采集流程中同一批次内的重复记录检测,重复条目将被记录并跳过以防止重复上报
    • 优化了记录 ID 的分配时机,由聚合级别调整为逐条资源级别分配,提升记录一致性与准确性

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link

coderabbitai bot commented Jan 8, 2026

Walkthrough

在Metrics.Collect中为同一批次添加了基于RecordID的去重守护(seenIDs),并在amazon提供者的ListRecords中将RecordID赋值移动到每个资源记录的循环内,防止同名标签导致Prometheus重复采集错误。

Changes

Cohort / File(s) 变更摘要
重复指标守护与RecordID赋值调整
pkg/export/export_gauge.go, pkg/provider/amazon.go
pkg/export/export_gauge.go的Metrics.Collect中引入seenIDs映射以对同批次的RecordID进行去重;在pkg/provider/amazon.go的ListRecords中将RecordID赋值从顶层移至每个ResourceRecord循环内,确保每个资源记录具有独立RecordID。

Sequence Diagram(s)

(已跳过:本次变更为错误修复/改进,未生成序列图)

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Suggested labels

bug

Poem

🐰 轻步蹦跳来庆贺,
重复记录终删它,
RecordID各自到位,
指标清晰又美好,
Prometheus 微笑啦!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed 标题清晰准确地描述了主要改动:修复AWS Route53 RecordID重复导致的Prometheus指标报错问题。
Linked Issues check ✅ Passed 代码改动完整地解决了问题#36的要求:AWS环境下多个ResourceRecords复用同一RecordID导致Prometheus指标冲突。
Out of Scope Changes check ✅ Passed 所有改动均与修复RecordID重复问题直接相关,无超范围的修改。
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

📜 Recent review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e4e1ff5 and 011aed3.

📒 Files selected for processing (2)
  • pkg/export/export_gauge.go
  • pkg/provider/amazon.go
🚧 Files skipped from review as they are similar to previous changes (2)
  • pkg/provider/amazon.go
  • pkg/export/export_gauge.go

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (2)
pkg/provider/amazon.go (1)

143-143: 正确移除了外层的 RecordID 赋值

将 RecordID 赋值从外层循环移除是正确的,因为这会导致同一 ResourceRecordSet 下的所有 ResourceRecord 共享相同的 ID。

♻️ 可选:清理注释代码

建议直接删除注释行而非保留,以保持代码整洁:

             recordInfo := Record{
                 CloudProvider: a.account.CloudProvider,
                 CloudName:     a.account.CloudName,
                 DomainName:    domain,
-                // RecordID:      public.GetID(),
                 RecordType:    string(record.Type),
                 RecordWeight:  fmt.Sprintf("%d", record.Weight),
                 RecordStatus:  oneStatus("enable"),
pkg/export/export_gauge.go (1)

190-206: 建议为自定义记录添加同样的重复检测

当前重复检测逻辑仅应用于云服务商的证书信息(lines 174-183),但自定义记录(custom records)部分缺少相同的保护机制。为保持一致性并避免潜在的重复 RecordID 问题,建议添加类似的防御逻辑。

♻️ 建议的改进方案
     // get custom record cert info list from cache
     if len(public.Config.CustomRecords) != 0 {
         recordCertInfoCacheKey := public.RecordCertInfo + "_" + public.CustomRecords
         var recordCerts []provider.RecordCert
         recordCertInfoCacheValue, err := public.CertCache.Get(recordCertInfoCacheKey)
         if err != nil {
             logger.Error(fmt.Sprintf("[ %s ] get record list failed: %v", recordCertInfoCacheKey, err))
             return
         }
         err = json.Unmarshal(recordCertInfoCacheValue, &recordCerts)
         if err != nil {
             logger.Error(fmt.Sprintf("[ %s ] json.Unmarshal error: %v", recordCertInfoCacheKey, err))
             return
         }
+        seenIDs := make(map[string]bool)
         for _, v := range recordCerts {
+            if v.RecordID == "" {
+                continue
+            }
+            if seenIDs[v.RecordID] {
+                logger.Error(fmt.Sprintf("【Duplicate Found】Custom Record: %s, RecordID: %s is duplicated!", v.FullRecord, v.RecordID))
+                continue
+            }
+            seenIDs[v.RecordID] = true
             ch <- prometheus.MustNewConstMetric(c.metrics[public.RecordCertInfo], prometheus.GaugeValue, float64(v.DaysUntilExpiry), v.CloudProvider, v.CloudName, v.DomainName, v.RecordID, v.FullRecord, v.SubjectCommonName, v.SubjectOrganization, v.SubjectOrganizationalUnit, v.IssuerCommonName, v.IssuerOrganization, v.IssuerOrganizationalUnit, v.CreatedDate, v.ExpiryDate, fmt.Sprintf("%t", v.CertMatched), v.ErrorMsg)
         }
     }
📜 Review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1a76524 and e4e1ff5.

📒 Files selected for processing (2)
  • pkg/export/export_gauge.go
  • pkg/provider/amazon.go
🧰 Additional context used
🧬 Code graph analysis (2)
pkg/provider/amazon.go (1)
public/public.go (1)
  • GetID (101-103)
pkg/export/export_gauge.go (1)
public/logger/logger.go (1)
  • Error (38-40)
🔇 Additional comments (2)
pkg/provider/amazon.go (1)

163-168: 正确修复了 RecordID 重复问题

将 RecordID 的生成移至内层循环是正确的解决方案。现在每个 ResourceRecord 都会获得唯一的 ID,避免了多个记录值共享同一 RecordID 导致的 Prometheus 指标冲突。

这个改动有效解决了 issue #36 中报告的 "collected before with the same name and label values" 错误。

pkg/export/export_gauge.go (1)

174-184: 有效的重复检测防御逻辑

添加的 seenIDs 映射能够有效检测并跳过重复的 RecordID,防止 Prometheus 采集错误。这是一个很好的防御层,即使在数据源层面已经修复的情况下,仍能捕获可能出现的重复。

逻辑实现正确:

  • 空 RecordID 检查避免了无效数据
  • 重复检测及时记录错误并跳过
  • seenIDs 的作用域限定在每个云账号批次内,符合预期

@eryajf
Copy link
Member

eryajf commented Jan 8, 2026

话说,这里是不是已经用随机ID填充了,为啥还会重复呢,好奇怪:

RecordID: public.GetID(),

@hangliuc
Copy link
Author

hangliuc commented Jan 8, 2026

for _, record := range record.ResourceRecords {
recordInfo.RecordValue = tea.StringValue(record.Value)
dataObj = append(dataObj, recordInfo)
}

如果record.ResourceRecords 是多个值会共享前面的同一个随机ID

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Prometheus - as collected before with the same name and label values

2 participants