Skip to content

Commit a4451af

Browse files
authored
✨ feat(metrics): 为监控指标添加构建信息 (#34)
* ✨ feat(metrics): 为监控指标添加构建信息 - 在 Metrics 结构体中新增版本、Git 提交、Go 版本、操作系统架构和构建时间字段 - 新增 build_info 指标用于暴露应用构建信息 - 更新 NewMetrics 函数签名以接收构建信息参数 🌈 style(metrics): 优化代码格式和结构 - 调整代码缩进和空行以提升可读性 - 规范化指标标签名称使用下划线分隔 * 📝 docs(README): Add build_info metric documentation. - Add build_info metric description in both English and Chinese README files. - Include detailed metric configuration examples with version, git_commit, go_version, os_arch and build_time fields. * 🐛 fix(export): 修正结构体字段命名不一致问题 - 将 `gitCommit` 字段重命名为 `goVersion`,确保字段命名与实际用途一致
1 parent e512ad7 commit a4451af

File tree

5 files changed

+54
-4
lines changed

5 files changed

+54
-4
lines changed

README-en.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,21 @@ The indicators provided by this project include the following:
7676

7777
| NAME | Description |
7878
| ------------------ | -------------------- |
79+
| `build_info` | Build Information |
7980
| `domain_list` | Domain Name List |
8081
| `record_list` | Domain name resolution record list |
8182
| `record_cert_info` | Parse record certificate information list |
8283

8384
Indicator label description:
8485

8586
```
87+
<!-- Build Information -->
88+
build_info{
89+
version="version number",
90+
git_commit="git commit number",
91+
go_version="go version",
92+
os_arch="system architecture",
93+
build_time="build time"} 1
8694
<!-- Domain Name List -->
8795
domain_list{
8896
cloud_provider="DNS Provider",

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,21 @@ docker-compose up -d
7878

7979
| 名称 | 说明 |
8080
| ------------------ | -------------------- |
81+
| `build_info` | 构建信息 |
8182
| `domain_list` | 域名列表 |
8283
| `record_list` | 域名解析记录列表 |
8384
| `record_cert_info` | 解析记录证书信息列表 |
8485

8586
指标标签说明:
8687

8788
```
89+
<!-- 构建信息 -->
90+
build_info{
91+
version="版本号",
92+
git_commit="git提交号",
93+
go_version="go版本",
94+
os_arch="系统架构",
95+
build_time="构建时间"} 1
8896
<!-- 域名列表 -->
8997
domain_list{
9098
cloud_provider="DNS提供商",

pkg/cmd/root.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ var rootCmd = &cobra.Command{
5555
}
5656

5757
func RunServer() {
58-
metrics := export.NewMetrics("")
58+
osArch := fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH)
59+
metrics := export.NewMetrics("", Version, GitCommit, runtime.Version(), osArch, BuildTime)
5960
registory := prometheus.NewRegistry()
6061
registory.MustRegister(metrics)
6162

pkg/export/export_gauge.go

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,13 @@ import (
1414

1515
// 指标结构体
1616
type Metrics struct {
17-
metrics map[string]*prometheus.Desc
18-
mutex sync.Mutex
17+
metrics map[string]*prometheus.Desc
18+
mutex sync.Mutex
19+
version string
20+
gitCommit string
21+
goVersion string
22+
osArch string
23+
buildTime string
1924
}
2025

2126
// newGlobalMetric 创建指标描述符
@@ -28,8 +33,13 @@ func newGlobalMetric(namespace string, metricName string, docString string, labe
2833
}
2934

3035
// NewMetrics 初始化指标信息,即Metrics结构体
31-
func NewMetrics(namespace string) *Metrics {
36+
func NewMetrics(namespace, version, gitCommit, goVersion, osArch, buildTime string) *Metrics {
3237
return &Metrics{
38+
version: version,
39+
gitCommit: gitCommit,
40+
goVersion: goVersion,
41+
osArch: osArch,
42+
buildTime: buildTime,
3343
metrics: map[string]*prometheus.Desc{
3444
public.DomainList: newGlobalMetric(namespace,
3545
public.DomainList,
@@ -82,6 +92,16 @@ func NewMetrics(namespace string) *Metrics {
8292
"cert_matched",
8393
"error_msg",
8494
}),
95+
public.BuildInfo: newGlobalMetric(namespace,
96+
public.BuildInfo,
97+
"Application build information",
98+
[]string{
99+
"version",
100+
"git_commit",
101+
"go_version",
102+
"os_arch",
103+
"build_time",
104+
}),
85105
},
86106
}
87107
}
@@ -178,4 +198,16 @@ func (c *Metrics) Collect(ch chan<- prometheus.Metric) {
178198
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)
179199
}
180200
}
201+
202+
// build info metric
203+
ch <- prometheus.MustNewConstMetric(
204+
c.metrics[public.BuildInfo],
205+
prometheus.GaugeValue,
206+
1,
207+
c.version,
208+
c.gitCommit,
209+
c.goVersion,
210+
c.osArch,
211+
c.buildTime,
212+
)
181213
}

public/public.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const (
3333
DomainList string = "domain_list"
3434
RecordList string = "record_list"
3535
RecordCertInfo string = "record_cert_info"
36+
BuildInfo string = "build_info"
3637
// Cron Config Defaults
3738
DefaultDomainRecordSyncInterval = 30 // 默认域名记录同步间隔(秒)
3839
DefaultCertInfoSyncInterval = 3600 // 默认证书信息同步间隔(秒),24小时

0 commit comments

Comments
 (0)