From 5341e04d25cdea9e5e8fc256eb2fba20e6a0bb63 Mon Sep 17 00:00:00 2001 From: wuhua3 Date: Mon, 28 Oct 2024 18:04:12 +0800 Subject: [PATCH] update runtime info update runtime info bugfix code review --- agent.go | 9 ++++++ agent_test.go | 27 ++++++++++++----- cluster/command.go | 13 ++++++++- core/constants.go | 1 + core/switcher.go | 3 +- filter/accessLog.go | 7 ++--- manageHandler.go | 70 ++++++++++++++++++++++++++++----------------- 7 files changed, 89 insertions(+), 41 deletions(-) diff --git a/agent.go b/agent.go index 013f08f6..43c5c5bf 100644 --- a/agent.go +++ b/agent.go @@ -44,6 +44,7 @@ const ( ) var ( + startTime = time.Now() initParamLock sync.Mutex setAgentLock sync.Mutex notFoundProviderCount int64 = 0 @@ -134,6 +135,14 @@ func (a *Agent) OnAfterStart(f func(a *Agent)) { a.onAfterStart = append(a.onAfterStart, f) } +func (a *Agent) GetRuntimeInfo() map[string]interface{} { + info := map[string]interface{}{} + info[motan.RuntimeStartTimeKey] = startTime + info[motan.RuntimeCpuPercentKey] = GetCpuPercent() + info[motan.RuntimeRssMemoryKey] = GetRssMemory() + return info +} + func (a *Agent) RegisterCommandHandler(f CommandHandler) { a.commandHandlers = append(a.commandHandlers, f) } diff --git a/agent_test.go b/agent_test.go index 1546a367..893ac673 100644 --- a/agent_test.go +++ b/agent_test.go @@ -1064,15 +1064,28 @@ func TestRuntimeHandler(t *testing.T) { err = json.Unmarshal(bodyBytes, &runtimeInfo) assert.Nil(t, err) - for _, s := range []string{ - core.RuntimeInstanceTypeKey, + for _, s := range defaultKeys { + info, ok := runtimeInfo[s] + assert.True(t, ok) + assert.NotNil(t, info) + t.Logf("key: %s", s) + } + + // test param keys + keys := []string{ core.RuntimeExportersKey, - core.RuntimeClustersKey, - core.RuntimeHttpClustersKey, - core.RuntimeExtensionFactoryKey, core.RuntimeServersKey, - core.RuntimeBasicKey, - } { + core.RuntimeHttpClustersKey, + } + resp, err = http.Get("http://127.0.0.1:8002/runtime/info" + "?keys=" + strings.Join(keys, ",")) + assert.Nil(t, err) + bodyBytes, err = ioutil.ReadAll(resp.Body) + assert.Nil(t, err) + err = resp.Body.Close() + assert.Nil(t, err) + err = json.Unmarshal(bodyBytes, &runtimeInfo) + assert.Nil(t, err) + for _, s := range keys { info, ok := runtimeInfo[s] assert.True(t, ok) assert.NotNil(t, info) diff --git a/cluster/command.go b/cluster/command.go index cbcefae5..6f99433f 100644 --- a/cluster/command.go +++ b/cluster/command.go @@ -330,12 +330,23 @@ func (c *CommandRegistryWrapper) getResultWithCommand(needNotify bool) []*motan. } if needNotify { c.notifyListener.Notify(c.registry.GetURL(), result) - c.notifyRecorder.AddRecord(result) + c.notifyRecorder.AddRecord(toRecordInfo(result)) } vlog.Infof("%s get result with command. tcCommand: %t, degradeCommand:%t, result size %d, will notify:%t", c.cluster.GetURL().GetIdentity(), currentCommand != nil, c.degradeCommand != nil, len(result), needNotify) return result } +func toRecordInfo(urls []*motan.URL) []string { + if len(urls) == 0 { + return []string{} + } + infoList := make([]string, len(urls)) + for i, command := range urls { + infoList[i] = command.GetIdentityWithRegistry() + } + return infoList +} + func processRoute(urls []*motan.URL, routers []string) []*motan.URL { if len(urls) > 0 && len(routers) > 0 { lastURLs := urls diff --git a/core/constants.go b/core/constants.go index 5815bc9e..af9d6f9d 100644 --- a/core/constants.go +++ b/core/constants.go @@ -244,6 +244,7 @@ const ( // -----------basic keys------------- + RuntimeStartTimeKey = "startTime" RuntimeCpuPercentKey = "cpuPercent" RuntimeRssMemoryKey = "rssMemory" ) diff --git a/core/switcher.go b/core/switcher.go index a064eb13..90f21967 100644 --- a/core/switcher.go +++ b/core/switcher.go @@ -28,13 +28,12 @@ func initSwitcher(c *Context) { var sc switcherConfig err := c.Config.GetStruct("switchers", &sc) if err != nil { - vlog.Warningf("init switcher config fail: %v", err) return } for k, v := range sc { GetSwitcherManager().Register(k, v) + vlog.Infof("[switcher] init switcher %s, value:%v", k, v) } - } func (s *SwitcherManager) Register(name string, value bool, listeners ...SwitcherListener) { diff --git a/filter/accessLog.go b/filter/accessLog.go index 2b9cc0c6..ac4a1076 100644 --- a/filter/accessLog.go +++ b/filter/accessLog.go @@ -36,20 +36,19 @@ func (t *AccessLogFilter) NewFilter(url *motan.URL) motan.Filter { func (t *AccessLogFilter) Filter(caller motan.Caller, request motan.Request) motan.Response { role := defaultRole - var ip string + var address string var start time.Time switch caller.(type) { case motan.Provider: role = serverAgentRole - ip = request.GetAttachment(motan.HostKey) + address = request.GetAttachment(motan.HostKey) start = request.GetRPCContext(true).RequestReceiveTime case motan.EndPoint: role = clientAgentRole - ip = caller.GetURL().Host + address = caller.GetURL().Host + ":" + caller.GetURL().GetPortStr() start = time.Now() } response := t.GetNext().Filter(caller, request) - address := ip + ":" + caller.GetURL().GetPortStr() if _, ok := caller.(motan.Provider); ok { reqCtx := request.GetRPCContext(true) resCtx := response.GetRPCContext(true) diff --git a/manageHandler.go b/manageHandler.go index 8cd84f0c..73e1bad6 100644 --- a/manageHandler.go +++ b/manageHandler.go @@ -799,12 +799,38 @@ func (h *HotReload) ServeHTTP(w http.ResponseWriter, r *http.Request) { } } +var ( + defaultKeys = []string{ + motan.RuntimeClustersKey, + motan.RuntimeHttpClustersKey, + motan.RuntimeExportersKey, + motan.RuntimeServersKey, + motan.RuntimeExtensionFactoryKey, + motan.RuntimeBasicKey, + } +) + type RuntimeHandler struct { - agent *Agent + agent *Agent + functions map[string]func() map[string]interface{} } func (h *RuntimeHandler) SetAgent(agent *Agent) { h.agent = agent + h.functions = map[string]func() map[string]interface{}{ + // cluster info + motan.RuntimeClustersKey: h.getClusterInfo, + // http cluster info + motan.RuntimeHttpClustersKey: h.getHttpClusterInfo, + // exporter info + motan.RuntimeExportersKey: h.getExporterInfo, + // servers info + motan.RuntimeServersKey: h.getServersInfo, + // extensionFactory info + motan.RuntimeExtensionFactoryKey: GetDefaultExtFactory().GetRuntimeInfo, + // basic info + motan.RuntimeBasicKey: h.agent.GetRuntimeInfo, + } } func (h *RuntimeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { @@ -814,24 +840,19 @@ func (h *RuntimeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { "result": "ok", motan.RuntimeInstanceTypeKey: "motan-agent", } - // cluster info - result[motan.RuntimeClustersKey] = h.getClusterInfo() - - // http cluster info - result[motan.RuntimeHttpClustersKey] = h.getHttpClusterInfo() - - // exporter info - result[motan.RuntimeExportersKey] = h.getExporterInfo() - - // extensionFactory info - result[motan.RuntimeExtensionFactoryKey] = GetDefaultExtFactory().GetRuntimeInfo() - - // servers info - result[motan.RuntimeServersKey] = h.getServersInfo() - - // basic info - result[motan.RuntimeBasicKey] = h.getBasicInfo() - + keyString := r.FormValue("keys") + var keys []string + if keyString == "" { + keys = defaultKeys + } else { + keys = strings.Split(keyString, ",") + } + for _, key := range keys { + runtimeFunc, ok := h.functions[key] + if ok { + h.addInfos(runtimeFunc(), key, result) + } + } res, _ := json.Marshal(result) w.Write(res) } @@ -888,16 +909,11 @@ func (h *RuntimeHandler) getServersInfo() map[string]interface{} { return info } -func (h *RuntimeHandler) getBasicInfo() map[string]interface{} { - info := map[string]interface{}{} - info[motan.RuntimeCpuPercentKey] = GetCpuPercent() - info[motan.RuntimeRssMemoryKey] = GetRssMemory() - return info -} - func (h *RuntimeHandler) addInfos(info map[string]interface{}, key string, result map[string]interface{}) { - if info != nil && len(info) > 0 { + if info != nil { result[key] = info + } else { + result[key] = map[string]interface{}{} } }