mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-12-16 11:02:56 +08:00
resolve the prometheus discovery issue of multiple instances with same service
This commit is contained in:
parent
48928d7d84
commit
2eb30b2df0
@ -36,7 +36,6 @@ import (
|
||||
"github.com/openimsdk/tools/mw"
|
||||
"github.com/openimsdk/tools/system/program"
|
||||
"github.com/openimsdk/tools/utils/datautil"
|
||||
"github.com/openimsdk/tools/utils/jsonutil"
|
||||
"github.com/openimsdk/tools/utils/network"
|
||||
"github.com/openimsdk/tools/utils/runtimeenv"
|
||||
"google.golang.org/grpc"
|
||||
@ -108,11 +107,7 @@ func Start(ctx context.Context, index int, config *Config) error {
|
||||
}
|
||||
|
||||
etcdClient := client.(*etcd.SvcDiscoveryRegistryImpl).GetClient()
|
||||
|
||||
_, err = etcdClient.Put(ctx, prommetrics.BuildDiscoveryKey(prommetrics.APIKeyName, registerIP, prometheusPort), jsonutil.StructToJsonString(prommetrics.BuildDefaultTarget(registerIP, prometheusPort)))
|
||||
if err != nil {
|
||||
return errs.WrapMsg(err, "etcd put err")
|
||||
}
|
||||
prommetrics.Register(ctx, etcdClient, prommetrics.APIKeyName, registerIP, prometheusPort)
|
||||
} else {
|
||||
prometheusPort, err = datautil.GetElemByIndex(config.API.Prometheus.Ports, index)
|
||||
if err != nil {
|
||||
|
||||
@ -28,7 +28,6 @@ import (
|
||||
disetcd "github.com/openimsdk/open-im-server/v3/pkg/common/discovery/etcd"
|
||||
"github.com/openimsdk/tools/discovery"
|
||||
"github.com/openimsdk/tools/discovery/etcd"
|
||||
"github.com/openimsdk/tools/utils/jsonutil"
|
||||
"github.com/openimsdk/tools/utils/network"
|
||||
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/common/prommetrics"
|
||||
@ -193,11 +192,7 @@ func (m *MsgTransfer) Start(index int, config *Config, client discovery.SvcDisco
|
||||
}
|
||||
|
||||
etcdClient := client.(*etcd.SvcDiscoveryRegistryImpl).GetClient()
|
||||
|
||||
_, err = etcdClient.Put(context.TODO(), prommetrics.BuildDiscoveryKey(prommetrics.MessageTransferKeyName, registerIP, prometheusPort), jsonutil.StructToJsonString(prommetrics.BuildDefaultTarget(registerIP, prometheusPort)))
|
||||
if err != nil {
|
||||
return errs.WrapMsg(err, "etcd put err")
|
||||
}
|
||||
prommetrics.Register(context.TODO(), etcdClient, prommetrics.MessageTransferKeyName, registerIP, prometheusPort)
|
||||
} else {
|
||||
prometheusPort, err = datautil.GetElemByIndex(config.MsgTransfer.Prometheus.Ports, index)
|
||||
if err != nil {
|
||||
|
||||
@ -1,6 +1,13 @@
|
||||
package prommetrics
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/openimsdk/tools/errs"
|
||||
"github.com/openimsdk/tools/utils/jsonutil"
|
||||
clientv3 "go.etcd.io/etcd/client/v3"
|
||||
)
|
||||
|
||||
const (
|
||||
APIKeyName = "api"
|
||||
@ -33,3 +40,30 @@ func BuildDefaultTarget(host string, ip int) Target {
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func Register(ctx context.Context, etcdClient *clientv3.Client, rpcRegisterName string, registerIP string, prometheusPort int) error {
|
||||
// create lease
|
||||
leaseResp, err := etcdClient.Grant(ctx, 30)
|
||||
if err != nil {
|
||||
return errs.WrapMsg(err, "failed to create lease in etcd")
|
||||
}
|
||||
// release
|
||||
keepAliveChan, err := etcdClient.KeepAlive(ctx, leaseResp.ID)
|
||||
if err != nil {
|
||||
return errs.WrapMsg(err, "failed to keep alive lease")
|
||||
}
|
||||
// release resp
|
||||
go func() {
|
||||
for range keepAliveChan {
|
||||
}
|
||||
}()
|
||||
putOpts := []clientv3.OpOption{}
|
||||
if leaseResp != nil {
|
||||
putOpts = append(putOpts, clientv3.WithLease(leaseResp.ID))
|
||||
}
|
||||
_, err = etcdClient.Put(ctx, BuildDiscoveryKey(rpcRegisterName, registerIP, prometheusPort), jsonutil.StructToJsonString(BuildDefaultTarget(registerIP, prometheusPort)), putOpts...)
|
||||
if err != nil {
|
||||
return errs.WrapMsg(err, "etcd put err")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -30,7 +30,6 @@ import (
|
||||
disetcd "github.com/openimsdk/open-im-server/v3/pkg/common/discovery/etcd"
|
||||
"github.com/openimsdk/tools/discovery/etcd"
|
||||
"github.com/openimsdk/tools/utils/datautil"
|
||||
"github.com/openimsdk/tools/utils/jsonutil"
|
||||
"google.golang.org/grpc/status"
|
||||
|
||||
"github.com/openimsdk/tools/utils/runtimeenv"
|
||||
@ -127,11 +126,7 @@ func Start[T any](ctx context.Context, discovery *conf.Discovery, prometheusConf
|
||||
}
|
||||
|
||||
etcdClient := client.(*etcd.SvcDiscoveryRegistryImpl).GetClient()
|
||||
|
||||
_, err = etcdClient.Put(ctx, prommetrics.BuildDiscoveryKey(rpcRegisterName, registerIP, prometheusPort), jsonutil.StructToJsonString(prommetrics.BuildDefaultTarget(registerIP, prometheusPort)))
|
||||
if err != nil {
|
||||
return errs.WrapMsg(err, "etcd put err")
|
||||
}
|
||||
prommetrics.Register(ctx, etcdClient, rpcRegisterName, registerIP, prometheusPort)
|
||||
} else {
|
||||
prometheusPort, err = datautil.GetElemByIndex(prometheusConfig.Ports, index)
|
||||
if err != nil {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user