提交 e7696ef2 编写于 作者: P peng-yongsheng

Merge branch 'master' into feature/423

# Conflicts:
#	apm-collector/apm-collector-agentregister/src/main/java/org/skywalking/apm/collector/agentregister/worker/application/dao/ApplicationH2DAO.java
#	apm-collector/apm-collector-agentregister/src/main/java/org/skywalking/apm/collector/agentregister/worker/instance/dao/InstanceH2DAO.java
#	apm-collector/apm-collector-agentregister/src/main/java/org/skywalking/apm/collector/agentregister/worker/servicename/dao/ServiceNameH2DAO.java
#	apm-collector/apm-collector-cache/src/main/java/org/skywalking/apm/collector/cache/dao/ApplicationH2DAO.java
......@@ -24,7 +24,7 @@ Sky Walking | [中文](README_ZH.md)
# Architecture
* Architecture graph for 3.2+
<img src="https://skywalkingtest.github.io/page-resources/3.2/architecture/3.2-architecture.jpg"/>
<img src="https://skywalkingtest.github.io/page-resources/3.x-architecture.jpg"/>
# Document
* [WIKI](https://github.com/OpenSkywalking/skywalking/wiki)
......@@ -65,8 +65,8 @@ This project adheres to the Contributor Covenant [code of conduct](CODE_OF_CONDU
# Open Skywalking Organization
[Open Skywalking Organization Teams and Contributors](https://github.com/OpenSkywalking/Organization/blob/master/README.md)
# Commercial Support Partners
[![Shurenyun Logo](https://skywalkingtest.github.io/page-resources/partners/shurenyun-logo.png)](https://www.shurenyun.com/)
# Partners
<img src="https://skywalkingtest.github.io/page-resources/3.2.3/partners.png" width="600"/>
# License
[Apache 2.0 License.](/LICENSE)
......@@ -25,7 +25,7 @@ Sky Walking | [English](README.md)
# Architecture
* 3.2+版本架构图
<img src="https://skywalkingtest.github.io/page-resources/3.2/architecture/3.2-architecture.jpg"/>
<img src="https://skywalkingtest.github.io/page-resources/3.x-architecture.jpg"/>
# Document
* [WIKI](https://github.com/OpenSkywalking/skywalking/wiki)
......@@ -67,9 +67,8 @@ This project adheres to the Contributor Covenant [code of conduct](CODE_OF_CONDU
# Open Skywalking Organization
[Open Skywalking Organization Teams and Contributors](https://github.com/OpenSkywalking/Organization/blob/master/README.md)
# Commercial Support Partners
[![Shurenyun Logo](https://skywalkingtest.github.io/page-resources/partners/shurenyun-logo.png)](https://www.shurenyun.com/)
# Partners
<img src="https://skywalkingtest.github.io/page-resources/3.2.3/partners.png" width="600"/>
# License
[Apache 2.0 License.](/LICENSE)
......@@ -18,10 +18,44 @@
package org.skywalking.apm.collector.agentjvm.worker.cpu.dao;
import org.skywalking.apm.collector.core.stream.Data;
import org.skywalking.apm.collector.storage.define.DataDefine;
import org.skywalking.apm.collector.storage.define.jvm.CpuMetricTable;
import org.skywalking.apm.collector.storage.h2.SqlBuilder;
import org.skywalking.apm.collector.storage.h2.dao.H2DAO;
import org.skywalking.apm.collector.storage.h2.define.H2SqlEntity;
import org.skywalking.apm.collector.stream.worker.impl.dao.IPersistenceDAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.Map;
/**
* @author pengys5
* @author pengys5, clevertension
*/
public class CpuMetricH2DAO extends H2DAO implements ICpuMetricDAO {
public class CpuMetricH2DAO extends H2DAO implements ICpuMetricDAO, IPersistenceDAO<H2SqlEntity, H2SqlEntity> {
private final Logger logger = LoggerFactory.getLogger(CpuMetricH2DAO.class);
@Override public Data get(String id, DataDefine dataDefine) {
return null;
}
@Override public H2SqlEntity prepareBatchInsert(Data data) {
H2SqlEntity entity = new H2SqlEntity();
Map<String, Object> source = new HashMap<>();
source.put("id", data.getDataString(0));
source.put(CpuMetricTable.COLUMN_INSTANCE_ID, data.getDataInteger(0));
source.put(CpuMetricTable.COLUMN_USAGE_PERCENT, data.getDataDouble(0));
source.put(CpuMetricTable.COLUMN_TIME_BUCKET, data.getDataLong(0));
logger.debug("prepare cpu metric batch insert, id: {}", data.getDataString(0));
String sql = SqlBuilder.buildBatchInsertSql(CpuMetricTable.TABLE, source.keySet());
entity.setSql(sql);
entity.setParams(source.values().toArray(new Object[0]));
return entity;
}
@Override public H2SqlEntity prepareBatchUpdate(Data data) {
return null;
}
}
......@@ -18,10 +18,42 @@
package org.skywalking.apm.collector.agentjvm.worker.gc.dao;
import java.util.HashMap;
import java.util.Map;
import org.skywalking.apm.collector.core.stream.Data;
import org.skywalking.apm.collector.storage.define.DataDefine;
import org.skywalking.apm.collector.storage.define.jvm.GCMetricTable;
import org.skywalking.apm.collector.storage.h2.SqlBuilder;
import org.skywalking.apm.collector.storage.h2.dao.H2DAO;
import org.skywalking.apm.collector.storage.h2.define.H2SqlEntity;
import org.skywalking.apm.collector.stream.worker.impl.dao.IPersistenceDAO;
/**
* @author pengys5
* @author pengys5, clevertension
*/
public class GCMetricH2DAO extends H2DAO implements IGCMetricDAO {
public class GCMetricH2DAO extends H2DAO implements IGCMetricDAO, IPersistenceDAO<H2SqlEntity, H2SqlEntity> {
@Override public Data get(String id, DataDefine dataDefine) {
return null;
}
@Override public H2SqlEntity prepareBatchInsert(Data data) {
H2SqlEntity entity = new H2SqlEntity();
Map<String, Object> source = new HashMap<>();
source.put("id", data.getDataString(0));
source.put(GCMetricTable.COLUMN_INSTANCE_ID, data.getDataInteger(0));
source.put(GCMetricTable.COLUMN_PHRASE, data.getDataInteger(1));
source.put(GCMetricTable.COLUMN_COUNT, data.getDataLong(0));
source.put(GCMetricTable.COLUMN_TIME, data.getDataLong(1));
source.put(GCMetricTable.COLUMN_TIME_BUCKET, data.getDataLong(2));
String sql = SqlBuilder.buildBatchInsertSql(GCMetricTable.TABLE, source.keySet());
entity.setSql(sql);
entity.setParams(source.values().toArray(new Object[0]));
return entity;
}
@Override public H2SqlEntity prepareBatchUpdate(Data data) {
return null;
}
}
......@@ -18,10 +18,62 @@
package org.skywalking.apm.collector.agentjvm.worker.heartbeat.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.skywalking.apm.collector.client.h2.H2Client;
import org.skywalking.apm.collector.client.h2.H2ClientException;
import org.skywalking.apm.collector.core.framework.UnexpectedException;
import org.skywalking.apm.collector.core.stream.Data;
import org.skywalking.apm.collector.storage.define.DataDefine;
import org.skywalking.apm.collector.storage.define.register.InstanceTable;
import org.skywalking.apm.collector.storage.h2.SqlBuilder;
import org.skywalking.apm.collector.storage.h2.dao.H2DAO;
import org.skywalking.apm.collector.storage.h2.define.H2SqlEntity;
import org.skywalking.apm.collector.stream.worker.impl.dao.IPersistenceDAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author pengys5
* @author pengys5, clevertension
*/
public class InstanceHeartBeatH2DAO extends H2DAO implements IInstanceHeartBeatDAO {
public class InstanceHeartBeatH2DAO extends H2DAO implements IInstanceHeartBeatDAO, IPersistenceDAO<H2SqlEntity, H2SqlEntity> {
private final Logger logger = LoggerFactory.getLogger(InstanceHeartBeatEsDAO.class);
private static final String GET_INSTANCE_HEARTBEAT_SQL = "select * from {0} where {1} = ?";
@Override public Data get(String id, DataDefine dataDefine) {
H2Client client = getClient();
String sql = SqlBuilder.buildSql(GET_INSTANCE_HEARTBEAT_SQL, InstanceTable.TABLE, InstanceTable.COLUMN_INSTANCE_ID);
Object[] params = new Object[]{id};
try (ResultSet rs = client.executeQuery(sql, params)) {
if (rs.next()) {
Data data = dataDefine.build(id);
data.setDataInteger(0, rs.getInt(InstanceTable.COLUMN_INSTANCE_ID));
data.setDataLong(0, rs.getLong(InstanceTable.COLUMN_HEARTBEAT_TIME));
return data;
}
} catch (SQLException | H2ClientException e) {
logger.error(e.getMessage(), e);
}
return null;
}
@Override public H2SqlEntity prepareBatchInsert(Data data) {
throw new UnexpectedException("There is no need to merge stream data with database data.");
}
@Override public H2SqlEntity prepareBatchUpdate(Data data) {
H2SqlEntity entity = new H2SqlEntity();
Map<String, Object> source = new HashMap<>();
source.put(InstanceTable.COLUMN_HEARTBEAT_TIME, data.getDataLong(0));
String sql = SqlBuilder.buildBatchUpdateSql(InstanceTable.TABLE, source.keySet(), InstanceTable.COLUMN_APPLICATION_ID);
entity.setSql(sql);
List<Object> params = new ArrayList<>(source.values());
params.add(data.getDataString(0));
entity.setParams(params.toArray(new Object[0]));
return entity;
}
}
......@@ -18,10 +18,44 @@
package org.skywalking.apm.collector.agentjvm.worker.memory.dao;
import java.util.HashMap;
import java.util.Map;
import org.skywalking.apm.collector.core.stream.Data;
import org.skywalking.apm.collector.storage.define.DataDefine;
import org.skywalking.apm.collector.storage.define.jvm.MemoryMetricTable;
import org.skywalking.apm.collector.storage.h2.SqlBuilder;
import org.skywalking.apm.collector.storage.h2.dao.H2DAO;
import org.skywalking.apm.collector.storage.h2.define.H2SqlEntity;
import org.skywalking.apm.collector.stream.worker.impl.dao.IPersistenceDAO;
/**
* @author pengys5
* @author pengys5, clevertension
*/
public class MemoryMetricH2DAO extends H2DAO implements IMemoryMetricDAO {
public class MemoryMetricH2DAO extends H2DAO implements IMemoryMetricDAO, IPersistenceDAO<H2SqlEntity, H2SqlEntity> {
@Override public Data get(String id, DataDefine dataDefine) {
return null;
}
@Override public H2SqlEntity prepareBatchInsert(Data data) {
H2SqlEntity entity = new H2SqlEntity();
Map<String, Object> source = new HashMap<>();
source.put("id", data.getDataString(0));
source.put(MemoryMetricTable.COLUMN_APPLICATION_INSTANCE_ID, data.getDataInteger(0));
source.put(MemoryMetricTable.COLUMN_IS_HEAP, data.getDataBoolean(0));
source.put(MemoryMetricTable.COLUMN_INIT, data.getDataLong(0));
source.put(MemoryMetricTable.COLUMN_MAX, data.getDataLong(1));
source.put(MemoryMetricTable.COLUMN_USED, data.getDataLong(2));
source.put(MemoryMetricTable.COLUMN_COMMITTED, data.getDataLong(3));
source.put(MemoryMetricTable.COLUMN_TIME_BUCKET, data.getDataLong(4));
String sql = SqlBuilder.buildBatchInsertSql(MemoryMetricTable.TABLE, source.keySet());
entity.setSql(sql);
entity.setParams(source.values().toArray(new Object[0]));
return entity;
}
@Override public H2SqlEntity prepareBatchUpdate(Data data) {
return null;
}
}
......@@ -18,10 +18,44 @@
package org.skywalking.apm.collector.agentjvm.worker.memorypool.dao;
import java.util.HashMap;
import java.util.Map;
import org.skywalking.apm.collector.core.stream.Data;
import org.skywalking.apm.collector.storage.define.DataDefine;
import org.skywalking.apm.collector.storage.define.jvm.MemoryPoolMetricTable;
import org.skywalking.apm.collector.storage.h2.SqlBuilder;
import org.skywalking.apm.collector.storage.h2.dao.H2DAO;
import org.skywalking.apm.collector.storage.h2.define.H2SqlEntity;
import org.skywalking.apm.collector.stream.worker.impl.dao.IPersistenceDAO;
/**
* @author pengys5
* @author pengys5, clevertension
*/
public class MemoryPoolMetricH2DAO extends H2DAO implements IMemoryPoolMetricDAO {
public class MemoryPoolMetricH2DAO extends H2DAO implements IMemoryPoolMetricDAO, IPersistenceDAO<H2SqlEntity, H2SqlEntity> {
@Override public Data get(String id, DataDefine dataDefine) {
return null;
}
@Override public H2SqlEntity prepareBatchInsert(Data data) {
H2SqlEntity entity = new H2SqlEntity();
Map<String, Object> source = new HashMap<>();
source.put("id", data.getDataString(0));
source.put(MemoryPoolMetricTable.COLUMN_INSTANCE_ID, data.getDataInteger(0));
source.put(MemoryPoolMetricTable.COLUMN_POOL_TYPE, data.getDataInteger(1));
source.put(MemoryPoolMetricTable.COLUMN_INIT, data.getDataLong(0));
source.put(MemoryPoolMetricTable.COLUMN_MAX, data.getDataLong(1));
source.put(MemoryPoolMetricTable.COLUMN_USED, data.getDataLong(2));
source.put(MemoryPoolMetricTable.COLUMN_COMMITTED, data.getDataLong(3));
source.put(MemoryPoolMetricTable.COLUMN_TIME_BUCKET, data.getDataLong(4));
String sql = SqlBuilder.buildBatchInsertSql(MemoryPoolMetricTable.TABLE, source.keySet());
entity.setSql(sql);
entity.setParams(source.values().toArray(new Object[0]));
return entity;
}
@Override public H2SqlEntity prepareBatchUpdate(Data data) {
return null;
}
}
......@@ -18,23 +18,42 @@
package org.skywalking.apm.collector.agentregister.worker.application.dao;
import org.skywalking.apm.collector.client.h2.H2Client;
import org.skywalking.apm.collector.client.h2.H2ClientException;
import org.skywalking.apm.collector.storage.define.register.ApplicationDataDefine;
import org.skywalking.apm.collector.storage.define.register.ApplicationTable;
import org.skywalking.apm.collector.storage.h2.SqlBuilder;
import org.skywalking.apm.collector.storage.h2.dao.H2DAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author pengys5
* @author pengys5, clevertension
*/
public class ApplicationH2DAO extends H2DAO implements IApplicationDAO {
private final Logger logger = LoggerFactory.getLogger(ApplicationH2DAO.class);
private static final String INSERT_APPLICATION_SQL = "insert into {0}({1}, {2}) values(?, ?)";
@Override public int getMaxApplicationId() {
return 0;
@Override
public int getMaxApplicationId() {
return getMaxId(ApplicationTable.TABLE, ApplicationTable.COLUMN_APPLICATION_ID);
}
@Override public int getMinApplicationId() {
return 0;
@Override
public int getMinApplicationId() {
return getMinId(ApplicationTable.TABLE, ApplicationTable.COLUMN_APPLICATION_ID);
}
@Override public void save(ApplicationDataDefine.Application application) {
@Override
public void save(ApplicationDataDefine.Application application) {
H2Client client = getClient();
String sql = SqlBuilder.buildSql(INSERT_APPLICATION_SQL, ApplicationTable.TABLE, ApplicationTable.COLUMN_APPLICATION_ID,
ApplicationTable.COLUMN_APPLICATION_CODE);
Object[] params = new Object[] {application.getApplicationId(), application.getApplicationCode()};
try {
client.execute(sql, params);
} catch (H2ClientException e) {
logger.error(e.getMessage(), e);
}
}
}
......@@ -18,31 +18,79 @@
package org.skywalking.apm.collector.agentregister.worker.instance.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import org.skywalking.apm.collector.client.h2.H2Client;
import org.skywalking.apm.collector.client.h2.H2ClientException;
import org.skywalking.apm.collector.storage.define.register.InstanceDataDefine;
import org.skywalking.apm.collector.storage.define.register.InstanceTable;
import org.skywalking.apm.collector.storage.h2.SqlBuilder;
import org.skywalking.apm.collector.storage.h2.dao.H2DAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author pengys5
* @author pengys5, clevertension
*/
public class InstanceH2DAO extends H2DAO implements IInstanceDAO {
private final Logger logger = LoggerFactory.getLogger(InstanceH2DAO.class);
private static final String GET_INSTANCE_ID_SQL = "select {0} from {1} where {2} = ? and {3} = ?";
private static final String UPDATE_HEARTBEAT_TIME_SQL = "updatte {0} set {1} = ? where {2} = ?";
@Override public int getInstanceId(int applicationId, String agentUUID) {
logger.info("get the application id with application id = {}, agentUUID = {}", applicationId, agentUUID);
H2Client client = getClient();
String sql = SqlBuilder.buildSql(GET_INSTANCE_ID_SQL, InstanceTable.COLUMN_INSTANCE_ID, InstanceTable.TABLE, InstanceTable.COLUMN_APPLICATION_ID,
InstanceTable.COLUMN_AGENT_UUID);
Object[] params = new Object[] {applicationId, agentUUID};
try (ResultSet rs = client.executeQuery(sql, params)) {
if (rs.next()) {
return rs.getInt(InstanceTable.COLUMN_INSTANCE_ID);
}
} catch (SQLException | H2ClientException e) {
logger.error(e.getMessage(), e);
}
return 0;
}
@Override public int getMaxInstanceId() {
return 0;
return getMaxId(InstanceTable.TABLE, InstanceTable.COLUMN_INSTANCE_ID);
}
@Override public int getMinInstanceId() {
return 0;
return getMinId(InstanceTable.TABLE, InstanceTable.COLUMN_INSTANCE_ID);
}
@Override public void save(InstanceDataDefine.Instance instance) {
H2Client client = getClient();
Map<String, Object> source = new HashMap<>();
source.put(InstanceTable.COLUMN_INSTANCE_ID, instance.getInstanceId());
source.put(InstanceTable.COLUMN_APPLICATION_ID, instance.getApplicationId());
source.put(InstanceTable.COLUMN_AGENT_UUID, instance.getAgentUUID());
source.put(InstanceTable.COLUMN_REGISTER_TIME, instance.getRegisterTime());
source.put(InstanceTable.COLUMN_HEARTBEAT_TIME, instance.getHeartBeatTime());
source.put(InstanceTable.COLUMN_OS_INFO, instance.getOsInfo());
String sql = SqlBuilder.buildBatchInsertSql(InstanceTable.TABLE, source.keySet());
Object[] params = source.values().toArray(new Object[0]);
try {
client.execute(sql, params);
} catch (H2ClientException e) {
logger.error(e.getMessage(), e);
}
}
@Override public void updateHeartbeatTime(int instanceId, long heartbeatTime) {
H2Client client = getClient();
String sql = SqlBuilder.buildSql(UPDATE_HEARTBEAT_TIME_SQL, InstanceTable.TABLE, InstanceTable.COLUMN_HEARTBEAT_TIME,
InstanceTable.COLUMN_INSTANCE_ID);
Object[] params = new Object[] {heartbeatTime, instanceId};
try {
client.execute(sql, params);
} catch (H2ClientException e) {
logger.error(e.getMessage(), e);
}
}
}
......@@ -18,23 +18,48 @@
package org.skywalking.apm.collector.agentregister.worker.servicename.dao;
import java.util.HashMap;
import java.util.Map;
import org.skywalking.apm.collector.client.h2.H2Client;
import org.skywalking.apm.collector.client.h2.H2ClientException;
import org.skywalking.apm.collector.storage.define.register.ServiceNameDataDefine;
import org.skywalking.apm.collector.storage.define.register.ServiceNameTable;
import org.skywalking.apm.collector.storage.h2.SqlBuilder;
import org.skywalking.apm.collector.storage.h2.dao.H2DAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author pengys5
* @author pengys5, clevertension
*/
public class ServiceNameH2DAO extends H2DAO implements IServiceNameDAO {
private final Logger logger = LoggerFactory.getLogger(ServiceNameH2DAO.class);
@Override public int getMaxServiceId() {
return 0;
@Override
public int getMaxServiceId() {
return getMaxId(ServiceNameTable.TABLE, ServiceNameTable.COLUMN_SERVICE_ID);
}
@Override public int getMinServiceId() {
return 0;
@Override
public int getMinServiceId() {
return getMinId(ServiceNameTable.TABLE, ServiceNameTable.COLUMN_SERVICE_ID);
}
@Override public void save(ServiceNameDataDefine.ServiceName serviceName) {
@Override
public void save(ServiceNameDataDefine.ServiceName serviceName) {
logger.debug("save service name register info, application id: {}, service name: {}", serviceName.getApplicationId(), serviceName.getServiceName());
H2Client client = getClient();
Map<String, Object> source = new HashMap<>();
source.put(ServiceNameTable.COLUMN_SERVICE_ID, serviceName.getServiceId());
source.put(ServiceNameTable.COLUMN_APPLICATION_ID, serviceName.getApplicationId());
source.put(ServiceNameTable.COLUMN_SERVICE_NAME, serviceName.getServiceName());
String sql = SqlBuilder.buildBatchInsertSql(ServiceNameTable.TABLE, source.keySet());
Object[] params = source.values().toArray(new Object[0]);
try {
client.execute(sql, params);
} catch (H2ClientException e) {
logger.error(e.getMessage(), e);
}
}
}
......@@ -18,10 +18,45 @@
package org.skywalking.apm.collector.agentstream.worker.global.dao;
import org.skywalking.apm.collector.core.framework.UnexpectedException;
import org.skywalking.apm.collector.core.stream.Data;
import org.skywalking.apm.collector.storage.define.DataDefine;
import org.skywalking.apm.collector.storage.define.global.GlobalTraceTable;
import org.skywalking.apm.collector.storage.h2.SqlBuilder;
import org.skywalking.apm.collector.storage.h2.dao.H2DAO;
import org.skywalking.apm.collector.storage.h2.define.H2SqlEntity;
import org.skywalking.apm.collector.stream.worker.impl.dao.IPersistenceDAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.Map;
/**
* @author pengys5
* @author pengys5, clevertension
*/
public class GlobalTraceH2DAO extends H2DAO implements IGlobalTraceDAO {
public class GlobalTraceH2DAO extends H2DAO implements IGlobalTraceDAO, IPersistenceDAO<H2SqlEntity, H2SqlEntity> {
private final Logger logger = LoggerFactory.getLogger(GlobalTraceH2DAO.class);
@Override public Data get(String id, DataDefine dataDefine) {
throw new UnexpectedException("There is no need to merge stream data with database data.");
}
@Override public H2SqlEntity prepareBatchUpdate(Data data) {
throw new UnexpectedException("There is no need to merge stream data with database data.");
}
@Override public H2SqlEntity prepareBatchInsert(Data data) {
Map<String, Object> source = new HashMap<>();
H2SqlEntity entity = new H2SqlEntity();
source.put("id", data.getDataString(0));
source.put(GlobalTraceTable.COLUMN_SEGMENT_ID, data.getDataString(1));
source.put(GlobalTraceTable.COLUMN_GLOBAL_TRACE_ID, data.getDataString(2));
source.put(GlobalTraceTable.COLUMN_TIME_BUCKET, data.getDataLong(0));
logger.debug("global trace source: {}", source.toString());
String sql = SqlBuilder.buildBatchInsertSql(GlobalTraceTable.TABLE, source.keySet());
entity.setSql(sql);
entity.setParams(source.values().toArray(new Object[0]));
return entity;
}
}
......@@ -18,11 +18,78 @@
package org.skywalking.apm.collector.agentstream.worker.instance.performance.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.skywalking.apm.collector.client.h2.H2Client;
import org.skywalking.apm.collector.client.h2.H2ClientException;
import org.skywalking.apm.collector.core.stream.Data;
import org.skywalking.apm.collector.storage.define.DataDefine;
import org.skywalking.apm.collector.storage.define.instance.InstPerformanceTable;
import org.skywalking.apm.collector.storage.h2.SqlBuilder;
import org.skywalking.apm.collector.storage.h2.dao.H2DAO;
import org.skywalking.apm.collector.storage.h2.define.H2SqlEntity;
import org.skywalking.apm.collector.stream.worker.impl.dao.IPersistenceDAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author pengys5
* @author pengys5, clevertension
*/
public class InstPerformanceH2DAO extends H2DAO implements IInstPerformanceDAO {
public class InstPerformanceH2DAO extends H2DAO implements IInstPerformanceDAO, IPersistenceDAO<H2SqlEntity, H2SqlEntity> {
private final Logger logger = LoggerFactory.getLogger(InstPerformanceH2DAO.class);
private static final String GET_SQL = "select * from {0} where {1} = ?";
@Override public Data get(String id, DataDefine dataDefine) {
H2Client client = getClient();
String sql = SqlBuilder.buildSql(GET_SQL, InstPerformanceTable.TABLE, "id");
Object[] params = new Object[]{id};
try (ResultSet rs = client.executeQuery(sql, params)) {
if (rs.next()) {
Data data = dataDefine.build(id);
data.setDataInteger(0, rs.getInt(InstPerformanceTable.COLUMN_APPLICATION_ID));
data.setDataInteger(1, rs.getInt(InstPerformanceTable.COLUMN_INSTANCE_ID));
data.setDataInteger(2, rs.getInt(InstPerformanceTable.COLUMN_CALLS));
data.setDataLong(0, rs.getLong(InstPerformanceTable.COLUMN_COST_TOTAL));
data.setDataLong(1, rs.getLong(InstPerformanceTable.COLUMN_TIME_BUCKET));
return data;
}
} catch (SQLException | H2ClientException e) {
logger.error(e.getMessage(), e);
}
return null;
}
@Override public H2SqlEntity prepareBatchInsert(Data data) {
Map<String, Object> source = new HashMap<>();
H2SqlEntity entity = new H2SqlEntity();
source.put("id", data.getDataString(0));
source.put(InstPerformanceTable.COLUMN_APPLICATION_ID, data.getDataInteger(0));
source.put(InstPerformanceTable.COLUMN_INSTANCE_ID, data.getDataInteger(1));
source.put(InstPerformanceTable.COLUMN_CALLS, data.getDataInteger(2));
source.put(InstPerformanceTable.COLUMN_COST_TOTAL, data.getDataLong(0));
source.put(InstPerformanceTable.COLUMN_TIME_BUCKET, data.getDataLong(1));
String sql = SqlBuilder.buildBatchInsertSql(InstPerformanceTable.TABLE, source.keySet());
entity.setSql(sql);
entity.setParams(source.values().toArray(new Object[0]));
return entity;
}
@Override public H2SqlEntity prepareBatchUpdate(Data data) {
Map<String, Object> source = new HashMap<>();
H2SqlEntity entity = new H2SqlEntity();
source.put(InstPerformanceTable.COLUMN_APPLICATION_ID, data.getDataInteger(0));
source.put(InstPerformanceTable.COLUMN_INSTANCE_ID, data.getDataInteger(1));
source.put(InstPerformanceTable.COLUMN_CALLS, data.getDataInteger(2));
source.put(InstPerformanceTable.COLUMN_COST_TOTAL, data.getDataLong(0));
source.put(InstPerformanceTable.COLUMN_TIME_BUCKET, data.getDataLong(1));
String id = data.getDataString(0);
String sql = SqlBuilder.buildBatchUpdateSql(InstPerformanceTable.TABLE, source.keySet(), "id");
entity.setSql(sql);
List<Object> values = new ArrayList<>(source.values());
values.add(id);
entity.setParams(values.toArray(new Object[0]));
return entity;
}
}
......@@ -18,11 +18,86 @@
package org.skywalking.apm.collector.agentstream.worker.node.component.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.skywalking.apm.collector.client.h2.H2Client;
import org.skywalking.apm.collector.client.h2.H2ClientException;
import org.skywalking.apm.collector.core.stream.Data;
import org.skywalking.apm.collector.storage.define.DataDefine;
import org.skywalking.apm.collector.storage.define.node.NodeComponentTable;
import org.skywalking.apm.collector.storage.define.serviceref.ServiceReferenceTable;
import org.skywalking.apm.collector.storage.h2.SqlBuilder;
import org.skywalking.apm.collector.storage.h2.dao.H2DAO;
import org.skywalking.apm.collector.storage.h2.define.H2SqlEntity;
import org.skywalking.apm.collector.stream.worker.impl.dao.IPersistenceDAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author pengys5
* @author pengys5, clevertension
*/
public class NodeComponentH2DAO extends H2DAO implements INodeComponentDAO {
public class NodeComponentH2DAO extends H2DAO implements INodeComponentDAO, IPersistenceDAO<H2SqlEntity, H2SqlEntity> {
private final Logger logger = LoggerFactory.getLogger(NodeComponentH2DAO.class);
private static final String GET_SQL = "select * from {0} where {1} = ?";
@Override
public Data get(String id, DataDefine dataDefine) {
H2Client client = getClient();
String sql = SqlBuilder.buildSql(GET_SQL, ServiceReferenceTable.TABLE, "id");
Object[] params = new Object[]{id};
try (ResultSet rs = client.executeQuery(sql, params)) {
if (rs.next()) {
Data data = dataDefine.build(id);
data.setDataInteger(0, rs.getInt(NodeComponentTable.COLUMN_COMPONENT_ID));
data.setDataString(1, rs.getString(NodeComponentTable.COLUMN_COMPONENT_NAME));
data.setDataInteger(1, rs.getInt(NodeComponentTable.COLUMN_PEER_ID));
data.setDataString(2, rs.getString(NodeComponentTable.COLUMN_PEER));
data.setDataLong(0, rs.getLong(NodeComponentTable.COLUMN_TIME_BUCKET));
return data;
}
} catch (SQLException | H2ClientException e) {
logger.error(e.getMessage(), e);
}
return null;
}
@Override
public H2SqlEntity prepareBatchInsert(Data data) {
Map<String, Object> source = new HashMap<>();
H2SqlEntity entity = new H2SqlEntity();
source.put("id", data.getDataString(0));
source.put(NodeComponentTable.COLUMN_COMPONENT_ID, data.getDataInteger(0));
source.put(NodeComponentTable.COLUMN_COMPONENT_NAME, data.getDataString(1));
source.put(NodeComponentTable.COLUMN_PEER_ID, data.getDataInteger(1));
source.put(NodeComponentTable.COLUMN_PEER, data.getDataString(2));
source.put(NodeComponentTable.COLUMN_TIME_BUCKET, data.getDataLong(0));
String sql = SqlBuilder.buildBatchInsertSql(NodeComponentTable.TABLE, source.keySet());
entity.setSql(sql);
entity.setParams(source.values().toArray(new Object[0]));
return entity;
}
@Override
public H2SqlEntity prepareBatchUpdate(Data data) {
Map<String, Object> source = new HashMap<>();
H2SqlEntity entity = new H2SqlEntity();
source.put(NodeComponentTable.COLUMN_COMPONENT_ID, data.getDataInteger(0));
source.put(NodeComponentTable.COLUMN_COMPONENT_NAME, data.getDataString(1));
source.put(NodeComponentTable.COLUMN_PEER_ID, data.getDataInteger(1));
source.put(NodeComponentTable.COLUMN_PEER, data.getDataString(2));
source.put(NodeComponentTable.COLUMN_TIME_BUCKET, data.getDataLong(0));
String id = data.getDataString(0);
String sql = SqlBuilder.buildBatchUpdateSql(NodeComponentTable.TABLE, source.keySet(), "id");
entity.setSql(sql);
List<Object> values = new ArrayList<>(source.values());
values.add(id);
entity.setParams(values.toArray(new Object[0]));
return entity;
}
}
......@@ -18,10 +18,77 @@
package org.skywalking.apm.collector.agentstream.worker.node.mapping.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.skywalking.apm.collector.agentstream.worker.node.component.dao.NodeComponentH2DAO;
import org.skywalking.apm.collector.client.h2.H2Client;
import org.skywalking.apm.collector.client.h2.H2ClientException;
import org.skywalking.apm.collector.core.stream.Data;
import org.skywalking.apm.collector.storage.define.DataDefine;
import org.skywalking.apm.collector.storage.define.node.NodeMappingTable;
import org.skywalking.apm.collector.storage.h2.SqlBuilder;
import org.skywalking.apm.collector.storage.h2.dao.H2DAO;
import org.skywalking.apm.collector.storage.h2.define.H2SqlEntity;
import org.skywalking.apm.collector.stream.worker.impl.dao.IPersistenceDAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author pengys5
* @author pengys5, clevertension
*/
public class NodeMappingH2DAO extends H2DAO implements INodeMappingDAO {
public class NodeMappingH2DAO extends H2DAO implements INodeMappingDAO, IPersistenceDAO<H2SqlEntity, H2SqlEntity> {
private final Logger logger = LoggerFactory.getLogger(NodeComponentH2DAO.class);
private static final String GET_SQL = "select * from {0} where {1} = ?";
@Override public Data get(String id, DataDefine dataDefine) {
H2Client client = getClient();
String sql = SqlBuilder.buildSql(GET_SQL, NodeMappingTable.TABLE, "id");
Object[] params = new Object[]{id};
try (ResultSet rs = client.executeQuery(sql, params)) {
if (rs.next()) {
Data data = dataDefine.build(id);
data.setDataInteger(0, rs.getInt(NodeMappingTable.COLUMN_APPLICATION_ID));
data.setDataInteger(1, rs.getInt(NodeMappingTable.COLUMN_ADDRESS_ID));
data.setDataString(1, rs.getString(NodeMappingTable.COLUMN_ADDRESS));
data.setDataLong(0, rs.getLong(NodeMappingTable.COLUMN_TIME_BUCKET));
return data;
}
} catch (SQLException | H2ClientException e) {
logger.error(e.getMessage(), e);
}
return null;
}
@Override public H2SqlEntity prepareBatchInsert(Data data) {
Map<String, Object> source = new HashMap<>();
H2SqlEntity entity = new H2SqlEntity();
source.put("id", data.getDataString(0));
source.put(NodeMappingTable.COLUMN_APPLICATION_ID, data.getDataInteger(0));
source.put(NodeMappingTable.COLUMN_ADDRESS_ID, data.getDataInteger(1));
source.put(NodeMappingTable.COLUMN_ADDRESS, data.getDataString(1));
source.put(NodeMappingTable.COLUMN_TIME_BUCKET, data.getDataLong(0));
String sql = SqlBuilder.buildBatchInsertSql(NodeMappingTable.TABLE, source.keySet());
entity.setSql(sql);
entity.setParams(source.values().toArray(new Object[0]));
return entity;
}
@Override public H2SqlEntity prepareBatchUpdate(Data data) {
Map<String, Object> source = new HashMap<>();
H2SqlEntity entity = new H2SqlEntity();
source.put(NodeMappingTable.COLUMN_APPLICATION_ID, data.getDataInteger(0));
source.put(NodeMappingTable.COLUMN_ADDRESS_ID, data.getDataInteger(1));
source.put(NodeMappingTable.COLUMN_ADDRESS, data.getDataString(1));
source.put(NodeMappingTable.COLUMN_TIME_BUCKET, data.getDataLong(0));
String id = data.getDataString(0);
String sql = SqlBuilder.buildBatchUpdateSql(NodeMappingTable.TABLE, source.keySet(), "id");
entity.setSql(sql);
List<Object> values = new ArrayList<>(source.values());
values.add(id);
entity.setParams(values.toArray(new Object[0]));
return entity;
}
}
......@@ -18,10 +18,94 @@
package org.skywalking.apm.collector.agentstream.worker.noderef.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.skywalking.apm.collector.client.h2.H2Client;
import org.skywalking.apm.collector.client.h2.H2ClientException;
import org.skywalking.apm.collector.core.stream.Data;
import org.skywalking.apm.collector.storage.define.DataDefine;
import org.skywalking.apm.collector.storage.define.noderef.NodeReferenceTable;
import org.skywalking.apm.collector.storage.h2.SqlBuilder;
import org.skywalking.apm.collector.storage.h2.dao.H2DAO;
import org.skywalking.apm.collector.storage.h2.define.H2SqlEntity;
import org.skywalking.apm.collector.stream.worker.impl.dao.IPersistenceDAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author pengys5
* @author pengys5, clevertension
*/
public class NodeReferenceH2DAO extends H2DAO implements INodeReferenceDAO {
public class NodeReferenceH2DAO extends H2DAO implements INodeReferenceDAO, IPersistenceDAO<H2SqlEntity, H2SqlEntity> {
private final Logger logger = LoggerFactory.getLogger(NodeReferenceH2DAO.class);
private static final String GET_SQL = "select * from {0} where {1} = ?";
@Override public Data get(String id, DataDefine dataDefine) {
H2Client client = getClient();
String sql = SqlBuilder.buildSql(GET_SQL, NodeReferenceTable.TABLE, "id");
Object[] params = new Object[]{id};
try (ResultSet rs = client.executeQuery(sql, params)) {
if (rs.next()) {
Data data = dataDefine.build(id);
data.setDataInteger(0, rs.getInt(NodeReferenceTable.COLUMN_FRONT_APPLICATION_ID));
data.setDataInteger(1, rs.getInt(NodeReferenceTable.COLUMN_BEHIND_APPLICATION_ID));
data.setDataString(1, rs.getString(NodeReferenceTable.COLUMN_BEHIND_PEER));
data.setDataInteger(2, rs.getInt(NodeReferenceTable.COLUMN_S1_LTE));
data.setDataInteger(3, rs.getInt(NodeReferenceTable.COLUMN_S3_LTE));
data.setDataInteger(4, rs.getInt(NodeReferenceTable.COLUMN_S5_LTE));
data.setDataInteger(5, rs.getInt(NodeReferenceTable.COLUMN_S5_GT));
data.setDataInteger(6, rs.getInt(NodeReferenceTable.COLUMN_SUMMARY));
data.setDataInteger(7, rs.getInt(NodeReferenceTable.COLUMN_ERROR));
data.setDataLong(0, rs.getLong(NodeReferenceTable.COLUMN_TIME_BUCKET));
return data;
}
} catch (SQLException | H2ClientException e) {
logger.error(e.getMessage(), e);
}
return null;
}
@Override public H2SqlEntity prepareBatchInsert(Data data) {
Map<String, Object> source = new HashMap<>();
H2SqlEntity entity = new H2SqlEntity();
source.put("id", data.getDataString(0));
source.put(NodeReferenceTable.COLUMN_FRONT_APPLICATION_ID, data.getDataInteger(0));
source.put(NodeReferenceTable.COLUMN_BEHIND_APPLICATION_ID, data.getDataInteger(1));
source.put(NodeReferenceTable.COLUMN_BEHIND_PEER, data.getDataString(1));
source.put(NodeReferenceTable.COLUMN_S1_LTE, data.getDataInteger(2));
source.put(NodeReferenceTable.COLUMN_S3_LTE, data.getDataInteger(3));
source.put(NodeReferenceTable.COLUMN_S5_LTE, data.getDataInteger(4));
source.put(NodeReferenceTable.COLUMN_S5_GT, data.getDataInteger(5));
source.put(NodeReferenceTable.COLUMN_SUMMARY, data.getDataInteger(6));
source.put(NodeReferenceTable.COLUMN_ERROR, data.getDataInteger(7));
source.put(NodeReferenceTable.COLUMN_TIME_BUCKET, data.getDataLong(0));
String sql = SqlBuilder.buildBatchInsertSql(NodeReferenceTable.TABLE, source.keySet());
entity.setSql(sql);
entity.setParams(source.values().toArray(new Object[0]));
return entity;
}
@Override public H2SqlEntity prepareBatchUpdate(Data data) {
Map<String, Object> source = new HashMap<>();
H2SqlEntity entity = new H2SqlEntity();
source.put(NodeReferenceTable.COLUMN_FRONT_APPLICATION_ID, data.getDataInteger(0));
source.put(NodeReferenceTable.COLUMN_BEHIND_APPLICATION_ID, data.getDataInteger(1));
source.put(NodeReferenceTable.COLUMN_BEHIND_PEER, data.getDataString(1));
source.put(NodeReferenceTable.COLUMN_S1_LTE, data.getDataInteger(2));
source.put(NodeReferenceTable.COLUMN_S3_LTE, data.getDataInteger(3));
source.put(NodeReferenceTable.COLUMN_S5_LTE, data.getDataInteger(4));
source.put(NodeReferenceTable.COLUMN_S5_GT, data.getDataInteger(5));
source.put(NodeReferenceTable.COLUMN_SUMMARY, data.getDataInteger(6));
source.put(NodeReferenceTable.COLUMN_ERROR, data.getDataInteger(7));
source.put(NodeReferenceTable.COLUMN_TIME_BUCKET, data.getDataLong(0));
String id = data.getDataString(0);
String sql = SqlBuilder.buildBatchUpdateSql(NodeReferenceTable.TABLE, source.keySet(), "id");
entity.setSql(sql);
List<Object> values = new ArrayList<>(source.values());
values.add(id);
entity.setParams(values.toArray(new Object[0]));
return entity;
}
}
......@@ -32,6 +32,7 @@ public class NodeReferenceH2TableDefine extends H2TableDefine {
}
@Override public void initialize() {
addColumn(new H2ColumnDefine(NodeReferenceTable.COLUMN_ID, H2ColumnDefine.Type.Varchar.name()));
addColumn(new H2ColumnDefine(NodeReferenceTable.COLUMN_FRONT_APPLICATION_ID, H2ColumnDefine.Type.Int.name()));
addColumn(new H2ColumnDefine(NodeReferenceTable.COLUMN_BEHIND_APPLICATION_ID, H2ColumnDefine.Type.Int.name()));
addColumn(new H2ColumnDefine(NodeReferenceTable.COLUMN_BEHIND_PEER, H2ColumnDefine.Type.Varchar.name()));
......
......@@ -18,11 +18,48 @@
package org.skywalking.apm.collector.agentstream.worker.segment.cost.dao;
import org.skywalking.apm.collector.core.stream.Data;
import org.skywalking.apm.collector.storage.define.DataDefine;
import org.skywalking.apm.collector.storage.define.segment.SegmentCostTable;
import org.skywalking.apm.collector.storage.h2.SqlBuilder;
import org.skywalking.apm.collector.storage.h2.dao.H2DAO;
import org.skywalking.apm.collector.storage.h2.define.H2SqlEntity;
import org.skywalking.apm.collector.stream.worker.impl.dao.IPersistenceDAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.Map;
/**
* @author pengys5
* @author pengys5, clevertension
*/
public class SegmentCostH2DAO extends H2DAO implements ISegmentCostDAO {
public class SegmentCostH2DAO extends H2DAO implements ISegmentCostDAO, IPersistenceDAO<H2SqlEntity, H2SqlEntity> {
private final Logger logger = LoggerFactory.getLogger(SegmentCostH2DAO.class);
@Override public Data get(String id, DataDefine dataDefine) {
return null;
}
@Override public H2SqlEntity prepareBatchInsert(Data data) {
logger.debug("segment cost prepareBatchInsert, id: {}", data.getDataString(0));
H2SqlEntity entity = new H2SqlEntity();
Map<String, Object> source = new HashMap<>();
source.put("id", data.getDataString(0));
source.put(SegmentCostTable.COLUMN_SEGMENT_ID, data.getDataString(1));
source.put(SegmentCostTable.COLUMN_APPLICATION_ID, data.getDataInteger(0));
source.put(SegmentCostTable.COLUMN_SERVICE_NAME, data.getDataString(2));
source.put(SegmentCostTable.COLUMN_COST, data.getDataLong(0));
source.put(SegmentCostTable.COLUMN_START_TIME, data.getDataLong(1));
source.put(SegmentCostTable.COLUMN_END_TIME, data.getDataLong(2));
source.put(SegmentCostTable.COLUMN_IS_ERROR, data.getDataBoolean(0));
source.put(SegmentCostTable.COLUMN_TIME_BUCKET, data.getDataLong(3));
logger.debug("segment cost source: {}", source.toString());
String sql = SqlBuilder.buildBatchInsertSql(SegmentCostTable.TABLE, source.keySet());
entity.setSql(sql);
entity.setParams(source.values().toArray(new Object[0]));
return entity;
}
@Override public H2SqlEntity prepareBatchUpdate(Data data) {
return null;
}
}
......@@ -18,10 +18,42 @@
package org.skywalking.apm.collector.agentstream.worker.segment.origin.dao;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import org.skywalking.apm.collector.agentstream.worker.segment.cost.dao.SegmentCostH2DAO;
import org.skywalking.apm.collector.core.stream.Data;
import org.skywalking.apm.collector.storage.define.DataDefine;
import org.skywalking.apm.collector.storage.define.segment.SegmentTable;
import org.skywalking.apm.collector.storage.h2.SqlBuilder;
import org.skywalking.apm.collector.storage.h2.dao.H2DAO;
import org.skywalking.apm.collector.storage.h2.define.H2SqlEntity;
import org.skywalking.apm.collector.stream.worker.impl.dao.IPersistenceDAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author pengys5
* @author pengys5, clevertension
*/
public class SegmentH2DAO extends H2DAO implements ISegmentDAO {
public class SegmentH2DAO extends H2DAO implements ISegmentDAO, IPersistenceDAO<H2SqlEntity, H2SqlEntity> {
private final Logger logger = LoggerFactory.getLogger(SegmentCostH2DAO.class);
@Override public Data get(String id, DataDefine dataDefine) {
return null;
}
@Override public H2SqlEntity prepareBatchInsert(Data data) {
Map<String, Object> source = new HashMap<>();
H2SqlEntity entity = new H2SqlEntity();
source.put("id", data.getDataString(0));
source.put(SegmentTable.COLUMN_DATA_BINARY, Base64.getEncoder().encode(data.getDataBytes(0)));
logger.debug("segment source: {}", source.toString());
String sql = SqlBuilder.buildBatchInsertSql(SegmentTable.TABLE, source.keySet());
entity.setSql(sql);
entity.setParams(source.values().toArray(new Object[0]));
return entity;
}
@Override public H2SqlEntity prepareBatchUpdate(Data data) {
return null;
}
}
......@@ -18,10 +18,78 @@
package org.skywalking.apm.collector.agentstream.worker.service.entry.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.skywalking.apm.collector.client.h2.H2Client;
import org.skywalking.apm.collector.client.h2.H2ClientException;
import org.skywalking.apm.collector.core.stream.Data;
import org.skywalking.apm.collector.storage.define.DataDefine;
import org.skywalking.apm.collector.storage.define.service.ServiceEntryTable;
import org.skywalking.apm.collector.storage.h2.SqlBuilder;
import org.skywalking.apm.collector.storage.h2.dao.H2DAO;
import org.skywalking.apm.collector.storage.h2.define.H2SqlEntity;
import org.skywalking.apm.collector.stream.worker.impl.dao.IPersistenceDAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author pengys5
* @author pengys5, clevertension
*/
public class ServiceEntryH2DAO extends H2DAO implements IServiceEntryDAO {
public class ServiceEntryH2DAO extends H2DAO implements IServiceEntryDAO, IPersistenceDAO<H2SqlEntity, H2SqlEntity> {
private final Logger logger = LoggerFactory.getLogger(ServiceEntryH2DAO.class);
private static final String GET_SERIVCE_ENTRY_SQL = "select * from {0} where {1} = ?";
@Override public Data get(String id, DataDefine dataDefine) {
H2Client client = getClient();
String sql = SqlBuilder.buildSql(ServiceEntryTable.TABLE, "id");
Object[] params = new Object[] {id};
try (ResultSet rs = client.executeQuery(sql, params)) {
if (rs.next()) {
Data data = dataDefine.build(id);
data.setDataInteger(0, rs.getInt(ServiceEntryTable.COLUMN_APPLICATION_ID));
data.setDataInteger(1, rs.getInt(ServiceEntryTable.COLUMN_ENTRY_SERVICE_ID));
data.setDataString(1, rs.getString(ServiceEntryTable.COLUMN_ENTRY_SERVICE_NAME));
data.setDataLong(0, rs.getLong(ServiceEntryTable.COLUMN_REGISTER_TIME));
data.setDataLong(1, rs.getLong(ServiceEntryTable.COLUMN_NEWEST_TIME));
return data;
}
} catch (SQLException | H2ClientException e) {
logger.error(e.getMessage(), e);
}
return null;
}
@Override public H2SqlEntity prepareBatchInsert(Data data) {
H2SqlEntity entity = new H2SqlEntity();
Map<String, Object> source = new HashMap<>();
source.put("id", data.getDataString(0));
source.put(ServiceEntryTable.COLUMN_APPLICATION_ID, data.getDataInteger(0));
source.put(ServiceEntryTable.COLUMN_ENTRY_SERVICE_ID, data.getDataInteger(1));
source.put(ServiceEntryTable.COLUMN_ENTRY_SERVICE_NAME, data.getDataString(1));
source.put(ServiceEntryTable.COLUMN_REGISTER_TIME, data.getDataLong(0));
source.put(ServiceEntryTable.COLUMN_NEWEST_TIME, data.getDataLong(1));
String sql = SqlBuilder.buildBatchInsertSql(ServiceEntryTable.TABLE, source.keySet());
entity.setSql(sql);
entity.setParams(source.values().toArray(new Object[0]));
return entity;
}
@Override public H2SqlEntity prepareBatchUpdate(Data data) {
H2SqlEntity entity = new H2SqlEntity();
Map<String, Object> source = new HashMap<>();
source.put(ServiceEntryTable.COLUMN_APPLICATION_ID, data.getDataInteger(0));
source.put(ServiceEntryTable.COLUMN_ENTRY_SERVICE_ID, data.getDataInteger(1));
source.put(ServiceEntryTable.COLUMN_ENTRY_SERVICE_NAME, data.getDataString(1));
source.put(ServiceEntryTable.COLUMN_REGISTER_TIME, data.getDataLong(0));
source.put(ServiceEntryTable.COLUMN_NEWEST_TIME, data.getDataLong(1));
String id = data.getDataString(0);
String sql = SqlBuilder.buildBatchUpdateSql(ServiceEntryTable.TABLE, source.keySet(), "id");
entity.setSql(sql);
List<Object> values = new ArrayList<>(source.values());
values.add(id);
entity.setParams(values.toArray(new Object[0]));
return entity;
}
}
......@@ -18,25 +18,112 @@
package org.skywalking.apm.collector.agentstream.worker.serviceref.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.skywalking.apm.collector.client.h2.H2Client;
import org.skywalking.apm.collector.client.h2.H2ClientException;
import org.skywalking.apm.collector.core.stream.Data;
import org.skywalking.apm.collector.storage.define.DataDefine;
import org.skywalking.apm.collector.storage.define.serviceref.ServiceReferenceTable;
import org.skywalking.apm.collector.storage.h2.SqlBuilder;
import org.skywalking.apm.collector.storage.h2.dao.H2DAO;
import org.skywalking.apm.collector.storage.h2.define.H2SqlEntity;
import org.skywalking.apm.collector.stream.worker.impl.dao.IPersistenceDAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author pengys5
* @author pengys5, clevertension
*/
public class ServiceReferenceH2DAO extends H2DAO implements IServiceReferenceDAO, IPersistenceDAO<String, String> {
@Override public Data get(String id, DataDefine dataDefine) {
public class ServiceReferenceH2DAO extends H2DAO implements IServiceReferenceDAO, IPersistenceDAO<H2SqlEntity, H2SqlEntity> {
private final Logger logger = LoggerFactory.getLogger(ServiceReferenceH2DAO.class);
private static final String GET_SQL = "select * from {0} where {1} = ?";
@Override
public Data get(String id, DataDefine dataDefine) {
H2Client client = getClient();
String sql = SqlBuilder.buildSql(GET_SQL, ServiceReferenceTable.TABLE, "id");
Object[] params = new Object[]{id};
try (ResultSet rs = client.executeQuery(sql, params)) {
if (rs.next()) {
Data data = dataDefine.build(id);
data.setDataInteger(0, rs.getInt(ServiceReferenceTable.COLUMN_ENTRY_SERVICE_ID));
data.setDataString(1, rs.getString(ServiceReferenceTable.COLUMN_ENTRY_SERVICE_NAME));
data.setDataInteger(1, rs.getInt(ServiceReferenceTable.COLUMN_FRONT_SERVICE_ID));
data.setDataString(2, rs.getString(ServiceReferenceTable.COLUMN_FRONT_SERVICE_NAME));
data.setDataInteger(2, rs.getInt(ServiceReferenceTable.COLUMN_BEHIND_SERVICE_ID));
data.setDataString(3, rs.getString(ServiceReferenceTable.COLUMN_BEHIND_SERVICE_NAME));
data.setDataLong(0, rs.getLong(ServiceReferenceTable.COLUMN_S1_LTE));
data.setDataLong(1, rs.getLong(ServiceReferenceTable.COLUMN_S3_LTE));
data.setDataLong(2, rs.getLong(ServiceReferenceTable.COLUMN_S5_LTE));
data.setDataLong(3, rs.getLong(ServiceReferenceTable.COLUMN_S5_GT));
data.setDataLong(4, rs.getLong(ServiceReferenceTable.COLUMN_SUMMARY));
data.setDataLong(5, rs.getLong(ServiceReferenceTable.COLUMN_ERROR));
data.setDataLong(6, rs.getLong(ServiceReferenceTable.COLUMN_COST_SUMMARY));
data.setDataLong(7, rs.getLong(ServiceReferenceTable.COLUMN_TIME_BUCKET));
return data;
}
} catch (SQLException | H2ClientException e) {
logger.error(e.getMessage(), e);
}
return null;
}
@Override public String prepareBatchInsert(Data data) {
return null;
@Override
public H2SqlEntity prepareBatchInsert(Data data) {
H2SqlEntity entity = new H2SqlEntity();
Map<String, Object> source = new HashMap<>();
source.put("id", data.getDataString(0));
source.put(ServiceReferenceTable.COLUMN_ENTRY_SERVICE_ID, data.getDataInteger(0));
source.put(ServiceReferenceTable.COLUMN_ENTRY_SERVICE_NAME, data.getDataString(1));
source.put(ServiceReferenceTable.COLUMN_FRONT_SERVICE_ID, data.getDataInteger(1));
source.put(ServiceReferenceTable.COLUMN_FRONT_SERVICE_NAME, data.getDataString(2));
source.put(ServiceReferenceTable.COLUMN_BEHIND_SERVICE_ID, data.getDataInteger(2));
source.put(ServiceReferenceTable.COLUMN_BEHIND_SERVICE_NAME, data.getDataString(3));
source.put(ServiceReferenceTable.COLUMN_S1_LTE, data.getDataLong(0));
source.put(ServiceReferenceTable.COLUMN_S3_LTE, data.getDataLong(1));
source.put(ServiceReferenceTable.COLUMN_S5_LTE, data.getDataLong(2));
source.put(ServiceReferenceTable.COLUMN_S5_GT, data.getDataLong(3));
source.put(ServiceReferenceTable.COLUMN_SUMMARY, data.getDataLong(4));
source.put(ServiceReferenceTable.COLUMN_ERROR, data.getDataLong(5));
source.put(ServiceReferenceTable.COLUMN_COST_SUMMARY, data.getDataLong(6));
source.put(ServiceReferenceTable.COLUMN_TIME_BUCKET, data.getDataLong(7));
String sql = SqlBuilder.buildBatchInsertSql(ServiceReferenceTable.TABLE, source.keySet());
entity.setSql(sql);
entity.setParams(source.values().toArray(new Object[0]));
return entity;
}
@Override public String prepareBatchUpdate(Data data) {
return null;
@Override
public H2SqlEntity prepareBatchUpdate(Data data) {
H2SqlEntity entity = new H2SqlEntity();
Map<String, Object> source = new HashMap<>();
source.put(ServiceReferenceTable.COLUMN_ENTRY_SERVICE_ID, data.getDataInteger(0));
source.put(ServiceReferenceTable.COLUMN_ENTRY_SERVICE_NAME, data.getDataString(1));
source.put(ServiceReferenceTable.COLUMN_FRONT_SERVICE_ID, data.getDataInteger(1));
source.put(ServiceReferenceTable.COLUMN_FRONT_SERVICE_NAME, data.getDataString(2));
source.put(ServiceReferenceTable.COLUMN_BEHIND_SERVICE_ID, data.getDataInteger(2));
source.put(ServiceReferenceTable.COLUMN_BEHIND_SERVICE_NAME, data.getDataString(3));
source.put(ServiceReferenceTable.COLUMN_S1_LTE, data.getDataLong(0));
source.put(ServiceReferenceTable.COLUMN_S3_LTE, data.getDataLong(1));
source.put(ServiceReferenceTable.COLUMN_S5_LTE, data.getDataLong(2));
source.put(ServiceReferenceTable.COLUMN_S5_GT, data.getDataLong(3));
source.put(ServiceReferenceTable.COLUMN_SUMMARY, data.getDataLong(4));
source.put(ServiceReferenceTable.COLUMN_ERROR, data.getDataLong(5));
source.put(ServiceReferenceTable.COLUMN_COST_SUMMARY, data.getDataLong(6));
source.put(ServiceReferenceTable.COLUMN_TIME_BUCKET, data.getDataLong(7));
String id = data.getDataString(0);
String sql = SqlBuilder.buildBatchUpdateSql(ServiceReferenceTable.TABLE, source.keySet(), "id");
entity.setSql(sql);
List<Object> values = new ArrayList<>(source.values());
values.add(id);
entity.setParams(values.toArray(new Object[0]));
return entity;
}
}
cluster:
zookeeper:
hostPort: localhost:2181
sessionTimeout: 100000
#cluster:
# zookeeper:
# hostPort: localhost:2181
# sessionTimeout: 100000
agent_server:
jetty:
host: localhost
......@@ -34,3 +34,9 @@ storage:
cluster_nodes: localhost:9300
index_shards_number: 2
index_replicas_number: 0
# uncomment to enable h2 storage
#storage:
# h2:
# url: jdbc:h2:~/collector
# user_name: sa
# password: sa
\ No newline at end of file
......@@ -18,20 +18,45 @@
package org.skywalking.apm.collector.cache.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.skywalking.apm.collector.client.h2.H2Client;
import org.skywalking.apm.collector.client.h2.H2ClientException;
import org.skywalking.apm.collector.core.util.Const;
import org.skywalking.apm.collector.storage.define.register.ApplicationTable;
import org.skywalking.apm.collector.storage.h2.SqlBuilder;
import org.skywalking.apm.collector.storage.h2.dao.H2DAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author pengys5
* @author pengys5, clevertension
*/
public class ApplicationH2DAO extends H2DAO implements IApplicationDAO {
@Override public int getApplicationId(String applicationCode) {
H2Client client = getClient();
return 100;
private final Logger logger = LoggerFactory.getLogger(ApplicationH2DAO.class);
private static final String GET_APPLICATION_CODE_SQL = "select {0} from {1} where {2} = ?";
@Override
public int getApplicationId(String applicationCode) {
logger.info("get the application id with application code = {}", applicationCode);
String sql = "select " + ApplicationTable.COLUMN_APPLICATION_ID + " from " +
ApplicationTable.TABLE + " where " + ApplicationTable.COLUMN_APPLICATION_CODE + "='" + applicationCode + "'";
return getIntValueBySQL(sql);
}
@Override public String getApplicationCode(int applicationId) {
return null;
logger.debug("get application code, applicationId: {}", applicationId);
H2Client client = getClient();
String sql = SqlBuilder.buildSql(GET_APPLICATION_CODE_SQL, ApplicationTable.COLUMN_APPLICATION_CODE, ApplicationTable.TABLE, ApplicationTable.COLUMN_APPLICATION_ID);
Object[] params = new Object[] {applicationId};
try (ResultSet rs = client.executeQuery(sql, params)) {
if (rs.next()) {
return rs.getString(1);
}
} catch (SQLException | H2ClientException e) {
logger.error(e.getMessage(), e);
}
return Const.UNKNOWN;
}
}
......@@ -18,13 +18,37 @@
package org.skywalking.apm.collector.cache.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.skywalking.apm.collector.client.h2.H2Client;
import org.skywalking.apm.collector.client.h2.H2ClientException;
import org.skywalking.apm.collector.storage.define.register.InstanceTable;
import org.skywalking.apm.collector.storage.h2.SqlBuilder;
import org.skywalking.apm.collector.storage.h2.dao.H2DAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author pengys5
*/
public class InstanceH2DAO extends H2DAO implements IInstanceDAO {
private final Logger logger = LoggerFactory.getLogger(InstanceH2DAO.class);
private static final String GET_APPLICATION_ID_SQL = "select {0} from {1} where {2} = ?";
@Override public int getApplicationId(int applicationInstanceId) {
logger.info("get the application id with application id = {}", applicationInstanceId);
H2Client client = getClient();
String sql = SqlBuilder.buildSql(GET_APPLICATION_ID_SQL, InstanceTable.COLUMN_APPLICATION_ID, InstanceTable.TABLE, InstanceTable.COLUMN_APPLICATION_ID);
Object[] params = new Object[] {applicationInstanceId};
try (ResultSet rs = client.executeQuery(sql, params)) {
if (rs.next()) {
return rs.getInt(InstanceTable.COLUMN_APPLICATION_ID);
}
} catch (SQLException | H2ClientException e) {
logger.error(e.getMessage(), e);
}
return 0;
}
}
......@@ -18,18 +18,53 @@
package org.skywalking.apm.collector.cache.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.skywalking.apm.collector.client.h2.H2Client;
import org.skywalking.apm.collector.client.h2.H2ClientException;
import org.skywalking.apm.collector.core.util.Const;
import org.skywalking.apm.collector.storage.define.register.ServiceNameTable;
import org.skywalking.apm.collector.storage.h2.SqlBuilder;
import org.skywalking.apm.collector.storage.h2.dao.H2DAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author pengys5
* @author pengys5, clevertension
*/
public class ServiceNameH2DAO extends H2DAO implements IServiceNameDAO {
private final Logger logger = LoggerFactory.getLogger(ServiceNameH2DAO.class);
private static final String GET_SERVICE_NAME_SQL = "select {0} from {1} where {2} = ?";
private static final String GET_SERVICE_ID_SQL = "select {0} from {1} where {2} = ? and {3} = ? limit 1";
@Override public String getServiceName(int serviceId) {
return null;
H2Client client = getClient();
String sql = SqlBuilder.buildSql(GET_SERVICE_NAME_SQL, ServiceNameTable.COLUMN_SERVICE_NAME,
ServiceNameTable.TABLE, ServiceNameTable.COLUMN_SERVICE_ID);
Object[] params = new Object[] {serviceId};
try (ResultSet rs = client.executeQuery(sql, params)) {
if (rs.next()) {
return rs.getString(ServiceNameTable.COLUMN_SERVICE_NAME);
}
} catch (SQLException | H2ClientException e) {
logger.error(e.getMessage(), e);
}
return Const.UNKNOWN;
}
@Override public int getServiceId(int applicationId, String serviceName) {
H2Client client = getClient();
String sql = SqlBuilder.buildSql(GET_SERVICE_ID_SQL, ServiceNameTable.COLUMN_SERVICE_ID,
ServiceNameTable.TABLE, ServiceNameTable.COLUMN_APPLICATION_ID, ServiceNameTable.COLUMN_SERVICE_NAME);
Object[] params = new Object[] {applicationId, serviceName};
try (ResultSet rs = client.executeQuery(sql, params)) {
if (rs.next()) {
int serviceId = rs.getInt(ServiceNameTable.COLUMN_SERVICE_ID);
return serviceId;
}
} catch (SQLException | H2ClientException e) {
logger.error(e.getMessage(), e);
}
return 0;
}
}
......@@ -18,11 +18,10 @@
package org.skywalking.apm.collector.client.h2;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.*;
import org.h2.jdbcx.JdbcConnectionPool;
import org.h2.util.IOUtils;
import org.skywalking.apm.collector.core.client.Client;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -34,25 +33,47 @@ public class H2Client implements Client {
private final Logger logger = LoggerFactory.getLogger(H2Client.class);
private JdbcConnectionPool cp;
private Connection conn;
private String url;
private String userName;
private String password;
public H2Client() {
this.url = "jdbc:h2:mem:collector";
this.userName = "";
this.password = "";
}
public H2Client(String url, String userName, String password) {
this.url = url;
this.userName = userName;
this.password = password;
}
@Override public void initialize() throws H2ClientException {
try {
Class.forName("org.h2.Driver");
conn = DriverManager.getConnection("jdbc:h2:mem:collector");
} catch (ClassNotFoundException | SQLException e) {
cp = JdbcConnectionPool.
create(this.url, this.userName, this.password);
conn = cp.getConnection();
} catch (Exception e) {
throw new H2ClientException(e.getMessage(), e);
}
}
@Override public void shutdown() {
if (cp != null) {
cp.dispose();
}
IOUtils.closeSilently(conn);
}
public Connection getConnection() throws H2ClientException {
return conn;
}
public void execute(String sql) throws H2ClientException {
Statement statement = null;
try {
statement = conn.createStatement();
try (Statement statement = getConnection().createStatement()) {
statement.execute(sql);
statement.closeOnCompletion();
} catch (SQLException e) {
......@@ -60,17 +81,40 @@ public class H2Client implements Client {
}
}
public void executeQuery(String sql) throws H2ClientException {
Statement statement = null;
public ResultSet executeQuery(String sql, Object[] params) throws H2ClientException {
logger.debug("execute query with result: {}", sql);
ResultSet rs;
PreparedStatement statement;
try {
statement = conn.createStatement();
ResultSet rs = statement.executeQuery(sql);
while (rs.next()) {
logger.debug(rs.getString("ADDRESS") + "," + rs.getString("DATA"));
statement = getConnection().prepareStatement(sql);
if (params != null) {
for (int i = 0; i < params.length; i++) {
statement.setObject(i + 1, params[i]);
}
}
rs = statement.executeQuery();
statement.closeOnCompletion();
} catch (SQLException e) {
throw new H2ClientException(e.getMessage(), e);
}
return rs;
}
public boolean execute(String sql, Object[] params) throws H2ClientException {
logger.debug("execute insert/update/delete: {}", sql);
boolean flag;
Connection conn = getConnection();
try (PreparedStatement statement = conn.prepareStatement(sql)) {
conn.setAutoCommit(true);
if (params != null) {
for (int i = 0; i < params.length; i++) {
statement.setObject(i + 1, params[i]);
}
}
flag = statement.execute();
} catch (SQLException e) {
throw new H2ClientException(e.getMessage(), e);
}
return flag;
}
}
......@@ -78,6 +78,14 @@ public class ZookeeperClient implements Client {
}
}
public void delete(final String path, int version) throws ZookeeperClientException {
try {
zk.delete(path, version);
} catch (KeeperException | InterruptedException e) {
throw new ZookeeperClientException(e.getMessage(), e);
}
}
public byte[] getData(String path, boolean watch, Stat stat) throws ZookeeperClientException {
try {
return zk.getData(path, watch, stat);
......
......@@ -115,9 +115,15 @@ public class ClusterZKDataMonitor implements DataMonitor, Watcher {
client.getChildren(next.getKey(), true);
String serverPath = next.getKey() + "/" + value.getHostPort();
if (client.exists(serverPath, false) == null) {
Stat stat = client.exists(serverPath, false);
if (stat != null) {
client.delete(serverPath, stat.getVersion());
}
stat = client.exists(serverPath, false);
if (stat == null) {
setData(serverPath, contextPath);
} else {
client.delete(serverPath, stat.getVersion());
throw new ClusterNodeExistException("current address: " + value.getHostPort() + " has been registered, check the host and port configuration or wait a moment.");
}
}
......
......@@ -19,7 +19,7 @@
package org.skywalking.apm.collector.core.module;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.Reader;
import java.util.Map;
import org.skywalking.apm.collector.core.config.ConfigLoader;
import org.skywalking.apm.collector.core.framework.DefineException;
......@@ -39,8 +39,8 @@ public class ModuleConfigLoader implements ConfigLoader<Map<String, Map>> {
Yaml yaml = new Yaml();
try {
try {
FileReader applicationFileReader = ResourceUtils.read("application.yml");
return (Map<String, Map>)yaml.load(applicationFileReader);
Reader applicationReader = ResourceUtils.read("application.yml");
return (Map<String, Map>)yaml.load(applicationReader);
} catch (FileNotFoundException e) {
logger.info("Could not found application.yml file, use default");
return (Map<String, Map>)yaml.load(ResourceUtils.read("application-default.yml"));
......
......@@ -18,9 +18,10 @@
package org.skywalking.apm.collector.core.util;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
/**
......@@ -28,12 +29,12 @@ import java.net.URL;
*/
public class ResourceUtils {
public static FileReader read(String fileName) throws FileNotFoundException {
public static Reader read(String fileName) throws FileNotFoundException {
URL url = ResourceUtils.class.getClassLoader().getResource(fileName);
if (url == null) {
throw new FileNotFoundException("file not found: " + fileName);
}
File file = new File(ResourceUtils.class.getClassLoader().getResource(fileName).getFile());
return new FileReader(file);
InputStream inputStream = ResourceUtils.class.getClassLoader().getResourceAsStream(fileName);
return new InputStreamReader(inputStream);
}
}
/*
* Copyright 2017, OpenSkywalking Organization All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.collector.core.module;
import java.util.Map;
import org.junit.Assert;
import org.junit.Test;
import org.skywalking.apm.collector.core.framework.DefineException;
/**
* @author neeuq
*/
public class ModuleConfigLoaderTestCase {
@SuppressWarnings({ "rawtypes" })
@Test
public void testLoad() throws DefineException {
ModuleConfigLoader configLoader = new ModuleConfigLoader();
Map<String, Map> configuration = configLoader.load();
Assert.assertNotNull(configuration.get("cluster"));
Assert.assertNotNull(configuration.get("cluster").get("zookeeper"));
}
}
/*
* Copyright 2017, OpenSkywalking Organization All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.collector.core.utils;
import java.io.IOException;
import java.io.Reader;
import org.junit.Assert;
import org.junit.Test;
import org.skywalking.apm.collector.core.util.ResourceUtils;
/**
* @author neeuq
*/
public class ResourceUtilsTestCase {
@Test
public void testRead() throws IOException {
Reader reader = ResourceUtils.read("application.yml");
Assert.assertNotNull(reader);
reader.close();
}
}
......@@ -28,7 +28,7 @@ import org.skywalking.apm.collector.storage.define.AttributeType;
import org.skywalking.apm.collector.storage.define.DataDefine;
/**
* @author pengys5
* @author pengys5H
*/
public class SegmentCostDataDefine extends DataDefine {
......
/*
* Copyright 2017, OpenSkywalking Organization All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.collector.storage.h2;
import java.text.MessageFormat;
import java.util.List;
import java.util.Set;
public class SqlBuilder {
public static final String buildSql(String sql, Object ... args) {
return MessageFormat.format(sql, args);
}
public static final String buildSql(String sql, List<Object> args) {
MessageFormat messageFormat = new MessageFormat(sql);
return messageFormat.format(args.toArray(new Object[0]));
}
public static final String buildBatchInsertSql(String tableName, Set<String> columnNames) {
StringBuilder sb = new StringBuilder("insert into ");
sb.append(tableName).append("(");
columnNames.forEach((columnName) -> {
sb.append(columnName).append(",");
});
sb.delete(sb.length() - 1, sb.length());
sb.append(") values(");
for (int i = 0; i < columnNames.size(); i++) {
sb.append("?,");
}
sb.delete(sb.length() - 1, sb.length());
sb.append(")");
return sb.toString();
}
public static final String buildBatchUpdateSql(String tableName, Set<String> columnNames, String whereClauseName) {
StringBuilder sb = new StringBuilder("update ");
sb.append(tableName).append(" set ");
columnNames.forEach((columnName) -> {
sb.append(columnName).append("=?,");
});
sb.delete(sb.length() - 1, sb.length());
sb.append(" where ").append(whereClauseName).append("=?");
return sb.toString();
}
}
/*
* Copyright 2017, OpenSkywalking Organization All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.collector.storage.h2;
/**
* @author clevertension
*/
public class StorageH2Config {
public static String URL;
public static String USER_NAME;
public static String PASSWORD;
}
......@@ -21,12 +21,25 @@ package org.skywalking.apm.collector.storage.h2;
import java.util.Map;
import org.skywalking.apm.collector.core.config.ConfigParseException;
import org.skywalking.apm.collector.core.module.ModuleConfigParser;
import org.skywalking.apm.collector.core.util.ObjectUtils;
import org.skywalking.apm.collector.core.util.StringUtils;
/**
* @author pengys5
*/
public class StorageH2ConfigParser implements ModuleConfigParser {
private static final String URL = "url";
public static final String USER_NAME = "user_name";
public static final String PASSWORD = "password";
@Override public void parse(Map config) throws ConfigParseException {
if (ObjectUtils.isNotEmpty(config) && StringUtils.isNotEmpty(config.get(URL))) {
StorageH2Config.URL = (String)config.get(URL);
}
if (ObjectUtils.isNotEmpty(config) && StringUtils.isNotEmpty(config.get(USER_NAME))) {
StorageH2Config.USER_NAME = (String)config.get(USER_NAME);
}
if (ObjectUtils.isNotEmpty(config) && StringUtils.isNotEmpty(config.get(PASSWORD))) {
StorageH2Config.PASSWORD = (String)config.get(PASSWORD);
}
}
}
......@@ -51,7 +51,7 @@ public class StorageH2ModuleDefine extends StorageModuleDefine {
}
@Override protected Client createClient() {
return new H2Client();
return new H2Client(StorageH2Config.URL, StorageH2Config.USER_NAME, StorageH2Config.PASSWORD);
}
@Override public StorageInstaller storageInstaller() {
......
......@@ -18,15 +18,69 @@
package org.skywalking.apm.collector.storage.h2.dao;
import java.util.List;
import org.skywalking.apm.collector.client.h2.H2ClientException;
import org.skywalking.apm.collector.storage.dao.IBatchDAO;
import org.skywalking.apm.collector.storage.h2.define.H2SqlEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author pengys5
*/
public class BatchH2DAO extends H2DAO implements IBatchDAO {
private final Logger logger = LoggerFactory.getLogger(BatchH2DAO.class);
@Override public void batchPersistence(List<?> batchCollection) {
@Override
public void batchPersistence(List<?> batchCollection) {
if (batchCollection != null && batchCollection.size() > 0) {
logger.debug("the batch collection size is {}", batchCollection.size());
Connection conn = null;
final Map<String, PreparedStatement> batchSqls = new HashMap<>();
try {
conn = getClient().getConnection();
conn.setAutoCommit(true);
PreparedStatement ps;
for (Object entity : batchCollection) {
H2SqlEntity e = getH2SqlEntity(entity);
String sql = e.getSql();
if (batchSqls.containsKey(sql)) {
ps = batchSqls.get(sql);
} else {
ps = conn.prepareStatement(sql);
batchSqls.put(sql, ps);
}
Object[] params = e.getParams();
if (params != null) {
logger.debug("the sql is {}, params size is {}", e.getSql(), params.length);
for (int i = 0; i < params.length; i++) {
ps.setObject(i + 1, params[i]);
}
}
ps.addBatch();
}
for (String k : batchSqls.keySet()) {
batchSqls.get(k).executeBatch();
}
} catch (SQLException | H2ClientException e) {
logger.error(e.getMessage(), e);
}
batchSqls.clear();
}
}
private H2SqlEntity getH2SqlEntity(Object entity) {
if (entity instanceof H2SqlEntity) {
return (H2SqlEntity) entity;
}
return null;
}
}
......@@ -18,11 +18,45 @@
package org.skywalking.apm.collector.storage.h2.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.skywalking.apm.collector.client.h2.H2Client;
import org.skywalking.apm.collector.client.h2.H2ClientException;
import org.skywalking.apm.collector.storage.dao.DAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author pengys5
*/
public abstract class H2DAO extends DAO<H2Client> {
private final Logger logger = LoggerFactory.getLogger(H2DAO.class);
public final int getMaxId(String tableName, String columnName) {
String sql = "select max(" + columnName + ") from " + tableName;
return getIntValueBySQL(sql);
}
public final int getMinId(String tableName, String columnName) {
String sql = "select min(" + columnName + ") from " + tableName;
return getIntValueBySQL(sql);
}
public final int getIntValueBySQL(String sql) {
H2Client client = getClient();
try (ResultSet rs = client.executeQuery(sql, null)) {
if (rs.next()) {
int id = rs.getInt(1);
if (id == Integer.MAX_VALUE || id == Integer.MIN_VALUE) {
return 0;
} else {
return id;
}
}
} catch (SQLException | H2ClientException e) {
logger.error(e.getMessage(), e);
}
return 0;
}
}
/*
* Copyright 2017, OpenSkywalking Organization All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.collector.storage.h2.define;
/**
* @author clevertension
*/
public class H2SqlEntity {
private String sql;
private Object[] params;
public String getSql() {
return sql;
}
public void setSql(String sql) {
this.sql = sql;
}
public Object[] getParams() {
return params;
}
public void setParams(Object[] params) {
this.params = params;
}
}
......@@ -18,7 +18,6 @@
package org.skywalking.apm.collector.storage.h2.define;
import java.util.List;
import org.skywalking.apm.collector.client.h2.H2Client;
import org.skywalking.apm.collector.client.h2.H2ClientException;
import org.skywalking.apm.collector.core.client.Client;
......@@ -26,12 +25,20 @@ import org.skywalking.apm.collector.core.storage.StorageException;
import org.skywalking.apm.collector.core.storage.StorageInstallException;
import org.skywalking.apm.collector.core.storage.StorageInstaller;
import org.skywalking.apm.collector.core.storage.TableDefine;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
/**
* @author pengys5
*/
public class H2StorageInstaller extends StorageInstaller {
private final Logger logger = LoggerFactory.getLogger(H2StorageInstaller.class);
@Override protected void defineFilter(List<TableDefine> tableDefines) {
int size = tableDefines.size();
for (int i = size - 1; i >= 0; i--) {
......@@ -42,11 +49,36 @@ public class H2StorageInstaller extends StorageInstaller {
}
@Override protected boolean isExists(Client client, TableDefine tableDefine) throws StorageException {
H2Client h2Client = (H2Client)client;
ResultSet rs = null;
try {
logger.info("check if table {} exist ", tableDefine.getName());
rs = h2Client.getConnection().getMetaData().getTables(null, null, tableDefine.getName().toUpperCase(), null);
if (rs.next()) {
return true;
}
} catch (SQLException | H2ClientException e) {
throw new StorageInstallException(e.getMessage(), e);
} finally {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
throw new StorageInstallException(e.getMessage(), e);
}
}
return false;
}
@Override protected boolean deleteTable(Client client, TableDefine tableDefine) throws StorageException {
return false;
H2Client h2Client = (H2Client)client;
try {
h2Client.execute("drop table if exists " + tableDefine.getName());
return true;
} catch (H2ClientException e) {
throw new StorageInstallException(e.getMessage(), e);
}
}
@Override protected boolean createTable(Client client, TableDefine tableDefine) throws StorageException {
......@@ -59,14 +91,16 @@ public class H2StorageInstaller extends StorageInstaller {
h2TableDefine.getColumnDefines().forEach(columnDefine -> {
H2ColumnDefine h2ColumnDefine = (H2ColumnDefine)columnDefine;
if (h2ColumnDefine.getType().equals(H2ColumnDefine.Type.Varchar.name())) {
sqlBuilder.append(h2ColumnDefine.getName()).append(" ").append(h2ColumnDefine.getType()).append("(255)");
sqlBuilder.append(h2ColumnDefine.getName()).append(" ").append(h2ColumnDefine.getType()).append("(255),");
} else {
sqlBuilder.append(h2ColumnDefine.getName()).append(" ").append(h2ColumnDefine.getType());
sqlBuilder.append(h2ColumnDefine.getName()).append(" ").append(h2ColumnDefine.getType()).append(",");
}
});
//remove last comma
sqlBuilder.delete(sqlBuilder.length() - 1, sqlBuilder.length());
sqlBuilder.append(")");
try {
logger.info("create h2 table with sql {}", sqlBuilder);
h2Client.execute(sqlBuilder.toString());
} catch (H2ClientException e) {
throw new StorageInstallException(e.getMessage(), e);
......
/*
* Copyright 2017, OpenSkywalking Organization All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.collector.ui.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.skywalking.apm.collector.client.h2.H2Client;
import org.skywalking.apm.collector.client.h2.H2ClientException;
import org.skywalking.apm.collector.core.util.Const;
import org.skywalking.apm.collector.core.util.TimeBucketUtils;
import org.skywalking.apm.collector.storage.define.jvm.CpuMetricTable;
import org.skywalking.apm.collector.storage.h2.SqlBuilder;
import org.skywalking.apm.collector.storage.h2.dao.H2DAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.gson.JsonArray;
/**
* @author pengys5, clevertension
*/
public class CpuMetricH2DAO extends H2DAO implements ICpuMetricDAO {
private final Logger logger = LoggerFactory.getLogger(InstanceH2DAO.class);
private static final String GET_CPU_METRIC_SQL = "select * from {0} where {1} = ?";
private static final String GET_CPU_METRICS_SQL = "select * from {0} where {1} in (";
@Override public int getMetric(int instanceId, long timeBucket) {
String id = timeBucket + Const.ID_SPLIT + instanceId;
H2Client client = getClient();
String sql = SqlBuilder.buildSql(GET_CPU_METRIC_SQL, CpuMetricTable.TABLE, "id");
Object[] params = new Object[]{id};
try (ResultSet rs = client.executeQuery(sql, params)) {
if (rs.next()) {
return rs.getInt(CpuMetricTable.COLUMN_USAGE_PERCENT);
}
} catch (SQLException | H2ClientException e) {
logger.error(e.getMessage(), e);
}
return 0;
}
@Override public JsonArray getMetric(int instanceId, long startTimeBucket, long endTimeBucket) {
H2Client client = getClient();
String sql = SqlBuilder.buildSql(GET_CPU_METRICS_SQL, CpuMetricTable.TABLE, "id");
long timeBucket = startTimeBucket;
List<String> idList = new ArrayList<>();
do {
timeBucket = TimeBucketUtils.INSTANCE.addSecondForSecondTimeBucket(TimeBucketUtils.TimeBucketType.SECOND.name(), timeBucket, 1);
String id = timeBucket + Const.ID_SPLIT + instanceId;
idList.add(id);
}
while (timeBucket <= endTimeBucket);
StringBuilder builder = new StringBuilder();
for (int i = 0; i < idList.size(); i++) {
builder.append("?,");
}
builder.delete(builder.length() - 1, builder.length());
builder.append(")");
sql = sql + builder;
Object[] params = idList.toArray(new String[0]);
JsonArray metrics = new JsonArray();
try (ResultSet rs = client.executeQuery(sql, params)) {
while (rs.next()) {
double cpuUsed = rs.getDouble(CpuMetricTable.COLUMN_USAGE_PERCENT);
metrics.add((int)(cpuUsed * 100));
}
} catch (SQLException | H2ClientException e) {
logger.error(e.getMessage(), e);
}
return metrics;
}
}
......@@ -18,23 +18,158 @@
package org.skywalking.apm.collector.ui.dao;
import com.google.gson.JsonObject;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.skywalking.apm.collector.client.h2.H2Client;
import org.skywalking.apm.collector.client.h2.H2ClientException;
import org.skywalking.apm.collector.core.util.Const;
import org.skywalking.apm.collector.core.util.TimeBucketUtils;
import org.skywalking.apm.collector.storage.define.jvm.GCMetricTable;
import org.skywalking.apm.collector.storage.h2.SqlBuilder;
import org.skywalking.apm.collector.storage.h2.dao.H2DAO;
import org.skywalking.apm.network.proto.GCPhrase;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
/**
* @author pengys5
* @author pengys5, clevertension
*/
public class GCMetricH2DAO extends H2DAO implements IGCMetricDAO {
private final Logger logger = LoggerFactory.getLogger(GCMetricH2DAO.class);
private static final String GET_GC_COUNT_SQL = "select {1}, sum({0}) as cnt, {1} from {2} where {3} = ? and {4} in (";
private static final String GET_GC_METRIC_SQL = "select * from {0} where {1} = ?";
private static final String GET_GC_METRICS_SQL = "select * from {0} where {1} in (";
@Override public GCCount getGCCount(long[] timeBuckets, int instanceId) {
return null;
GCCount gcCount = new GCCount();
H2Client client = getClient();
String sql = GET_GC_COUNT_SQL;
StringBuilder builder = new StringBuilder();
for (int i = 0; i < timeBuckets.length; i++) {
builder.append("?,");
}
builder.delete(builder.length() - 1, builder.length());
builder.append(")");
sql = sql + builder + " group by {1}";
sql = SqlBuilder.buildSql(sql, GCMetricTable.COLUMN_COUNT, GCMetricTable.COLUMN_PHRASE,
GCMetricTable.TABLE, GCMetricTable.COLUMN_INSTANCE_ID, "id");
Object[] params = new Object[timeBuckets.length + 1];
for (int i = 0; i < timeBuckets.length; i++) {
params[i + 1] = timeBuckets[i];
}
params[0] = instanceId;
try (ResultSet rs = client.executeQuery(sql, params)) {
if (rs.next()) {
int phrase = rs.getInt(GCMetricTable.COLUMN_PHRASE);
int count = rs.getInt("cnt");
if (phrase == GCPhrase.NEW_VALUE) {
gcCount.setYoung(count);
} else if (phrase == GCPhrase.OLD_VALUE) {
gcCount.setOld(count);
}
}
} catch (SQLException | H2ClientException e) {
logger.error(e.getMessage(), e);
}
return gcCount;
}
@Override public JsonObject getMetric(int instanceId, long timeBucket) {
return null;
JsonObject response = new JsonObject();
H2Client client = getClient();
String sql = SqlBuilder.buildSql(GET_GC_METRIC_SQL, GCMetricTable.TABLE, "id");
String youngId = timeBucket + Const.ID_SPLIT + GCPhrase.NEW_VALUE + instanceId;
Object[] params = new Object[]{youngId};
try (ResultSet rs = client.executeQuery(sql, params)) {
if (rs.next()) {
response.addProperty("ygc", rs.getInt(GCMetricTable.COLUMN_COUNT));
}
} catch (SQLException | H2ClientException e) {
logger.error(e.getMessage(), e);
}
String oldId = timeBucket + Const.ID_SPLIT + GCPhrase.OLD_VALUE + instanceId;
Object[] params1 = new Object[]{oldId};
try (ResultSet rs = client.executeQuery(sql, params1)) {
if (rs.next()) {
response.addProperty("ogc", rs.getInt(GCMetricTable.COLUMN_COUNT));
}
} catch (SQLException | H2ClientException e) {
logger.error(e.getMessage(), e);
}
return response;
}
@Override public JsonObject getMetric(int instanceId, long startTimeBucket, long endTimeBucket) {
return null;
JsonObject response = new JsonObject();
H2Client client = getClient();
String sql = SqlBuilder.buildSql(GET_GC_METRICS_SQL, GCMetricTable.TABLE, "id");
long timeBucket = startTimeBucket;
List<String> idList = new ArrayList<>();
do {
timeBucket = TimeBucketUtils.INSTANCE.addSecondForSecondTimeBucket(TimeBucketUtils.TimeBucketType.SECOND.name(), timeBucket, 1);
String youngId = timeBucket + Const.ID_SPLIT + instanceId + Const.ID_SPLIT + GCPhrase.NEW_VALUE;
idList.add(youngId);
}
while (timeBucket <= endTimeBucket);
StringBuilder builder = new StringBuilder();
for (int i = 0; i < idList.size(); i++) {
builder.append("?,");
}
builder.delete(builder.length() - 1, builder.length());
builder.append(")");
sql = sql + builder;
Object[] params = idList.toArray(new String[0]);
JsonArray youngArray = new JsonArray();
try (ResultSet rs = client.executeQuery(sql, params)) {
while (rs.next()) {
youngArray.add(rs.getInt(GCMetricTable.COLUMN_COUNT));
}
if (youngArray.size() == 0) {
youngArray.add(0);
}
} catch (SQLException | H2ClientException e) {
logger.error(e.getMessage(), e);
}
response.add("ygc", youngArray);
List<String> idList1 = new ArrayList<>();
timeBucket = startTimeBucket;
do {
timeBucket = TimeBucketUtils.INSTANCE.addSecondForSecondTimeBucket(TimeBucketUtils.TimeBucketType.SECOND.name(), timeBucket, 1);
String oldId = timeBucket + Const.ID_SPLIT + instanceId + Const.ID_SPLIT + GCPhrase.OLD_VALUE;
idList1.add(oldId);
}
while (timeBucket <= endTimeBucket);
String sql1 = SqlBuilder.buildSql(GET_GC_METRICS_SQL, GCMetricTable.TABLE, "id");
StringBuilder builder1 = new StringBuilder();
for (int i = 0; i < idList1.size(); i++) {
builder1.append("?,");
}
builder1.delete(builder1.length() - 1, builder1.length());
builder1.append(")");
sql1 = sql1 + builder1;
Object[] params1 = idList.toArray(new String[0]);
JsonArray oldArray = new JsonArray();
try (ResultSet rs = client.executeQuery(sql1, params1)) {
while (rs.next()) {
oldArray.add(rs.getInt(GCMetricTable.COLUMN_COUNT));
}
if (oldArray.size() == 0) {
oldArray.add(0);
}
} catch (SQLException | H2ClientException e) {
logger.error(e.getMessage(), e);
}
response.add("ogc", oldArray);
return response;
}
}
......@@ -18,18 +18,59 @@
package org.skywalking.apm.collector.ui.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.skywalking.apm.collector.client.h2.H2Client;
import org.skywalking.apm.collector.client.h2.H2ClientException;
import org.skywalking.apm.collector.storage.define.global.GlobalTraceTable;
import org.skywalking.apm.collector.storage.h2.SqlBuilder;
import org.skywalking.apm.collector.storage.h2.dao.H2DAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author pengys5
* @author pengys5, clevertension
*/
public class GlobalTraceH2DAO extends H2DAO implements IGlobalTraceDAO {
private final Logger logger = LoggerFactory.getLogger(GlobalTraceH2DAO.class);
private static final String GET_GLOBAL_TRACE_ID_SQL = "select {0} from {1} where {2} = ? limit 10";
private static final String GET_SEGMENT_IDS_SQL = "select {0} from {1} where {2} = ? limit 10";
@Override public List<String> getGlobalTraceId(String segmentId) {
return null;
List<String> globalTraceIds = new ArrayList<>();
H2Client client = getClient();
String sql = SqlBuilder.buildSql(GET_GLOBAL_TRACE_ID_SQL, GlobalTraceTable.COLUMN_GLOBAL_TRACE_ID,
GlobalTraceTable.TABLE, GlobalTraceTable.COLUMN_SEGMENT_ID);
Object[] params = new Object[]{segmentId};
try (ResultSet rs = client.executeQuery(sql, params)) {
while (rs.next()) {
String globalTraceId = rs.getString(GlobalTraceTable.COLUMN_GLOBAL_TRACE_ID);
logger.debug("segmentId: {}, global trace id: {}", segmentId, globalTraceId);
globalTraceIds.add(globalTraceId);
}
} catch (SQLException | H2ClientException e) {
logger.error(e.getMessage(), e);
}
return globalTraceIds;
}
@Override public List<String> getSegmentIds(String globalTraceId) {
return null;
List<String> segmentIds = new ArrayList<>();
H2Client client = getClient();
String sql = SqlBuilder.buildSql(GET_SEGMENT_IDS_SQL, GlobalTraceTable.COLUMN_SEGMENT_ID,
GlobalTraceTable.TABLE, GlobalTraceTable.COLUMN_GLOBAL_TRACE_ID);
Object[] params = new Object[]{globalTraceId};
try (ResultSet rs = client.executeQuery(sql, params)) {
while (rs.next()) {
String segmentId = rs.getString(GlobalTraceTable.COLUMN_SEGMENT_ID);
logger.debug("segmentId: {}, global trace id: {}", segmentId, globalTraceId);
segmentIds.add(globalTraceId);
}
} catch (SQLException | H2ClientException e) {
logger.error(e.getMessage(), e);
}
return segmentIds;
}
}
......@@ -18,31 +18,157 @@
package org.skywalking.apm.collector.ui.dao;
import com.google.gson.JsonArray;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.skywalking.apm.collector.client.h2.H2Client;
import org.skywalking.apm.collector.client.h2.H2ClientException;
import org.skywalking.apm.collector.core.util.Const;
import org.skywalking.apm.collector.core.util.TimeBucketUtils;
import org.skywalking.apm.collector.storage.define.instance.InstPerformanceTable;
import org.skywalking.apm.collector.storage.h2.SqlBuilder;
import org.skywalking.apm.collector.storage.h2.dao.H2DAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.gson.JsonArray;
/**
* @author pengys5
* @author pengys5, clevertension
*/
public class InstPerformanceH2DAO extends H2DAO implements IInstPerformanceDAO {
private final Logger logger = LoggerFactory.getLogger(InstPerformanceH2DAO.class);
private static final String GET_INST_PERF_SQL = "select * from {0} where {1} = ? and {2} in (";
private static final String GET_TPS_METRIC_SQL = "select * from {0} where {1} = ?";
private static final String GET_TPS_METRICS_SQL = "select * from {0} where {1} in (";
@Override public InstPerformance get(long[] timeBuckets, int instanceId) {
H2Client client = getClient();
logger.info("the inst performance inst id = {}", instanceId);
String sql = SqlBuilder.buildSql(GET_INST_PERF_SQL, InstPerformanceTable.TABLE, InstPerformanceTable.COLUMN_INSTANCE_ID, InstPerformanceTable.COLUMN_TIME_BUCKET);
StringBuilder builder = new StringBuilder();
for (int i = 0; i < timeBuckets.length; i++) {
builder.append("?,");
}
builder.delete(builder.length() - 1, builder.length());
builder.append(")");
sql = sql + builder;
Object[] params = new Object[timeBuckets.length + 1];
for (int i = 0; i < timeBuckets.length; i++) {
params[i + 1] = timeBuckets[i];
}
params[0] = instanceId;
try (ResultSet rs = client.executeQuery(sql, params)) {
if (rs.next()) {
int callTimes = rs.getInt(InstPerformanceTable.COLUMN_CALLS);
int costTotal = rs.getInt(InstPerformanceTable.COLUMN_COST_TOTAL);
return new InstPerformance(instanceId, callTimes, costTotal);
}
} catch (SQLException | H2ClientException e) {
logger.error(e.getMessage(), e);
}
return null;
}
@Override public int getTpsMetric(int instanceId, long timeBucket) {
logger.info("getTpMetric instanceId = {}, startTimeBucket = {}", instanceId, timeBucket);
H2Client client = getClient();
String sql = SqlBuilder.buildSql(GET_TPS_METRIC_SQL, InstPerformanceTable.TABLE, "id");
Object[] params = new Object[]{instanceId};
try (ResultSet rs = client.executeQuery(sql, params)) {
if (rs.next()) {
return rs.getInt(InstPerformanceTable.COLUMN_CALLS);
}
} catch (SQLException | H2ClientException e) {
logger.error(e.getMessage(), e);
}
return 0;
}
@Override public JsonArray getTpsMetric(int instanceId, long startTimeBucket, long endTimeBucket) {
return null;
logger.info("getTpsMetric instanceId = {}, startTimeBucket = {}, endTimeBucket = {}", instanceId, startTimeBucket, endTimeBucket);
H2Client client = getClient();
String sql = SqlBuilder.buildSql(GET_TPS_METRICS_SQL, InstPerformanceTable.TABLE, "id");
long timeBucket = startTimeBucket;
List<String> idList = new ArrayList<>();
do {
String id = timeBucket + Const.ID_SPLIT + instanceId;
timeBucket = TimeBucketUtils.INSTANCE.addSecondForSecondTimeBucket(TimeBucketUtils.TimeBucketType.SECOND.name(), timeBucket, 1);
idList.add(id);
}
while (timeBucket <= endTimeBucket);
StringBuilder builder = new StringBuilder();
for (int i = 0; i < idList.size(); i++) {
builder.append("?,");
}
builder.delete(builder.length() - 1, builder.length());
builder.append(")");
sql = sql + builder;
Object[] params = idList.toArray(new String[0]);
JsonArray metrics = new JsonArray();
try (ResultSet rs = client.executeQuery(sql, params)) {
while (rs.next()) {
int calls = rs.getInt(InstPerformanceTable.COLUMN_CALLS);
metrics.add(calls);
}
} catch (SQLException | H2ClientException e) {
logger.error(e.getMessage(), e);
}
return metrics;
}
@Override public int getRespTimeMetric(int instanceId, long timeBucket) {
H2Client client = getClient();
String sql = SqlBuilder.buildSql(GET_TPS_METRIC_SQL, InstPerformanceTable.TABLE, "id");
Object[] params = new Object[]{instanceId};
try (ResultSet rs = client.executeQuery(sql, params)) {
if (rs.next()) {
int callTimes = rs.getInt(InstPerformanceTable.COLUMN_CALLS);
int costTotal = rs.getInt(InstPerformanceTable.COLUMN_COST_TOTAL);
return costTotal / callTimes;
}
} catch (SQLException | H2ClientException e) {
logger.error(e.getMessage(), e);
}
return 0;
}
@Override public JsonArray getRespTimeMetric(int instanceId, long startTimeBucket, long endTimeBucket) {
return null;
H2Client client = getClient();
String sql = SqlBuilder.buildSql(GET_TPS_METRICS_SQL, InstPerformanceTable.TABLE, "id");
long timeBucket = startTimeBucket;
List<String> idList = new ArrayList<>();
do {
String id = timeBucket + Const.ID_SPLIT + instanceId;
timeBucket = TimeBucketUtils.INSTANCE.addSecondForSecondTimeBucket(TimeBucketUtils.TimeBucketType.SECOND.name(), timeBucket, 1);
idList.add(id);
}
while (timeBucket <= endTimeBucket);
StringBuilder builder = new StringBuilder();
for (int i = 0; i < idList.size(); i++) {
builder.append("?,");
}
builder.delete(builder.length() - 1, builder.length());
builder.append(")");
sql = sql + builder;
Object[] params = idList.toArray(new String[0]);
JsonArray metrics = new JsonArray();
try (ResultSet rs = client.executeQuery(sql, params)) {
while (rs.next()) {
int callTimes = rs.getInt(InstPerformanceTable.COLUMN_CALLS);
int costTotal = rs.getInt(InstPerformanceTable.COLUMN_COST_TOTAL);
metrics.add(costTotal / callTimes);
}
} catch (SQLException | H2ClientException e) {
logger.error(e.getMessage(), e);
}
return metrics;
}
}
......@@ -18,32 +18,134 @@
package org.skywalking.apm.collector.ui.dao;
import com.google.gson.JsonArray;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import org.skywalking.apm.collector.client.h2.H2Client;
import org.skywalking.apm.collector.client.h2.H2ClientException;
import org.skywalking.apm.collector.core.util.TimeBucketUtils;
import org.skywalking.apm.collector.storage.define.register.InstanceDataDefine;
import org.skywalking.apm.collector.storage.define.register.InstanceTable;
import org.skywalking.apm.collector.storage.h2.SqlBuilder;
import org.skywalking.apm.collector.storage.h2.dao.H2DAO;
import org.skywalking.apm.collector.ui.cache.ApplicationCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
/**
* @author pengys5
* @author pengys5, clevertension
*/
public class InstanceH2DAO extends H2DAO implements IInstanceDAO {
@Override public Long lastHeartBeatTime() {
return null;
private final Logger logger = LoggerFactory.getLogger(InstanceH2DAO.class);
private static final String GET_LAST_HEARTBEAT_TIME_SQL = "select {0} from {1} where {2} > ? limit 1";
private static final String GET_INST_LAST_HEARTBEAT_TIME_SQL = "select {0} from {1} where {2} > ? and {3} = ? limit 1";
private static final String GET_INSTANCE_SQL = "select * from {0} where {1} = ?";
private static final String GET_INSTANCES_SQL = "select * from {0} where {1} = ? and {2} >= ?";
private static final String GET_APPLICATIONS_SQL = "select {3}, count({0}) as cnt from {1} where {2} >= ? and {2} <= ? group by {3} limit 100";
@Override
public Long lastHeartBeatTime() {
H2Client client = getClient();
long fiveMinuteBefore = System.currentTimeMillis() - 5 * 60 * 1000;
fiveMinuteBefore = TimeBucketUtils.INSTANCE.getSecondTimeBucket(fiveMinuteBefore);
String sql = SqlBuilder.buildSql(GET_LAST_HEARTBEAT_TIME_SQL, InstanceTable.COLUMN_HEARTBEAT_TIME, InstanceTable.TABLE, InstanceTable.COLUMN_HEARTBEAT_TIME);
Object[] params = new Object[]{fiveMinuteBefore};
try (ResultSet rs = client.executeQuery(sql, params)) {
if (rs.next()) {
return rs.getLong(1);
}
} catch (SQLException | H2ClientException e) {
logger.error(e.getMessage(), e);
}
return 0L;
}
@Override public Long instanceLastHeartBeatTime(long applicationInstanceId) {
return null;
@Override
public Long instanceLastHeartBeatTime(long applicationInstanceId) {
H2Client client = getClient();
long fiveMinuteBefore = System.currentTimeMillis() - 5 * 60 * 1000;
fiveMinuteBefore = TimeBucketUtils.INSTANCE.getSecondTimeBucket(fiveMinuteBefore);
String sql = SqlBuilder.buildSql(GET_INST_LAST_HEARTBEAT_TIME_SQL, InstanceTable.COLUMN_HEARTBEAT_TIME, InstanceTable.TABLE,
InstanceTable.COLUMN_HEARTBEAT_TIME, InstanceTable.COLUMN_INSTANCE_ID);
Object[] params = new Object[]{fiveMinuteBefore, applicationInstanceId};
try (ResultSet rs = client.executeQuery(sql, params)) {
if (rs.next()) {
return rs.getLong(1);
}
} catch (SQLException | H2ClientException e) {
logger.error(e.getMessage(), e);
}
return 0L;
}
@Override public JsonArray getApplications(long startTime, long endTime) {
return null;
@Override
public JsonArray getApplications(long startTime, long endTime) {
H2Client client = getClient();
JsonArray applications = new JsonArray();
String sql = SqlBuilder.buildSql(GET_APPLICATIONS_SQL, InstanceTable.COLUMN_INSTANCE_ID,
InstanceTable.TABLE, InstanceTable.COLUMN_HEARTBEAT_TIME, InstanceTable.COLUMN_APPLICATION_ID);
Object[] params = new Object[]{startTime, endTime};
try (ResultSet rs = client.executeQuery(sql, params)) {
while (rs.next()) {
Integer applicationId = rs.getInt(InstanceTable.COLUMN_APPLICATION_ID);
logger.debug("applicationId: {}", applicationId);
JsonObject application = new JsonObject();
application.addProperty("applicationId", applicationId);
application.addProperty("applicationCode", ApplicationCache.getForUI(applicationId));
application.addProperty("instanceCount", rs.getInt("cnt"));
applications.add(application);
}
} catch (SQLException | H2ClientException e) {
logger.error(e.getMessage(), e);
}
return applications;
}
@Override public InstanceDataDefine.Instance getInstance(int instanceId) {
@Override
public InstanceDataDefine.Instance getInstance(int instanceId) {
H2Client client = getClient();
String sql = SqlBuilder.buildSql(GET_INSTANCE_SQL, InstanceTable.TABLE, InstanceTable.COLUMN_INSTANCE_ID);
Object[] params = new Object[]{instanceId};
try (ResultSet rs = client.executeQuery(sql, params)) {
if (rs.next()) {
InstanceDataDefine.Instance instance = new InstanceDataDefine.Instance();
instance.setId(String.valueOf(instanceId));
instance.setApplicationId(rs.getInt(InstanceTable.COLUMN_APPLICATION_ID));
instance.setAgentUUID(rs.getString(InstanceTable.COLUMN_AGENT_UUID));
instance.setRegisterTime(rs.getLong(InstanceTable.COLUMN_REGISTER_TIME));
instance.setHeartBeatTime(rs.getLong(InstanceTable.COLUMN_HEARTBEAT_TIME));
instance.setOsInfo(rs.getString(InstanceTable.COLUMN_OS_INFO));
return instance;
}
} catch (SQLException | H2ClientException e) {
logger.error(e.getMessage(), e);
}
return null;
}
@Override public List<InstanceDataDefine.Instance> getInstances(int applicationId, long timeBucket) {
return null;
@Override
public List<InstanceDataDefine.Instance> getInstances(int applicationId, long timeBucket) {
logger.debug("get instances info, application id: {}, timeBucket: {}", applicationId, timeBucket);
List<InstanceDataDefine.Instance> instanceList = new LinkedList<>();
H2Client client = getClient();
String sql = SqlBuilder.buildSql(GET_INSTANCES_SQL, InstanceTable.TABLE, InstanceTable.COLUMN_APPLICATION_ID, InstanceTable.COLUMN_HEARTBEAT_TIME);
Object[] params = new Object[]{applicationId, timeBucket};
try (ResultSet rs = client.executeQuery(sql, params)) {
while (rs.next()) {
InstanceDataDefine.Instance instance = new InstanceDataDefine.Instance();
instance.setApplicationId(rs.getInt(InstanceTable.COLUMN_APPLICATION_ID));
instance.setHeartBeatTime(rs.getLong(InstanceTable.COLUMN_HEARTBEAT_TIME));
instance.setInstanceId(rs.getInt(InstanceTable.COLUMN_INSTANCE_ID));
instanceList.add(instance);
}
} catch (SQLException | H2ClientException e) {
logger.error(e.getMessage(), e);
}
return instanceList;
}
}
/*
* Copyright 2017, OpenSkywalking Organization All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.collector.ui.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.skywalking.apm.collector.client.h2.H2Client;
import org.skywalking.apm.collector.client.h2.H2ClientException;
import org.skywalking.apm.collector.core.util.Const;
import org.skywalking.apm.collector.core.util.TimeBucketUtils;
import org.skywalking.apm.collector.storage.define.jvm.MemoryMetricTable;
import org.skywalking.apm.collector.storage.h2.SqlBuilder;
import org.skywalking.apm.collector.storage.h2.dao.H2DAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
/**
* @author clevertension
*/
public class MemoryMetricH2DAO extends H2DAO implements IMemoryMetricDAO {
private final Logger logger = LoggerFactory.getLogger(InstanceH2DAO.class);
private static final String GET_MEMORY_METRIC_SQL = "select * from {0} where {1} =?";
private static final String GET_MEMORY_METRICS_SQL = "select * from {0} where {1} in (";
@Override public JsonObject getMetric(int instanceId, long timeBucket, boolean isHeap) {
H2Client client = getClient();
String id = timeBucket + Const.ID_SPLIT + instanceId + Const.ID_SPLIT + isHeap;
String sql = SqlBuilder.buildSql(GET_MEMORY_METRIC_SQL, MemoryMetricTable.TABLE, "id");
Object[] params = new Object[]{id};
JsonObject metric = new JsonObject();
try (ResultSet rs = client.executeQuery(sql, params)) {
if (rs.next()) {
metric.addProperty("max", rs.getInt(MemoryMetricTable.COLUMN_MAX));
metric.addProperty("init", rs.getInt(MemoryMetricTable.COLUMN_INIT));
metric.addProperty("used", rs.getInt(MemoryMetricTable.COLUMN_USED));
} else {
metric.addProperty("max", 0);
metric.addProperty("init", 0);
metric.addProperty("used", 0);
}
} catch (SQLException | H2ClientException e) {
logger.error(e.getMessage(), e);
}
return metric;
}
@Override public JsonObject getMetric(int instanceId, long startTimeBucket, long endTimeBucket, boolean isHeap) {
H2Client client = getClient();
String sql = SqlBuilder.buildSql(GET_MEMORY_METRICS_SQL, MemoryMetricTable.TABLE, "id");
List<String> idList = new ArrayList<>();
long timeBucket = startTimeBucket;
do {
timeBucket = TimeBucketUtils.INSTANCE.addSecondForSecondTimeBucket(TimeBucketUtils.TimeBucketType.SECOND.name(), timeBucket, 1);
String id = timeBucket + Const.ID_SPLIT + instanceId + Const.ID_SPLIT + isHeap;
idList.add(id);
}
while (timeBucket <= endTimeBucket);
StringBuilder builder = new StringBuilder();
for (int i = 0; i < idList.size(); i++) {
builder.append("?,");
}
builder.delete(builder.length() - 1, builder.length());
builder.append(")");
sql = sql + builder;
Object[] params = idList.toArray(new String[0]);
JsonObject metric = new JsonObject();
JsonArray usedMetric = new JsonArray();
try (ResultSet rs = client.executeQuery(sql, params)) {
while (rs.next()) {
metric.addProperty("max", rs.getLong(MemoryMetricTable.COLUMN_MAX));
metric.addProperty("init", rs.getLong(MemoryMetricTable.COLUMN_INIT));
usedMetric.add(rs.getLong(MemoryMetricTable.COLUMN_USED));
}
if (usedMetric.size() == 0) {
metric.addProperty("max", 0);
metric.addProperty("init",0);
usedMetric.add(0);
}
} catch (SQLException | H2ClientException e) {
logger.error(e.getMessage(), e);
}
metric.add("used", usedMetric);
return metric;
}
}
/*
* Copyright 2017, OpenSkywalking Organization All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.collector.ui.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.skywalking.apm.collector.client.h2.H2Client;
import org.skywalking.apm.collector.client.h2.H2ClientException;
import org.skywalking.apm.collector.core.util.Const;
import org.skywalking.apm.collector.core.util.TimeBucketUtils;
import org.skywalking.apm.collector.storage.define.jvm.MemoryPoolMetricTable;
import org.skywalking.apm.collector.storage.h2.SqlBuilder;
import org.skywalking.apm.collector.storage.h2.dao.H2DAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
/**
* @author clevertension
*/
public class MemoryPoolMetricH2DAO extends H2DAO implements IMemoryPoolMetricDAO {
private final Logger logger = LoggerFactory.getLogger(InstanceH2DAO.class);
private static final String GET_MEMORY_POOL_METRIC_SQL = "select * from {0} where {1} =?";
private static final String GET_MEMORY_POOL_METRICS_SQL = "select * from {0} where {1} in (";
@Override public JsonObject getMetric(int instanceId, long timeBucket, int poolType) {
H2Client client = getClient();
String id = timeBucket + Const.ID_SPLIT + instanceId + Const.ID_SPLIT + poolType;
String sql = SqlBuilder.buildSql(GET_MEMORY_POOL_METRIC_SQL, MemoryPoolMetricTable.TABLE, "id");
Object[] params = new Object[]{id};
JsonObject metric = new JsonObject();
try (ResultSet rs = client.executeQuery(sql, params)) {
if (rs.next()) {
metric.addProperty("max", rs.getInt(MemoryPoolMetricTable.COLUMN_MAX));
metric.addProperty("init", rs.getInt(MemoryPoolMetricTable.COLUMN_INIT));
metric.addProperty("used", rs.getInt(MemoryPoolMetricTable.COLUMN_USED));
} else {
metric.addProperty("max", 0);
metric.addProperty("init", 0);
metric.addProperty("used", 0);
}
} catch (SQLException | H2ClientException e) {
logger.error(e.getMessage(), e);
}
return metric;
}
@Override public JsonObject getMetric(int instanceId, long startTimeBucket, long endTimeBucket, int poolType) {
H2Client client = getClient();
String sql = SqlBuilder.buildSql(GET_MEMORY_POOL_METRICS_SQL, MemoryPoolMetricTable.TABLE, "id");
List<String> idList = new ArrayList<>();
long timeBucket = startTimeBucket;
do {
timeBucket = TimeBucketUtils.INSTANCE.addSecondForSecondTimeBucket(TimeBucketUtils.TimeBucketType.SECOND.name(), timeBucket, 1);
String id = timeBucket + Const.ID_SPLIT + instanceId + Const.ID_SPLIT + poolType;
idList.add(id);
}
while (timeBucket <= endTimeBucket);
StringBuilder builder = new StringBuilder();
for (int i = 0; i < idList.size(); i++) {
builder.append("?,");
}
builder.delete(builder.length() - 1, builder.length());
builder.append(")");
sql = sql + builder;
Object[] params = idList.toArray(new String[0]);
JsonObject metric = new JsonObject();
JsonArray usedMetric = new JsonArray();
try (ResultSet rs = client.executeQuery(sql, params)) {
while (rs.next()) {
metric.addProperty("max", rs.getLong(MemoryPoolMetricTable.COLUMN_MAX));
metric.addProperty("init", rs.getLong(MemoryPoolMetricTable.COLUMN_INIT));
usedMetric.add(rs.getLong(MemoryPoolMetricTable.COLUMN_USED));
}
if (usedMetric.size() == 0) {
metric.addProperty("max", 0);
metric.addProperty("init",0);
usedMetric.add(0);
}
} catch (SQLException | H2ClientException e) {
logger.error(e.getMessage(), e);
}
metric.add("used", usedMetric);
return metric;
}
}
......@@ -18,15 +18,66 @@
package org.skywalking.apm.collector.ui.dao;
import com.google.gson.JsonArray;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.skywalking.apm.collector.client.h2.H2Client;
import org.skywalking.apm.collector.client.h2.H2ClientException;
import org.skywalking.apm.collector.core.util.StringUtils;
import org.skywalking.apm.collector.storage.define.node.NodeComponentTable;
import org.skywalking.apm.collector.storage.h2.SqlBuilder;
import org.skywalking.apm.collector.storage.h2.dao.H2DAO;
import org.skywalking.apm.collector.ui.cache.ApplicationCache;
import org.skywalking.apm.network.trace.component.ComponentsDefine;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
/**
* @author pengys5
* @author pengys5, clevertension
*/
public class NodeComponentH2DAO extends H2DAO implements INodeComponentDAO {
private final Logger logger = LoggerFactory.getLogger(NodeComponentH2DAO.class);
private static final String AGGREGATE_COMPONENT_SQL = "select {0}, {1}, {2} from {3} where {4} >= ? and {4} <= ? group by {0}, {1}, {2} limit 100";
@Override public JsonArray load(long startTime, long endTime) {
return null;
JsonArray nodeComponentArray = new JsonArray();
nodeComponentArray.addAll(aggregationComponent(startTime, endTime));
return nodeComponentArray;
}
private JsonArray aggregationComponent(long startTime, long endTime) {
H2Client client = getClient();
JsonArray nodeComponentArray = new JsonArray();
String sql = SqlBuilder.buildSql(AGGREGATE_COMPONENT_SQL, NodeComponentTable.COLUMN_COMPONENT_ID,
NodeComponentTable.COLUMN_PEER, NodeComponentTable.COLUMN_PEER_ID,
NodeComponentTable.TABLE, NodeComponentTable.COLUMN_TIME_BUCKET);
Object[] params = new Object[]{startTime, endTime};
try (ResultSet rs = client.executeQuery(sql, params)) {
while (rs.next()) {
int peerId = rs.getInt(NodeComponentTable.COLUMN_PEER_ID);
int componentId = rs.getInt(NodeComponentTable.COLUMN_COMPONENT_ID);
String componentName = ComponentsDefine.getInstance().getComponentName(componentId);
if (peerId != 0) {
String peer = ApplicationCache.getForUI(peerId);
nodeComponentArray.add(buildNodeComponent(peer, componentName));
}
String peer = rs.getString(NodeComponentTable.COLUMN_PEER);
if (StringUtils.isNotEmpty(peer)) {
nodeComponentArray.add(buildNodeComponent(peer, componentName));
}
}
} catch (SQLException | H2ClientException e) {
logger.error(e.getMessage(), e);
}
return nodeComponentArray;
}
private JsonObject buildNodeComponent(String peer, String componentName) {
JsonObject nodeComponentObj = new JsonObject();
nodeComponentObj.addProperty("componentName", componentName);
nodeComponentObj.addProperty("peer", peer);
return nodeComponentObj;
}
}
......@@ -18,15 +18,60 @@
package org.skywalking.apm.collector.ui.dao;
import com.google.gson.JsonArray;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.skywalking.apm.collector.client.h2.H2Client;
import org.skywalking.apm.collector.client.h2.H2ClientException;
import org.skywalking.apm.collector.core.util.StringUtils;
import org.skywalking.apm.collector.storage.define.node.NodeMappingTable;
import org.skywalking.apm.collector.storage.h2.SqlBuilder;
import org.skywalking.apm.collector.storage.h2.dao.H2DAO;
import org.skywalking.apm.collector.ui.cache.ApplicationCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
/**
* @author pengys5
* @author pengys5, clevertension
*/
public class NodeMappingH2DAO extends H2DAO implements INodeMappingDAO {
private final Logger logger = LoggerFactory.getLogger(NodeMappingH2DAO.class);
private static final String NODE_MAPPING_SQL = "select {0}, {1}, {2} from {3} where {4} >= ? and {4} <= ? group by {0}, {1}, {2} limit 100";
@Override public JsonArray load(long startTime, long endTime) {
return null;
H2Client client = getClient();
JsonArray nodeMappingArray = new JsonArray();
String sql = SqlBuilder.buildSql(NODE_MAPPING_SQL, NodeMappingTable.COLUMN_APPLICATION_ID,
NodeMappingTable.COLUMN_ADDRESS_ID, NodeMappingTable.COLUMN_ADDRESS,
NodeMappingTable.TABLE, NodeMappingTable.COLUMN_TIME_BUCKET);
Object[] params = new Object[]{startTime, endTime};
try (ResultSet rs = client.executeQuery(sql, params)) {
while (rs.next()) {
int applicationId = rs.getInt(NodeMappingTable.COLUMN_APPLICATION_ID);
String applicationCode = ApplicationCache.getForUI(applicationId);
int addressId = rs.getInt(NodeMappingTable.COLUMN_ADDRESS_ID);
if (addressId != 0) {
String address = ApplicationCache.getForUI(addressId);
JsonObject nodeMappingObj = new JsonObject();
nodeMappingObj.addProperty("applicationCode", applicationCode);
nodeMappingObj.addProperty("address", address);
nodeMappingArray.add(nodeMappingObj);
}
String address = rs.getString(NodeMappingTable.COLUMN_ADDRESS);
if (StringUtils.isNotEmpty(address)) {
JsonObject nodeMappingObj = new JsonObject();
nodeMappingObj.addProperty("applicationCode", applicationCode);
nodeMappingObj.addProperty("address", address);
nodeMappingArray.add(nodeMappingObj);
}
}
} catch (SQLException | H2ClientException e) {
logger.error(e.getMessage(), e);
}
logger.debug("node mapping data: {}", nodeMappingArray.toString());
return nodeMappingArray;
}
}
......@@ -18,14 +18,75 @@
package org.skywalking.apm.collector.ui.dao;
import com.google.gson.JsonArray;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.skywalking.apm.collector.client.h2.H2Client;
import org.skywalking.apm.collector.client.h2.H2ClientException;
import org.skywalking.apm.collector.core.util.StringUtils;
import org.skywalking.apm.collector.storage.define.noderef.NodeReferenceTable;
import org.skywalking.apm.collector.storage.h2.SqlBuilder;
import org.skywalking.apm.collector.storage.h2.dao.H2DAO;
import org.skywalking.apm.collector.ui.cache.ApplicationCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
/**
* @author pengys5
* @author pengys5, clevertension
*/
public class NodeReferenceH2DAO extends H2DAO implements INodeReferenceDAO {
private final Logger logger = LoggerFactory.getLogger(NodeReferenceH2DAO.class);
private static final String NODE_REFERENCE_SQL = "select {8}, {9}, {10}, sum({0}) as {0}, sum({1}) as {1}, sum({2}) as {2}, " +
"sum({3}) as {3}, sum({4}) as {4}, sum({5}) as {5} from {6} where {7} >= ? and {7} <= ? group by {8}, {9}, {10} limit 100";
@Override public JsonArray load(long startTime, long endTime) {
return null;
H2Client client = getClient();
JsonArray nodeRefResSumArray = new JsonArray();
String sql = SqlBuilder.buildSql(NODE_REFERENCE_SQL, NodeReferenceTable.COLUMN_S1_LTE,
NodeReferenceTable.COLUMN_S3_LTE, NodeReferenceTable.COLUMN_S5_LTE,
NodeReferenceTable.COLUMN_S5_GT, NodeReferenceTable.COLUMN_SUMMARY,
NodeReferenceTable.COLUMN_ERROR, NodeReferenceTable.TABLE, NodeReferenceTable.COLUMN_TIME_BUCKET,
NodeReferenceTable.COLUMN_FRONT_APPLICATION_ID, NodeReferenceTable.COLUMN_BEHIND_APPLICATION_ID, NodeReferenceTable.COLUMN_BEHIND_PEER);
Object[] params = new Object[]{startTime, endTime};
try (ResultSet rs = client.executeQuery(sql, params)) {
while (rs.next()) {
int applicationId = rs.getInt(NodeReferenceTable.COLUMN_FRONT_APPLICATION_ID);
String applicationCode = ApplicationCache.getForUI(applicationId);
int behindApplicationId = rs.getInt(NodeReferenceTable.COLUMN_BEHIND_APPLICATION_ID);
if (behindApplicationId != 0) {
String behindApplicationCode = ApplicationCache.getForUI(behindApplicationId);
JsonObject nodeRefResSumObj = new JsonObject();
nodeRefResSumObj.addProperty("front", applicationCode);
nodeRefResSumObj.addProperty("behind", behindApplicationCode);
nodeRefResSumObj.addProperty(NodeReferenceTable.COLUMN_S1_LTE, rs.getDouble(NodeReferenceTable.COLUMN_S1_LTE));
nodeRefResSumObj.addProperty(NodeReferenceTable.COLUMN_S3_LTE, rs.getDouble(NodeReferenceTable.COLUMN_S3_LTE));
nodeRefResSumObj.addProperty(NodeReferenceTable.COLUMN_S5_LTE, rs.getDouble(NodeReferenceTable.COLUMN_S5_LTE));
nodeRefResSumObj.addProperty(NodeReferenceTable.COLUMN_S5_GT, rs.getDouble(NodeReferenceTable.COLUMN_S5_GT));
nodeRefResSumObj.addProperty(NodeReferenceTable.COLUMN_ERROR, rs.getDouble(NodeReferenceTable.COLUMN_ERROR));
nodeRefResSumObj.addProperty(NodeReferenceTable.COLUMN_SUMMARY, rs.getDouble(NodeReferenceTable.COLUMN_SUMMARY));
nodeRefResSumArray.add(nodeRefResSumObj);
}
String behindPeer = rs.getString(NodeReferenceTable.COLUMN_BEHIND_PEER);
if (StringUtils.isNotEmpty(behindPeer)) {
JsonObject nodeRefResSumObj = new JsonObject();
nodeRefResSumObj.addProperty("front", applicationCode);
nodeRefResSumObj.addProperty("behind", behindPeer);
nodeRefResSumObj.addProperty(NodeReferenceTable.COLUMN_S1_LTE, rs.getDouble(NodeReferenceTable.COLUMN_S1_LTE));
nodeRefResSumObj.addProperty(NodeReferenceTable.COLUMN_S3_LTE, rs.getDouble(NodeReferenceTable.COLUMN_S3_LTE));
nodeRefResSumObj.addProperty(NodeReferenceTable.COLUMN_S5_LTE, rs.getDouble(NodeReferenceTable.COLUMN_S5_LTE));
nodeRefResSumObj.addProperty(NodeReferenceTable.COLUMN_S5_GT, rs.getDouble(NodeReferenceTable.COLUMN_S5_GT));
nodeRefResSumObj.addProperty(NodeReferenceTable.COLUMN_ERROR, rs.getDouble(NodeReferenceTable.COLUMN_ERROR));
nodeRefResSumObj.addProperty(NodeReferenceTable.COLUMN_SUMMARY, rs.getDouble(NodeReferenceTable.COLUMN_SUMMARY));
nodeRefResSumArray.add(nodeRefResSumObj);
}
}
} catch (SQLException | H2ClientException e) {
logger.error(e.getMessage(), e);
}
return nodeRefResSumArray;
}
}
......@@ -18,16 +18,142 @@
package org.skywalking.apm.collector.ui.dao;
import com.google.gson.JsonObject;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.elasticsearch.search.sort.SortOrder;
import org.skywalking.apm.collector.client.h2.H2Client;
import org.skywalking.apm.collector.client.h2.H2ClientException;
import org.skywalking.apm.collector.core.util.CollectionUtils;
import org.skywalking.apm.collector.core.util.StringUtils;
import org.skywalking.apm.collector.storage.dao.DAOContainer;
import org.skywalking.apm.collector.storage.define.global.GlobalTraceTable;
import org.skywalking.apm.collector.storage.define.segment.SegmentCostTable;
import org.skywalking.apm.collector.storage.h2.SqlBuilder;
import org.skywalking.apm.collector.storage.h2.dao.H2DAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
/**
* @author pengys5
* @author pengys5, clevertension
*/
public class SegmentCostH2DAO extends H2DAO implements ISegmentCostDAO {
private final Logger logger = LoggerFactory.getLogger(SegmentCostH2DAO.class);
private static final String GET_SEGMENT_COST_SQL = "select * from {0} where {1} >= ? and {1} <= ?";
@Override public JsonObject loadTop(long startTime, long endTime, long minCost, long maxCost, String operationName,
Error error, int applicationId, List<String> segmentIds, int limit, int from, Sort sort) {
return null;
H2Client client = getClient();
String sql = GET_SEGMENT_COST_SQL;
List<Object> params = new ArrayList<>();
List<Object> columns = new ArrayList<>();
columns.add(SegmentCostTable.TABLE);
columns.add(SegmentCostTable.COLUMN_TIME_BUCKET);
params.add(startTime);
params.add(endTime);
int paramIndex = 1;
if (minCost != -1 || maxCost != -1) {
if (minCost != -1) {
paramIndex++;
sql = sql + " and {" + paramIndex + "} >= ?";
params.add(minCost);
columns.add(SegmentCostTable.COLUMN_COST);
}
if (maxCost != -1) {
paramIndex++;
sql = sql + " and {" + paramIndex + "} <= ?";
params.add(maxCost);
columns.add(SegmentCostTable.COLUMN_COST);
}
}
if (StringUtils.isNotEmpty(operationName)) {
paramIndex++;
sql = sql + " and {" + paramIndex + "} = ?";
params.add(operationName);
columns.add(SegmentCostTable.COLUMN_SERVICE_NAME);
}
if (CollectionUtils.isNotEmpty(segmentIds)) {
paramIndex++;
sql = sql + " and {" + paramIndex + "} in (";
columns.add(SegmentCostTable.COLUMN_SEGMENT_ID);
StringBuilder builder = new StringBuilder();
for (int i = 0; i < segmentIds.size(); i++) {
builder.append("?,");
}
builder.delete(builder.length() - 1, builder.length());
builder.append(")");
sql = sql + builder;
for (String segmentId : segmentIds) {
params.add(segmentId);
}
}
if (Error.True.equals(error)) {
paramIndex++;
sql = sql + " and {" + paramIndex + "} = ?";
params.add(true);
columns.add(SegmentCostTable.COLUMN_IS_ERROR);
} else if (Error.False.equals(error)) {
paramIndex++;
sql = sql + " and {" + paramIndex + "} = ?";
params.add(false);
columns.add(SegmentCostTable.COLUMN_IS_ERROR);
}
if (applicationId != 0) {
paramIndex++;
sql = sql + " and {" + paramIndex + "} = ?";
params.add(applicationId);
columns.add(SegmentCostTable.COLUMN_APPLICATION_ID);
}
if (Sort.Cost.equals(sort)) {
sql = sql + " order by " + SegmentCostTable.COLUMN_COST + " " + SortOrder.DESC;
} else if (Sort.Time.equals(sort)) {
sql = sql + " order by " + SegmentCostTable.COLUMN_START_TIME + " " + SortOrder.DESC;
}
sql = sql + " limit " + from + "," + limit;
sql = SqlBuilder.buildSql(sql, columns);
Object[] p = params.toArray(new Object[0]);
JsonObject topSegPaging = new JsonObject();
JsonArray topSegArray = new JsonArray();
topSegPaging.add("data", topSegArray);
int cnt = 0;
int num = from;
try (ResultSet rs = client.executeQuery(sql, p)) {
while (rs.next()) {
JsonObject topSegmentJson = new JsonObject();
topSegmentJson.addProperty("num", num);
String segmentId = rs.getString(SegmentCostTable.COLUMN_SEGMENT_ID);
topSegmentJson.addProperty(SegmentCostTable.COLUMN_SEGMENT_ID, segmentId);
topSegmentJson.addProperty(SegmentCostTable.COLUMN_START_TIME, rs.getLong(SegmentCostTable.COLUMN_START_TIME));
topSegmentJson.addProperty(SegmentCostTable.COLUMN_END_TIME, rs.getLong(SegmentCostTable.COLUMN_END_TIME));
IGlobalTraceDAO globalTraceDAO = (IGlobalTraceDAO) DAOContainer.INSTANCE.get(IGlobalTraceDAO.class.getName());
List<String> globalTraces = globalTraceDAO.getGlobalTraceId(segmentId);
if (CollectionUtils.isNotEmpty(globalTraces)) {
topSegmentJson.addProperty(GlobalTraceTable.COLUMN_GLOBAL_TRACE_ID, globalTraces.get(0));
}
topSegmentJson.addProperty(SegmentCostTable.COLUMN_APPLICATION_ID, rs.getInt(SegmentCostTable.COLUMN_APPLICATION_ID));
topSegmentJson.addProperty(SegmentCostTable.COLUMN_SERVICE_NAME, rs.getString(SegmentCostTable.COLUMN_SERVICE_NAME));
topSegmentJson.addProperty(SegmentCostTable.COLUMN_COST, rs.getLong(SegmentCostTable.COLUMN_COST));
topSegmentJson.addProperty(SegmentCostTable.COLUMN_IS_ERROR, rs.getBoolean(SegmentCostTable.COLUMN_IS_ERROR));
num++;
topSegArray.add(topSegmentJson);
cnt++;
}
} catch (SQLException | H2ClientException e) {
logger.error(e.getMessage(), e);
}
topSegPaging.addProperty("recordsTotal", cnt);
return topSegPaging;
}
}
......@@ -18,14 +18,48 @@
package org.skywalking.apm.collector.ui.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Base64;
import org.skywalking.apm.collector.client.h2.H2Client;
import org.skywalking.apm.collector.client.h2.H2ClientException;
import org.skywalking.apm.collector.core.util.StringUtils;
import org.skywalking.apm.collector.storage.define.segment.SegmentTable;
import org.skywalking.apm.collector.storage.h2.SqlBuilder;
import org.skywalking.apm.collector.storage.h2.dao.H2DAO;
import org.skywalking.apm.network.proto.TraceSegmentObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.protobuf.InvalidProtocolBufferException;
/**
* @author pengys5
* @author pengys5, clevertension
*/
public class SegmentH2DAO extends H2DAO implements ISegmentDAO {
private final Logger logger = LoggerFactory.getLogger(SegmentH2DAO.class);
private static final String GET_SEGMENT_SQL = "select {0} from {1} where {2} = ?";
@Override public TraceSegmentObject load(String segmentId) {
H2Client client = getClient();
String sql = SqlBuilder.buildSql(GET_SEGMENT_SQL, SegmentTable.COLUMN_DATA_BINARY,
SegmentTable.TABLE, "id");
Object[] params = new Object[]{segmentId};
try (ResultSet rs = client.executeQuery(sql, params)) {
if (rs.next()) {
String dataBinaryBase64 = rs.getString(SegmentTable.COLUMN_DATA_BINARY);
if (StringUtils.isNotEmpty(dataBinaryBase64)) {
byte[] dataBinary = Base64.getDecoder().decode(dataBinaryBase64);
try {
return TraceSegmentObject.parseFrom(dataBinary);
} catch (InvalidProtocolBufferException e) {
logger.error(e.getMessage(), e);
}
}
}
} catch (SQLException | H2ClientException e) {
logger.error(e.getMessage(), e);
}
return null;
}
}
......@@ -18,15 +18,82 @@
package org.skywalking.apm.collector.ui.dao;
import com.google.gson.JsonObject;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.skywalking.apm.collector.client.h2.H2Client;
import org.skywalking.apm.collector.client.h2.H2ClientException;
import org.skywalking.apm.collector.core.util.ColumnNameUtils;
import org.skywalking.apm.collector.core.util.StringUtils;
import org.skywalking.apm.collector.storage.define.service.ServiceEntryTable;
import org.skywalking.apm.collector.storage.h2.SqlBuilder;
import org.skywalking.apm.collector.storage.h2.dao.H2DAO;
import org.skywalking.apm.collector.ui.cache.ApplicationCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
/**
* @author pengys5
*/
public class ServiceEntryH2DAO extends H2DAO implements IServiceEntryDAO {
private final Logger logger = LoggerFactory.getLogger(SegmentH2DAO.class);
private static final String GET_SERVICE_ENTRY_SQL = "select * from {0} where {1} >= ? and {2} <= ?";
@Override public JsonObject load(int applicationId, String entryServiceName, long startTime, long endTime, int from,
int size) {
return null;
H2Client client = getClient();
String sql = GET_SERVICE_ENTRY_SQL;
List<Object> params = new ArrayList<>();
List<Object> columns = new ArrayList<>();
columns.add(ServiceEntryTable.TABLE);
columns.add(ServiceEntryTable.COLUMN_NEWEST_TIME);
columns.add(ServiceEntryTable.COLUMN_REGISTER_TIME);
params.add(startTime);
params.add(endTime);
int paramIndex = 2;
if (applicationId != 0) {
paramIndex++;
sql = sql + " and {" + paramIndex + "} = ?";
params.add(applicationId);
columns.add(ServiceEntryTable.COLUMN_APPLICATION_ID);
}
if (StringUtils.isNotEmpty(entryServiceName)) {
paramIndex++;
sql = sql + " and {" + paramIndex + "} = ?";
params.add(entryServiceName);
columns.add(ServiceEntryTable.COLUMN_ENTRY_SERVICE_NAME);
}
sql = sql + " limit " + from + "," + size;
sql = SqlBuilder.buildSql(sql, columns);
Object[] p = params.toArray(new Object[0]);
JsonArray serviceArray = new JsonArray();
JsonObject response = new JsonObject();
int index = 0;
try (ResultSet rs = client.executeQuery(sql, p)) {
while (rs.next()) {
int appId = rs.getInt(ServiceEntryTable.COLUMN_APPLICATION_ID);
int entryServiceId = rs.getInt(ServiceEntryTable.COLUMN_ENTRY_SERVICE_ID);
String applicationCode = ApplicationCache.getForUI(applicationId);
String entryServiceName1 = rs.getString(ServiceEntryTable.COLUMN_ENTRY_SERVICE_NAME);
JsonObject row = new JsonObject();
row.addProperty(ColumnNameUtils.INSTANCE.rename(ServiceEntryTable.COLUMN_ENTRY_SERVICE_ID), entryServiceId);
row.addProperty(ColumnNameUtils.INSTANCE.rename(ServiceEntryTable.COLUMN_ENTRY_SERVICE_NAME), entryServiceName1);
row.addProperty(ColumnNameUtils.INSTANCE.rename(ServiceEntryTable.COLUMN_APPLICATION_ID), appId);
row.addProperty("applicationCode", applicationCode);
serviceArray.add(row);
index++;
}
} catch (SQLException | H2ClientException e) {
logger.error(e.getMessage(), e);
}
response.addProperty("total", index);
response.add("array", serviceArray);
return response;
}
}
......@@ -18,19 +18,263 @@
package org.skywalking.apm.collector.ui.dao;
import com.google.gson.JsonArray;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import org.skywalking.apm.collector.client.h2.H2Client;
import org.skywalking.apm.collector.client.h2.H2ClientException;
import org.skywalking.apm.collector.core.util.ColumnNameUtils;
import org.skywalking.apm.collector.core.util.Const;
import org.skywalking.apm.collector.core.util.ObjectUtils;
import org.skywalking.apm.collector.core.util.StringUtils;
import org.skywalking.apm.collector.storage.define.serviceref.ServiceReferenceTable;
import org.skywalking.apm.collector.storage.h2.SqlBuilder;
import org.skywalking.apm.collector.storage.h2.dao.H2DAO;
import org.skywalking.apm.collector.ui.cache.ServiceIdCache;
import org.skywalking.apm.collector.ui.cache.ServiceNameCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
/**
* @author pengys5
* @author pengys5, clevertension
*/
public class ServiceReferenceH2DAO extends H2DAO implements IServiceReferenceDAO {
private final Logger logger = LoggerFactory.getLogger(ServiceReferenceH2DAO.class);
private static final String GET_SRV_REF_LOAD1 = "select {4}, {5}, {6}, {7}, sum({8}) as cnt1, sum({9}) as cnt2, sum({10}) as cnt3" +
",sum({11}) as cnt4, sum({12}) cnt5, sum({13}) as cnt6, sum({14}) as cnt7 from {0} where {1} >= ? and {1} <= ? and {2} = ? and {3} = ? group by {4}, {5}, {6}, {7}";
private static final String GET_SRV_REF_LOAD2 = "select {3}, {4}, {5}, {6}, sum({7}) as cnt1, sum({8}) as cnt2, sum({9}) as cnt3" +
",sum({10}) as cnt4, sum({11}) cnt5, sum({12}) as cnt6, sum({13}) as cnt7 from {0} where {1} >= ? and {1} <= ? and {2} = ? group by {3}, {4}, {5}, {6}";
@Override public JsonArray load(int entryServiceId, long startTime, long endTime) {
H2Client client = getClient();
String sql = SqlBuilder.buildSql(GET_SRV_REF_LOAD1, ServiceReferenceTable.TABLE,
ServiceReferenceTable.COLUMN_TIME_BUCKET, ServiceReferenceTable.COLUMN_ENTRY_SERVICE_ID, ServiceReferenceTable.COLUMN_ENTRY_SERVICE_NAME,
ServiceReferenceTable.COLUMN_FRONT_SERVICE_ID, ServiceReferenceTable.COLUMN_BEHIND_SERVICE_ID,
ServiceReferenceTable.COLUMN_FRONT_SERVICE_NAME, ServiceReferenceTable.COLUMN_BEHIND_SERVICE_NAME,
ServiceReferenceTable.COLUMN_S1_LTE, ServiceReferenceTable.COLUMN_S3_LTE, ServiceReferenceTable.COLUMN_S5_LTE,
ServiceReferenceTable.COLUMN_S5_GT, ServiceReferenceTable.COLUMN_ERROR, ServiceReferenceTable.COLUMN_SUMMARY,
ServiceReferenceTable.COLUMN_COST_SUMMARY);
String entryServiceName = ServiceNameCache.get(entryServiceId);
Object[] params = new Object[]{startTime, endTime, entryServiceId, entryServiceName};
try (ResultSet rs = client.executeQuery(sql, params)) {
if (rs.next()) {
Map<String, JsonObject> serviceReferenceMap = new LinkedHashMap<>();
int frontServiceId = rs.getInt(ServiceReferenceTable.COLUMN_FRONT_SERVICE_ID);
if (frontServiceId != 0) {
parseSubAggregate(serviceReferenceMap, rs, frontServiceId);
}
String frontServiceName = rs.getString(ServiceReferenceTable.COLUMN_FRONT_SERVICE_NAME);
if (StringUtils.isNotEmpty(frontServiceName)) {
String[] serviceNames = frontServiceName.split(Const.ID_SPLIT);
int frontServiceId1 = ServiceIdCache.getForUI(Integer.parseInt(serviceNames[0]), serviceNames[1]);
parseSubAggregate(serviceReferenceMap, rs, frontServiceId1);
}
JsonArray serviceReferenceArray = new JsonArray();
JsonObject rootServiceReference = findRoot(serviceReferenceMap);
if (ObjectUtils.isNotEmpty(rootServiceReference)) {
serviceReferenceArray.add(rootServiceReference);
String id = rootServiceReference.get(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_FRONT_SERVICE_ID)) + Const.ID_SPLIT + rootServiceReference.get(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_BEHIND_SERVICE_ID));
serviceReferenceMap.remove(id);
int rootServiceId = rootServiceReference.get(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_BEHIND_SERVICE_ID)).getAsInt();
sortAsTree(rootServiceId, serviceReferenceArray, serviceReferenceMap);
}
return serviceReferenceArray;
}
} catch (SQLException | H2ClientException e) {
logger.error(e.getMessage(), e);
}
return null;
}
@Override public JsonArray load(String entryServiceName, int entryApplicationId, long startTime, long endTime) {
H2Client client = getClient();
String sql = SqlBuilder.buildSql(GET_SRV_REF_LOAD2, ServiceReferenceTable.TABLE,
ServiceReferenceTable.COLUMN_TIME_BUCKET, ServiceReferenceTable.COLUMN_ENTRY_SERVICE_NAME,
ServiceReferenceTable.COLUMN_FRONT_SERVICE_ID, ServiceReferenceTable.COLUMN_BEHIND_SERVICE_ID,
ServiceReferenceTable.COLUMN_FRONT_SERVICE_NAME, ServiceReferenceTable.COLUMN_BEHIND_SERVICE_NAME,
ServiceReferenceTable.COLUMN_S1_LTE, ServiceReferenceTable.COLUMN_S3_LTE, ServiceReferenceTable.COLUMN_S5_LTE,
ServiceReferenceTable.COLUMN_S5_GT, ServiceReferenceTable.COLUMN_ERROR, ServiceReferenceTable.COLUMN_SUMMARY,
ServiceReferenceTable.COLUMN_COST_SUMMARY);
entryServiceName = entryApplicationId + Const.ID_SPLIT + entryServiceName;
Object[] params = new Object[]{startTime, endTime, entryServiceName};
int entryServiceId = ServiceIdCache.get(entryApplicationId, entryServiceName);
if (entryServiceId != 0) {
sql = SqlBuilder.buildSql(GET_SRV_REF_LOAD1, ServiceReferenceTable.TABLE,
ServiceReferenceTable.COLUMN_TIME_BUCKET, ServiceReferenceTable.COLUMN_ENTRY_SERVICE_ID, ServiceReferenceTable.COLUMN_ENTRY_SERVICE_NAME,
ServiceReferenceTable.COLUMN_FRONT_SERVICE_ID, ServiceReferenceTable.COLUMN_BEHIND_SERVICE_ID,
ServiceReferenceTable.COLUMN_FRONT_SERVICE_NAME, ServiceReferenceTable.COLUMN_BEHIND_SERVICE_NAME,
ServiceReferenceTable.COLUMN_S1_LTE, ServiceReferenceTable.COLUMN_S3_LTE, ServiceReferenceTable.COLUMN_S5_LTE,
ServiceReferenceTable.COLUMN_S5_GT, ServiceReferenceTable.COLUMN_ERROR, ServiceReferenceTable.COLUMN_SUMMARY,
ServiceReferenceTable.COLUMN_COST_SUMMARY);
params = new Object[]{startTime, endTime, entryServiceId, entryServiceName};
}
try (ResultSet rs = client.executeQuery(sql, params)) {
if (rs.next()) {
Map<String, JsonObject> serviceReferenceMap = new LinkedHashMap<>();
int frontServiceId = rs.getInt(ServiceReferenceTable.COLUMN_FRONT_SERVICE_ID);
if (frontServiceId != 0) {
parseSubAggregate(serviceReferenceMap, rs, frontServiceId);
}
String frontServiceName = rs.getString(ServiceReferenceTable.COLUMN_FRONT_SERVICE_NAME);
if (StringUtils.isNotEmpty(frontServiceName)) {
String[] serviceNames = frontServiceName.split(Const.ID_SPLIT);
int frontServiceId1 = ServiceIdCache.getForUI(Integer.parseInt(serviceNames[0]), serviceNames[1]);
parseSubAggregate(serviceReferenceMap, rs, frontServiceId1);
}
JsonArray serviceReferenceArray = new JsonArray();
JsonObject rootServiceReference = findRoot(serviceReferenceMap);
if (ObjectUtils.isNotEmpty(rootServiceReference)) {
serviceReferenceArray.add(rootServiceReference);
String id = rootServiceReference.get(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_FRONT_SERVICE_ID)) + Const.ID_SPLIT + rootServiceReference.get(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_BEHIND_SERVICE_ID));
serviceReferenceMap.remove(id);
int rootServiceId = rootServiceReference.get(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_BEHIND_SERVICE_ID)).getAsInt();
sortAsTree(rootServiceId, serviceReferenceArray, serviceReferenceMap);
}
return serviceReferenceArray;
}
} catch (SQLException | H2ClientException e) {
logger.error(e.getMessage(), e);
}
return null;
}
private void parseSubAggregate(Map<String, JsonObject> serviceReferenceMap,
ResultSet rs,
int frontServiceId) {
try {
int behindServiceId = rs.getInt(ServiceReferenceTable.COLUMN_BEHIND_SERVICE_ID);
if (behindServiceId != 0) {
long s1LteSum = rs.getLong("cnt1");
long s3LteSum = rs.getLong("cnt2");
long s5LteSum = rs.getLong("cnt3");
long s5GtSum = rs.getLong("cnt3");
long error = rs.getLong("cnt3");
long summary = rs.getLong("cnt3");
long costSum = rs.getLong("cnt3");
String frontServiceName = ServiceNameCache.getForUI(frontServiceId);
if (StringUtils.isNotEmpty(frontServiceName)) {
frontServiceName = frontServiceName.split(Const.ID_SPLIT)[1];
}
String behindServiceName = ServiceNameCache.getForUI(behindServiceId);
if (StringUtils.isNotEmpty(frontServiceName)) {
behindServiceName = behindServiceName.split(Const.ID_SPLIT)[1];
}
JsonObject serviceReference = new JsonObject();
serviceReference.addProperty(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_FRONT_SERVICE_ID), frontServiceId);
serviceReference.addProperty(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_FRONT_SERVICE_NAME), frontServiceName);
serviceReference.addProperty(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_BEHIND_SERVICE_ID), behindServiceId);
serviceReference.addProperty(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_BEHIND_SERVICE_NAME), behindServiceName);
serviceReference.addProperty(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_S1_LTE), s1LteSum);
serviceReference.addProperty(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_S3_LTE), s3LteSum);
serviceReference.addProperty(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_S5_LTE), s5LteSum);
serviceReference.addProperty(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_S5_GT), s5GtSum);
serviceReference.addProperty(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_ERROR), error);
serviceReference.addProperty(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_SUMMARY), summary);
serviceReference.addProperty(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_COST_SUMMARY), costSum);
merge(serviceReferenceMap, serviceReference);
}
} catch (SQLException e) {
logger.error(e.getMessage(), e);
}
try {
String behindServiceName = rs.getString(ServiceReferenceTable.COLUMN_BEHIND_SERVICE_NAME);
if (StringUtils.isNotEmpty(behindServiceName)) {
long s1LteSum = rs.getLong("cnt1");
long s3LteSum = rs.getLong("cnt2");
long s5LteSum = rs.getLong("cnt3");
long s5GtSum = rs.getLong("cnt3");
long error = rs.getLong("cnt3");
long summary = rs.getLong("cnt3");
long costSum = rs.getLong("cnt3");
String frontServiceName = ServiceNameCache.getForUI(frontServiceId);
String[] serviceNames = behindServiceName.split(Const.ID_SPLIT);
int behindServiceId = ServiceIdCache.getForUI(Integer.parseInt(serviceNames[0]), serviceNames[1]);
behindServiceName = serviceNames[1];
JsonObject serviceReference = new JsonObject();
serviceReference.addProperty(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_FRONT_SERVICE_ID), frontServiceId);
serviceReference.addProperty(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_FRONT_SERVICE_NAME), frontServiceName);
serviceReference.addProperty(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_BEHIND_SERVICE_ID), behindServiceId);
serviceReference.addProperty(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_BEHIND_SERVICE_NAME), behindServiceName);
serviceReference.addProperty(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_S1_LTE), s1LteSum);
serviceReference.addProperty(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_S3_LTE), s3LteSum);
serviceReference.addProperty(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_S5_LTE), s5LteSum);
serviceReference.addProperty(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_S5_GT), s5GtSum);
serviceReference.addProperty(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_ERROR), error);
serviceReference.addProperty(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_SUMMARY), summary);
serviceReference.addProperty(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_COST_SUMMARY), costSum);
merge(serviceReferenceMap, serviceReference);
}
} catch (SQLException e) {
logger.error(e.getMessage(), e);
}
}
private void merge(Map<String, JsonObject> serviceReferenceMap, JsonObject serviceReference) {
String id = serviceReference.get(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_FRONT_SERVICE_ID)) + Const.ID_SPLIT + serviceReference.get(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_BEHIND_SERVICE_ID));
if (serviceReferenceMap.containsKey(id)) {
JsonObject reference = serviceReferenceMap.get(id);
add(reference, serviceReference, ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_S1_LTE));
add(reference, serviceReference, ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_S3_LTE));
add(reference, serviceReference, ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_S5_LTE));
add(reference, serviceReference, ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_S5_GT));
add(reference, serviceReference, ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_ERROR));
add(reference, serviceReference, ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_SUMMARY));
add(reference, serviceReference, ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_COST_SUMMARY));
} else {
serviceReferenceMap.put(id, serviceReference);
}
}
private void add(JsonObject oldReference, JsonObject newReference, String key) {
long oldValue = oldReference.get(key).getAsLong();
long newValue = newReference.get(key).getAsLong();
oldReference.addProperty(key, oldValue + newValue);
}
private JsonObject findRoot(Map<String, JsonObject> serviceReferenceMap) {
for (JsonObject serviceReference : serviceReferenceMap.values()) {
int frontServiceId = serviceReference.get(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_FRONT_SERVICE_ID)).getAsInt();
if (frontServiceId == 1) {
return serviceReference;
}
}
return null;
}
private void sortAsTree(int serviceId, JsonArray serviceReferenceArray,
Map<String, JsonObject> serviceReferenceMap) {
Iterator<JsonObject> iterator = serviceReferenceMap.values().iterator();
while (iterator.hasNext()) {
JsonObject serviceReference = iterator.next();
int frontServiceId = serviceReference.get(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_FRONT_SERVICE_ID)).getAsInt();
if (serviceId == frontServiceId) {
serviceReferenceArray.add(serviceReference);
int behindServiceId = serviceReference.get(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_BEHIND_SERVICE_ID)).getAsInt();
sortAsTree(behindServiceId, serviceReferenceArray, serviceReferenceMap);
}
}
}
}
......@@ -59,10 +59,14 @@ public class InstanceHealthService {
IGCMetricDAO gcMetricDAO = (IGCMetricDAO)DAOContainer.INSTANCE.get(IGCMetricDAO.class.getName());
JsonObject instanceJson = new JsonObject();
instanceJson.addProperty("id", instance.getInstanceId());
instanceJson.addProperty("tps", performance.getCalls());
if (performance != null) {
instanceJson.addProperty("tps", performance.getCalls());
} else {
instanceJson.addProperty("tps", 0);
}
int avg = 0;
if (performance.getCalls() != 0) {
if (performance != null && performance.getCalls() != 0) {
avg = (int)(performance.getCostTotal() / performance.getCalls());
}
instanceJson.addProperty("avg", avg);
......
......@@ -6,6 +6,9 @@ org.skywalking.apm.collector.ui.dao.GlobalTraceH2DAO
org.skywalking.apm.collector.ui.dao.SegmentH2DAO
org.skywalking.apm.collector.ui.dao.InstanceH2DAO
org.skywalking.apm.collector.ui.dao.InstPerformanceH2DAO
org.skywalking.apm.collector.ui.dao.CpuMetricH2DAO
org.skywalking.apm.collector.ui.dao.GCMetricH2DAO
org.skywalking.apm.collector.ui.dao.MemoryMetricH2DAO
org.skywalking.apm.collector.ui.dao.MemoryPoolMetricH2DAO
org.skywalking.apm.collector.ui.dao.ServiceEntryH2DAO
org.skywalking.apm.collector.ui.dao.ServiceReferenceH2DAO
\ No newline at end of file
......@@ -63,10 +63,14 @@ public class ComponentsDefine {
public static final OfficialComponent JETTY_SERVER = new OfficialComponent(19, "JettyServer");
public static final OfficialComponent MEMCACHE = new OfficialComponent(20, "Memcache");
public static final OfficialComponent MEMCACHED = new OfficialComponent(20, "Memcached");
public static final OfficialComponent SHARDING_JDBC = new OfficialComponent(21, "ShardingJDBC");
public static final OfficialComponent POSTGRESQL = new OfficialComponent(22, "PostgreSQL");
public static final OfficialComponent GRPC = new OfficialComponent(23, "GRPC");
private static ComponentsDefine instance = new ComponentsDefine();
private String[] components;
......@@ -76,7 +80,7 @@ public class ComponentsDefine {
}
public ComponentsDefine() {
components = new String[22];
components = new String[24];
addComponent(TOMCAT);
addComponent(HTTPCLIENT);
addComponent(DUBBO);
......@@ -96,8 +100,10 @@ public class ComponentsDefine {
addComponent(NUTZ_HTTP);
addComponent(JETTY_CLIENT);
addComponent(JETTY_SERVER);
addComponent(MEMCACHE);
addComponent(MEMCACHED);
addComponent(SHARDING_JDBC);
addComponent(POSTGRESQL);
addComponent(GRPC);
}
private void addComponent(OfficialComponent component) {
......@@ -111,4 +117,4 @@ public class ComponentsDefine {
return components[componentId];
}
}
}
\ No newline at end of file
}
......@@ -153,6 +153,16 @@
<artifactId>apm-sharding-jdbc-1.5.x-plugin</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.skywalking</groupId>
<artifactId>apm-xmemcached-2.x-plugin</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.skywalking</groupId>
<artifactId>apm-grpc-1.x-plugin</artifactId>
<version>${project.version}</version>
</dependency>
<!-- activation -->
<dependency>
......
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2017, OpenSkywalking Organization All rights reserved.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
~
~ Project repository: https://github.com/OpenSkywalking/skywalking
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.skywalking</groupId>
<artifactId>apm-sdk-plugin</artifactId>
<version>3.2.3-2017</version>
</parent>
<artifactId>apm-grpc-1.x-plugin</artifactId>
<name>grpc-1.x-plugin</name>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-all</artifactId>
<version>1.6.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
<configuration>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
/*
* Copyright 2017, OpenSkywalking Organization All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.plugin.grpc.v1;
import io.grpc.MethodDescriptor;
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceConstructorInterceptor;
import org.skywalking.apm.plugin.grpc.v1.vo.GRPCDynamicFields;
/**
* {@link ClientCallIConstructorInterceptor} pass the {@link GRPCDynamicFields} into the
* <code>io.grpc.internal.ClientCallImpl</code> instance for propagate the information of build span.
*
* @author zhangxin
*/
public class ClientCallIConstructorInterceptor implements InstanceConstructorInterceptor {
@Override
public void onConstruct(EnhancedInstance objInst, Object[] allArguments) {
GRPCDynamicFields dynamicFields = new GRPCDynamicFields();
dynamicFields.setDescriptor((MethodDescriptor)allArguments[0]);
objInst.setSkyWalkingDynamicField(dynamicFields);
}
}
/*
* Copyright 2017, OpenSkywalking Organization All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.plugin.grpc.v1;
import java.lang.reflect.Method;
import org.skywalking.apm.agent.core.context.ContextManager;
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
import org.skywalking.apm.plugin.grpc.v1.vo.GRPCDynamicFields;
import static org.skywalking.apm.plugin.grpc.v1.define.Constants.STREAM_OPERATION_NAME_SUFFIX;
/**
* {@link ClientCallOnNextInterceptor} create a local span when the client stream receive an message that send from
* server stream and record the value of <code>OnNext.count</code> tag.
*
* @author zhangxin
*/
public class ClientCallOnNextInterceptor implements InstanceMethodsAroundInterceptor {
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
MethodInterceptResult result) throws Throwable {
GRPCDynamicFields cachedObjects = (GRPCDynamicFields)objInst.getSkyWalkingDynamicField();
ContextManager.createLocalSpan(cachedObjects.getRequestMethodName() + STREAM_OPERATION_NAME_SUFFIX);
}
@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
Object ret) throws Throwable {
ContextManager.stopSpan();
// record the call count of onNext method
GRPCDynamicFields cachedObjects = (GRPCDynamicFields)objInst.getSkyWalkingDynamicField();
cachedObjects.incrementOnNextCount();
return ret;
}
@Override public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
Class<?>[] argumentsTypes, Throwable t) {
ContextManager.activeSpan().errorOccurred().log(t);
}
}
/*
* Copyright 2017, OpenSkywalking Organization All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.plugin.grpc.v1;
import io.grpc.Metadata;
import io.grpc.MethodDescriptor;
import java.lang.reflect.Method;
import org.skywalking.apm.agent.core.context.CarrierItem;
import org.skywalking.apm.agent.core.context.ContextCarrier;
import org.skywalking.apm.agent.core.context.ContextManager;
import org.skywalking.apm.agent.core.context.trace.AbstractSpan;
import org.skywalking.apm.agent.core.context.trace.SpanLayer;
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
import org.skywalking.apm.network.trace.component.ComponentsDefine;
import org.skywalking.apm.plugin.grpc.v1.vo.GRPCDynamicFields;
/**
* {@link ClientCallOnNextInterceptor} create a exist span when the grpc start call. it will stop span when the method
* type is non-unary.
*
* @author zhangxin
*/
public class ClientCallStartInterceptor
implements InstanceMethodsAroundInterceptor {
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
MethodInterceptResult result) throws Throwable {
GRPCDynamicFields cachedObjects = (GRPCDynamicFields)objInst.getSkyWalkingDynamicField();
final Metadata headers = (Metadata)allArguments[1];
final AbstractSpan span = ContextManager.createExitSpan(cachedObjects.getRequestMethodName(), cachedObjects.getAuthority());
span.setComponent(ComponentsDefine.GRPC);
SpanLayer.asRPCFramework(span);
final ContextCarrier contextCarrier = new ContextCarrier();
ContextManager.inject(contextCarrier);
CarrierItem contextItem = contextCarrier.items();
while (contextItem.hasNext()) {
contextItem = contextItem.next();
Metadata.Key<String> headerKey = Metadata.Key.of(contextItem.getHeadKey(), Metadata.ASCII_STRING_MARSHALLER);
headers.put(headerKey, contextItem.getHeadValue());
}
GRPCDynamicFields listenerCachedObject = new GRPCDynamicFields();
listenerCachedObject.setSnapshot(ContextManager.capture());
listenerCachedObject.setDescriptor(cachedObjects.getDescriptor());
((EnhancedInstance)allArguments[0]).setSkyWalkingDynamicField(listenerCachedObject);
}
@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
Object ret) throws Throwable {
if (((GRPCDynamicFields)objInst.getSkyWalkingDynamicField()).getMethodType() != MethodDescriptor.MethodType.UNARY) {
ContextManager.stopSpan();
}
return ret;
}
@Override public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
Class<?>[] argumentsTypes, Throwable t) {
ContextManager.activeSpan().errorOccurred().log(t);
}
}
/*
* Copyright 2017, OpenSkywalking Organization All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.plugin.grpc.v1;
import io.grpc.internal.ManagedChannelImpl;
import java.lang.reflect.Method;
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
import org.skywalking.apm.plugin.grpc.v1.vo.GRPCDynamicFields;
/**
* {@link ManagedChannelInterceptor} record the IP address of the GRPC server into {@link GRPCDynamicFields} for build
* span.
*
* @author zhangxin
*/
public class ManagedChannelInterceptor implements InstanceMethodsAroundInterceptor {
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
MethodInterceptResult result) throws Throwable {
}
@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
Object ret) throws Throwable {
GRPCDynamicFields cachedObjects = (GRPCDynamicFields)((EnhancedInstance)ret).getSkyWalkingDynamicField();
cachedObjects.setAuthority(((ManagedChannelImpl)((Object)objInst)).authority());
return ret;
}
@Override public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
Class<?>[] argumentsTypes, Throwable t) {
}
}
/*
* Copyright 2017, OpenSkywalking Organization All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.plugin.grpc.v1;
import io.grpc.Metadata;
import io.grpc.ServerCall;
import java.lang.reflect.Method;
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
import org.skywalking.apm.plugin.grpc.v1.vo.GRPCDynamicFields;
/**
* {@link ServerCallHandlerInterceptor} record the {@link Metadata} argument into {@link GRPCDynamicFields} for
* propagate {@link org.skywalking.apm.agent.core.context.ContextCarrier} and also record the {@link
* io.grpc.MethodDescriptor} into {@link GRPCDynamicFields} for building span.
*
* @author zhangxin
*/
public class ServerCallHandlerInterceptor implements InstanceMethodsAroundInterceptor {
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
MethodInterceptResult result) throws Throwable {
}
@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
Object ret) throws Throwable {
GRPCDynamicFields cachedObjects = new GRPCDynamicFields();
cachedObjects.setMetadata((Metadata)allArguments[1]);
cachedObjects.setDescriptor(((ServerCall)allArguments[0]).getMethodDescriptor());
((EnhancedInstance)ret).setSkyWalkingDynamicField(cachedObjects);
return ret;
}
@Override public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
Class<?>[] argumentsTypes, Throwable t) {
}
}
/*
* Copyright 2017, OpenSkywalking Organization All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.plugin.grpc.v1;
import java.lang.reflect.Method;
import org.skywalking.apm.agent.core.context.ContextManager;
import org.skywalking.apm.agent.core.context.trace.AbstractSpan;
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
import org.skywalking.apm.plugin.grpc.v1.vo.GRPCDynamicFields;
import static org.skywalking.apm.plugin.grpc.v1.define.Constants.ON_NEXT_COUNT_TAG_KEY;
/**
* {@link ServerCallOnCancelInterceptor} stop the active span when the call cancelled.
*
* @author zhangxin
*/
public class ServerCallOnCancelInterceptor implements InstanceMethodsAroundInterceptor {
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
MethodInterceptResult result) throws Throwable {
}
@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
Object ret) throws Throwable {
AbstractSpan abstractSpan = ContextManager.activeSpan();
abstractSpan.tag(ON_NEXT_COUNT_TAG_KEY, String.valueOf(((GRPCDynamicFields)objInst.getSkyWalkingDynamicField()).getOnNextCount()));
ContextManager.stopSpan();
return ret;
}
@Override public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
Class<?>[] argumentsTypes, Throwable t) {
ContextManager.activeSpan().errorOccurred().log(t);
}
}
/*
* Copyright 2017, OpenSkywalking Organization All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.plugin.grpc.v1;
import java.lang.reflect.Method;
import org.skywalking.apm.agent.core.context.ContextManager;
import org.skywalking.apm.agent.core.context.trace.AbstractSpan;
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
import org.skywalking.apm.plugin.grpc.v1.vo.GRPCDynamicFields;
import static org.skywalking.apm.plugin.grpc.v1.define.Constants.ON_NEXT_COUNT_TAG_KEY;
/**
* {@link ServerCallOnCloseInterceptor} stop the active span when the call end.
*
* @author zhangxin
*/
public class ServerCallOnCloseInterceptor implements InstanceMethodsAroundInterceptor {
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
MethodInterceptResult result) throws Throwable {
}
@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
Object ret) throws Throwable {
AbstractSpan abstractSpan = ContextManager.activeSpan();
abstractSpan.tag(ON_NEXT_COUNT_TAG_KEY, String.valueOf(((GRPCDynamicFields)objInst.getSkyWalkingDynamicField()).getOnNextCount()));
ContextManager.stopSpan();
return ret;
}
@Override public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
Class<?>[] argumentsTypes, Throwable t) {
ContextManager.activeSpan().errorOccurred().log(t);
}
}
/*
* Copyright 2017, OpenSkywalking Organization All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.plugin.grpc.v1;
import java.lang.reflect.Method;
import org.skywalking.apm.agent.core.context.ContextManager;
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
import org.skywalking.apm.plugin.grpc.v1.vo.GRPCDynamicFields;
import static org.skywalking.apm.plugin.grpc.v1.define.Constants.STREAM_OPERATION_NAME_SUFFIX;
/**
* {@link ServerCallOnMessageInterceptor} create a local span when the server stream receive a message that send by the
* client.
*
* @author zhangxin
*/
public class ServerCallOnMessageInterceptor implements InstanceMethodsAroundInterceptor {
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
MethodInterceptResult result) throws Throwable {
GRPCDynamicFields cachedObjects = (GRPCDynamicFields)objInst.getSkyWalkingDynamicField();
ContextManager.createLocalSpan(cachedObjects.getRequestMethodName() + STREAM_OPERATION_NAME_SUFFIX);
}
@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
Object ret) throws Throwable {
ContextManager.stopSpan();
// record the call count of onNext method
GRPCDynamicFields cachedObjects = (GRPCDynamicFields)objInst.getSkyWalkingDynamicField();
cachedObjects.incrementOnNextCount();
return ret;
}
@Override public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
Class<?>[] argumentsTypes, Throwable t) {
ContextManager.activeSpan().errorOccurred().log(t);
}
}
/*
* Copyright 2017, OpenSkywalking Organization All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.plugin.grpc.v1;
import io.grpc.Metadata;
import io.grpc.MethodDescriptor;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import org.skywalking.apm.agent.core.context.CarrierItem;
import org.skywalking.apm.agent.core.context.ContextCarrier;
import org.skywalking.apm.agent.core.context.ContextManager;
import org.skywalking.apm.agent.core.context.trace.AbstractSpan;
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
import org.skywalking.apm.network.trace.component.ComponentsDefine;
import org.skywalking.apm.plugin.grpc.v1.vo.GRPCDynamicFields;
import org.skywalking.apm.util.StringUtil;
import static org.skywalking.apm.plugin.grpc.v1.define.Constants.STREAM_CALL_OPERATION_NAME_SUFFIX;
import static org.skywalking.apm.plugin.grpc.v1.define.Constants.BLOCK_CALL_OPERATION_NAME_SUFFIX;
/**
* {@link ServerCallOnReadyInterceptor} create a entry span when the server side is ready for receive the message from
* the client side.
*
* @author zhangxin
*/
public class ServerCallOnReadyInterceptor implements InstanceMethodsAroundInterceptor {
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
MethodInterceptResult result) throws Throwable {
GRPCDynamicFields cachedObjects = (GRPCDynamicFields)objInst.getSkyWalkingDynamicField();
Metadata headers = cachedObjects.getMetadata();
Map<String, String> headerMap = new HashMap<String, String>();
for (String key : headers.keys()) {
if (!key.endsWith(Metadata.BINARY_HEADER_SUFFIX)) {
String value = headers.get(Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER));
headerMap.put(key, value);
}
}
ContextCarrier contextCarrier = new ContextCarrier();
CarrierItem next = contextCarrier.items();
while (next.hasNext()) {
next = next.next();
String contextValue = headerMap.get(next.getHeadKey());
if (!StringUtil.isEmpty(contextValue)) {
next.setHeadValue(contextValue);
}
}
final AbstractSpan span = ContextManager.createEntrySpan(cachedObjects.getRequestMethodName() + (cachedObjects.getMethodType() != MethodDescriptor.MethodType.UNARY ? STREAM_CALL_OPERATION_NAME_SUFFIX : BLOCK_CALL_OPERATION_NAME_SUFFIX), contextCarrier);
span.setComponent(ComponentsDefine.GRPC);
}
@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
Object ret) throws Throwable {
return ret;
}
@Override public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
Class<?>[] argumentsTypes, Throwable t) {
ContextManager.activeSpan().errorOccurred().log(t);
}
}
/*
* Copyright 2017, OpenSkywalking Organization All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.plugin.grpc.v1;
import io.grpc.Metadata;
import io.grpc.Status;
import java.lang.reflect.Method;
import org.skywalking.apm.agent.core.context.ContextManager;
import org.skywalking.apm.agent.core.context.tag.Tags;
import org.skywalking.apm.agent.core.context.trace.AbstractSpan;
import org.skywalking.apm.agent.core.context.trace.SpanLayer;
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
import org.skywalking.apm.network.trace.component.ComponentsDefine;
import org.skywalking.apm.plugin.grpc.v1.vo.GRPCDynamicFields;
import static org.skywalking.apm.plugin.grpc.v1.define.Constants.ON_NEXT_COUNT_TAG_KEY;
import static org.skywalking.apm.plugin.grpc.v1.define.Constants.STREAM_CALL_OPERATION_NAME_SUFFIX;
/**
* {@link StreamClientOnCloseInterceptor} stop the active span when the call end.
*
* @author zhangxin
*/
public class StreamClientOnCloseInterceptor implements InstanceMethodsAroundInterceptor {
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
MethodInterceptResult result) throws Throwable {
Status status = (Status)allArguments[0];
if (status.getCode() == Status.Code.UNAVAILABLE) {
GRPCDynamicFields cachedObjects = (GRPCDynamicFields)objInst.getSkyWalkingDynamicField();
AbstractSpan span = ContextManager.createLocalSpan(cachedObjects.getRequestMethodName() + STREAM_CALL_OPERATION_NAME_SUFFIX);
span.setComponent(ComponentsDefine.GRPC);
span.setLayer(SpanLayer.RPC_FRAMEWORK);
ContextManager.continued(cachedObjects.getSnapshot());
}
}
@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
Object ret) throws Throwable {
AbstractSpan activeSpan = ContextManager.activeSpan();
activeSpan.tag(ON_NEXT_COUNT_TAG_KEY, String.valueOf(((GRPCDynamicFields)objInst.getSkyWalkingDynamicField()).getOnNextCount()));
Status status = (Status)allArguments[0];
if (status != Status.OK) {
activeSpan.errorOccurred().log(status.asRuntimeException((Metadata)allArguments[1]));
Tags.STATUS_CODE.set(activeSpan, status.getCode().toString());
}
ContextManager.stopSpan();
return ret;
}
@Override public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
Class<?>[] argumentsTypes, Throwable t) {
ContextManager.activeSpan().errorOccurred().log(t);
}
}
/*
* Copyright 2017, OpenSkywalking Organization All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.plugin.grpc.v1;
import java.lang.reflect.Method;
import org.skywalking.apm.agent.core.context.ContextManager;
import org.skywalking.apm.agent.core.context.trace.AbstractSpan;
import org.skywalking.apm.agent.core.context.trace.SpanLayer;
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
import org.skywalking.apm.network.trace.component.ComponentsDefine;
import org.skywalking.apm.plugin.grpc.v1.vo.GRPCDynamicFields;
import static org.skywalking.apm.plugin.grpc.v1.define.Constants.STREAM_CALL_OPERATION_NAME_SUFFIX;
/**
* {@link ServerCallOnReadyInterceptor} create a local span when the client side is ready for send the message to the
* server side.
*
* @author zhangxin
*/
public class StreamClientOnReadyInterceptor implements InstanceMethodsAroundInterceptor {
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
MethodInterceptResult result) throws Throwable {
GRPCDynamicFields cachedObjects = (GRPCDynamicFields)objInst.getSkyWalkingDynamicField();
AbstractSpan span = ContextManager.createLocalSpan(cachedObjects.getRequestMethodName() + STREAM_CALL_OPERATION_NAME_SUFFIX);
span.setComponent(ComponentsDefine.GRPC);
span.setLayer(SpanLayer.RPC_FRAMEWORK);
ContextManager.continued(cachedObjects.getSnapshot());
}
@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
Object ret) throws Throwable {
return ret;
}
@Override public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
Class<?>[] argumentsTypes, Throwable t) {
ContextManager.activeSpan().errorOccurred().log(t);
}
}
/*
* Copyright 2017, OpenSkywalking Organization All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.plugin.grpc.v1;
import io.grpc.Metadata;
import io.grpc.Status;
import java.lang.reflect.Method;
import org.skywalking.apm.agent.core.context.ContextManager;
import org.skywalking.apm.agent.core.context.tag.Tags;
import org.skywalking.apm.agent.core.context.trace.AbstractSpan;
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
/**
* {@link UnaryClientOnCloseInterceptor} stop the active span when the call end.
*
* @author zhangxin
*/
public class UnaryClientOnCloseInterceptor implements InstanceMethodsAroundInterceptor {
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
MethodInterceptResult result) throws Throwable {
}
@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
Object ret) throws Throwable {
AbstractSpan activeSpan = ContextManager.activeSpan();
Status status = (Status)allArguments[0];
if (status != Status.OK) {
activeSpan.errorOccurred().log(status.asRuntimeException((Metadata)allArguments[1]));
Tags.STATUS_CODE.set(activeSpan, status.getCode().toString());
}
ContextManager.stopSpan();
return ret;
}
@Override public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
Class<?>[] argumentsTypes, Throwable t) {
ContextManager.activeSpan().errorOccurred().log(t);
}
}
/*
* Copyright 2017, OpenSkywalking Organization All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.plugin.grpc.v1.define;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.matcher.ElementMatcher;
import org.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint;
import org.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint;
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine;
import org.skywalking.apm.agent.core.plugin.match.ClassMatch;
import static net.bytebuddy.matcher.ElementMatchers.any;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static org.skywalking.apm.agent.core.plugin.match.NameMatch.byName;
/**
* {@link ClientCallInstrumentation} presents that skywalking intercept the <code>start</code> method in
* <code>io.grpc.internal.ClientCallImpl</code> class by <code>org.skywalking.apm.plugin.grpc.v1.ClientCallStartInterceptor</code>
* and the constructor in <code>io.grpc.internal.ClientCallImpl</code> by <code>org.skywalking.apm.plugin.grpc.v1.ClientCallIConstructorInterceptor</code>
*
* @author zhangxin
*/
public class ClientCallInstrumentation extends ClassInstanceMethodsEnhancePluginDefine {
private static final String ENHANCE_CLASS = "io.grpc.internal.ClientCallImpl";
private static final String ENHANCE_METHOD = "start";
public static final String CONSTRUCTOR_CLASS = "org.skywalking.apm.plugin.grpc.v1.ClientCallIConstructorInterceptor";
public static final String START_METHOD_INTERCEPT_CLASS = "org.skywalking.apm.plugin.grpc.v1.ClientCallStartInterceptor";
@Override protected ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
return new ConstructorInterceptPoint[] {
new ConstructorInterceptPoint() {
@Override public ElementMatcher<MethodDescription> getConstructorMatcher() {
return any();
}
@Override public String getConstructorInterceptor() {
return CONSTRUCTOR_CLASS;
}
}
};
}
@Override protected InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
return new InstanceMethodsInterceptPoint[] {
new InstanceMethodsInterceptPoint() {
@Override public ElementMatcher<MethodDescription> getMethodsMatcher() {
return named(ENHANCE_METHOD);
}
@Override public String getMethodsInterceptor() {
return START_METHOD_INTERCEPT_CLASS;
}
@Override public boolean isOverrideArgs() {
return true;
}
}
};
}
@Override protected ClassMatch enhanceClass() {
return byName(ENHANCE_CLASS);
}
}
/*
* Copyright 2017, OpenSkywalking Organization All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.plugin.grpc.v1.define;
/**
* GRPC Plugin constants variables.
*
* @author zhangxin
*/
public final class Constants {
public static final String STREAM_OPERATION_NAME_SUFFIX = "/ResponseStreamObserver/OnNext";
public static final String ON_NEXT_COUNT_TAG_KEY = "onNext.count";
public static final String STREAM_CALL_OPERATION_NAME_SUFFIX = "/StreamCall";
public static final String BLOCK_CALL_OPERATION_NAME_SUFFIX = "/BlockCall";
}
/*
* Copyright 2017, OpenSkywalking Organization All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.plugin.grpc.v1.define;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.matcher.ElementMatcher;
import org.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint;
import org.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint;
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine;
import org.skywalking.apm.agent.core.plugin.match.ClassMatch;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static org.skywalking.apm.agent.core.plugin.match.NameMatch.byName;
/**
* {@link ManagedChannelInstrumentation} presents that skywalking intercept the <code>newCall</code> method in
* <code>io.grpc.internal.ManagedChannelImpl</code> class by <code>org.skywalking.apm.plugin.grpc.v1.ManagedChannelInterceptor</code>
*
* @author zhangxin
*/
public class ManagedChannelInstrumentation extends ClassInstanceMethodsEnhancePluginDefine {
private static final String ENHANCE_CLASS = "io.grpc.internal.ManagedChannelImpl";
private static final String ENHANCE_METHOD = "newCall";
public static final String INTERCEPT_CLASS = "org.skywalking.apm.plugin.grpc.v1.ManagedChannelInterceptor";
@Override protected ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
return new ConstructorInterceptPoint[0];
}
@Override protected InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
return new InstanceMethodsInterceptPoint[] {
new InstanceMethodsInterceptPoint() {
@Override public ElementMatcher<MethodDescription> getMethodsMatcher() {
return named(ENHANCE_METHOD);
}
@Override public String getMethodsInterceptor() {
return INTERCEPT_CLASS;
}
@Override public boolean isOverrideArgs() {
return false;
}
}
};
}
@Override protected ClassMatch enhanceClass() {
return byName(ENHANCE_CLASS);
}
}
/*
* Copyright 2017, OpenSkywalking Organization All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.plugin.grpc.v1.define;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.matcher.ElementMatcher;
import org.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint;
import org.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint;
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine;
import org.skywalking.apm.agent.core.plugin.match.ClassMatch;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static org.skywalking.apm.agent.core.plugin.match.NameMatch.byName;
/**
* {@link StreamingServerCallHandlerInstrumentation} presents that skywalking intercept the <code>onReady</code> method
* by <code>org.skywalking.apm.plugin.grpc.v1.ServerCallOnReadyInterceptor</code>, the <code>onHalfClose</code> method
* by <code>org.skywalking.apm.plugin.grpc.v1.ServerCallOnCloseInterceptor</code> and the <code>onMessage</code> method
* by <code>org.skywalking.apm.plugin.grpc.v1.ServerCallOnMessageInterceptor</code> in
* <code>io.grpc.stub.ServerCalls$StreamingServerCallHandler$StreamingServerCallListener</code> class
*
* @author zhangxin
*/
public class StreamObserverToCallListenerInstrumentation extends ClassInstanceMethodsEnhancePluginDefine {
private static final String ENHANCE_CLASS = "io.grpc.stub.ClientCalls$StreamObserverToCallListenerAdapter";
public static final String ON_READY_METHOD = "onReady";
public static final String ON_READY_INTERCEPT_CLASS = "org.skywalking.apm.plugin.grpc.v1.StreamClientOnReadyInterceptor";
public static final String ON_CLASS_METHOD = "onClose";
public static final String ON_CLOSE_INTERCEPT_CLASS = "org.skywalking.apm.plugin.grpc.v1.StreamClientOnCloseInterceptor";
public static final String ON_MESSAGE_METHOD = "onMessage";
public static final String ON_MESSAGE_INTERCEPT_CLASS = "org.skywalking.apm.plugin.grpc.v1.ClientCallOnNextInterceptor";
@Override protected ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
return new ConstructorInterceptPoint[0];
}
@Override protected InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
return new InstanceMethodsInterceptPoint[] {
new InstanceMethodsInterceptPoint() {
@Override public ElementMatcher<MethodDescription> getMethodsMatcher() {
return named(ON_READY_METHOD);
}
@Override public String getMethodsInterceptor() {
return ON_READY_INTERCEPT_CLASS;
}
@Override public boolean isOverrideArgs() {
return false;
}
},
new InstanceMethodsInterceptPoint() {
@Override public ElementMatcher<MethodDescription> getMethodsMatcher() {
return named(ON_CLASS_METHOD);
}
@Override public String getMethodsInterceptor() {
return ON_CLOSE_INTERCEPT_CLASS;
}
@Override public boolean isOverrideArgs() {
return false;
}
},
new InstanceMethodsInterceptPoint() {
@Override public ElementMatcher<MethodDescription> getMethodsMatcher() {
return named(ON_MESSAGE_METHOD);
}
@Override public String getMethodsInterceptor() {
return ON_MESSAGE_INTERCEPT_CLASS;
}
@Override public boolean isOverrideArgs() {
return false;
}
}
};
}
@Override protected ClassMatch enhanceClass() {
return byName(ENHANCE_CLASS);
}
}
/*
* Copyright 2017, OpenSkywalking Organization All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.plugin.grpc.v1.define;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.matcher.ElementMatcher;
import org.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint;
import org.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint;
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine;
import org.skywalking.apm.agent.core.plugin.match.ClassMatch;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static org.skywalking.apm.agent.core.plugin.match.NameMatch.byName;
/**
* {@link StreamingServerCallHandlerInstrumentation} presents that skywalking intercept the <code>startCall</code>
* method in <code>io.grpc.stub.ServerCalls$UnaryServerCallHandler</code> class by
* <code>org.skywalking.apm.plugin.grpc.v1.ServerCallHandlerInterceptor</code>
*
* @author zhangxin
*/
public class StreamingServerCallHandlerInstrumentation extends ClassInstanceMethodsEnhancePluginDefine {
private static final String ENHANCE_CLASS = "io.grpc.stub.ServerCalls$UnaryServerCallHandler";
private static final String ENHANCE_METHOD = "startCall";
public static final String INTERCEPT_CLASS = "org.skywalking.apm.plugin.grpc.v1.ServerCallHandlerInterceptor";
@Override protected ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
return new ConstructorInterceptPoint[0];
}
@Override protected InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
return new InstanceMethodsInterceptPoint[] {
new InstanceMethodsInterceptPoint() {
@Override public ElementMatcher<MethodDescription> getMethodsMatcher() {
return named(ENHANCE_METHOD);
}
@Override public String getMethodsInterceptor() {
return INTERCEPT_CLASS;
}
@Override public boolean isOverrideArgs() {
return false;
}
}
};
}
@Override protected ClassMatch enhanceClass() {
return byName(ENHANCE_CLASS);
}
}
/*
* Copyright 2017, OpenSkywalking Organization All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.plugin.grpc.v1.define;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.matcher.ElementMatcher;
import org.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint;
import org.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint;
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine;
import org.skywalking.apm.agent.core.plugin.match.ClassMatch;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static org.skywalking.apm.agent.core.plugin.match.NameMatch.byName;
/**
* {@link StreamingServerCallHandlerInstrumentation} presents that skywalking intercept the <code>onReady</code> method
* by <code>org.skywalking.apm.plugin.grpc.v1.ServerCallOnReadyInterceptor</code>, the <code>onHalfClose</code> method
* by <code>org.skywalking.apm.plugin.grpc.v1.ServerCallOnCloseInterceptor</code>, the <code>onMessage</code> method by
* <code>org.skywalking.apm.plugin.grpc.v1.ServerCallOnMessageInterceptor</code> and the <code>onCancel</code> method by
* <code>org.skywalking.apm.plugin.grpc.v1.ServerCallOnCancelInterceptor</code> in
* <code>io.grpc.stub.ServerCalls$StreamingServerCallHandler$StreamingServerCallListener</code> class
*
* @author zhangxin
*/
public class StreamingServerCallListenerInstrumentation extends ClassInstanceMethodsEnhancePluginDefine {
private static final String ENHANCE_CLASS = "io.grpc.stub.ServerCalls$StreamingServerCallHandler$StreamingServerCallListener";
public static final String ON_READY_METHOD = "onReady";
public static final String ON_READ_INTERCEPT_CLASS = "org.skywalking.apm.plugin.grpc.v1.ServerCallOnReadyInterceptor";
public static final String ON_HALF_CLOSE_METHOD = "onHalfClose";
public static final String ON_HALF_CLOSE_INTERCEPT_CLASS = "org.skywalking.apm.plugin.grpc.v1.ServerCallOnCloseInterceptor";
public static final String ON_MESSAGE_METHOD = "onMessage";
public static final String ON_MESSAGE_INTERCEPT_CLASS = "org.skywalking.apm.plugin.grpc.v1.ServerCallOnMessageInterceptor";
public static final String ON_CANCEL_METHOD = "onCancel";
public static final String ON_CANCEL_INTERCEPT_CLASS = "org.skywalking.apm.plugin.grpc.v1.ServerCallOnCancelInterceptor";
@Override protected ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
return new ConstructorInterceptPoint[0];
}
@Override protected InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
return new InstanceMethodsInterceptPoint[] {
new InstanceMethodsInterceptPoint() {
@Override public ElementMatcher<MethodDescription> getMethodsMatcher() {
return named(ON_READY_METHOD);
}
@Override public String getMethodsInterceptor() {
return ON_READ_INTERCEPT_CLASS;
}
@Override public boolean isOverrideArgs() {
return false;
}
},
new InstanceMethodsInterceptPoint() {
@Override public ElementMatcher<MethodDescription> getMethodsMatcher() {
return named(ON_HALF_CLOSE_METHOD);
}
@Override public String getMethodsInterceptor() {
return ON_HALF_CLOSE_INTERCEPT_CLASS;
}
@Override public boolean isOverrideArgs() {
return false;
}
},
new InstanceMethodsInterceptPoint() {
@Override public ElementMatcher<MethodDescription> getMethodsMatcher() {
return named(ON_MESSAGE_METHOD);
}
@Override public String getMethodsInterceptor() {
return ON_MESSAGE_INTERCEPT_CLASS;
}
@Override public boolean isOverrideArgs() {
return false;
}
},
new InstanceMethodsInterceptPoint() {
@Override public ElementMatcher<MethodDescription> getMethodsMatcher() {
return named(ON_CANCEL_METHOD);
}
@Override public String getMethodsInterceptor() {
return ON_CANCEL_INTERCEPT_CLASS;
}
@Override public boolean isOverrideArgs() {
return false;
}
}
};
}
@Override protected ClassMatch enhanceClass() {
return byName(ENHANCE_CLASS);
}
}
/*
* Copyright 2017, OpenSkywalking Organization All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.plugin.grpc.v1.define;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.matcher.ElementMatcher;
import org.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint;
import org.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint;
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine;
import org.skywalking.apm.agent.core.plugin.match.ClassMatch;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static org.skywalking.apm.agent.core.plugin.match.NameMatch.byName;
/**
* {@link UnaryClientCallListenerInstrumentation} indicates that skywalking enhance the <code>onClose</code> method in
* <code>io.grpc.stub.ClientCalls$UnaryStreamToFuture</code> class by <code>org.skywalking.apm.plugin.grpc.v1.UnaryClientOnCloseInterceptor</code>
*
* @author zhangxin
*/
public class UnaryClientCallListenerInstrumentation extends ClassInstanceMethodsEnhancePluginDefine {
private static final String ENHANCE_CLASS = "io.grpc.stub.ClientCalls$UnaryStreamToFuture";
private static final String ENHANCE_METHOD = "onClose";
public static final String INTERCEPT_CLASS = "org.skywalking.apm.plugin.grpc.v1.UnaryClientOnCloseInterceptor";
@Override protected ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
return new ConstructorInterceptPoint[0];
}
@Override protected InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
return new InstanceMethodsInterceptPoint[] {
new InstanceMethodsInterceptPoint() {
@Override public ElementMatcher<MethodDescription> getMethodsMatcher() {
return named(ENHANCE_METHOD);
}
@Override public String getMethodsInterceptor() {
return INTERCEPT_CLASS;
}
@Override public boolean isOverrideArgs() {
return false;
}
}
};
}
@Override protected ClassMatch enhanceClass() {
return byName(ENHANCE_CLASS);
}
}
grpc-1.x=org.skywalking.apm.plugin.grpc.v1.define.ClientCallInstrumentation
grpc-1.x=org.skywalking.apm.plugin.grpc.v1.define.UnaryClientCallListenerInstrumentation
grpc-1.x=org.skywalking.apm.plugin.grpc.v1.define.UnaryServerCallListenerInstrumentation
grpc-1.x=org.skywalking.apm.plugin.grpc.v1.define.UnaryServerCallHandlerInstrumentation
grpc-1.x=org.skywalking.apm.plugin.grpc.v1.define.ManagedChannelInstrumentation
grpc-1.x=org.skywalking.apm.plugin.grpc.v1.define.StreamingServerCallHandlerInstrumentation
grpc-1.x=org.skywalking.apm.plugin.grpc.v1.define.StreamingServerCallListenerInstrumentation
grpc-1.x=org.skywalking.apm.plugin.grpc.v1.define.StreamObserverToCallListenerInstrumentation
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册