Skip to content

Commit

Permalink
update runtime info
Browse files Browse the repository at this point in the history
  • Loading branch information
wuhua3 committed Oct 28, 2024
1 parent 7179c8d commit 4a03836
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 33 deletions.
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
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(transCommandsToRecordInfo(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 transCommandsToRecordInfo(commands []*motan.URL) []string {
if len(commands) == 0 {
return []string{}
}
infoList := make([]string, len(commands))
for i, command := range commands {
infoList[i] = command.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"
)
2 changes: 0 additions & 2 deletions core/switcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,11 @@ 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)
}

}

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
66 changes: 40 additions & 26 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,13 +909,6 @@ 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 {
result[key] = info
Expand Down

0 comments on commit 4a03836

Please sign in to comment.