This commit is contained in:
withchao 2025-02-13 16:35:11 +08:00
parent 12b5371ed7
commit a16b95b25f
5 changed files with 35 additions and 46 deletions

View File

@ -8,12 +8,14 @@ import (
"fmt" "fmt"
"net" "net"
"os" "os"
"os/signal"
"path" "path"
"path/filepath" "path/filepath"
"reflect" "reflect"
"runtime" "runtime"
"strings" "strings"
"sync" "sync"
"syscall"
"time" "time"
"github.com/mitchellh/mapstructure" "github.com/mitchellh/mapstructure"
@ -250,16 +252,17 @@ func (x *cmds) run(ctx context.Context) error {
}() }()
} }
//go func() { go func() {
// sigs := make(chan os.Signal, 1) sigs := make(chan os.Signal, 1)
// signal.Notify(sigs, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL) signal.Notify(sigs, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL)
// select { select {
// case <-ctx.Done(): case <-ctx.Done():
// return return
// case val := <-sigs: case val := <-sigs:
// cancel(fmt.Errorf("signal %s", val.String())) log.ZDebug(ctx, "recv signal", "signal", val.String())
// } cancel(fmt.Errorf("signal %s", val.String()))
//}() }
}()
for i := range x.cmds { for i := range x.cmds {
cmd := x.cmds[i] cmd := x.cmds[i]

View File

@ -2,6 +2,7 @@ package api
import ( import (
"encoding/json" "encoding/json"
"errors"
"net/http" "net/http"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@ -11,13 +12,13 @@ import (
"github.com/openimsdk/tools/discovery" "github.com/openimsdk/tools/discovery"
"github.com/openimsdk/tools/discovery/etcd" "github.com/openimsdk/tools/discovery/etcd"
"github.com/openimsdk/tools/errs" "github.com/openimsdk/tools/errs"
"github.com/openimsdk/tools/log"
clientv3 "go.etcd.io/etcd/client/v3" clientv3 "go.etcd.io/etcd/client/v3"
) )
type PrometheusDiscoveryApi struct { type PrometheusDiscoveryApi struct {
config *Config config *Config
client *clientv3.Client client *clientv3.Client
kv discovery.KeyValue
} }
func NewPrometheusDiscoveryApi(config *Config, client discovery.Conn) *PrometheusDiscoveryApi { func NewPrometheusDiscoveryApi(config *Config, client discovery.Conn) *PrometheusDiscoveryApi {
@ -30,43 +31,26 @@ func NewPrometheusDiscoveryApi(config *Config, client discovery.Conn) *Prometheu
return api return api
} }
func (p *PrometheusDiscoveryApi) Enable(c *gin.Context) {
if p.config.Discovery.Enable != conf.ETCD {
c.JSON(http.StatusOK, []struct{}{})
c.Abort()
}
}
func (p *PrometheusDiscoveryApi) discovery(c *gin.Context, key string) { func (p *PrometheusDiscoveryApi) discovery(c *gin.Context, key string) {
eResp, err := p.client.Get(c, prommetrics.BuildDiscoveryKey(key)) value, err := p.kv.GetKey(c, prommetrics.BuildDiscoveryKey(key))
if err != nil { if err != nil {
// Log and respond with an error if preparation fails. if errors.Is(err, discovery.ErrNotSupported) {
apiresp.GinError(c, errs.WrapMsg(err, "etcd get err")) c.JSON(http.StatusOK, []struct{}{})
return
}
apiresp.GinError(c, errs.WrapMsg(err, "get key value"))
return return
} }
if len(eResp.Kvs) == 0 { if len(value) == 0 {
c.JSON(http.StatusOK, []*prommetrics.Target{}) c.JSON(http.StatusOK, []*prommetrics.RespTarget{})
return
} }
var resp prommetrics.RespTarget
var ( if err := json.Unmarshal(value, &resp); err != nil {
resp = &prommetrics.RespTarget{ apiresp.GinError(c, errs.WrapMsg(err, "json unmarshal err"))
Targets: make([]string, 0, len(eResp.Kvs)), return
}
)
for i := range eResp.Kvs {
var target prommetrics.Target
err = json.Unmarshal(eResp.Kvs[i].Value, &target)
if err != nil {
log.ZError(c, "prometheus unmarshal err", errs.Wrap(err))
}
resp.Targets = append(resp.Targets, target.Target)
if resp.Labels == nil {
resp.Labels = target.Labels
}
} }
c.JSON(http.StatusOK, []*prommetrics.RespTarget{&resp})
c.JSON(200, []*prommetrics.RespTarget{resp})
} }
func (p *PrometheusDiscoveryApi) Api(c *gin.Context) { func (p *PrometheusDiscoveryApi) Api(c *gin.Context) {

View File

@ -283,7 +283,7 @@ func newGinRouter(ctx context.Context, client discovery.Conn, cfg *Config) (*gin
} }
{ {
pd := NewPrometheusDiscoveryApi(cfg, client) pd := NewPrometheusDiscoveryApi(cfg, client)
proDiscoveryGroup := r.Group("/prometheus_discovery", pd.Enable) proDiscoveryGroup := r.Group("/prometheus_discovery")
proDiscoveryGroup.GET("/api", pd.Api) proDiscoveryGroup.GET("/api", pd.Api)
proDiscoveryGroup.GET("/user", pd.User) proDiscoveryGroup.GET("/user", pd.User)
proDiscoveryGroup.GET("/group", pd.Group) proDiscoveryGroup.GET("/group", pd.Group)

View File

@ -25,11 +25,11 @@ type Config struct {
Discovery config.Discovery Discovery config.Discovery
} }
// func Start(ctx context.Context, conf *CronTaskConfig) error {
func Start(ctx context.Context, conf *Config, client discovery.Conn, service grpc.ServiceRegistrar) error { func Start(ctx context.Context, conf *Config, client discovery.Conn, service grpc.ServiceRegistrar) error {
log.CInfo(ctx, "CRON-TASK server is initializing", "runTimeEnv", runtimeenv.RuntimeEnvironment(), "chatRecordsClearTime", conf.CronTask.CronExecuteTime, "msgDestructTime", conf.CronTask.RetainChatRecords) log.CInfo(ctx, "CRON-TASK server is initializing", "runTimeEnv", runtimeenv.RuntimeEnvironment(), "chatRecordsClearTime", conf.CronTask.CronExecuteTime, "msgDestructTime", conf.CronTask.RetainChatRecords)
if conf.CronTask.RetainChatRecords < 1 { if conf.CronTask.RetainChatRecords < 1 {
log.ZInfo(ctx, "disable cron")
<-ctx.Done()
return nil return nil
} }
ctx = mcontext.SetOpUserID(ctx, conf.Share.IMAdminUserID[0]) ctx = mcontext.SetOpUserID(ctx, conf.Share.IMAdminUserID[0])
@ -78,7 +78,9 @@ func Start(ctx context.Context, conf *Config, client discovery.Conn, service grp
} }
log.ZDebug(ctx, "start cron task", "CronExecuteTime", conf.CronTask.CronExecuteTime) log.ZDebug(ctx, "start cron task", "CronExecuteTime", conf.CronTask.CronExecuteTime)
srv.cron.Start() srv.cron.Start()
log.ZDebug(ctx, "cron task server is running")
<-ctx.Done() <-ctx.Done()
log.ZDebug(ctx, "cron task server is shutting down")
return nil return nil
} }

View File

@ -323,7 +323,7 @@ type RPC struct {
} }
type Redis struct { type Redis struct {
Disable bool `yaml:"disable"` Disable bool `yaml:"-"`
Address []string `yaml:"address"` Address []string `yaml:"address"`
Username string `yaml:"username"` Username string `yaml:"username"`
Password string `yaml:"password"` Password string `yaml:"password"`