feat: 支持环境变量控制 Prometheus metrics
All checks were successful
itom-platform auto build image / build (push) Successful in 3m17s

This commit is contained in:
kim.dev.6789
2026-01-16 23:21:11 +08:00
parent 59aa8c4fce
commit e018f742ad
5 changed files with 73 additions and 0 deletions

5
go.mod
View File

@@ -29,6 +29,7 @@ require (
github.com/mitchellh/mapstructure v1.5.0 github.com/mitchellh/mapstructure v1.5.0
github.com/openimsdk/gomake v0.0.15-alpha.11 github.com/openimsdk/gomake v0.0.15-alpha.11
github.com/openimsdk/tools v0.0.50-alpha.65 github.com/openimsdk/tools v0.0.50-alpha.65
github.com/prometheus/client_golang v1.19.0
github.com/redis/go-redis/v9 v9.5.1 github.com/redis/go-redis/v9 v9.5.1
github.com/sashabaranov/go-openai v1.38.1 github.com/sashabaranov/go-openai v1.38.1
github.com/spf13/cobra v1.8.0 github.com/spf13/cobra v1.8.0
@@ -48,6 +49,7 @@ require (
github.com/alibabacloud-go/tea-utils v1.4.5 // indirect github.com/alibabacloud-go/tea-utils v1.4.5 // indirect
github.com/alibabacloud-go/tea-xml v1.1.2 // indirect github.com/alibabacloud-go/tea-xml v1.1.2 // indirect
github.com/aliyun/credentials-go v1.1.2 // indirect github.com/aliyun/credentials-go v1.1.2 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bytedance/sonic v1.9.1 // indirect github.com/bytedance/sonic v1.9.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
@@ -97,6 +99,9 @@ require (
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/openimsdk/protocol v0.0.69-alpha.4 // indirect github.com/openimsdk/protocol v0.0.69-alpha.4 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.48.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/richardlehane/mscfb v1.0.4 // indirect github.com/richardlehane/mscfb v1.0.4 // indirect
github.com/richardlehane/msoleps v1.0.3 // indirect github.com/richardlehane/msoleps v1.0.3 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect github.com/sagikazarmark/locafero v0.4.0 // indirect

View File

@@ -7,6 +7,7 @@ import (
"net/http" "net/http"
"os" "os"
"os/signal" "os/signal"
"strings"
"syscall" "syscall"
"time" "time"
@@ -26,6 +27,7 @@ import (
"github.com/openimsdk/tools/system/program" "github.com/openimsdk/tools/system/program"
"github.com/openimsdk/tools/utils/datautil" "github.com/openimsdk/tools/utils/datautil"
"github.com/openimsdk/tools/utils/runtimeenv" "github.com/openimsdk/tools/utils/runtimeenv"
"github.com/prometheus/client_golang/prometheus/promhttp"
clientv3 "go.etcd.io/etcd/client/v3" clientv3 "go.etcd.io/etcd/client/v3"
"google.golang.org/grpc" "google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure" "google.golang.org/grpc/credentials/insecure"
@@ -81,6 +83,24 @@ func Start(ctx context.Context, index int, config *Config) error {
} }
c.Next() c.Next()
}) })
// 可选的 Prometheus metrics 端点
// 支持配置文件或环境变量控制(环境变量优先)
prometheusEnable := config.AdminAPI.Prometheus.Enable
if envEnable := os.Getenv("PROMETHEUS_ENABLE"); envEnable != "" {
prometheusEnable = strings.ToLower(envEnable) == "true" || envEnable == "1"
}
if prometheusEnable {
metricsPath := config.AdminAPI.Prometheus.Path
if envPath := os.Getenv("PROMETHEUS_PATH"); envPath != "" {
metricsPath = envPath
}
if metricsPath == "" {
metricsPath = "/metrics"
}
engine.GET(metricsPath, gin.WrapH(promhttp.Handler()))
}
SetAdminRoute(engine, adminApi, mwApi, config, client) SetAdminRoute(engine, adminApi, mwApi, config, client)
if config.Discovery.Enable == kdisc.ETCDCONST { if config.Discovery.Enable == kdisc.ETCDCONST {

View File

@@ -24,8 +24,10 @@ import (
"github.com/openimsdk/tools/system/program" "github.com/openimsdk/tools/system/program"
"github.com/openimsdk/tools/utils/datautil" "github.com/openimsdk/tools/utils/datautil"
"github.com/openimsdk/tools/utils/runtimeenv" "github.com/openimsdk/tools/utils/runtimeenv"
"github.com/prometheus/client_golang/prometheus/promhttp"
"google.golang.org/grpc" "google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure" "google.golang.org/grpc/credentials/insecure"
"strings"
) )
type Config struct { type Config struct {
@@ -75,6 +77,24 @@ func Start(ctx context.Context, index int, cfg *Config) error {
} }
c.Next() c.Next()
}) })
// 可选的 Prometheus metrics 端点
// 支持配置文件或环境变量控制(环境变量优先)
prometheusEnable := cfg.ApiConfig.Prometheus.Enable
if envEnable := os.Getenv("PROMETHEUS_ENABLE"); envEnable != "" {
prometheusEnable = strings.ToLower(envEnable) == "true" || envEnable == "1"
}
if prometheusEnable {
metricsPath := cfg.ApiConfig.Prometheus.Path
if envPath := os.Getenv("PROMETHEUS_PATH"); envPath != "" {
metricsPath = envPath
}
if metricsPath == "" {
metricsPath = "/metrics"
}
engine.GET(metricsPath, gin.WrapH(promhttp.Handler()))
}
SetBotRoute(engine, botApi, mwApi) SetBotRoute(engine, botApi, mwApi)
var ( var (

View File

@@ -7,6 +7,7 @@ import (
"net/http" "net/http"
"os" "os"
"os/signal" "os/signal"
"strings"
"syscall" "syscall"
"time" "time"
@@ -25,6 +26,7 @@ import (
"github.com/openimsdk/tools/system/program" "github.com/openimsdk/tools/system/program"
"github.com/openimsdk/tools/utils/datautil" "github.com/openimsdk/tools/utils/datautil"
"github.com/openimsdk/tools/utils/runtimeenv" "github.com/openimsdk/tools/utils/runtimeenv"
"github.com/prometheus/client_golang/prometheus/promhttp"
"google.golang.org/grpc" "google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure" "google.golang.org/grpc/credentials/insecure"
) )
@@ -74,6 +76,24 @@ func Start(ctx context.Context, index int, cfg *Config) error {
gin.SetMode(gin.ReleaseMode) gin.SetMode(gin.ReleaseMode)
engine := gin.New() engine := gin.New()
engine.Use(gin.Recovery(), mw.CorsHandler(), mw.GinParseOperationID()) engine.Use(gin.Recovery(), mw.CorsHandler(), mw.GinParseOperationID())
// 可选的 Prometheus metrics 端点
// 支持配置文件或环境变量控制(环境变量优先)
prometheusEnable := cfg.ApiConfig.Prometheus.Enable
if envEnable := os.Getenv("PROMETHEUS_ENABLE"); envEnable != "" {
prometheusEnable = strings.ToLower(envEnable) == "true" || envEnable == "1"
}
if prometheusEnable {
metricsPath := cfg.ApiConfig.Prometheus.Path
if envPath := os.Getenv("PROMETHEUS_PATH"); envPath != "" {
metricsPath = envPath
}
if metricsPath == "" {
metricsPath = "/metrics"
}
engine.GET(metricsPath, gin.WrapH(promhttp.Handler()))
}
SetChatRoute(engine, adminApi, mwApi) SetChatRoute(engine, adminApi, mwApi)
var ( var (

View File

@@ -43,6 +43,7 @@ type API struct {
ListenIP string `mapstructure:"listenIP"` ListenIP string `mapstructure:"listenIP"`
Ports []int `mapstructure:"ports"` Ports []int `mapstructure:"ports"`
} `mapstructure:"api"` } `mapstructure:"api"`
Prometheus Prometheus `mapstructure:"prometheus"` // 可选的 Prometheus 配置
} }
type APIBot struct { type APIBot struct {
@@ -50,6 +51,7 @@ type APIBot struct {
ListenIP string `mapstructure:"listenIP"` ListenIP string `mapstructure:"listenIP"`
Ports []int `mapstructure:"ports"` Ports []int `mapstructure:"ports"`
} `mapstructure:"api"` } `mapstructure:"api"`
Prometheus Prometheus `mapstructure:"prometheus"` // 可选的 Prometheus 配置
} }
type Mongo struct { type Mongo struct {
@@ -104,6 +106,12 @@ type Discovery struct {
RpcService RpcService `mapstructure:"rpcService"` RpcService RpcService `mapstructure:"rpcService"`
} }
// Prometheus 配置(可选,用于暴露 /metrics 端点)
type Prometheus struct {
Enable bool `mapstructure:"enable"` // 是否启用 Prometheus metrics默认 false
Path string `mapstructure:"path"` // metrics 路径,默认 /metrics
}
type Kubernetes struct { type Kubernetes struct {
Namespace string `mapstructure:"namespace"` Namespace string `mapstructure:"namespace"`
} }