Search in sources :

Example 1 with OperationAccessor

use of com.pamirs.attach.plugin.mongodb.utils.OperationAccessor in project LinkAgent by shulieTech.

the class DelegateOperationExecutorTraceInterceptor method beforeTrace.

@Override
public SpanRecord beforeTrace(Advice advice) {
    Object[] args = advice.getParameterArray();
    SpanRecord spanRecord = new SpanRecord();
    spanRecord.setMiddlewareName("mongodb");
    Class operationClass = args[0].getClass();
    spanRecord.setMethod(operationClass.getSimpleName());
    spanRecord.setService("unknown");
    OperationAccessor operationAccessor = OperationAccessorFactory.getOperationAccessor(operationClass);
    if (operationAccessor != null) {
        try {
            spanRecord.setService((operationAccessor.getMongoNamespace(args[0]).getFullName()));
        } catch (Exception e) {
            LOGGER.error("not support operation class is {} ", args[0].getClass().getName());
        }
    }
    try {
        Object mongoClientDelegate = Caches.getMongoClientDelegate(advice.getTarget());
        String addressesStr = StringUtils.join(((Cluster) (ReflectionUtils.getFieldValue(mongoClientDelegate, "cluster"))).getSettings().getHosts(), ",");
        spanRecord.setRemoteIp(addressesStr);
    } catch (Exception e) {
        LOGGER.error("can not get mongoClientDelegate");
    }
    return spanRecord;
}
Also used : SpanRecord(com.pamirs.pradar.interceptor.SpanRecord) OperationAccessor(com.pamirs.attach.plugin.mongodb.utils.OperationAccessor) Cluster(com.mongodb.internal.connection.Cluster)

Example 2 with OperationAccessor

use of com.pamirs.attach.plugin.mongodb.utils.OperationAccessor in project LinkAgent by shulieTech.

the class DelegateOperationExecutorInterceptor method cutoff0.

@Override
public CutOffResult cutoff0(Advice advice) throws Throwable {
    if (!Pradar.isClusterTest()) {
        return CutOffResult.passed();
    }
    // 影子库重入
    if (manager.getDynamicField(advice.getTarget(), "isCluster") != null) {
        return CutOffResult.passed();
    }
    Object[] args = advice.getParameterArray();
    Class operationClass = args[0].getClass();
    Object operation = args[0];
    OperationAccessor operationAccessor = OperationAccessorFactory.getOperationAccessor(operationClass);
    if (operationAccessor == null) {
        LOGGER.error("not support operation class is {} ", args[0].getClass().getName());
        throw new PressureMeasureError("mongo not support pressure operation class is " + args[0].getClass().getName());
    }
    Object mongoClientDelegate = Caches.getMongoClientDelegate(advice.getTarget());
    ShadowDatabaseConfig shadowDatabaseConfig = getShadowDatabaseConfig(mongoClientDelegate);
    if (shadowDatabaseConfig == null) {
        if (operationAccessor.isRead()) {
            return CutOffResult.passed();
        } else {
            MongoNamespace busMongoNamespace = operationAccessor.getMongoNamespace(operation);
            ErrorReporter.Error error = ErrorReporter.buildError().setErrorType(ErrorTypeEnum.DataSource).setErrorCode("datasource-0005").setMessage("mongodb 不存在对应影子表或影子库:" + busMongoNamespace.getFullName()).setDetail("mongodb " + StringUtils.join(((Cluster) (ReflectionUtils.getFieldValue(mongoClientDelegate, "cluster"))).getSettings().getHosts(), ",") + "不存在对应影子表或影子库:" + busMongoNamespace.getFullName());
            error.closePradar(ConfigNames.SHADOW_DATABASE_CONFIGS);
            error.report();
            throw new PressureMeasureError("mongo 对应影子表或影子库:" + busMongoNamespace.getFullName());
        }
    }
    if (shadowDatabaseConfig.isShadowTable()) {
        doShadowTable(operation, operationAccessor, shadowDatabaseConfig);
        return CutOffResult.passed();
    } else {
        return CutOffResult.cutoff(doShadowDb(args, operationAccessor, shadowDatabaseConfig));
    }
}
Also used : ErrorReporter(com.pamirs.pradar.pressurement.agent.shared.service.ErrorReporter) OperationAccessor(com.pamirs.attach.plugin.mongodb.utils.OperationAccessor) PressureMeasureError(com.pamirs.pradar.exception.PressureMeasureError) Cluster(com.mongodb.internal.connection.Cluster) ShadowDatabaseConfig(com.pamirs.pradar.internal.config.ShadowDatabaseConfig) MongoNamespace(com.mongodb.MongoNamespace)

Aggregations

Cluster (com.mongodb.internal.connection.Cluster)2 OperationAccessor (com.pamirs.attach.plugin.mongodb.utils.OperationAccessor)2 MongoNamespace (com.mongodb.MongoNamespace)1 PressureMeasureError (com.pamirs.pradar.exception.PressureMeasureError)1 SpanRecord (com.pamirs.pradar.interceptor.SpanRecord)1 ShadowDatabaseConfig (com.pamirs.pradar.internal.config.ShadowDatabaseConfig)1 ErrorReporter (com.pamirs.pradar.pressurement.agent.shared.service.ErrorReporter)1