Skip to content

Commit

Permalink
[#9641] Add new API for agent statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
intr3p1d committed Jan 20, 2023
1 parent fdb5b0e commit bfabae7
Show file tree
Hide file tree
Showing 14 changed files with 170 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import com.navercorp.pinpoint.web.vo.timeline.inspector.InspectorTimeline;
import com.navercorp.pinpoint.web.view.tree.SimpleTreeView;
import com.navercorp.pinpoint.web.view.tree.TreeView;
import com.navercorp.pinpoint.web.vo.tree.SortByAgentInfo;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -59,6 +60,8 @@ public class AgentInfoController {

private final AgentEventService agentEventService;

private SortByAgentInfo.Rules DEFAULT_SORTBY = SortByAgentInfo.Rules.AGENT_ID_ASC;

public AgentInfoController(AgentInfoService agentInfoService, AgentEventService agentEventService) {
this.agentInfoService = Objects.requireNonNull(agentInfoService, "agentInfoService");
this.agentEventService = Objects.requireNonNull(agentEventService, "agentEventService");
Expand All @@ -76,19 +79,19 @@ public TreeView<TreeNode<AgentAndStatus>> getAgentList(
@RequestParam("to") long to) {
AgentInfoFilter filter = new DefaultAgentInfoFilter(from);
long timestamp = to;
AgentsMapByApplication allAgentsList = this.agentInfoService.getAllAgentsList(filter, Range.between(from, to));
AgentsMapByApplication<AgentAndStatus> allAgentsList = this.agentInfoService.getAllAgentsList(filter, Range.between(from, to));
return treeView(allAgentsList);
}


@GetMapping(value = "/getAgentList", params = {"!application", "timestamp"})
public TreeView<TreeNode<AgentAndStatus>> getAgentList(
@RequestParam("timestamp") long timestamp) {
AgentsMapByApplication allAgentsList = this.agentInfoService.getAllAgentsList(AgentInfoFilter::accept, Range.between(timestamp, timestamp));
AgentsMapByApplication<AgentAndStatus> allAgentsList = this.agentInfoService.getAllAgentsList(AgentInfoFilter::accept, Range.between(timestamp, timestamp));
return treeView(allAgentsList);
}

private static TreeView<TreeNode<AgentAndStatus>> treeView(AgentsMapByApplication agentsListsList) {
private static TreeView<TreeNode<AgentAndStatus>> treeView(AgentsMapByApplication<AgentAndStatus> agentsListsList) {
List<InstancesList<AgentAndStatus>> list = agentsListsList.getAgentsListsList();
return new SimpleTreeView<>(list, InstancesList::getGroupName, InstancesList::getInstancesList);
}
Expand All @@ -108,7 +111,12 @@ public TreeView<TreeNode<AgentStatusAndLink>> getAgentList(
new DefaultAgentInfoFilter(from)
);
long timestamp = to;
AgentsMapByHost list = this.agentInfoService.getAgentsListByApplicationName(currentRunFilter, applicationName, Range.between(from, to));
AgentsMapByHost list = this.agentInfoService.getAgentsListByApplicationName(
currentRunFilter,
applicationName,
Range.between(from, to),
DEFAULT_SORTBY
);
return treeView(list);
}

Expand All @@ -119,7 +127,11 @@ public TreeView<TreeNode<AgentStatusAndLink>> getAgentList(
AgentInfoFilter runningAgentFilter = new AgentInfoFilterChain(
AgentInfoFilter::filterRunning
);
AgentsMapByHost list = this.agentInfoService.getAgentsListByApplicationName(runningAgentFilter, applicationName, Range.between(timestamp, timestamp));
AgentsMapByHost list = this.agentInfoService.getAgentsListByApplicationName(runningAgentFilter,
applicationName,
Range.between(timestamp, timestamp),
DEFAULT_SORTBY
);
return treeView(list);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.navercorp.pinpoint.web.vo.agent.AgentInfoFilterChain;
import com.navercorp.pinpoint.web.vo.agent.AgentStatusAndLink;
import com.navercorp.pinpoint.web.vo.agent.DefaultAgentInfoFilter;
import com.navercorp.pinpoint.web.vo.agent.DetailedAgentInfo;
import com.navercorp.pinpoint.web.vo.tree.InstancesList;
import com.navercorp.pinpoint.web.vo.tree.AgentsMapByApplication;
import com.navercorp.pinpoint.web.vo.tree.AgentsMapByHost;
Expand Down Expand Up @@ -39,7 +40,7 @@ public AgentListController(AgentInfoService agentInfoService) {
@GetMapping(value = "/search-all")
public TreeView<InstancesList<AgentAndStatus>> getAllAgentsList() {
long timestamp = System.currentTimeMillis();
AgentsMapByApplication allAgentsList = this.agentInfoService.getAllAgentsList(
AgentsMapByApplication<AgentAndStatus> allAgentsList = this.agentInfoService.getAllAgentsList(
AgentInfoFilter::accept,
Range.between(timestamp, timestamp)
);
Expand All @@ -51,19 +52,18 @@ public TreeView<InstancesList<AgentAndStatus>> getAllAgentsList(
@RequestParam("from") long from,
@RequestParam("to") long to) {
AgentInfoFilter filter = new DefaultAgentInfoFilter(from);
AgentsMapByApplication allAgentsList = this.agentInfoService.getAllAgentsList(
AgentsMapByApplication<AgentAndStatus> allAgentsList = this.agentInfoService.getAllAgentsList(
filter,
Range.between(from, to)
);
return treeView(allAgentsList);
}

private static TreeView<InstancesList<AgentAndStatus>> treeView(AgentsMapByApplication agentsListsList) {
List<InstancesList<AgentAndStatus>> list = agentsListsList.getAgentsListsList();
private static <T> TreeView<InstancesList<T>> treeView(AgentsMapByApplication<T> agentsListsList) {
List<InstancesList<T>> list = agentsListsList.getAgentsListsList();
return new StaticTreeView<>(list);
}


@GetMapping(value = "/search-application", params = {"application"})
public TreeView<InstancesList<AgentStatusAndLink>> getAgentsList(
@RequestParam("application") String applicationName,
Expand Down Expand Up @@ -106,4 +106,26 @@ private static TreeView<InstancesList<AgentStatusAndLink>> treeView(AgentsMapByH
return new StaticTreeView<>(list);
}

@GetMapping(value = "/statistics")
public TreeView<InstancesList<DetailedAgentInfo>> getAllAgentStatistics(
) {
long timestamp = System.currentTimeMillis();
AgentsMapByApplication<DetailedAgentInfo> allAgentsList = this.agentInfoService.getAllAgentsStatisticsList(
AgentInfoFilter::accept,
Range.between(timestamp, timestamp)
);
return treeView(allAgentsList);
}

@GetMapping(value = "/statistics", params = {"from", "to"})
public TreeView<InstancesList<DetailedAgentInfo>> getAllAgentStatistics(
@RequestParam("from") long from,
@RequestParam("to") long to
) {
AgentsMapByApplication<DetailedAgentInfo> allAgentsList = this.agentInfoService.getAllAgentsStatisticsList(
AgentInfoFilter::accept,
Range.between(from, to)
);
return treeView(allAgentsList);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,6 @@ public interface AgentInfoDao {
* No ServerMetaData, No JvmInfo
*/
List<AgentInfo> getSimpleAgentInfos(List<String> agentIds, long timestamp);

List<DetailedAgentInfo> getDetailedAgentInfos(List<String> agentIds, long timestamp, boolean withServerMetadata, boolean withJVM);
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ public List<AgentInfo> getAgentInfos(List<String> agentIds, long timestamp) {
return getAgentInfos0(agentIds, timestamp, agentInfoResultsExtractor, AgentInfoColumn.simple());
}

@Override
public List<DetailedAgentInfo> getDetailedAgentInfos(List<String> agentIds, long timestamp, boolean withServerMetadata, boolean withJVM) {
return getAgentInfos0(agentIds, timestamp, detailedAgentInfoResultsExtractor, new AgentInfoColumn(true, withServerMetadata, withJVM));
}

@Override
public List<AgentInfo> getSimpleAgentInfos(List<String> agentIds, long timestamp) {
return getAgentInfos0(agentIds, timestamp, agentInfoResultsExtractor, AgentInfoColumn.simple());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.navercorp.pinpoint.web.service;

import com.navercorp.pinpoint.common.server.util.time.Range;
import com.navercorp.pinpoint.web.vo.agent.DetailedAgentInfo;
import com.navercorp.pinpoint.web.vo.tree.AgentsMapByApplication;
import com.navercorp.pinpoint.web.vo.tree.ApplicationAgentHostList;
import com.navercorp.pinpoint.web.vo.tree.AgentsMapByHost;
Expand All @@ -41,9 +42,9 @@ public interface AgentInfoService {

int NO_DURATION = -1;

AgentsMapByApplication getAllAgentsList(AgentInfoFilter filter, Range range);
AgentsMapByApplication<AgentAndStatus> getAllAgentsList(AgentInfoFilter filter, Range range);

AgentsMapByHost getAgentsListByApplicationName(AgentInfoFilter filter, String applicationName, Range range);
AgentsMapByApplication<DetailedAgentInfo> getAllAgentsStatisticsList(AgentInfoFilter filter, Range range);

AgentsMapByHost getAgentsListByApplicationName(AgentInfoFilter filter, String applicationName, Range range, SortByAgentInfo.Rules sortBy);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public AgentInfoServiceImpl(AgentEventService agentEventService,
}

@Override
public AgentsMapByApplication getAllAgentsList(AgentInfoFilter filter, Range range) {
public AgentsMapByApplication<AgentAndStatus> getAllAgentsList(AgentInfoFilter filter, Range range) {
Objects.requireNonNull(filter, "filter");

List<Application> applications = applicationIndexDao.selectAllApplicationNames();
Expand All @@ -115,15 +115,26 @@ public AgentsMapByApplication getAllAgentsList(AgentInfoFilter filter, Range ran
agents.addAll(getAgentsByApplicationName(application.getName(), range.getTo()));
}

return AgentsMapByApplication.newAgentsMapByApplication(
return AgentsMapByApplication.newAgentAndStatusMap(
filter,
agents
);
}

@Override
public AgentsMapByHost getAgentsListByApplicationName(AgentInfoFilter filter, String applicationName, Range range) {
return getAgentsListByApplicationName(filter, applicationName, range, SortByAgentInfo.Rules.AGENT_ID_ASC);
public AgentsMapByApplication<DetailedAgentInfo> getAllAgentsStatisticsList(AgentInfoFilter filter, Range range) {
Objects.requireNonNull(filter, "filter");

List<Application> applications = applicationIndexDao.selectAllApplicationNames();
List<DetailedAgentAndStatus> agents = new ArrayList<>();
for (Application application : applications) {
agents.addAll(getDetailedAgentsByApplicationName(application.getName(), range.getTo()));
}

return AgentsMapByApplication.newDetailedAgentInfoMap(
filter,
agents
);
}

@Override
Expand All @@ -137,7 +148,7 @@ public AgentsMapByHost getAgentsListByApplicationName(AgentInfoFilter filter,
Set<AgentAndStatus> agentInfoAndStatuses = getAgentsByApplicationName(applicationName, range.getTo());
AgentInfoFilter activeAgentFilter = new AgentInfoFilterChain(
filter,
x -> isActiveAgent(x.getAgentInfo().getAgentId(), range)
x -> isActiveAgent(x.getAgentId(), range)
);

if (agentInfoAndStatuses.isEmpty()) {
Expand Down Expand Up @@ -279,6 +290,37 @@ public List<AgentInfo> getAgentsByApplicationNameWithoutStatus0(String applicati

}

public Set<DetailedAgentAndStatus> getDetailedAgentsByApplicationName(String applicationName, long timestamp) {
List<DetailedAgentInfo> agentInfos = this.getDetailedAgentsByApplicationNameWithoutStatus0(applicationName, timestamp);

List<DetailedAgentAndStatus> result = new ArrayList<>(agentInfos.size());

AgentStatusQuery query = AgentStatusQuery.buildGenericQuery(agentInfos, DetailedAgentInfo::getAgentInfo, Instant.ofEpochMilli(timestamp));
List<Optional<AgentStatus>> agentStatus = this.agentLifeCycleDao.getAgentStatus(query);

for (int i = 0; i < agentStatus.size(); i++) {
Optional<AgentStatus> status = agentStatus.get(i);
DetailedAgentInfo agentInfo = agentInfos.get(i);
result.add(new DetailedAgentAndStatus(agentInfo, status.orElse(null)));
}

return new HashSet<>(result);
}

public List<DetailedAgentInfo> getDetailedAgentsByApplicationNameWithoutStatus0(String applicationName, long timestamp) {
Objects.requireNonNull(applicationName, "applicationName");
if (timestamp < 0) {
throw new IllegalArgumentException("timestamp must not be less than 0");
}

List<String> agentIds = this.applicationIndexDao.selectAgentIds(applicationName);
List<DetailedAgentInfo> agentInfos = this.agentInfoDao.getDetailedAgentInfos(agentIds, timestamp, false, true);

return agentInfos.stream()
.filter(Objects::nonNull)
.collect(Collectors.toList());
}

@Override
public Set<AgentAndStatus> getRecentAgentsByApplicationName(String applicationName, long timestamp, long timeDiff) {
if (timeDiff > timestamp) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@ public interface AgentInfoFilter {
boolean ACCEPT = true;
boolean REJECT = false;

boolean filter(AgentAndStatus agentInfo);
boolean filter(AgentStatus agentStatus);

static boolean accept(AgentAndStatus agentAndStatus) {
static boolean accept(AgentStatus agentStatus) {
return ACCEPT;
}

static boolean reject(AgentAndStatus agentAndStatus) {
static boolean reject(AgentStatus agentStatus) {
return REJECT;
}

static boolean filterRunning(AgentAndStatus agentAndStatus) {
final AgentStatus agentStatus = agentAndStatus.getStatus();
static boolean filterRunning(AgentStatus agentStatus) {
if (agentStatus == null) {
return REJECT;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ public AgentInfoFilterChain(AgentInfoFilter... agentInfoFilters) {
}

@Override
public boolean filter(AgentAndStatus agentAndStatus) {
public boolean filter(AgentStatus agentStatus) {
for (AgentInfoFilter agentFilter : this.agentInfoFilters) {
if (agentFilter.filter(agentAndStatus) == REJECT) {
if (agentFilter.filter(agentStatus) == REJECT) {
return REJECT;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ public static AgentStatusQuery buildQuery(Collection<AgentInfo> agentInfos, Inst
return buildQuery(agentInfos, AgentStatusQuery::apply, timestamp);
}

public static <T> AgentStatusQuery buildGenericQuery(Collection<T> agentInfos, Function<T, AgentInfo> agentInfoFunction, Instant timestamp) {
return buildQuery(agentInfos, agentInfoFunction.andThen(AgentStatusQuery::apply), timestamp);
}

private static SimpleAgentKey apply(AgentInfo agentInfo) {
if (agentInfo == null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ public DefaultAgentInfoFilter(long from) {
}

@Override
public boolean filter(AgentAndStatus agentAndStatus) {
final AgentStatus agentStatus = agentAndStatus.getStatus();
public boolean filter(AgentStatus agentStatus) {
if (agentStatus == null) {
return REJECT;
}
Expand Down
Loading

0 comments on commit bfabae7

Please sign in to comment.