Skip to content

Commit 6e642df

Browse files
Merge branch 'feature/582' of https://github.com/OpenSkywalking/skywalking into feature/582
2 parents 4d415a8 + 584e9b8 commit 6e642df

File tree

10 files changed

+654
-2
lines changed

10 files changed

+654
-2
lines changed

apm-sniffer/apm-agent-core/src/main/java/org/skywalking/apm/agent/core/conf/Config.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public static class Collector {
7676
/**
7777
* Collector service discovery REST service name
7878
*/
79-
public static String DISCOVERY_SERVICE_NAME = "/agentstream/grpc";
79+
public static String DISCOVERY_SERVICE_NAME = "/agent/gRPC";
8080
}
8181

8282
public static class Jvm {
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ /*
4+
~ * Copyright 2017, OpenSkywalking Organization All rights reserved.
5+
~ *
6+
~ * Licensed under the Apache License, Version 2.0 (the "License");
7+
~ * you may not use this file except in compliance with the License.
8+
~ * You may obtain a copy of the License at
9+
~ *
10+
~ * http://www.apache.org/licenses/LICENSE-2.0
11+
~ *
12+
~ * Unless required by applicable law or agreed to in writing, software
13+
~ * distributed under the License is distributed on an "AS IS" BASIS,
14+
~ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
~ * See the License for the specific language governing permissions and
16+
~ * limitations under the License.
17+
~ *
18+
~ * Project repository: https://github.com/OpenSkywalking/skywalking
19+
~ */
20+
-->
21+
22+
<project xmlns="http://maven.apache.org/POM/4.0.0"
23+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
24+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
25+
<parent>
26+
<artifactId>apm-sdk-plugin</artifactId>
27+
<groupId>org.skywalking</groupId>
28+
<version>3.2.5-2017</version>
29+
</parent>
30+
<modelVersion>4.0.0</modelVersion>
31+
32+
<artifactId>apm-mongodb-2.x-plugin</artifactId>
33+
<packaging>jar</packaging>
34+
35+
<properties>
36+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
37+
</properties>
38+
39+
<dependencies>
40+
<dependency>
41+
<groupId>org.mongodb</groupId>
42+
<artifactId>mongo-java-driver</artifactId>
43+
<version>2.14.2</version>
44+
<scope>provided</scope>
45+
</dependency>
46+
<dependency>
47+
<groupId>org.mongodb</groupId>
48+
<artifactId>bson</artifactId>
49+
<version>2.14.2</version>
50+
<scope>provided</scope>
51+
</dependency>
52+
</dependencies>
53+
<build>
54+
<plugins>
55+
<plugin>
56+
<!-- 源码插件 -->
57+
<groupId>org.apache.maven.plugins</groupId>
58+
<artifactId>maven-source-plugin</artifactId>
59+
<!-- 发布时自动将源码同时发布的配置 -->
60+
<executions>
61+
<execution>
62+
<id>attach-sources</id>
63+
<goals>
64+
<goal>jar</goal>
65+
</goals>
66+
</execution>
67+
</executions>
68+
</plugin>
69+
</plugins>
70+
</build>
71+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright 2017, OpenSkywalking Organization All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* Project repository: https://github.com/OpenSkywalking/skywalking
17+
*/
18+
19+
package org.skywalking.apm.plugin.mongodb.v2;
20+
21+
import com.mongodb.AggregationOutput;
22+
import com.mongodb.CommandResult;
23+
import com.mongodb.DB;
24+
import com.mongodb.ServerAddress;
25+
import com.mongodb.WriteResult;
26+
import java.lang.reflect.Method;
27+
import java.util.List;
28+
import org.skywalking.apm.agent.core.context.ContextCarrier;
29+
import org.skywalking.apm.agent.core.context.ContextManager;
30+
import org.skywalking.apm.agent.core.context.tag.Tags;
31+
import org.skywalking.apm.agent.core.context.trace.AbstractSpan;
32+
import org.skywalking.apm.agent.core.context.trace.SpanLayer;
33+
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
34+
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceConstructorInterceptor;
35+
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
36+
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
37+
import org.skywalking.apm.network.trace.component.ComponentsDefine;
38+
39+
/**
40+
* {@link MongoDBCollectionMethodInterceptor} intercepts constructor of {@link com.mongodb.DBCollection}or {@link
41+
* com.mongodb.DBCollectionImpl} recording the ServerAddress and creating the exit span.
42+
*
43+
* @author liyuntao
44+
*/
45+
46+
public class MongoDBCollectionMethodInterceptor implements InstanceMethodsAroundInterceptor, InstanceConstructorInterceptor {
47+
48+
private static final String DB_TYPE = "MongoDB";
49+
50+
private static final String MONGO_DB_OP_PREFIX = "MongoDB/";
51+
52+
@Override public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments,
53+
Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {
54+
55+
String remotePeer = (String)objInst.getSkyWalkingDynamicField();
56+
String opertaion = method.getName();
57+
AbstractSpan span = ContextManager.createExitSpan(MONGO_DB_OP_PREFIX + opertaion, new ContextCarrier(), remotePeer);
58+
span.setComponent(ComponentsDefine.MONGODB);
59+
Tags.DB_TYPE.set(span, DB_TYPE);
60+
SpanLayer.asDB(span);
61+
62+
}
63+
64+
@Override public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments,
65+
Class<?>[] argumentsTypes, Object ret) throws Throwable {
66+
AbstractSpan activeSpan = ContextManager.activeSpan();
67+
CommandResult cresult = null;
68+
if (ret instanceof WriteResult) {
69+
WriteResult wresult = (WriteResult)ret;
70+
cresult = wresult.getCachedLastError();
71+
} else if (ret instanceof AggregationOutput) {
72+
AggregationOutput aresult = (AggregationOutput)ret;
73+
cresult = aresult.getCommandResult();
74+
}
75+
if (null != cresult && !cresult.ok()) {
76+
activeSpan.log(cresult.getException());
77+
}
78+
ContextManager.stopSpan();
79+
return ret;
80+
}
81+
82+
@Override public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
83+
Class<?>[] argumentsTypes, Throwable t) {
84+
AbstractSpan activeSpan = ContextManager.activeSpan();
85+
activeSpan.errorOccurred();
86+
activeSpan.log(t);
87+
}
88+
89+
@Override
90+
public void onConstruct(EnhancedInstance objInst, Object[] allArguments) {
91+
List<ServerAddress> servers = null;
92+
DB db = (DB)allArguments[0];
93+
servers = db.getMongo().getAllAddress();
94+
StringBuilder peers = new StringBuilder();
95+
for (ServerAddress address : servers) {
96+
peers.append(address.getHost() + ":" + address.getPort() + ";");
97+
}
98+
99+
objInst.setSkyWalkingDynamicField(peers.subSequence(0, peers.length() - 1).toString());
100+
}
101+
102+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2017, OpenSkywalking Organization All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* Project repository: https://github.com/OpenSkywalking/skywalking
17+
*/
18+
19+
package org.skywalking.apm.plugin.mongodb.v2.define;
20+
21+
import org.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint;
22+
23+
/**
24+
* @auther liyuntao
25+
*/
26+
public abstract class InterceptPoint implements InstanceMethodsInterceptPoint {
27+
private static final String MONGDB_METHOD_INTERCET_CLASS = "org.skywalking.apm.plugin.mongodb.v2.MongoDBCollectionMethodInterceptor";
28+
29+
@Override
30+
public String getMethodsInterceptor() {
31+
return MONGDB_METHOD_INTERCET_CLASS;
32+
}
33+
34+
@Override
35+
public boolean isOverrideArgs() {
36+
return false;
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/*
2+
* Copyright 2017, OpenSkywalking Organization All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* Project repository: https://github.com/OpenSkywalking/skywalking
17+
*/
18+
19+
package org.skywalking.apm.plugin.mongodb.v2.define;
20+
21+
import net.bytebuddy.description.method.MethodDescription;
22+
import net.bytebuddy.matcher.ElementMatcher;
23+
import org.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint;
24+
import org.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint;
25+
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine;
26+
import org.skywalking.apm.agent.core.plugin.match.ClassMatch;
27+
28+
import static net.bytebuddy.matcher.ElementMatchers.any;
29+
import static net.bytebuddy.matcher.ElementMatchers.named;
30+
import static net.bytebuddy.matcher.ElementMatchers.takesArguments;
31+
import static org.skywalking.apm.agent.core.plugin.match.NameMatch.byName;
32+
33+
/**
34+
* {@link MongoDBCollectionImplInstrumentation} define that the MongoDB Java Driver 2.13.x-2.14.x plugin intercepts the
35+
* following methods in the {@link com.mongodb.DBCollectionImpl}class:
36+
* 1. find <br/>
37+
* 2. insert <br/>
38+
* 3. insertImpl <br/>
39+
* 4. update <br/>
40+
* 5. updateImpl <br/>
41+
* 6. remove <br/>
42+
* 7. createIndex <br/>
43+
*
44+
* @author liyuntao
45+
*/
46+
public class MongoDBCollectionImplInstrumentation extends ClassInstanceMethodsEnhancePluginDefine {
47+
48+
private static final String ENHANCE_CLASS = "com.mongodb.DBCollectionImpl";
49+
50+
private static final String MONGDB_METHOD_INTERCET_CLASS = "org.skywalking.apm.plugin.mongodb.v2.MongoDBCollectionMethodInterceptor";
51+
52+
@Override
53+
protected ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
54+
return new ConstructorInterceptPoint[] {
55+
new ConstructorInterceptPoint() {
56+
@Override
57+
public ElementMatcher<MethodDescription> getConstructorMatcher() {
58+
return any();
59+
}
60+
61+
@Override
62+
public String getConstructorInterceptor() {
63+
return MONGDB_METHOD_INTERCET_CLASS;
64+
}
65+
}
66+
};
67+
}
68+
69+
@Override
70+
protected InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
71+
return new InstanceMethodsInterceptPoint[] {
72+
73+
new InterceptPoint() {
74+
@Override
75+
public ElementMatcher<MethodDescription> getMethodsMatcher() {
76+
return named("find").and(takesArguments(9));
77+
}
78+
79+
},
80+
new InterceptPoint() {
81+
@Override
82+
public ElementMatcher<MethodDescription> getMethodsMatcher() {
83+
return named("insert").and(takesArguments(4));
84+
}
85+
86+
},
87+
new InterceptPoint() {
88+
@Override
89+
public ElementMatcher<MethodDescription> getMethodsMatcher() {
90+
return named("insertImpl");
91+
}
92+
93+
},
94+
new InterceptPoint() {
95+
@Override
96+
public ElementMatcher<MethodDescription> getMethodsMatcher() {
97+
return named("update");
98+
}
99+
},
100+
new InterceptPoint() {
101+
@Override
102+
public ElementMatcher<MethodDescription> getMethodsMatcher() {
103+
return named("updateImpl");
104+
}
105+
106+
},
107+
new InterceptPoint() {
108+
@Override
109+
public ElementMatcher<MethodDescription> getMethodsMatcher() {
110+
return named("remove").and(takesArguments(4));
111+
}
112+
113+
},
114+
new InterceptPoint() {
115+
@Override
116+
public ElementMatcher<MethodDescription> getMethodsMatcher() {
117+
return named("createIndex");
118+
}
119+
120+
},
121+
122+
};
123+
}
124+
125+
@Override
126+
protected ClassMatch enhanceClass() {
127+
return byName(ENHANCE_CLASS);
128+
}
129+
130+
@Override
131+
protected String[] witnessClasses() {
132+
/**
133+
* @see {@link com.mongodb.tools.ConnectionPoolStat}
134+
*/
135+
return new String[] {
136+
"com.mongodb.tools.ConnectionPoolStat"
137+
};
138+
}
139+
140+
}

0 commit comments

Comments
 (0)