Permalink
Browse files

Be smarter about passing arguments to the Operation classes. Extend t…

…he FreeMarker templates to include additional details. Finally get some details on DBCursor operations.
  • Loading branch information...
1 parent 5205412 commit bca1ea278c2572866121d2af083d8922d17aa6df @NogbadTheBad NogbadTheBad committed Dec 7, 2010
@@ -30,11 +30,6 @@
private static final int MAX_STRING_LENGTH = 1000;
/**
- * In any case, the maximum number of arguments we decode
- */
- private static final int MAX_ARGS = 10;
-
- /**
* How we show there's more
*/
private static final String ELLIPSIS = "...";
@@ -113,7 +108,7 @@ public String stringify(final DBObject[] array) {
* variable (STRING_FORM_MAP) in any of the put() calls, which you'd have to
* if you did it long hand.
*/
- private static Map<Class<?>, StringForm<? extends Object>> STRING_FORM_MAP = new HashMap<Class<?>, StringForm<? extends Object>>() {
+ private static final Map<Class<?>, StringForm<? extends Object>> STRING_FORM_MAP = new HashMap<Class<?>, StringForm<? extends Object>>() {
{
// Wrapper classes
//
@@ -153,17 +148,21 @@ private ArgUtils() {
*/
public static List<String> toString(final Object[] array,
final int maxLength) {
- final int nArgs = Math.min(MAX_ARGS, array.length);
- final int elementLength = maxLength / Math.max(1, nArgs);
-
return new ArrayList<String>() {
{
+ int soFar = 0;
+
for (final Object arg : array) {
- add(ArgUtils.toString(arg, elementLength));
- }
+ final String result = ArgUtils.toString(arg, maxLength
+ - soFar);
+
+ soFar += result.length();
+
+ add(result);
- if (array.length > MAX_ARGS) {
- add(ELLIPSIS);
+ if (soFar >= maxLength) {
+ break;
+ }
}
}
};
@@ -187,7 +186,8 @@ public static String toString(final Object object, final int maxLength) {
}
final Class<? extends Object> cls = object.getClass();
- final StringForm stringForm = STRING_FORM_MAP.get(cls);
+ final StringForm<Object> stringForm = (StringForm<Object>) STRING_FORM_MAP
+ .get(cls);
if (stringForm != null) {
return trimWithEllipsis(stringForm.stringify(object), maxLength);
@@ -1,6 +1,7 @@
package org.harrison.insight.plugin.mongodb;
import com.springsource.insight.intercept.operation.BasicOperation;
+import com.springsource.insight.intercept.operation.OperationType;
import com.springsource.insight.intercept.operation.SourceCodeLocation;
/**
@@ -10,25 +11,35 @@
* @author stephen harrison
*/
public abstract class MongoBasicOperation extends BasicOperation {
- // private Object rawReturnValue;
+ private final String method;
+ private final OperationType type;
+ private String returnValueAsString = "???";
- public MongoBasicOperation(final SourceCodeLocation scl) {
+ public MongoBasicOperation(final SourceCodeLocation scl,
+ final String method, final OperationType type) {
super(scl);
+
+ this.method = method;
+ this.type = type;
+ }
+
+ public String getMethod() {
+ return method;
+ }
+
+ public OperationType getType() {
+ return type;
}
- /**
- * A version of this method that knows about MongoDB types
- */
- // @Override
- // public void setReturnValue(final Object returnValue) {
- // super.setReturnValue(returnValue);
- //
- // rawReturnValue = returnValue;
- // }
-
- // @Override
- // public String getReturnValue() {
- // return "xyz";
- // // return ArgUtils.toString(rawReturnValue);
- // }
+ @Override
+ public void setReturnValue(final Object returnValue) {
+ super.setReturnValue(returnValue);
+
+ returnValueAsString = ArgUtils.toString(returnValue);
+ }
+
+ @Override
+ public String getReturnValue() {
+ return returnValueAsString;
+ }
}
@@ -17,35 +17,19 @@
public static final OperationType TYPE = OperationType.valueOf(NAME);
private final List<String> args;
- private final String method;
- private final String signature;
private final String collection;
public MongoCollectionOperation(final SourceCodeLocation scl,
- final List<String> args, final String method,
- final String signature, final String collection) {
- super(scl);
+ final String method, final List<String> args,
+ final String collection) {
+ super(scl, method, TYPE);
this.args = args;
- this.method = method;
- this.signature = signature;
this.collection = collection;
}
public String getLabel() {
- return "MongoDB: " + collection + "." + method + "()";
- }
-
- public OperationType getType() {
- return TYPE;
- }
-
- public String getMethod() {
- return method;
- }
-
- public String getSignature() {
- return signature;
+ return "MongoDB: " + collection + "." + getMethod() + "()";
}
public String getCollection() {
@@ -36,8 +36,6 @@ public aspect MongoCollectionOperationCollectionAspect extends
public pointcut ensureIndexExecute(): execution(void DBCollection.ensureIndex(..));
- public pointcut applyExecute(): execution(Object DBCollection.apply(..));
-
public pointcut saveExecute(): execution(WriteResult DBCollection.save(..));
public pointcut dropExecute(): execution(void DBCollection.drop());
@@ -62,7 +60,6 @@ public aspect MongoCollectionOperationCollectionAspect extends
findAndModifyExecute() ||
createIndexExecute() ||
ensureIndexExecute() ||
- applyExecute() ||
saveExecute() ||
dropExecute() ||
getCountExecute() ||
@@ -77,7 +74,7 @@ public aspect MongoCollectionOperationCollectionAspect extends
final DBCollection collection = (DBCollection) joinPoint.getThis();
return new MongoCollectionOperation(getSourceCodeLocation(joinPoint),
- ArgUtils.toString(joinPoint.getArgs()), signature.getName(),
- signature.toShortString(), collection.getFullName());
+ signature.getName(), ArgUtils.toString(joinPoint.getArgs()),
+ collection.getFullName());
}
}
@@ -1,5 +1,7 @@
package org.harrison.insight.plugin.mongodb;
+import java.util.List;
+
import com.springsource.insight.intercept.operation.BasicOperation;
import com.springsource.insight.intercept.operation.OperationType;
import com.springsource.insight.intercept.operation.SourceCodeLocation;
@@ -14,25 +16,24 @@
public static final String NAME = "mongo_cursor_operation";
public static final OperationType TYPE = OperationType.valueOf(NAME);
+ private final List<String> args;
private final String keysWanted;
private final String query;
private final String collection;
public MongoCursorOperation(final SourceCodeLocation scl,
+ final String method, final List<String> args,
final String keysWanted, final String query, final String collection) {
- super(scl);
+ super(scl, method, TYPE);
+ this.args = args;
this.keysWanted = keysWanted;
this.query = query;
this.collection = collection;
}
public String getLabel() {
- return "MongoDB: DBCursor.next()";
- }
-
- public OperationType getType() {
- return TYPE;
+ return "MongoDB: DBCursor." + getMethod() + "()";
}
public String getKeysWanted() {
@@ -46,4 +47,8 @@ public String getQuery() {
public String getCollection() {
return collection;
}
+
+ public List<String> getArgs() {
+ return args;
+ }
}
@@ -1,37 +1,67 @@
package org.harrison.insight.plugin.mongodb;
import java.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.List;
import org.aspectj.lang.JoinPoint;
+import org.aspectj.lang.Signature;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
-import com.mongodb.DBObject;
import com.springsource.insight.collection.AbstractOperationCollectionAspect;
import com.springsource.insight.intercept.operation.Operation;
public aspect MongoCursorOperationCollectionAspect extends
AbstractOperationCollectionAspect {
private static final String UNKNOWN = "?unknown?";
+ private static final List<String> EMPTY_ARGS = new ArrayList<String>();
- public pointcut collectionPoint(): execution(DBObject DBCursor.next());
+ private pointcut nextExecution():
+ execution(* DBCursor.next(..));
+
+ private pointcut skipExecution():
+ execution(* DBCursor.skip(..));
+
+ private pointcut limitExecution():
+ execution(* DBCursor.limit(..));
+
+ private pointcut toArrayExecution():
+ execution(* DBCursor.toArray(..));
+
+ private pointcut sortExecution():
+ execution(* DBCursor.sort(..));
+
+ private pointcut batchSizeExecution():
+ execution(* DBCursor.batchSize(..));
+
+ public pointcut collectionPoint():
+ (nextExecution() && !cflowbelow(nextExecution())) ||
+ (skipExecution() && !cflowbelow(skipExecution())) ||
+ (limitExecution() && !cflowbelow(limitExecution())) ||
+ (toArrayExecution() && !cflowbelow(toArrayExecution())) ||
+ (sortExecution() && !cflowbelow(sortExecution())) ||
+ (batchSizeExecution() && !cflowbelow(batchSizeExecution()))
+ ;
@Override
protected Operation createOperation(final JoinPoint joinPoint) {
+ final Signature signature = joinPoint.getSignature();
final DBCursor cursor = (DBCursor) joinPoint.getTarget();
- if (true || cursor == null) {
+ if (cursor == null) {
return new MongoCursorOperation(getSourceCodeLocation(joinPoint),
- UNKNOWN, UNKNOWN, UNKNOWN);
+ signature.getName(), EMPTY_ARGS, UNKNOWN, UNKNOWN, UNKNOWN);
}
final DBCollection collection = extractCollectionFieldTheUglyWay(cursor);
final String collectionName = collection == null ? UNKNOWN : collection
.getFullName();
return new MongoCursorOperation(getSourceCodeLocation(joinPoint),
- nullsafeToString(cursor.getKeysWanted()),
- nullsafeToString(cursor.getQuery()), collectionName);
+ signature.getName(), ArgUtils.toString(joinPoint.getArgs()),
+ ArgUtils.toString(cursor.getKeysWanted()),
+ ArgUtils.toString(cursor.getQuery()), collectionName);
}
/*
@@ -56,8 +86,4 @@ public aspect MongoCursorOperationCollectionAspect extends
return null;
}
}
-
- private static String nullsafeToString(final Object object) {
- return object == null ? "null" : object.toString();
- }
}
@@ -16,26 +16,16 @@
public static final OperationType TYPE = OperationType.valueOf(NAME);
private final List<String> args;
- private final String signature;
public MongoDbOperation(final SourceCodeLocation scl,
- final List<String> args, final String signature) {
- super(scl);
+ final String method, final List<String> args) {
+ super(scl, method, TYPE);
this.args = args;
- this.signature = signature;
}
public String getLabel() {
- return "MongoDB: " + signature;
- }
-
- public OperationType getType() {
- return TYPE;
- }
-
- public String getSignature() {
- return signature;
+ return "MongoDB: DB." + getMethod() + "()";
}
public List<String> getArgs() {
@@ -1,7 +1,6 @@
package org.harrison.insight.plugin.mongodb;
import org.aspectj.lang.JoinPoint;
-import org.aspectj.lang.Signature;
import com.mongodb.CommandResult;
import com.mongodb.DB;
@@ -15,10 +14,8 @@ public aspect MongoDbOperationCollectionAspect extends
@Override
protected Operation createOperation(final JoinPoint joinPoint) {
- final Signature signature = joinPoint.getSignature();
-
- return new MongoDbOperation(getSourceCodeLocation(joinPoint),
- ArgUtils.toString(joinPoint.getArgs()),
- signature.toShortString());
+ return new MongoDbOperation(getSourceCodeLocation(joinPoint), joinPoint
+ .getSignature().getName(), ArgUtils.toString(joinPoint
+ .getArgs()));
}
}
@@ -4,21 +4,14 @@
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/insight-idk http://www.springframework.org/schema/insight-idk/insight-idk-1.0.xsd">
- <insight:plugin name="mongodb" version="0.3"
- publisher="Stephen Harrison (stephen@harrison.org)" />
+ <insight:plugin name="mongodb" version="0.3" publisher="Stephen Harrison (stephen@harrison.org)" />
- <insight:operation-view operation="mongo_collection_operation"
- template="org/harrison/insight/plugin/mongodb/mongo_collection_operation.ftl" />
- <insight:operation-view operation="mongo_cursor_operation"
- template="org/harrison/insight/plugin/mongodb/mongo_cursor_operation.ftl" />
- <insight:operation-view operation="mongo_db_operation"
- template="org/harrison/insight/plugin/mongodb/mongo_db_operation.ftl" />
+ <insight:operation-view operation="mongo_collection_operation" template="org/harrison/insight/plugin/mongodb/mongo_collection_operation.ftl" />
+ <insight:operation-group operation="mongo_collection_operation" group="MongoDB" />
- <insight:operation-group group="MongoDB"
- operation="mongo_collection_operation" />
- <insight:operation-group group="MongoDB"
- operation="mongo_cursor_operation" />
- <insight:operation-group group="MongoDB"
- operation="mongo_db_operation" />
+ <insight:operation-view operation="mongo_cursor_operation" template="org/harrison/insight/plugin/mongodb/mongo_cursor_operation.ftl" />
+ <insight:operation-group operation="mongo_cursor_operation" group="MongoDB" />
+ <insight:operation-view operation="mongo_db_operation" template="org/harrison/insight/plugin/mongodb/mongo_db_operation.ftl" />
+ <insight:operation-group operation="mongo_db_operation" group="MongoDB" />
</beans>
Oops, something went wrong.

0 comments on commit bca1ea2

Please sign in to comment.