Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update runtime info #407

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const (
)

var (
startTime = time.Now()
initParamLock sync.Mutex
setAgentLock sync.Mutex
notFoundProviderCount int64 = 0
Expand Down Expand Up @@ -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)
}
Expand Down
27 changes: 20 additions & 7 deletions agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 12 additions & 1 deletion cluster/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, url := range urls {
infoList[i] = url.GetIdentityWithRegistry()
}
return infoList
}

func processRoute(urls []*motan.URL, routers []string) []*motan.URL {
if len(urls) > 0 && len(routers) > 0 {
lastURLs := urls
Expand Down
1 change: 1 addition & 0 deletions core/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ const (

// -----------basic keys-------------

RuntimeStartTimeKey = "startTime"
RuntimeCpuPercentKey = "cpuPercent"
RuntimeRssMemoryKey = "rssMemory"
)
3 changes: 1 addition & 2 deletions core/switcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
7 changes: 3 additions & 4 deletions filter/accessLog.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
70 changes: 43 additions & 27 deletions manageHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
}
Expand Down Expand Up @@ -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{}{}
}
}

Expand Down
Loading