提交 dd6e4755 编写于 作者: B baiyang

modify code style

上级 d016f31b
......@@ -81,9 +81,9 @@ public class Config {
*/
public static LogLevel LEVEL = LogLevel.DEBUG;
}
public static class Plugin{
public static class MongoDB{
public static class Plugin {
public static class MongoDB {
/**
* If true, trace all the parameters, default is false.
* Only trace the operation, not include parameters.
......
<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>
<artifactId>skywalking-sdk-plugin</artifactId>
<groupId>com.a.eye</groupId>
<version>3.0.1-2017</version>
</parent>
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>
<artifactId>skywalking-sdk-plugin</artifactId>
<groupId>com.a.eye</groupId>
<version>3.0.1-2017</version>
</parent>
<artifactId>skywalking-mongodb-3.x-plugin</artifactId>
<packaging>jar</packaging>
<artifactId>skywalking-mongodb-3.x-plugin</artifactId>
<packaging>jar</packaging>
<name>mongodb-plugin</name>
<url>http://maven.apache.org</url>
<name>mongodb-plugin</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.4.2</version>
<scope>provided</scope>
</dependency>
</dependencies>
<dependencies>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.4.2</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<!-- 源码插件 -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<!-- 发布时自动将源码同时发布的配置 -->
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<build>
<plugins>
<plugin>
<!-- 源码插件 -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<!-- 发布时自动将源码同时发布的配置 -->
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
......@@ -66,7 +66,7 @@ public class MongoDBMethodInterceptor implements InstanceMethodsAroundIntercepto
@Override
public void beforeMethod(final EnhancedClassInstanceContext context,
final InstanceMethodInvokeContext interceptorContext, final MethodInterceptResult result) {
final InstanceMethodInvokeContext interceptorContext, final MethodInterceptResult result) {
Object[] arguments = interceptorContext.allArguments();
String methodName = arguments[0].getClass().getSimpleName();
......@@ -84,17 +84,17 @@ public class MongoDBMethodInterceptor implements InstanceMethodsAroundIntercepto
@Override
public Object afterMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext,
Object ret) {
Object ret) {
Span span = ContextManager.activeSpan();
Tags.PEER_HOST.set(span, context.get(MONGODB_HOST, String.class));
Tags.PEER_PORT.set(span, (Integer) context.get(MONGODB_PORT));
Tags.PEER_PORT.set(span, (Integer)context.get(MONGODB_PORT));
ContextManager.stopSpan();
return ret;
}
@Override
public void handleMethodException(Throwable t, EnhancedClassInstanceContext context,
InstanceMethodInvokeContext interceptorContext) {
InstanceMethodInvokeContext interceptorContext) {
ContextManager.activeSpan().log(t);
}
......@@ -105,55 +105,55 @@ public class MongoDBMethodInterceptor implements InstanceMethodsAroundIntercepto
@SuppressWarnings("rawtypes")
private String getTraceParam(Object obj) {
if (obj instanceof CountOperation) {
BsonDocument filter = ((CountOperation) obj).getFilter();
BsonDocument filter = ((CountOperation)obj).getFilter();
return limitFilter(filter.toString());
} else if (obj instanceof DistinctOperation) {
BsonDocument filter = ((DistinctOperation) obj).getFilter();
BsonDocument filter = ((DistinctOperation)obj).getFilter();
return limitFilter(filter.toString());
} else if (obj instanceof FindOperation) {
BsonDocument filter = ((FindOperation) obj).getFilter();
BsonDocument filter = ((FindOperation)obj).getFilter();
return limitFilter(filter.toString());
} else if (obj instanceof GroupOperation) {
BsonDocument filter = ((GroupOperation) obj).getFilter();
BsonDocument filter = ((GroupOperation)obj).getFilter();
return limitFilter(filter.toString());
} else if (obj instanceof ListCollectionsOperation) {
BsonDocument filter = ((ListCollectionsOperation) obj).getFilter();
BsonDocument filter = ((ListCollectionsOperation)obj).getFilter();
return limitFilter(filter.toString());
} else if (obj instanceof MapReduceWithInlineResultsOperation) {
BsonDocument filter = ((ListCollectionsOperation) obj).getFilter();
BsonDocument filter = ((ListCollectionsOperation)obj).getFilter();
return limitFilter(filter.toString());
} else if (obj instanceof DeleteOperation) {
List<DeleteRequest> writeRequestList = ((DeleteOperation) obj).getDeleteRequests();
List<DeleteRequest> writeRequestList = ((DeleteOperation)obj).getDeleteRequests();
return getFilter(writeRequestList);
} else if (obj instanceof InsertOperation) {
List<InsertRequest> writeRequestList = ((InsertOperation) obj).getInsertRequests();
List<InsertRequest> writeRequestList = ((InsertOperation)obj).getInsertRequests();
return getFilter(writeRequestList);
} else if (obj instanceof UpdateOperation) {
List<UpdateRequest> writeRequestList = ((UpdateOperation) obj).getUpdateRequests();
List<UpdateRequest> writeRequestList = ((UpdateOperation)obj).getUpdateRequests();
return getFilter(writeRequestList);
} else if (obj instanceof CreateCollectionOperation) {
String filter = ((CreateCollectionOperation) obj).getCollectionName();
String filter = ((CreateCollectionOperation)obj).getCollectionName();
return limitFilter(filter);
} else if (obj instanceof CreateIndexesOperation) {
List<String> filter = ((CreateIndexesOperation) obj).getIndexNames();
List<String> filter = ((CreateIndexesOperation)obj).getIndexNames();
return limitFilter(filter.toString());
} else if (obj instanceof CreateViewOperation) {
String filter = ((CreateViewOperation) obj).getViewName();
String filter = ((CreateViewOperation)obj).getViewName();
return limitFilter(filter);
} else if (obj instanceof FindAndDeleteOperation) {
BsonDocument filter = ((FindAndDeleteOperation) obj).getFilter();
BsonDocument filter = ((FindAndDeleteOperation)obj).getFilter();
return limitFilter(filter.toString());
} else if (obj instanceof FindAndReplaceOperation) {
BsonDocument filter = ((FindAndReplaceOperation) obj).getFilter();
BsonDocument filter = ((FindAndReplaceOperation)obj).getFilter();
return limitFilter(filter.toString());
} else if (obj instanceof FindAndUpdateOperation) {
BsonDocument filter = ((FindAndUpdateOperation) obj).getFilter();
BsonDocument filter = ((FindAndUpdateOperation)obj).getFilter();
return limitFilter(filter.toString());
} else if (obj instanceof MapReduceToCollectionOperation) {
BsonDocument filter = ((MapReduceToCollectionOperation) obj).getFilter();
BsonDocument filter = ((MapReduceToCollectionOperation)obj).getFilter();
return limitFilter(filter.toString());
} else if (obj instanceof MixedBulkWriteOperation) {
List<? extends WriteRequest> writeRequestList = ((MixedBulkWriteOperation) obj).getWriteRequests();
List<? extends WriteRequest> writeRequestList = ((MixedBulkWriteOperation)obj).getWriteRequests();
return getFilter(writeRequestList);
} else {
return EMPTY;
......@@ -164,11 +164,11 @@ public class MongoDBMethodInterceptor implements InstanceMethodsAroundIntercepto
StringBuilder params = new StringBuilder();
for (WriteRequest request : writeRequestList) {
if (request instanceof InsertRequest) {
params.append(((InsertRequest) request).getDocument().toString()).append(",");
params.append(((InsertRequest)request).getDocument().toString()).append(",");
} else if (request instanceof DeleteRequest) {
params.append(((DeleteRequest) request).getFilter()).append(",");
params.append(((DeleteRequest)request).getFilter()).append(",");
} else if (request instanceof UpdateRequest) {
params.append(((UpdateRequest) request).getFilter()).append(",");
params.append(((UpdateRequest)request).getFilter()).append(",");
}
if (params.length() > FILTER_LENGTH_LIMIT) {
params.append("...");
......
......@@ -15,35 +15,35 @@ import com.mongodb.binding.ReadBinding;
* @author baiyang
*/
public class MongoDBReadBindingInterceptor implements
InstanceMethodsAroundInterceptor {
InstanceMethodsAroundInterceptor {
@Override
public void beforeMethod(EnhancedClassInstanceContext context,
InstanceMethodInvokeContext interceptorContext,
MethodInterceptResult result) {
}
@Override
public void beforeMethod(EnhancedClassInstanceContext context,
InstanceMethodInvokeContext interceptorContext,
MethodInterceptResult result) {
}
/**
* Execute after {@link com.mongodb.Mongo#getReadBinding(ReadPreference)},
* record the host and port information
*/
@Override
public Object afterMethod(EnhancedClassInstanceContext context,
InstanceMethodInvokeContext interceptorContext, Object ret) {
ReadBinding readBinding = (ReadBinding) ret;
ServerAddress serverAddress = readBinding.getReadConnectionSource()
.getServerDescription().getAddress();
String host = serverAddress.getHost();
Integer port = serverAddress.getPort();
context.set(MongoDBMethodInterceptor.MONGODB_HOST, host);
context.set(MongoDBMethodInterceptor.MONGODB_PORT, port);
return ret;
}
/**
* Execute after {@link com.mongodb.Mongo#getReadBinding(ReadPreference)},
* record the host and port information
*/
@Override
public Object afterMethod(EnhancedClassInstanceContext context,
InstanceMethodInvokeContext interceptorContext, Object ret) {
ReadBinding readBinding = (ReadBinding)ret;
ServerAddress serverAddress = readBinding.getReadConnectionSource()
.getServerDescription().getAddress();
String host = serverAddress.getHost();
Integer port = serverAddress.getPort();
context.set(MongoDBMethodInterceptor.MONGODB_HOST, host);
context.set(MongoDBMethodInterceptor.MONGODB_PORT, port);
return ret;
}
@Override
public void handleMethodException(Throwable t,
EnhancedClassInstanceContext context,
InstanceMethodInvokeContext interceptorContext) {
}
@Override
public void handleMethodException(Throwable t,
EnhancedClassInstanceContext context,
InstanceMethodInvokeContext interceptorContext) {
}
}
......@@ -8,7 +8,8 @@ import com.mongodb.ServerAddress;
import com.mongodb.binding.WriteBinding;
/**
* {@link MongoDBWriteBindingInterceptor} record the host and port information from {@link EnhancedClassInstanceContext#context}
* {@link MongoDBWriteBindingInterceptor} record the host and port information from {@link
* EnhancedClassInstanceContext#context}
*
* @author baiyang
*/
......@@ -16,7 +17,7 @@ public class MongoDBWriteBindingInterceptor implements InstanceMethodsAroundInte
@Override
public void beforeMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext,
MethodInterceptResult result) {
MethodInterceptResult result) {
}
/**
......@@ -25,8 +26,8 @@ public class MongoDBWriteBindingInterceptor implements InstanceMethodsAroundInte
*/
@Override
public Object afterMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext,
Object ret) {
WriteBinding writeBinding = (WriteBinding) ret;
Object ret) {
WriteBinding writeBinding = (WriteBinding)ret;
ServerAddress serverAddress = writeBinding.getWriteConnectionSource().getServerDescription().getAddress();
String host = serverAddress.getHost();
Integer port = serverAddress.getPort();
......@@ -37,7 +38,7 @@ public class MongoDBWriteBindingInterceptor implements InstanceMethodsAroundInte
@Override
public void handleMethodException(Throwable t, EnhancedClassInstanceContext context,
InstanceMethodInvokeContext interceptorContext) {
InstanceMethodInvokeContext interceptorContext) {
}
}
package com.a.eye.skywalking.plugin.mongodb.v3.define;
import static net.bytebuddy.matcher.ElementMatchers.named;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.matcher.ElementMatcher;
import com.a.eye.skywalking.api.plugin.interceptor.ConstructorInterceptPoint;
import com.a.eye.skywalking.api.plugin.interceptor.InstanceMethodsInterceptPoint;
import com.a.eye.skywalking.api.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine;
import com.a.eye.skywalking.plugin.mongodb.v3.MongoDBMethodInterceptor;
/**
* {@link MongoDBInstrumentation} presents that skywalking intercepts {@link com.mongodb.Mongo#execute(ReadOperation, ReadPreference)},{@link com.mongodb.Mongo#execute(WriteOperation)}
* by using {@link MongoDBMethodInterceptor}.
* {@link MongoDBInstrumentation} presents that skywalking intercepts {@link com.mongodb.Mongo#execute(ReadOperation,
* ReadPreference)},{@link com.mongodb.Mongo#execute(WriteOperation)} by using {@link MongoDBMethodInterceptor}.
*
* @author baiyang
*/
......@@ -31,37 +33,39 @@ public class MongoDBInstrumentation extends ClassInstanceMethodsEnhancePluginDef
@Override
protected InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
return new InstanceMethodsInterceptPoint[] { new InstanceMethodsInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getMethodsMatcher() {
return named("execute");
}
return new InstanceMethodsInterceptPoint[] {
new InstanceMethodsInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getMethodsMatcher() {
return named("execute");
}
@Override
public String getMethodsInterceptor() {
return MONGDB_METHOD_INTERCET_CLASS;
}
}, new InstanceMethodsInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getMethodsMatcher() {
return named("getReadBinding");
}
@Override
public String getMethodsInterceptor() {
return MONGDB_METHOD_INTERCET_CLASS;
}
}, new InstanceMethodsInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getMethodsMatcher() {
return named("getReadBinding");
}
@Override
public String getMethodsInterceptor() {
return MONGDB_READ_BINDING_CLASS;
}
}, new InstanceMethodsInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getMethodsMatcher() {
return named("getWriteBinding");
}
@Override
public String getMethodsInterceptor() {
return MONGDB_READ_BINDING_CLASS;
}
}, new InstanceMethodsInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getMethodsMatcher() {
return named("getWriteBinding");
}
@Override
public String getMethodsInterceptor() {
return MONGDB_WRITE_BINDING_CLASS;
@Override
public String getMethodsInterceptor() {
return MONGDB_WRITE_BINDING_CLASS;
}
}
} };
};
}
@Override
......
......@@ -45,8 +45,8 @@ public class MongoDBMethodInterceptorTest {
@Mock
private InstanceMethodInvokeContext methodInvokeContext;
@SuppressWarnings({ "rawtypes", "unchecked" })
@Before
@SuppressWarnings({"rawtypes", "unchecked"})
@Before
public void setUp() throws Exception {
ServiceManager.INSTANCE.boot();
......@@ -54,7 +54,7 @@ public class MongoDBMethodInterceptorTest {
mockTracerContextListener = new MockTracerContextListener();
TracerContext.ListenerManager.add(mockTracerContextListener);
Config.Plugin.MongoDB.TRACE_PARAM = true;
when(classInstanceContext.get(MongoDBMethodInterceptor.MONGODB_HOST, String.class)).thenReturn("127.0.0.1");
......@@ -63,16 +63,16 @@ public class MongoDBMethodInterceptorTest {
BsonDocument document = new BsonDocument();
document.append("name", new BsonString("by"));
MongoNamespace mongoNamespace = new MongoNamespace("test.user");
Decoder decoder = PowerMockito.mock(Decoder.class);
FindOperation findOperation = new FindOperation(mongoNamespace, decoder);
findOperation.filter(document);
when(methodInvokeContext.allArguments()).thenReturn(new Object[] { findOperation });
when(methodInvokeContext.allArguments()).thenReturn(new Object[] {findOperation});
}
@Test
......@@ -112,7 +112,7 @@ public class MongoDBMethodInterceptorTest {
private void assertLogData(LogData logData) {
MatcherAssert.assertThat(logData.getFields().size(), is(4));
MatcherAssert.assertThat(logData.getFields().get("event"), CoreMatchers.<Object> is("error"));
MatcherAssert.assertThat(logData.getFields().get("event"), CoreMatchers.<Object>is("error"));
assertEquals(logData.getFields().get("error.kind"), RuntimeException.class.getName());
assertNull(logData.getFields().get("message"));
}
......
......@@ -43,7 +43,7 @@ public class MongoDBReadBindingInterceptorTest {
interceptor = new MongoDBReadBindingInterceptor();
ServerDescription serverDescription =
ServerDescription.builder().address(address).state(ServerConnectionState.CONNECTED).build();
ServerDescription.builder().address(address).state(ServerConnectionState.CONNECTED).build();
PowerMockito.when(connectionSource.getServerDescription()).thenReturn(serverDescription);
......
......@@ -43,7 +43,7 @@ public class MongoDBWriteBindingInterceptorTest {
interceptor = new MongoDBWriteBindingInterceptor();
ServerDescription serverDescription =
ServerDescription.builder().address(address).state(ServerConnectionState.CONNECTED).build();
ServerDescription.builder().address(address).state(ServerConnectionState.CONNECTED).build();
PowerMockito.when(connectionSource.getServerDescription()).thenReturn(serverDescription);
......
......@@ -72,14 +72,14 @@ public class MongoDBWriteMethodInterceptorTest {
DeleteRequest deleteRequest = new DeleteRequest(document);
requestList.add(deleteRequest);
MongoNamespace mongoNamespace = new MongoNamespace("test.user");
WriteConcern writeConcern = PowerMockito.mock(WriteConcern.class);
DeleteOperation deleteOperation = new DeleteOperation(mongoNamespace,false,writeConcern,requestList);
when(methodInvokeContext.allArguments()).thenReturn(new Object[] { deleteOperation });
DeleteOperation deleteOperation = new DeleteOperation(mongoNamespace, false, writeConcern, requestList);
when(methodInvokeContext.allArguments()).thenReturn(new Object[] {deleteOperation});
}
@Test
......@@ -99,7 +99,7 @@ public class MongoDBWriteMethodInterceptorTest {
}
private void assertRedisSpan(Span span) {
assertThat(span.getOperationName(), is("MongoDB/DeleteOperation"));
assertThat(span.getOperationName(), is("MongoDB/DeleteOperation"));
assertThat(Tags.PEER_HOST.get(span), is("127.0.0.1"));
assertThat(Tags.PEER_PORT.get(span), is(27017));
assertThat(Tags.COMPONENT.get(span), is("MongoDB"));
......@@ -129,7 +129,7 @@ public class MongoDBWriteMethodInterceptorTest {
private void assertLogData(LogData logData) {
MatcherAssert.assertThat(logData.getFields().size(), is(4));
MatcherAssert.assertThat(logData.getFields().get("event"), CoreMatchers.<Object> is("error"));
MatcherAssert.assertThat(logData.getFields().get("event"), CoreMatchers.<Object>is("error"));
assertEquals(logData.getFields().get("error.kind"), RuntimeException.class.getName());
assertNull(logData.getFields().get("message"));
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册