Skip to content

Commit 87f04a5

Browse files
committed
[#9903] add system metric host management function
1 parent cec5c0a commit 87f04a5

19 files changed

+731
-10
lines changed

metric-module/metric/src/main/java/com/navercorp/pinpoint/metric/web/MetricWebApp.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.navercorp.pinpoint.metric.web;
22

33

4+
import com.navercorp.pinpoint.metric.web.config.MetricWebMysqlDaoConfiguration;
45
import com.navercorp.pinpoint.metric.web.config.MetricWebPinotDaoConfiguration;
56
import com.navercorp.pinpoint.pinot.config.PinotConfiguration;
67
import org.springframework.context.annotation.ComponentScan;
@@ -14,7 +15,8 @@
1415
@Import({
1516
WebMetricPropertySources.class,
1617
MetricWebPinotDaoConfiguration.class,
17-
PinotConfiguration.class
18+
PinotConfiguration.class,
19+
MetricWebMysqlDaoConfiguration.class
1820
})
1921
@Profile("metric")
2022
public class MetricWebApp {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.navercorp.pinpoint.metric.web.authorization.controller;
2+
3+
import com.navercorp.pinpoint.metric.web.service.SystemMetricHostExclusionService;
4+
import com.navercorp.pinpoint.pinot.tenant.TenantProvider;
5+
import org.apache.logging.log4j.LogManager;
6+
import org.apache.logging.log4j.Logger;
7+
import org.springframework.web.bind.annotation.RequestMapping;
8+
import org.springframework.web.bind.annotation.RequestParam;
9+
import org.springframework.web.bind.annotation.RestController;
10+
11+
@RestController
12+
@RequestMapping(value = "/admin/systemMetric")
13+
public class SystemMetricHostExclusionController {
14+
private final Logger logger = LogManager.getLogger(this.getClass());
15+
16+
private final SystemMetricHostExclusionService systemMetricHostExclusionService;
17+
private final TenantProvider tenantProvider;
18+
19+
public SystemMetricHostExclusionController(SystemMetricHostExclusionService systemMetricHostExclusionService, TenantProvider tenantProvider) {
20+
this.systemMetricHostExclusionService = systemMetricHostExclusionService;
21+
this.tenantProvider = tenantProvider;
22+
}
23+
24+
@RequestMapping(value = "/hostGroup/addExclusion")
25+
public String excludeHostGroup(@RequestParam("hostGroupName") String hostGroupName) {
26+
logger.info("add hostGroup exclusion - hostGroupName: [{}]", hostGroupName);
27+
try {
28+
String tenantId = tenantProvider.getTenantId();
29+
systemMetricHostExclusionService.insertHostGroupExclusion(tenantId, hostGroupName);
30+
return "OK";
31+
} catch (Exception e) {
32+
logger.error("error while excluding hostGroupName", e);
33+
return e.getMessage();
34+
}
35+
}
36+
37+
@RequestMapping(value = "/hostGroup/host/addExclusion")
38+
public String excludeHostGroup(@RequestParam("hostGroupName") String hostGroupName,
39+
@RequestParam("hostName") String hostName) {
40+
logger.info("add host exclusion - hostGroupName: [{}], hostName: [{}]", hostGroupName, hostName);
41+
try {
42+
String tenantId = tenantProvider.getTenantId();
43+
systemMetricHostExclusionService.insertHostExclusion(tenantId, hostGroupName, hostName);
44+
return "OK";
45+
} catch (Exception e) {
46+
logger.error("error while excluding hostName", e);
47+
return e.getMessage();
48+
}
49+
}
50+
51+
@RequestMapping(value = "/hostGroup/removeExclusion")
52+
public String acceptHostGroup(@RequestParam("hostGroupName") String hostGroupName) {
53+
logger.info("remove host group exclusion - hostGroupName: [{}]", hostGroupName);
54+
try {
55+
String tenantId = tenantProvider.getTenantId();
56+
systemMetricHostExclusionService.deleteHostGroupExclusion(tenantId, hostGroupName);
57+
return "OK";
58+
} catch (Exception e) {
59+
logger.error("error while accepting hostGroupName", e);
60+
return e.getMessage();
61+
}
62+
}
63+
64+
@RequestMapping(value = "/hostGroup/host/removeExclusion")
65+
public String acceptHostGroup(@RequestParam("hostGroupName") String hostGroupName,
66+
@RequestParam("hostName") String hostName) {
67+
logger.info("remove host exclusion - hostGroupName: [{}], hostName: [{}]", hostGroupName, hostName);
68+
try {
69+
String tenantId = tenantProvider.getTenantId();
70+
systemMetricHostExclusionService.deleteHostExclusion(tenantId, hostGroupName, hostName);
71+
return "OK";
72+
} catch (Exception e) {
73+
logger.error("error while accepting hostName", e);
74+
return e.getMessage();
75+
}
76+
}
77+
78+
@RequestMapping(value = "/hostGroup/removeUnusedExclusions")
79+
public String cleanupUnusedExclusions(@RequestParam("hostGroupName") String hostGroupName) {
80+
logger.info("remove unused hostGroup exclusion and host exclusions - hostGroupName: [{}]", hostGroupName);
81+
try {
82+
String tenantId = tenantProvider.getTenantId();
83+
systemMetricHostExclusionService.deleteUnusedHostExclusion(tenantId, hostGroupName);
84+
return "OK";
85+
} catch (Exception e) {
86+
logger.error("error while removing unused exclusions", e);
87+
return e.getMessage();
88+
}
89+
}
90+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.navercorp.pinpoint.metric.web.cache;
2+
3+
import com.github.benmanes.caffeine.cache.Caffeine;
4+
import org.springframework.cache.CacheManager;
5+
import org.springframework.cache.annotation.EnableCaching;
6+
import org.springframework.cache.caffeine.CaffeineCacheManager;
7+
import org.springframework.context.annotation.Bean;
8+
import org.springframework.context.annotation.Configuration;
9+
10+
import java.util.concurrent.TimeUnit;
11+
12+
@Configuration
13+
@EnableCaching
14+
public class MetricWebCacheConfiguration {
15+
16+
public static final String METRIC_HOST_GROUP_EXCLUSION_CACHE_NAME = "metricHostGroupExclusion";
17+
public static final String METRIC_HOST_EXCLUSION_CACHE_NAME = "metricHostExclusion";
18+
19+
@Bean
20+
public CacheManager metricHostGroupExclusion() {
21+
CaffeineCacheManager caffeineCacheManager = new CaffeineCacheManager(METRIC_HOST_GROUP_EXCLUSION_CACHE_NAME);
22+
caffeineCacheManager.setCaffeine(Caffeine.newBuilder()
23+
.expireAfterWrite(300, TimeUnit.SECONDS));
24+
return caffeineCacheManager;
25+
}
26+
27+
@Bean
28+
public CacheManager metricHostExclusion() {
29+
CaffeineCacheManager caffeineCacheManager = new CaffeineCacheManager(METRIC_HOST_EXCLUSION_CACHE_NAME);
30+
caffeineCacheManager.setCaffeine(Caffeine.newBuilder()
31+
.expireAfterWrite(300, TimeUnit.SECONDS));
32+
return caffeineCacheManager;
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.navercorp.pinpoint.metric.web.config;
2+
3+
import com.navercorp.pinpoint.metric.collector.config.MyBatisRegistryHandler;
4+
import com.navercorp.pinpoint.pinot.mybatis.MyBatisConfiguration;
5+
import org.apache.ibatis.session.Configuration;
6+
import org.apache.ibatis.session.SqlSessionFactory;
7+
import org.apache.logging.log4j.LogManager;
8+
import org.apache.logging.log4j.Logger;
9+
import org.mybatis.spring.SqlSessionFactoryBean;
10+
import org.mybatis.spring.SqlSessionTemplate;
11+
import org.springframework.beans.factory.annotation.Qualifier;
12+
import org.springframework.beans.factory.annotation.Value;
13+
import org.springframework.context.annotation.Bean;
14+
import org.springframework.core.io.Resource;
15+
16+
import javax.sql.DataSource;
17+
18+
@org.springframework.context.annotation.Configuration
19+
public class MetricWebMysqlDaoConfiguration {
20+
private final Logger logger = LogManager.getLogger(MetricWebMysqlDaoConfiguration.class);
21+
22+
@Bean
23+
public SqlSessionFactoryBean metricSqlSessionFactory(
24+
@Qualifier("dataSource") DataSource dataSource,
25+
@Value("classpath*:/pinot-web/mapper/mysql/*Mapper.xml") Resource[] mappers) {
26+
27+
for (Resource mapper : mappers) {
28+
logger.info("Mapper location: {}", mapper.getDescription());
29+
}
30+
31+
SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
32+
sessionFactoryBean.setDataSource(dataSource);
33+
sessionFactoryBean.setMapperLocations(mappers);
34+
35+
Configuration config = MyBatisConfiguration.defaultConfiguration();
36+
sessionFactoryBean.setConfiguration(config);
37+
38+
MyBatisRegistryHandler registry = registryHandler();
39+
registry.registerTypeAlias(config.getTypeAliasRegistry());
40+
41+
sessionFactoryBean.setFailFast(true);
42+
43+
return sessionFactoryBean;
44+
}
45+
46+
private MyBatisRegistryHandler registryHandler() {
47+
return new WebRegistryHandler();
48+
}
49+
50+
@Bean
51+
public SqlSessionTemplate metricSqlSessionTemplate(
52+
@Qualifier("metricSqlSessionFactory") SqlSessionFactory sessionFactory) {
53+
return new SqlSessionTemplate(sessionFactory);
54+
}
55+
}

metric-module/metric/src/main/java/com/navercorp/pinpoint/metric/web/controller/SystemMetricController.java

+12-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
import com.navercorp.pinpoint.metric.web.model.MetricDataSearchKey;
2121
import com.navercorp.pinpoint.metric.web.model.MetricInfo;
2222
import com.navercorp.pinpoint.metric.web.model.SystemMetricData;
23+
import com.navercorp.pinpoint.metric.web.view.SystemMetricHostGroupExclusionInfo;
2324
import com.navercorp.pinpoint.metric.web.service.SystemMetricDataService;
25+
import com.navercorp.pinpoint.metric.web.service.SystemMetricHostExclusionService;
2426
import com.navercorp.pinpoint.metric.web.service.SystemMetricHostInfoService;
2527
import com.navercorp.pinpoint.metric.web.service.YMLSystemMetricBasicGroupManager;
2628
import com.navercorp.pinpoint.metric.web.util.Range;
@@ -46,17 +48,20 @@
4648
public class SystemMetricController {
4749
private final SystemMetricDataService systemMetricDataService;
4850
private final SystemMetricHostInfoService systemMetricHostInfoService;
51+
private final SystemMetricHostExclusionService systemMetricHostExclusionService;
4952
private final YMLSystemMetricBasicGroupManager systemMetricBasicGroupManager;
5053
private final TenantProvider tenantProvider;
5154

5255
private final TimeWindowSampler DEFAULT_TIME_WINDOW_SAMPLER = new TimeWindowSlotCentricSampler(10000L, 200);
53-
56+
5457
public SystemMetricController(SystemMetricDataService systemMetricDataService,
5558
SystemMetricHostInfoService systemMetricHostInfoService,
59+
SystemMetricHostExclusionService systemMetricHostExclusionService,
5660
YMLSystemMetricBasicGroupManager systemMetricBasicGroupManager,
5761
TenantProvider tenantProvider) {
5862
this.systemMetricDataService = Objects.requireNonNull(systemMetricDataService, "systemMetricService");
5963
this.systemMetricHostInfoService = Objects.requireNonNull(systemMetricHostInfoService, "systemMetricHostInfoService");
64+
this.systemMetricHostExclusionService = Objects.requireNonNull(systemMetricHostExclusionService, "systemMetricHostExclusionService");
6065
this.systemMetricBasicGroupManager = Objects.requireNonNull(systemMetricBasicGroupManager, "systemMetricBasicGroupManager");
6166
this.tenantProvider = Objects.requireNonNull(tenantProvider, "tenantProvider");
6267
}
@@ -102,4 +107,10 @@ public SystemMetricView getCollectedMetricData(@RequestParam("hostGroupName") St
102107
SystemMetricData<? extends Number> systemMetricData = systemMetricDataService.getCollectedMetricData(metricDataSearchKey, timeWindow, tagList);
103108
return new SystemMetricView(systemMetricData);
104109
}
110+
111+
@GetMapping(value = "/hostGroup/exclusionInfo")
112+
public SystemMetricHostGroupExclusionInfo getHostGroupExclusionInfo(@RequestParam("hostGroupName") String hostGroupName) {
113+
String tenantId = tenantProvider.getTenantId();
114+
return systemMetricHostExclusionService.getHostGroupExclusionInfo(tenantId, hostGroupName);
115+
}
105116
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.navercorp.pinpoint.metric.web.dao;
2+
3+
import java.util.List;
4+
5+
public interface SystemMetricHostExclusionDao {
6+
7+
List<String> selectExcludedHostGroupNameList(String tenantId);
8+
9+
void insertMetricHostGroupExclusion(String tenantId, String hostGroupName);
10+
11+
void deleteMetricHostGroupExclusion(String tenantId, String hostGroupName);
12+
13+
List<String> selectExcludedHostNameList(String tenantId, String hostGroupName);
14+
15+
void insertMetricHostExclusion(String tenantId, String hostGroupName, String hostName);
16+
17+
void deleteMetricHostExclusion(String tenantId, String hostGroupName, String hostName);
18+
}

metric-module/metric/src/main/java/com/navercorp/pinpoint/metric/web/dao/model/HostInfoSearchKey.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,16 @@ public class HostInfoSearchKey {
2525

2626
private final String tenantId;
2727
private final String hostGroupName;
28+
private final String hostName;
2829

29-
public HostInfoSearchKey(String tenantId, String hostGroupName) {
30+
public HostInfoSearchKey (String tenantId, String hostGroupName) {
31+
this(tenantId, hostGroupName, "");
32+
}
33+
34+
public HostInfoSearchKey(String tenantId, String hostGroupName, String hostName) {
3035
this.tenantId = Objects.requireNonNull(tenantId, "tenantId");
3136
this.hostGroupName = Objects.requireNonNull(hostGroupName, "hostGroupName");
37+
this.hostName = Objects.requireNonNull(hostName, "hostName");
3238
}
3339

3440
public String getTenantId() {
@@ -38,4 +44,8 @@ public String getTenantId() {
3844
public String getHostGroupName() {
3945
return hostGroupName;
4046
}
47+
48+
public String getHostName() {
49+
return hostName;
50+
}
4151
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.navercorp.pinpoint.metric.web.dao.mysql;
2+
3+
import com.navercorp.pinpoint.metric.web.dao.SystemMetricHostExclusionDao;
4+
import com.navercorp.pinpoint.metric.web.dao.model.HostInfoSearchKey;
5+
import org.mybatis.spring.SqlSessionTemplate;
6+
import org.springframework.beans.factory.annotation.Qualifier;
7+
import org.springframework.context.annotation.Primary;
8+
import org.springframework.stereotype.Repository;
9+
10+
import java.util.List;
11+
import java.util.Objects;
12+
13+
@Primary
14+
@Repository
15+
public class MysqlSystemMetricHostExclusionDao implements SystemMetricHostExclusionDao {
16+
17+
private static final String NAMESPACE = MysqlSystemMetricHostExclusionDao.class.getName() + ".";
18+
19+
private final SqlSessionTemplate sqlMetricSessionTemplate;
20+
21+
public MysqlSystemMetricHostExclusionDao(@Qualifier("metricSqlSessionTemplate") SqlSessionTemplate sqlMetricSessionTemplate) {
22+
this.sqlMetricSessionTemplate = Objects.requireNonNull(sqlMetricSessionTemplate, "sqlSessionTemplate");
23+
}
24+
25+
@Override
26+
public List<String> selectExcludedHostGroupNameList(String tenantId) {
27+
return sqlMetricSessionTemplate.selectList(NAMESPACE + "selectExcludedHostGroupNames", tenantId);
28+
}
29+
30+
@Override
31+
public void insertMetricHostGroupExclusion(String tenantId, String hostGroupName) {
32+
sqlMetricSessionTemplate.insert(NAMESPACE + "insertHostGroupExclusion", new HostInfoSearchKey(tenantId, hostGroupName));
33+
}
34+
35+
@Override
36+
public void deleteMetricHostGroupExclusion(String tenantId, String hostGroupName) {
37+
sqlMetricSessionTemplate.delete(NAMESPACE + "deleteHostGroupExclusion", new HostInfoSearchKey(tenantId, hostGroupName));
38+
}
39+
40+
@Override
41+
public List<String> selectExcludedHostNameList(String tenantId, String hostGroupName) {
42+
return sqlMetricSessionTemplate.selectList(NAMESPACE + "selectExcludedHostNames", new HostInfoSearchKey(tenantId, hostGroupName));
43+
}
44+
45+
@Override
46+
public void insertMetricHostExclusion(String tenantId, String hostGroupName, String hostName) {
47+
sqlMetricSessionTemplate.insert(NAMESPACE + "insertHostExclusion", new HostInfoSearchKey(tenantId, hostGroupName, hostName));
48+
}
49+
50+
@Override
51+
public void deleteMetricHostExclusion(String tenantId, String hostGroupName, String hostName) {
52+
sqlMetricSessionTemplate.delete(NAMESPACE + "deleteHostExclusion", new HostInfoSearchKey(tenantId, hostGroupName, hostName));
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.navercorp.pinpoint.metric.web.service;
2+
3+
4+
import com.navercorp.pinpoint.metric.web.cache.MetricWebCacheConfiguration;
5+
import com.navercorp.pinpoint.metric.web.dao.SystemMetricHostExclusionDao;
6+
import org.apache.logging.log4j.LogManager;
7+
import org.apache.logging.log4j.Logger;
8+
import org.springframework.cache.annotation.Cacheable;
9+
import org.springframework.stereotype.Component;
10+
11+
import java.util.Collections;
12+
import java.util.List;
13+
import java.util.Objects;
14+
15+
@Component
16+
public class SystemMetricHostExclusionCache {
17+
private final Logger logger = LogManager.getLogger(this.getClass());
18+
private final SystemMetricHostExclusionDao systemMetricHostExclusionDao;
19+
20+
public SystemMetricHostExclusionCache(SystemMetricHostExclusionDao systemMetricHostExclusionDao) {
21+
this.systemMetricHostExclusionDao = Objects.requireNonNull(systemMetricHostExclusionDao, "systemMetricHostExclusionDao");
22+
}
23+
24+
@Cacheable(cacheNames = "metricHostGroupExclusion", key = "#tenantId", cacheManager = MetricWebCacheConfiguration.METRIC_HOST_GROUP_EXCLUSION_CACHE_NAME)
25+
public List<String> getExcludedHostGroupNameList(String tenantId) {
26+
try {
27+
return systemMetricHostExclusionDao.selectExcludedHostGroupNameList(tenantId);
28+
} catch (Exception e) {
29+
logger.warn("error getting excludedHostGroupNameList, return empty list.", e);
30+
return Collections.emptyList();
31+
}
32+
}
33+
34+
@Cacheable(cacheNames = "metricHostExclusion", key = "{#tenantId, #hostGroupName}", cacheManager = MetricWebCacheConfiguration.METRIC_HOST_EXCLUSION_CACHE_NAME)
35+
public List<String> getExcludedHostNameList(String tenantId, String hostGroupName) {
36+
try {
37+
return systemMetricHostExclusionDao.selectExcludedHostNameList(tenantId, hostGroupName);
38+
} catch (Exception e) {
39+
logger.warn("error getting excludedHostNameList, return empty list.", e);
40+
return Collections.emptyList();
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)