Search in sources :

Example 31 with PressureMeasureError

use of com.pamirs.pradar.exception.PressureMeasureError in project LinkAgent by shulieTech.

the class HessianServiceExporterHandleRequestInterceptor method exceptionTrace.

@Override
public SpanRecord exceptionTrace(Advice advice) {
    Object[] args = advice.getParameterArray();
    if (args == null || args.length == 0) {
        return null;
    }
    if (!(args[0] instanceof HttpServletRequest)) {
        if (Pradar.isClusterTest()) {
            throw new PressureMeasureError("hessian servlet trace err! can't cast to HttpServletRequest");
        }
        return null;
    }
    WrapperRequest request = (WrapperRequest) args[0];
    if (!request.getMethod().equals("POST") && !request.getMethod().equals("post")) {
        return null;
    }
    String method = request.getHeader(HessianConstants.METHOD_HEADER);
    if (method == null) {
        method = Pradar.getMethod();
    }
    if (method != null && isSkip(method)) {
        return null;
    }
    SpanRecord spanRecord = new SpanRecord();
    spanRecord.setResponse(advice.getThrowable());
    spanRecord.setResultCode(ResultCode.INVOKE_RESULT_FAILED);
    return spanRecord;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) SpanRecord(com.pamirs.pradar.interceptor.SpanRecord) PressureMeasureError(com.pamirs.pradar.exception.PressureMeasureError) WrapperRequest(com.pamirs.attach.plugin.hessian.common.WrapperRequest)

Example 32 with PressureMeasureError

use of com.pamirs.pradar.exception.PressureMeasureError in project LinkAgent by shulieTech.

the class HessianServiceExporterHandleRequestInterceptor method beforeTrace.

@Override
public SpanRecord beforeTrace(Advice advice) {
    Object[] args = advice.getParameterArray();
    Object target = advice.getTarget();
    if (args == null || args.length == 0) {
        return null;
    }
    if (!(args[0] instanceof HttpServletRequest)) {
        if (Pradar.isClusterTest()) {
            throw new PressureMeasureError("hessian servlet trace err! can't cast to HttpServletRequest");
        }
        return null;
    }
    WrapperRequest request = (WrapperRequest) args[0];
    if (!request.getMethod().equals("POST") && !request.getMethod().equals("post")) {
        return null;
    }
    String method = request.getHeader(HessianConstants.METHOD_HEADER);
    if (method != null && isSkip(method)) {
        return null;
    }
    HessianServiceExporter serviceExporter = (HessianServiceExporter) target;
    HessianSkeleton hessianSkeleton = null;
    try {
        hessianSkeleton = Reflect.on(serviceExporter).get(HessianConstants.DYNAMIC_FIELD_OBJECT_SKELETON);
    } catch (ReflectException e) {
    }
    SerializerFactory serializerFactory = null;
    try {
        serializerFactory = Reflect.on(serviceExporter).get(HessianConstants.DYNAMIC_FIELD_SERIALIZER_FACTORY);
    } catch (ReflectException e) {
    }
    Object[] result = getMethodArgs(request.getInputStream(), serializerFactory, hessianSkeleton);
    Object[] arguments = (Object[]) result[1];
    if (method == null) {
        method = (String) result[0];
    }
    Class<?> clazz = serviceExporter.getServiceInterface();
    SpanRecord spanRecord = new SpanRecord();
    spanRecord.setService(clazz.getName());
    spanRecord.setMethod(method);
    spanRecord.setRequest(arguments);
    return spanRecord;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) SpanRecord(com.pamirs.pradar.interceptor.SpanRecord) PressureMeasureError(com.pamirs.pradar.exception.PressureMeasureError) WrapperRequest(com.pamirs.attach.plugin.hessian.common.WrapperRequest) HessianSkeleton(com.caucho.hessian.server.HessianSkeleton) ReflectException(com.shulie.instrument.simulator.api.reflect.ReflectException) HessianServiceExporter(org.springframework.remoting.caucho.HessianServiceExporter)

Example 33 with PressureMeasureError

use of com.pamirs.pradar.exception.PressureMeasureError in project LinkAgent by shulieTech.

the class DataSourceConnectionInterceptor method cutoff0.

