Commit 78398cc7 authored by lytscu's avatar lytscu
Browse files

Reconstruction

parent 5691a949
Loading
Loading
Loading
Loading
+13 −3
Original line number Diff line number Diff line
@@ -20,8 +20,11 @@ package org.skywalking.apm.plugin.mongodb.v2;

import com.mongodb.AggregationOutput;
import com.mongodb.CommandResult;
import com.mongodb.DB;
import com.mongodb.ServerAddress;
import com.mongodb.WriteResult;
import java.lang.reflect.Method;
import java.util.List;
import org.skywalking.apm.agent.core.context.ContextCarrier;
import org.skywalking.apm.agent.core.context.ContextManager;
import org.skywalking.apm.agent.core.context.tag.Tags;
@@ -48,8 +51,7 @@ public class MongoDBCollectionMethodInterceptor implements InstanceMethodsAround
        Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {

        String remotePeer = (String)objInst.getSkyWalkingDynamicField();
        String opertaion = null;
        opertaion = method.getName();
        String opertaion = method.getName();
        AbstractSpan span = ContextManager.createExitSpan(MONGO_DB_OP_PREFIX + opertaion, new ContextCarrier(), remotePeer);
        span.setComponent(ComponentsDefine.MONGODB);
        Tags.DB_TYPE.set(span, DB_TYPE);
@@ -70,6 +72,7 @@ public class MongoDBCollectionMethodInterceptor implements InstanceMethodsAround
        }
        if (null != cresult && !cresult.ok()) {
            activeSpan.tag("CommandError", cresult.getErrorMessage());
            activeSpan.log(cresult.getException());
        }
        ContextManager.stopSpan();
        return ret;
@@ -84,7 +87,14 @@ public class MongoDBCollectionMethodInterceptor implements InstanceMethodsAround

    @Override
    public void onConstruct(EnhancedInstance objInst, Object[] allArguments) {
        objInst.setSkyWalkingDynamicField(((EnhancedInstance)allArguments[0]).getSkyWalkingDynamicField().toString());
        List<ServerAddress> servers = null;
        DB db = (DB)allArguments[0];
        servers = db.getMongo().getAllAddress();
        StringBuilder peers = new StringBuilder();
        for (ServerAddress address : servers) {
            peers.append(address.getHost() + ":" + address.getPort() + ";");
        }
        objInst.setSkyWalkingDynamicField(peers.subSequence(0, peers.length() - 1).toString());
    }

}
+0 −101
Original line number Diff line number Diff line
/*
 * 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.mongodb.v2;

import com.mongodb.CommandResult;
import com.mongodb.DBEncoder;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.ServerAddress;
import java.lang.reflect.Method;
import java.util.List;
import org.skywalking.apm.agent.core.context.ContextCarrier;
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.InstanceConstructorInterceptor;
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;

/**
 * {@link MongoDBV2MethodInterceptor} intercept method of {@link com.mongodb.DB#command(DBObject, DBEncoder)} ()}
 *  record the mongoDB host, operation name ...
 */

public class MongoDBV2MethodInterceptor implements InstanceMethodsAroundInterceptor, InstanceConstructorInterceptor {

    private static final String DB_TYPE = "MongoDB";

    private static final String MONGO_DB_OP_PREFIX = "MongoDB/";

    @Override public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments,
        Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {

        String remotePeer = (String)objInst.getSkyWalkingDynamicField();
        String opertaion = "command";
        if (method.getName().equals("command")) {
            DBObject obj = (DBObject)allArguments[0];
            for (String key : obj.keySet()) {
                opertaion = key;
                break;
            }
        }
        AbstractSpan span = ContextManager.createExitSpan(MONGO_DB_OP_PREFIX + opertaion, new ContextCarrier(), remotePeer);
        span.setComponent(ComponentsDefine.MONGODB);
        Tags.DB_TYPE.set(span, DB_TYPE);
        SpanLayer.asDB(span);

    }

    @Override public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments,
        Class<?>[] argumentsTypes, Object ret) throws Throwable {
        AbstractSpan activeSpan = ContextManager.activeSpan();
        CommandResult cresult = null;
        cresult = (CommandResult)ret;
        if (null != cresult && !cresult.ok()) {
            activeSpan.tag("CommandError", cresult.getErrorMessage());
        }
        ContextManager.stopSpan();
        return ret;
    }

    @Override public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
        Class<?>[] argumentsTypes, Throwable t) {
        AbstractSpan activeSpan = ContextManager.activeSpan();
        activeSpan.errorOccurred();
        activeSpan.log(t);
    }

    @Override
    public void onConstruct(EnhancedInstance objInst, Object[] allArguments) {
        List<ServerAddress> servers = null;
        Mongo mongo = (Mongo)allArguments[0];
        servers = mongo.getAllAddress();
        StringBuilder peers = new StringBuilder();
        for (ServerAddress address : servers) {
            peers.append(address.getHost() + ":" + address.getPort() + ";");
        }

        objInst.setSkyWalkingDynamicField(peers.subSequence(0, peers.length() - 1).toString());
    }

}
+38 −0
Original line number Diff line number Diff line
/*
 * 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.mongodb.v2.define;

import org.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint;

/**
 * @Auther liyuntao
 */
