Skip to content

Commit

Permalink
[#9903] add system metric host management function
Browse files Browse the repository at this point in the history
  • Loading branch information
donghun-cho committed Apr 27, 2023
1 parent cec5c0a commit 87f04a5
Show file tree
Hide file tree
Showing 19 changed files with 731 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.navercorp.pinpoint.metric.web;


import com.navercorp.pinpoint.metric.web.config.MetricWebMysqlDaoConfiguration;
import com.navercorp.pinpoint.metric.web.config.MetricWebPinotDaoConfiguration;
import com.navercorp.pinpoint.pinot.config.PinotConfiguration;
import org.springframework.context.annotation.ComponentScan;
Expand All @@ -14,7 +15,8 @@
@Import({
WebMetricPropertySources.class,
MetricWebPinotDaoConfiguration.class,
PinotConfiguration.class
PinotConfiguration.class,
MetricWebMysqlDaoConfiguration.class
})
@Profile("metric")
public class MetricWebApp {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.navercorp.pinpoint.metric.web.authorization.controller;

import com.navercorp.pinpoint.metric.web.service.SystemMetricHostExclusionService;
import com.navercorp.pinpoint.pinot.tenant.TenantProvider;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/admin/systemMetric")
public class SystemMetricHostExclusionController {
private final Logger logger = LogManager.getLogger(this.getClass());

private final SystemMetricHostExclusionService systemMetricHostExclusionService;
private final TenantProvider tenantProvider;

public SystemMetricHostExclusionController(SystemMetricHostExclusionService systemMetricHostExclusionService, TenantProvider tenantProvider) {
this.systemMetricHostExclusionService = systemMetricHostExclusionService;
this.tenantProvider = tenantProvider;
}

@RequestMapping(value = "/hostGroup/addExclusion")
public String excludeHostGroup(@RequestParam("hostGroupName") String hostGroupName) {
logger.info("add hostGroup exclusion - hostGroupName: [{}]", hostGroupName);
try {
String tenantId = tenantProvider.getTenantId();
systemMetricHostExclusionService.insertHostGroupExclusion(tenantId, hostGroupName);
return "OK";
} catch (Exception e) {
logger.error("error while excluding hostGroupName", e);
return e.getMessage();
}
}

@RequestMapping(value = "/hostGroup/host/addExclusion")
public String excludeHostGroup(@RequestParam("hostGroupName") String hostGroupName,
@RequestParam("hostName") String hostName) {
logger.info("add host exclusion - hostGroupName: [{}], hostName: [{}]", hostGroupName, hostName);
try {
String tenantId = tenantProvider.getTenantId();
systemMetricHostExclusionService.insertHostExclusion(tenantId, hostGroupName, hostName);
return "OK";
} catch (Exception e) {
logger.error("error while excluding hostName", e);
return e.getMessage();
}
}

@RequestMapping(value = "/hostGroup/removeExclusion")
public String acceptHostGroup(@RequestParam("hostGroupName") String hostGroupName) {
logger.info("remove host group exclusion - hostGroupName: [{}]", hostGroupName);
try {
String tenantId = tenantProvider.getTenantId();
systemMetricHostExclusionService.deleteHostGroupExclusion(tenantId, hostGroupName);
return "OK";
} catch (Exception e) {
logger.error("error while accepting hostGroupName", e);
return e.getMessage();
}
}

@RequestMapping(value = "/hostGroup/host/removeExclusion")
public String acceptHostGroup(@RequestParam("hostGroupName") String hostGroupName,
@RequestParam("hostName") String hostName) {
logger.info("remove host exclusion - hostGroupName: [{}], hostName: [{}]", hostGroupName, hostName);
try {
String tenantId = tenantProvider.getTenantId();
systemMetricHostExclusionService.deleteHostExclusion(tenantId, hostGroupName, hostName);
return "OK";
} catch (Exception e) {
logger.error("error while accepting hostName", e);
return e.getMessage();
}
}

@RequestMapping(value = "/hostGroup/removeUnusedExclusions")
public String cleanupUnusedExclusions(@RequestParam("hostGroupName") String hostGroupName) {
logger.info("remove unused hostGroup exclusion and host exclusions - hostGroupName: [{}]", hostGroupName);
try {
String tenantId = tenantProvider.getTenantId();
systemMetricHostExclusionService.deleteUnusedHostExclusion(tenantId, hostGroupName);
return "OK";
} catch (Exception e) {
logger.error("error while removing unused exclusions", e);
return e.getMessage();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.navercorp.pinpoint.metric.web.cache;

import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

@Configuration
@EnableCaching
public class MetricWebCacheConfiguration {

public static final String METRIC_HOST_GROUP_EXCLUSION_CACHE_NAME = "metricHostGroupExclusion";
public static final String METRIC_HOST_EXCLUSION_CACHE_NAME = "metricHostExclusion";

@Bean
public CacheManager metricHostGroupExclusion() {
CaffeineCacheManager caffeineCacheManager = new CaffeineCacheManager(METRIC_HOST_GROUP_EXCLUSION_CACHE_NAME);
caffeineCacheManager.setCaffeine(Caffeine.newBuilder()
.expireAfterWrite(300, TimeUnit.SECONDS));
return caffeineCacheManager;
}

@Bean
public CacheManager metricHostExclusion() {
CaffeineCacheManager caffeineCacheManager = new CaffeineCacheManager(METRIC_HOST_EXCLUSION_CACHE_NAME);
caffeineCacheManager.setCaffeine(Caffeine.newBuilder()
.expireAfterWrite(300, TimeUnit.SECONDS));
return caffeineCacheManager;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.navercorp.pinpoint.metric.web.config;

import com.navercorp.pinpoint.metric.collector.config.MyBatisRegistryHandler;
import com.navercorp.pinpoint.pinot.mybatis.MyBatisConfiguration;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.Resource;

import javax.sql.DataSource;

@org.springframework.context.annotation.Configuration
public class MetricWebMysqlDaoConfiguration {
private final Logger logger = LogManager.getLogger(MetricWebMysqlDaoConfiguration.class);

@Bean
public SqlSessionFactoryBean metricSqlSessionFactory(
@Qualifier("dataSource") DataSource dataSource,
@Value("classpath*:/pinot-web/mapper/mysql/*Mapper.xml") Resource[] mappers) {

for (Resource mapper : mappers) {
logger.info("Mapper location: {}", mapper.getDescription());
}

SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
sessionFactoryBean.setDataSource(dataSource);
sessionFactoryBean.setMapperLocations(mappers);

Configuration config = MyBatisConfiguration.defaultConfiguration();
sessionFactoryBean.setConfiguration(config);

MyBatisRegistryHandler registry = registryHandler();
registry.registerTypeAlias(config.getTypeAliasRegistry());

sessionFactoryBean.setFailFast(true);

return sessionFactoryBean;
}

private MyBatisRegistryHandler registryHandler() {
return new WebRegistryHandler();
}

@Bean
public SqlSessionTemplate metricSqlSessionTemplate(
@Qualifier("metricSqlSessionFactory") SqlSessionFactory sessionFactory) {
return new SqlSessionTemplate(sessionFactory);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import com.navercorp.pinpoint.metric.web.model.MetricDataSearchKey;
import com.navercorp.pinpoint.metric.web.model.MetricInfo;
import com.navercorp.pinpoint.metric.web.model.SystemMetricData;
import com.navercorp.pinpoint.metric.web.view.SystemMetricHostGroupExclusionInfo;
import com.navercorp.pinpoint.metric.web.service.SystemMetricDataService;
import com.navercorp.pinpoint.metric.web.service.SystemMetricHostExclusionService;
import com.navercorp.pinpoint.metric.web.service.SystemMetricHostInfoService;
import com.navercorp.pinpoint.metric.web.service.YMLSystemMetricBasicGroupManager;
import com.navercorp.pinpoint.metric.web.util.Range;
Expand All @@ -46,17 +48,20 @@
public class SystemMetricController {
private final SystemMetricDataService systemMetricDataService;
private final SystemMetricHostInfoService systemMetricHostInfoService;
private final SystemMetricHostExclusionService systemMetricHostExclusionService;
private final YMLSystemMetricBasicGroupManager systemMetricBasicGroupManager;
private final TenantProvider tenantProvider;

private final TimeWindowSampler DEFAULT_TIME_WINDOW_SAMPLER = new TimeWindowSlotCentricSampler(10000L, 200);

public SystemMetricController(SystemMetricDataService systemMetricDataService,
SystemMetricHostInfoService systemMetricHostInfoService,
SystemMetricHostExclusionService systemMetricHostExclusionService,
YMLSystemMetricBasicGroupManager systemMetricBasicGroupManager,
TenantProvider tenantProvider) {
this.systemMetricDataService = Objects.requireNonNull(systemMetricDataService, "systemMetricService");
this.systemMetricHostInfoService = Objects.requireNonNull(systemMetricHostInfoService, "systemMetricHostInfoService");
this.systemMetricHostExclusionService = Objects.requireNonNull(systemMetricHostExclusionService, "systemMetricHostExclusionService");
this.systemMetricBasicGroupManager = Objects.requireNonNull(systemMetricBasicGroupManager, "systemMetricBasicGroupManager");
this.tenantProvider = Objects.requireNonNull(tenantProvider, "tenantProvider");
}
Expand Down Expand Up @@ -102,4 +107,10 @@ public SystemMetricView getCollectedMetricData(@RequestParam("hostGroupName") St
SystemMetricData<? extends Number> systemMetricData = systemMetricDataService.getCollectedMetricData(metricDataSearchKey, timeWindow, tagList);
return new SystemMetricView(systemMetricData);
}

@GetMapping(value = "/hostGroup/exclusionInfo")
public SystemMetricHostGroupExclusionInfo getHostGroupExclusionInfo(@RequestParam("hostGroupName") String hostGroupName) {
String tenantId = tenantProvider.getTenantId();
return systemMetricHostExclusionService.getHostGroupExclusionInfo(tenantId, hostGroupName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.navercorp.pinpoint.metric.web.dao;

import java.util.List;

public interface SystemMetricHostExclusionDao {

List<String> selectExcludedHostGroupNameList(String tenantId);

void insertMetricHostGroupExclusion(String tenantId, String hostGroupName);

void deleteMetricHostGroupExclusion(String tenantId, String hostGroupName);

List<String> selectExcludedHostNameList(String tenantId, String hostGroupName);

void insertMetricHostExclusion(String tenantId, String hostGroupName, String hostName);

void deleteMetricHostExclusion(String tenantId, String hostGroupName, String hostName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,16 @@ public class HostInfoSearchKey {

private final String tenantId;
private final String hostGroupName;
private final String hostName;

public HostInfoSearchKey(String tenantId, String hostGroupName) {
public HostInfoSearchKey (String tenantId, String hostGroupName) {
this(tenantId, hostGroupName, "");
}

public HostInfoSearchKey(String tenantId, String hostGroupName, String hostName) {
this.tenantId = Objects.requireNonNull(tenantId, "tenantId");
this.hostGroupName = Objects.requireNonNull(hostGroupName, "hostGroupName");
this.hostName = Objects.requireNonNull(hostName, "hostName");
}

public String getTenantId() {
Expand All @@ -38,4 +44,8 @@ public String getTenantId() {
public String getHostGroupName() {
return hostGroupName;
}

public String getHostName() {
return hostName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.navercorp.pinpoint.metric.web.dao.mysql;

import com.navercorp.pinpoint.metric.web.dao.SystemMetricHostExclusionDao;
import com.navercorp.pinpoint.metric.web.dao.model.HostInfoSearchKey;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Objects;

@Primary
@Repository
public class MysqlSystemMetricHostExclusionDao implements SystemMetricHostExclusionDao {

private static final String NAMESPACE = MysqlSystemMetricHostExclusionDao.class.getName() + ".";

private final SqlSessionTemplate sqlMetricSessionTemplate;

public MysqlSystemMetricHostExclusionDao(@Qualifier("metricSqlSessionTemplate") SqlSessionTemplate sqlMetricSessionTemplate) {
this.sqlMetricSessionTemplate = Objects.requireNonNull(sqlMetricSessionTemplate, "sqlSessionTemplate");
}

@Override
public List<String> selectExcludedHostGroupNameList(String tenantId) {
return sqlMetricSessionTemplate.selectList(NAMESPACE + "selectExcludedHostGroupNames", tenantId);
}

@Override
public void insertMetricHostGroupExclusion(String tenantId, String hostGroupName) {
sqlMetricSessionTemplate.insert(NAMESPACE + "insertHostGroupExclusion", new HostInfoSearchKey(tenantId, hostGroupName));
}

@Override
public void deleteMetricHostGroupExclusion(String tenantId, String hostGroupName) {
sqlMetricSessionTemplate.delete(NAMESPACE + "deleteHostGroupExclusion", new HostInfoSearchKey(tenantId, hostGroupName));
}

@Override
public List<String> selectExcludedHostNameList(String tenantId, String hostGroupName) {
return sqlMetricSessionTemplate.selectList(NAMESPACE + "selectExcludedHostNames", new HostInfoSearchKey(tenantId, hostGroupName));
}

@Override
public void insertMetricHostExclusion(String tenantId, String hostGroupName, String hostName) {
sqlMetricSessionTemplate.insert(NAMESPACE + "insertHostExclusion", new HostInfoSearchKey(tenantId, hostGroupName, hostName));
}

@Override
public void deleteMetricHostExclusion(String tenantId, String hostGroupName, String hostName) {
sqlMetricSessionTemplate.delete(NAMESPACE + "deleteHostExclusion", new HostInfoSearchKey(tenantId, hostGroupName, hostName));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.navercorp.pinpoint.metric.web.service;


import com.navercorp.pinpoint.metric.web.cache.MetricWebCacheConfiguration;
import com.navercorp.pinpoint.metric.web.dao.SystemMetricHostExclusionDao;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

@Component
public class SystemMetricHostExclusionCache {
private final Logger logger = LogManager.getLogger(this.getClass());
private final SystemMetricHostExclusionDao systemMetricHostExclusionDao;

public SystemMetricHostExclusionCache(SystemMetricHostExclusionDao systemMetricHostExclusionDao) {
this.systemMetricHostExclusionDao = Objects.requireNonNull(systemMetricHostExclusionDao, "systemMetricHostExclusionDao");
}

@Cacheable(cacheNames = "metricHostGroupExclusion", key = "#tenantId", cacheManager = MetricWebCacheConfiguration.METRIC_HOST_GROUP_EXCLUSION_CACHE_NAME)
public List<String> getExcludedHostGroupNameList(String tenantId) {
try {
return systemMetricHostExclusionDao.selectExcludedHostGroupNameList(tenantId);
} catch (Exception e) {
logger.warn("error getting excludedHostGroupNameList, return empty list.", e);
return Collections.emptyList();
}
}

@Cacheable(cacheNames = "metricHostExclusion", key = "{#tenantId, #hostGroupName}", cacheManager = MetricWebCacheConfiguration.METRIC_HOST_EXCLUSION_CACHE_NAME)
public List<String> getExcludedHostNameList(String tenantId, String hostGroupName) {
try {
return systemMetricHostExclusionDao.selectExcludedHostNameList(tenantId, hostGroupName);
} catch (Exception e) {
logger.warn("error getting excludedHostNameList, return empty list.", e);
return Collections.emptyList();
}
}
}
Loading

0 comments on commit 87f04a5

Please sign in to comment.