@Override
public CutOffResult cutoff0(Advice advice) {
    Object target = advice.getTarget();
    addListener();
    attachment(advice);
    ClusterTestUtils.validateClusterTest();
    HikariDataSource dataSource = (HikariDataSource) target;
    DataSourceMeta<HikariDataSource> dataSourceMeta = new DataSourceMeta<HikariDataSource>(dataSource.getJdbcUrl(), dataSource.getUsername(), dataSource);
    DataSourceWrapUtil.init(dataSourceMeta);
    Connection connection = null;
    /**
     * 所有的流量均切换到此逻辑上,防止业务有连接缓存后无法进入
     * 如果未找到配置情况下则当前流量为压测流量时返回null,非压测流量则执行业务连接池正常逻辑,此种情况可能由于数据源未配置的情况
     * 如果获取连接出错时如果流量为压测流量则返回null,非压测流量则执行业务连接池正常逻辑
     */
    HikariMediaDataSource mediatorDataSource = DataSourceWrapUtil.pressureDataSources.get(dataSourceMeta);
    if (mediatorDataSource != null) {
        try {
            connection = mediatorDataSource.getConnection();
        } catch (SQLException e) {
            throw new PressureMeasureError(e);
        }
        return CutOffResult.cutoff(connection);
    } else {
        if (!Pradar.isClusterTest()) {
            return CutOffResult.passed();
        }
        return CutOffResult.cutoff(null);
    }
}
Also used : HikariMediaDataSource(com.pamirs.attach.plugin.hikariCP.utils.HikariMediaDataSource) HikariDataSource(com.zaxxer.hikari.HikariDataSource) SQLException(java.sql.SQLException) PressureMeasureError(com.pamirs.pradar.exception.PressureMeasureError) Connection(java.sql.Connection) DataSourceMeta(com.pamirs.pradar.pressurement.agent.shared.service.DataSourceMeta)

Example 34 with PressureMeasureError

use of com.pamirs.pradar.exception.PressureMeasureError in project LinkAgent by shulieTech.

the class ProxyStatementInvokeInterceptor method getParameter0.

@Override
public Object[] getParameter0(Advice advice) throws Throwable {
    Object[] args = advice.getParameterArray();
    if (!Pradar.isClusterTest() || args == null || args.length != 3) {
        return args;
    }
    if (!(args[2] instanceof Object[])) {
        return args;
    }
    Object[] methodArgs = (Object[]) args[2];
    if (methodArgs == null || methodArgs.length == 0) {
        return args;
    }
    if (!(methodArgs[0] instanceof String)) {
        return args;
    }
    initConnectionPropertiesMethod(advice.getTarget());
    Object connectionPool = Reflect.on(advice.getTarget()).call(getConnectionPoolMethod).get();
    Object connectionPoolDefinition = Reflect.on(connectionPool).call(getDefinitionMethod).get();
    String url = Reflect.on(connectionPoolDefinition).call(getUrlMethod).get();
    if (url != null) {
        final int index = url.indexOf('?');
        if (index != -1) {
            url = url.substring(0, index);
        }
    }
    String username = Reflect.on(connectionPoolDefinition).call(getUserMethod).get();
    String dbType = JdbcUtils.getDbType(url, JdbcUtils.getDriverClassName(url));
    boolean useTable = !DatabaseUtils.isShadowDatasource(url, username);
    String dbConnectionKey = DbUrlUtils.getKey(url, username);
    if (!GlobalConfig.getInstance().containsShadowDatabaseConfig(dbConnectionKey)) {
        dbConnectionKey = DbUrlUtils.getKey(url, null);
    }
    // 因为获取的 url 和 username 有可能是影子的 url 和 username,需要是影子的 url 和 username 需要将是否影子表以及 dbConnectionKey 重新拿业务的 url 和 username计算
    if (!GlobalConfig.getInstance().containsShadowDatabaseConfig(dbConnectionKey)) {
        for (Map.Entry<String, ShadowDatabaseConfig> entry : GlobalConfig.getInstance().getShadowDatasourceConfigs().entrySet()) {
            if (StringUtils.equals(entry.getValue().getShadowUrl(), url) && StringUtils.equals(entry.getValue().getShadowUsername(), username)) {
                String bizUrl = entry.getValue().getUrl();
                String bizUser = entry.getValue().getUsername();
                dbConnectionKey = DbUrlUtils.getKey(bizUrl, bizUser);
                useTable = !DatabaseUtils.isShadowDatasource(bizUrl, bizUser);
                break;
            }
        }
    }
    String sql = (String) methodArgs[0];
    String ptSql = null;
    if (useTable) {
        try {
            ptSql = SqlParser.replaceTable(sql, dbConnectionKey, dbType, "proxool");
        } catch (SQLException e) {
            throw new PressureMeasureError(e.getMessage());
        }
    } else {
        ptSql = SqlParser.replaceSchema(sql, dbConnectionKey, dbType);
    }
    ((Object[]) args[2])[0] = ptSql;
    return args;
}
Also used : SQLException(java.sql.SQLException) PressureMeasureError(com.pamirs.pradar.exception.PressureMeasureError) ShadowDatabaseConfig(com.pamirs.pradar.internal.config.ShadowDatabaseConfig) Map(java.util.Map)

Example 35 with PressureMeasureError