public abstract class InterceptPoint implements InstanceMethodsInterceptPoint {
    private static final String MONGDB_METHOD_INTERCET_CLASS = "org.skywalking.apm.plugin.mongodb.v2.MongoDBCollectionMethodInterceptor";

    @Override
    public String getMethodsInterceptor() {
        return MONGDB_METHOD_INTERCET_CLASS;
    }

    @Override
    public boolean isOverrideArgs() {
        return false;
    }
}
+63 −52
Original line number Diff line number Diff line
@@ -32,7 +32,7 @@ import static org.skywalking.apm.agent.core.plugin.match.NameMatch.byName;

/**
 * {@link MongoDBCollectionInstrumentation} presents that skywalking intercepts {@link com.mongodb.DBCollection#find()},
 * {@link com.mongodb.DBCollection#mapReduce} by using {@link MongoDBV2MethodInterceptor}.
 * {@link com.mongodb.DBCollection#mapReduce} by using {@link MongoDBCollectionMethodInterceptor}.
 */
public class MongoDBCollectionInstrumentation extends ClassInstanceMethodsEnhancePluginDefine {

@@ -60,118 +60,129 @@ public class MongoDBCollectionInstrumentation extends ClassInstanceMethodsEnhanc
    @Override
    protected InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
        return new InstanceMethodsInterceptPoint[] {
            new InstanceMethodsInterceptPoint() {
            new InterceptPoint() {
                @Override
                public ElementMatcher<MethodDescription> getMethodsMatcher() {
                    return named("find");
                }

            },
            new InterceptPoint() {
                @Override
                public String getMethodsInterceptor() {
                    return MONGDB_METHOD_INTERCET_CLASS;
                public ElementMatcher<MethodDescription> getMethodsMatcher() {
                    return named("aggregate").and(takesArgumentWithType(2, "com.mongodb.ReadPreference"));
                }

                @Override
                public boolean isOverrideArgs() {
                    return false;
                }
            },
            new InstanceMethodsInterceptPoint() {
            new InterceptPoint() {
                @Override
                public ElementMatcher<MethodDescription> getMethodsMatcher() {
                    return named("aggregate").and(takesArgumentWithType(2, "com.mongodb.ReadPreference"));
                    return named("insert").and(takesArgumentWithType(2, "com.mongodb.DBEncoder"));
                }

            },
            new InterceptPoint() {
                @Override
                public String getMethodsInterceptor() {
                    return MONGDB_METHOD_INTERCET_CLASS;
                public ElementMatcher<MethodDescription> getMethodsMatcher() {
                    return named("update").and(takesArgumentWithType(5, "com.mongodb.DBEncoder"));
                }

            },
            new InterceptPoint() {
                @Override
                public boolean isOverrideArgs() {
                    return false;
                public ElementMatcher<MethodDescription> getMethodsMatcher() {
                    return named("remove").and(takesArgumentWithType(2, "com.mongodb.DBEncoder"));
                }

            },
            new InstanceMethodsInterceptPoint() {
            new InterceptPoint() {
                @Override
                public ElementMatcher<MethodDescription> getMethodsMatcher() {
                    return named("insert");
                    return named("findAndModify");
                }

            },
            new InterceptPoint() {
                @Override
                public String getMethodsInterceptor() {
                    return MONGDB_METHOD_INTERCET_CLASS;
                public ElementMatcher<MethodDescription> getMethodsMatcher() {
                    return named("createIndex").and(takesArgumentWithType(2, "com.mongodb.DBEncoder"));
                }

            },
            /**
             *Involved db_command operation
            */
            new InterceptPoint() {
                @Override
                public boolean isOverrideArgs() {
                    return false;
                public ElementMatcher<MethodDescription> getMethodsMatcher() {
                    return named("getCount").and(takesArgumentWithType(6, "java.util.concurrent.TimeUnit"));
                }
            },
            new InstanceMethodsInterceptPoint() {
            new InterceptPoint() {
                @Override
                public ElementMatcher<MethodDescription> getMethodsMatcher() {
                    return named("update");
                    return named("drop");
                }

            },
            new InterceptPoint() {
                @Override
                public String getMethodsInterceptor() {
                    return MONGDB_METHOD_INTERCET_CLASS;
                public ElementMatcher<MethodDescription> getMethodsMatcher() {
                    return named("dropIndexes");
                }

                @Override
                public boolean isOverrideArgs() {
                    return false;
                }
            },
            new InstanceMethodsInterceptPoint() {
            new InterceptPoint() {
                @Override
                public ElementMatcher<MethodDescription> getMethodsMatcher() {
                    return named("remove").and(takesArgumentWithType(2, "com.mongodb.DBEncoder"));
                    return named("rename").and(takesArgumentWithType(1, "boolean"));
                }

            },
            new InterceptPoint() {
                @Override
                public String getMethodsInterceptor() {
                    return MONGDB_METHOD_INTERCET_CLASS;
                public ElementMatcher<MethodDescription> getMethodsMatcher() {
                    return named("group").and(takesArgumentWithType(1, "boolean"));
                }

                @Override
                public boolean isOverrideArgs() {
                    return false;
                }
            },
            new InstanceMethodsInterceptPoint() {
            new InterceptPoint() {
                @Override
                public ElementMatcher<MethodDescription> getMethodsMatcher() {
                    return named("findAndModify");
                    return named("group").and(takesArgumentWithType(1, "com.mongodb.DBObject"));
                }

            },
            new InterceptPoint() {
                @Override
                public String getMethodsInterceptor() {
                    return MONGDB_METHOD_INTERCET_CLASS;
                public ElementMatcher<MethodDescription> getMethodsMatcher() {
                    return named("distinct").and(takesArgumentWithType(2, "com.mongodb.ReadPreference"));
                }

            },
            new InterceptPoint() {
                @Override
                public boolean isOverrideArgs() {
                    return false;
                public ElementMatcher<MethodDescription> getMethodsMatcher() {
                    return named("mapReduce").and(takesArgumentWithType(0, "com.mongodb.MapReduceCommand"));
                }
            },
            new InstanceMethodsInterceptPoint() {
            new InterceptPoint() {
                @Override
                public ElementMatcher<MethodDescription> getMethodsMatcher() {
                    return named("createIndex").and(takesArgumentWithType(2, "com.mongodb.DBEncoder"));
                    return named("mapReduce").and(takesArgumentWithType(0, "com.mongodb.DBObject"));
                }

            },
            new InterceptPoint() {
                @Override
                public String getMethodsInterceptor() {
                    return MONGDB_METHOD_INTERCET_CLASS;
                public ElementMatcher<MethodDescription> getMethodsMatcher() {
                    return named("aggregate").and(takesArgumentWithType(1, "com.mongodb.ReadPreference"));
                }

            },
            new InterceptPoint() {
                @Override
                public boolean isOverrideArgs() {
                    return false;
                public ElementMatcher<MethodDescription> getMethodsMatcher() {
                    return named("explainAggregate");
                }
            },

        };
    }

+0 −98
Original line number Diff line number Diff line
/*
 * 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.mongodb.v2.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.bytebuddy.ArgumentTypeNameMatch.takesArgumentWithType;
import static org.skywalking.apm.agent.core.plugin.match.NameMatch.byName;

/**
 * {@link MongoDBV2Instrumentation} presents that skywalking intercepts {@link com.mongodb.DB#command},
 * {@link com.mongodb.DBCollection#mapReduce} by using {@link MongoDBV2MethodInterceptor}.
 */
public class MongoDBV2Instrumentation extends ClassInstanceMethodsEnhancePluginDefine {

    private static final String ENHANCE_CLASS = "com.mongodb.DB";

    private static final String MONGDB_METHOD_INTERCET_CLASS = "org.skywalking.apm.plugin.mongodb.v2.MongoDBV2MethodInterceptor";

    @Override
    protected ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
        return new ConstructorInterceptPoint[] {
            new ConstructorInterceptPoint() {
                @Override
                public ElementMatcher<MethodDescription> getConstructorMatcher() {
                    return any();
                }

                @Override
                public String getConstructorInterceptor() {
                    return MONGDB_METHOD_INTERCET_CLASS;
                }
            }
        };
    }

    @Override
    protected InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
        return new InstanceMethodsInterceptPoint[] {

            new InstanceMethodsInterceptPoint() {
                @Override
                public ElementMatcher<MethodDescription> getMethodsMatcher() {
                    return named("command").and(takesArgumentWithType(3, "com.mongodb.DBEncoder"));
                }

                @Override
                public String getMethodsInterceptor() {
                    return MONGDB_METHOD_INTERCET_CLASS;
                }

                @Override
                public boolean isOverrideArgs() {
                    return false;
                }
            },
        };
    }

    @Override
    protected ClassMatch enhanceClass() {
        return byName(ENHANCE_CLASS);
    }

    @Override
    protected String[] witnessClasses() {
        /**
         * @see {@link com.mongodb.tools.ConnectionPoolStat}
         */
        return new String[] {
            "com.mongodb.tools.ConnectionPoolStat"
        };
    }

}
Loading