Skip to content

Commit 285412f

Browse files
committed
feat(prometheus_adapter): 添加获取绑定地址方法并优化端口配置逻辑
1 parent 4bc9b15 commit 285412f

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

cmd/prometheus_adapter/main.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,28 @@ func main() {
2020

2121
log.Info().Msg("Starting Prometheus Adapter server")
2222

23-
// 加载配置
24-
cfg := &config.Config{
25-
Server: config.ServerConfig{
26-
BindAddr: ":9999", // 默认端口
27-
},
23+
// 加载 Prometheus Adapter 配置
24+
adapter, err := prometheusadapter.NewPrometheusAdapterServer(&config.Config{})
25+
if err != nil {
26+
log.Fatal().Err(err).Msg("Failed to create Prometheus Adapter server")
27+
}
28+
29+
// 获取 Prometheus Adapter 内部配置的绑定地址
30+
bindAddr := ":9999" // 默认端口
31+
if adapter.GetBindAddr() != "" {
32+
bindAddr = adapter.GetBindAddr()
2833
}
2934

30-
// 如果有环境变量,使用环境变量的端口
35+
// 如果有环境变量,优先使用环境变量的端口
3136
if port := os.Getenv("ADAPTER_PORT"); port != "" {
32-
cfg.Server.BindAddr = ":" + port
37+
bindAddr = ":" + port
3338
}
3439

35-
// 创建 Prometheus Adapter 服务器
36-
adapter, err := prometheusadapter.NewPrometheusAdapterServer(cfg)
37-
if err != nil {
38-
log.Fatal().Err(err).Msg("Failed to create Prometheus Adapter server")
40+
// 更新配置(虽然已经创建了 adapter,但需要端口信息用于启动服务器)
41+
cfg := &config.Config{
42+
Server: config.ServerConfig{
43+
BindAddr: bindAddr,
44+
},
3945
}
4046

4147
// 创建路由

internal/prometheus_adapter/server.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ func NewPrometheusAdapterServer(cfg *config.Config) (*PrometheusAdapterServer, e
6060
return server, nil
6161
}
6262

63+
// GetBindAddr 获取配置文件中的绑定地址
64+
func (s *PrometheusAdapterServer) GetBindAddr() string {
65+
if s.promConfig != nil && s.promConfig.Server.BindAddr != "" {
66+
return s.promConfig.Server.BindAddr
67+
}
68+
return ""
69+
}
70+
6371
// UseApi 设置 API 路由
6472
func (s *PrometheusAdapterServer) UseApi(router *fox.Engine) error {
6573
var err error

0 commit comments

Comments
 (0)