use of com.pamirs.pradar.exception.PressureMeasureError in project LinkAgent by shulieTech.

the class PulsarTraceConsumerInterceptor method doBefore.

@Override
public void doBefore(Advice advice) {
    Object[] args = advice.getParameterArray();
    Object target = advice.getTarget();
    try {
        if (!(target instanceof org.apache.pulsar.client.impl.ConsumerBase)) {
            return;
        }
        if (args == null || args.length != 1) {
            return;
        }
        Object messageObj = args[0];
        if (!(messageObj instanceof Message)) {
            return;
        }
        Message<?> message = (Message<?>) messageObj;
        MQTraceContext mqTraceContext = new MQTraceContext();
        mqTraceContext.setMqType(MQType.PULSAR);
        mqTraceContext.setGroup(message.getProducerName() + ":" + message.getTopicName());
        MQTraceBean traceBean = new MQTraceBean();
        Map<String, String> properties = message.getProperties();
        if (properties != null) {
            Map<String, String> rpcContext = new HashMap<String, String>();
            for (String key : Pradar.getInvokeContextTransformKeys()) {
                String value = ObjectUtils.toString(properties.get(key));
                if (value != null) {
                    rpcContext.put(key, value);
                }
            }
            traceBean.setContext(rpcContext);
        }
        traceBean.setTopic(message.getTopicName());
        traceBean.setKeys(message.getKey());
        traceBean.setBodyLength(message.getData().length);
        // topic是否PT_开头
        boolean isClusterTest = message.getTopicName() != null && (Pradar.isClusterTestPrefix(message.getTopicName()));
        // 消息的properties是否包含Pradar.PRADAR_CLUSTER_TEST_KEY
        if (properties != null) {
            isClusterTest = isClusterTest || StringUtils.equalsIgnoreCase(Boolean.TRUE.toString(), ObjectUtils.toString(properties.get(PradarService.PRADAR_CLUSTER_TEST_KEY)));
        }
        if (isClusterTest) {
            traceBean.setClusterTest(Boolean.TRUE.toString());
        }
        ArrayList<MQTraceBean> traceBeans = new ArrayList<MQTraceBean>(1);
        traceBeans.add(traceBean);
        mqTraceContext.setTraceBeans(traceBeans);
        MQConsumeMessageTraceLog.consumeMessageBefore(mqTraceContext);
        threadLocal.set(mqTraceContext);
    } catch (PradarException e) {
        LOGGER.error("", e);
        if (Pradar.isClusterTest()) {
            throw e;
        }
    } catch (PressureMeasureError e) {
        LOGGER.error("", e);
        if (Pradar.isClusterTest()) {
            throw e;
        }
    } catch (Throwable e) {
        LOGGER.error("", e);
        if (Pradar.isClusterTest()) {
            throw new PressureMeasureError(e);
        }
    }
}
Also used : Message(org.apache.pulsar.client.api.Message) PradarException(com.pamirs.pradar.exception.PradarException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MQTraceBean(com.pamirs.attach.plugin.pulsar.common.MQTraceBean) MQTraceContext(com.pamirs.attach.plugin.pulsar.common.MQTraceContext) PressureMeasureError(com.pamirs.pradar.exception.PressureMeasureError)

Aggregations

PressureMeasureError (com.pamirs.pradar.exception.PressureMeasureError)150 PradarException (com.pamirs.pradar.exception.PradarException)34 DataSourceMeta (com.pamirs.pradar.pressurement.agent.shared.service.DataSourceMeta)14 Connection (java.sql.Connection)14 SQLException (java.sql.SQLException)13 ArrayList (java.util.ArrayList)13 Map (java.util.Map)11 MatchConfig (com.pamirs.pradar.internal.config.MatchConfig)8 ShadowDatabaseConfig (com.pamirs.pradar.internal.config.ShadowDatabaseConfig)8 HashMap (java.util.HashMap)8 MongoNamespace (com.mongodb.MongoNamespace)7 RequestIndexRename (com.pamirs.attach.plugin.es.common.RequestIndexRename)7 ProcessControlException (com.shulie.instrument.simulator.api.ProcessControlException)7 MQTraceContext (com.pamirs.attach.plugin.alibaba.rocketmq.common.MQTraceContext)6 SpanRecord (com.pamirs.pradar.interceptor.SpanRecord)6 ReflectException (com.shulie.instrument.simulator.api.reflect.ReflectException)6 List (java.util.List)6 ConsumeMessageContext (org.apache.rocketmq.client.hook.ConsumeMessageContext)6 ConsumeMessageContext (com.alibaba.rocketmq.client.hook.ConsumeMessageContext)5 WrapperRequest (com.pamirs.attach.plugin.hessian.common.WrapperRequest)5