Skip to content

Commit 116c29a

Browse files
author
youjie_li
committed
support mongo db.instance tag,db.collection tag and AggregateOperation:code review
1 parent 2133699 commit 116c29a

File tree

18 files changed

+420
-43
lines changed

18 files changed

+420
-43
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Release Notes.
1010
* Fix intermittent ClassCircularityError by preloading ThreadLocalRandom since ByteBuddy 1.12.11
1111
* Add witness class/method for resteasy-server plugin(v3/v4/v6)
1212
* Add async-profiler feature for performance analysis
13+
* Support db.instance tag,db.collection tag and AggregateOperation span for mongodb plugin(3.x/4.x)
1314

1415
All issues and pull requests are [here](https://github.com/apache/skywalking/milestone/222?closed=1)
1516

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
package org.apache.skywalking.apm.plugin.mongodb.v3.define.readOperation;
20+
21+
import net.bytebuddy.description.method.MethodDescription;
22+
import net.bytebuddy.matcher.ElementMatcher;
23+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint;
24+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint;
25+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine;
26+
import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch;
27+
import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch;
28+
29+
import static net.bytebuddy.matcher.ElementMatchers.any;
30+
31+
public class GroupOperationInstrumentation extends ClassInstanceMethodsEnhancePluginDefine {
32+
33+
private static final String ENHANCE_CLASS = "com.mongodb.operation.GroupOperation";
34+
35+
private static final String INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.mongodb.v3.interceptor.operation.OperationNamespaceConstructInterceptor";
36+
37+
@Override
38+
protected ClassMatch enhanceClass() {
39+
return NameMatch.byName(ENHANCE_CLASS);
40+
}
41+
42+
@Override
43+
public ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
44+
return new ConstructorInterceptPoint[] {
45+
new ConstructorInterceptPoint() {
46+
@Override
47+
public ElementMatcher<MethodDescription> getConstructorMatcher() {
48+
return any();
49+
}
50+
51+
@Override
52+
public String getConstructorInterceptor() {
53+
return INTERCEPTOR_CLASS;
54+
}
55+
}
56+
};
57+
}
58+
59+
@Override
60+
public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
61+
return new InstanceMethodsInterceptPoint[0];
62+
}
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
package org.apache.skywalking.apm.plugin.mongodb.v3.define.readOperation;
20+
21+
import net.bytebuddy.description.method.MethodDescription;
22+
import net.bytebuddy.matcher.ElementMatcher;
23+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint;
24+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint;
25+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine;
26+
import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch;
27+
import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch;
28+
29+
import static net.bytebuddy.matcher.ElementMatchers.any;
30+
31+
public class ParallelCollectionScanOperationInstrumentation extends ClassInstanceMethodsEnhancePluginDefine {
32+
33+
private static final String ENHANCE_CLASS = "com.mongodb.operation.ParallelCollectionScanOperation";
34+
35+
private static final String INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.mongodb.v3.interceptor.operation.OperationNamespaceConstructInterceptor";
36+
37+
@Override
38+
protected ClassMatch enhanceClass() {
39+
return NameMatch.byName(ENHANCE_CLASS);
40+
}
41+
42+
@Override
43+
public ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
44+
return new ConstructorInterceptPoint[] {
45+
new ConstructorInterceptPoint() {
46+
@Override
47+
public ElementMatcher<MethodDescription> getConstructorMatcher() {
48+
return any();
49+
}
50+
51+
@Override
52+
public String getConstructorInterceptor() {
53+
return INTERCEPTOR_CLASS;
54+
}
55+
}
56+
};
57+
}
58+
59+
@Override
60+
public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
61+
return new InstanceMethodsInterceptPoint[0];
62+
}
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
package org.apache.skywalking.apm.plugin.mongodb.v3.define.readOperation;
20+
21+
import net.bytebuddy.description.method.MethodDescription;
22+
import net.bytebuddy.matcher.ElementMatcher;
23+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint;
24+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint;
25+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine;
26+
import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch;
27+
import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch;
28+
29+
import static net.bytebuddy.matcher.ElementMatchers.any;
30+
31+
public class UserExistsOperationInstrumentation extends ClassInstanceMethodsEnhancePluginDefine {
32+
33+
private static final String ENHANCE_CLASS = "com.mongodb.operation.UserExistsOperation";
34+
35+
private static final String INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.mongodb.v3.interceptor.operation.OperationDatabaseConstructInterceptor";
36+
37+
@Override
38+
protected ClassMatch enhanceClass() {
39+
return NameMatch.byName(ENHANCE_CLASS);
40+
}
41+
42+
@Override
43+
public ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
44+
return new ConstructorInterceptPoint[] {
45+
new ConstructorInterceptPoint() {
46+
@Override
47+
public ElementMatcher<MethodDescription> getConstructorMatcher() {
48+
return any();
49+
}
50+
51+
@Override
52+
public String getConstructorInterceptor() {
53+
return INTERCEPTOR_CLASS;
54+
}
55+
}
56+
};
57+
}
58+
59+
@Override
60+
public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
61+
return new InstanceMethodsInterceptPoint[0];
62+
}
63+
}

apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/operation/OperationDatabaseConstructInterceptor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@
2020

2121
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
2222
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceConstructorInterceptor;
23+
import org.apache.skywalking.apm.plugin.mongodb.v3.support.MongoNamespaceInfo;
2324

2425
public class OperationDatabaseConstructInterceptor implements InstanceConstructorInterceptor {
2526

2627
@Override
2728
public void onConstruct(EnhancedInstance objInst, Object[] allArguments) {
2829
String databaseName = (String) allArguments[0];
29-
objInst.setSkyWalkingDynamicField(databaseName);
30+
objInst.setSkyWalkingDynamicField(new MongoNamespaceInfo(databaseName));
3031
}
3132

3233
}

apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/operation/OperationNamespaceConstructInterceptor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@
2121
import com.mongodb.MongoNamespace;
2222
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
2323
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceConstructorInterceptor;
24+
import org.apache.skywalking.apm.plugin.mongodb.v3.support.MongoNamespaceInfo;
2425

2526
public class OperationNamespaceConstructInterceptor implements InstanceConstructorInterceptor {
2627

2728
@Override
2829
public void onConstruct(EnhancedInstance objInst, Object[] allArguments) {
2930
MongoNamespace mongoNamespace = (MongoNamespace) allArguments[0];
30-
objInst.setSkyWalkingDynamicField(mongoNamespace);
31+
objInst.setSkyWalkingDynamicField(new MongoNamespaceInfo(mongoNamespace));
3132
}
3233

3334
}

apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/interceptor/operation/WrappedMapReduceReadOperationInterceptor.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,8 @@ public class WrappedMapReduceReadOperationInterceptor implements InstanceConstru
2525

2626
@Override
2727
public void onConstruct(EnhancedInstance objInst, Object[] allArguments) {
28-
if (allArguments[0] instanceof EnhancedInstance) {
2928
EnhancedInstance enhancedInstance = (EnhancedInstance) allArguments[0];
3029
objInst.setSkyWalkingDynamicField(enhancedInstance.getSkyWalkingDynamicField());
31-
}
3230
}
3331

3432
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
package org.apache.skywalking.apm.plugin.mongodb.v3.support;
20+
21+
import com.mongodb.MongoNamespace;
22+
import com.mongodb.annotations.Immutable;
23+
import org.apache.skywalking.apm.util.StringUtil;
24+
25+
@Immutable
26+
public class MongoNamespaceInfo {
27+
28+
private final String databaseName;
29+
private final String collectionName;
30+
31+
public MongoNamespaceInfo(String databaseName) {
32+
this(databaseName, null);
33+
}
34+
35+
public MongoNamespaceInfo(MongoNamespace mongoNamespace) {
36+
this(mongoNamespace.getDatabaseName(), mongoNamespace.getCollectionName());
37+
}
38+
39+
public MongoNamespaceInfo(String databaseName, String collectionName) {
40+
this.databaseName = databaseName;
41+
this.collectionName = collectionName;
42+
}
43+
44+
public String getDatabaseName() {
45+
return databaseName;
46+
}
47+
48+
public String getCollectionName() {
49+
return collectionName;
50+
}
51+
52+
public boolean equals(Object o) {
53+
if (this == o) {
54+
return true;
55+
} else if (o != null && this.getClass() == o.getClass()) {
56+
MongoNamespaceInfo that = (MongoNamespaceInfo) o;
57+
if (!equals(this.databaseName, that.databaseName)) {
58+
return false;
59+
} else {
60+
return equals(this.collectionName, that.collectionName);
61+
}
62+
} else {
63+
return false;
64+
}
65+
}
66+
67+
private boolean equals(String src, String tgt) {
68+
if (src == null && tgt == null) {
69+
return true;
70+
}
71+
if (src == null && tgt != null) {
72+
return false;
73+
}
74+
if (src != null && tgt == null) {
75+
return false;
76+
}
77+
return src.equals(tgt);
78+
}
79+
80+
public String toString() {
81+
if (StringUtil.isNotBlank(collectionName)) {
82+
return databaseName + '.' + collectionName;
83+
} else {
84+
return databaseName;
85+
}
86+
}
87+
88+
public int hashCode() {
89+
int result = this.databaseName.hashCode();
90+
result = 31 * result + this.collectionName.hashCode();
91+
return result;
92+
}
93+
94+
}

apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/mongodb/v3/support/MongoSpanHelper.java

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
package org.apache.skywalking.apm.plugin.mongodb.v3.support;
2020

21-
import com.mongodb.MongoNamespace;
2221
import lombok.SneakyThrows;
2322
import org.apache.skywalking.apm.agent.core.context.ContextCarrier;
2423
import org.apache.skywalking.apm.agent.core.context.ContextManager;
@@ -47,15 +46,14 @@ public static void createExitSpan(String executeMethod, String remotePeer, Objec
4746
SpanLayer.asDB(span);
4847

4948
if (operation instanceof EnhancedInstance) {
50-
Object dynamicFieldValue = ((EnhancedInstance) operation).getSkyWalkingDynamicField();
51-
if (dynamicFieldValue != null && dynamicFieldValue instanceof MongoNamespace) {
52-
MongoNamespace namespace = (MongoNamespace) dynamicFieldValue;
53-
Tags.DB_INSTANCE.set(span, namespace.getDatabaseName());
54-
if (StringUtil.isNotEmpty(namespace.getCollectionName())) {
55-
span.tag(DB_COLLECTION_TAG, namespace.getCollectionName());
49+
MongoNamespaceInfo mongoNamespaceInfo = (MongoNamespaceInfo) ((EnhancedInstance) operation).getSkyWalkingDynamicField();
50+
if (mongoNamespaceInfo != null) {
51+
if (StringUtil.isNotEmpty(mongoNamespaceInfo.getDatabaseName())) {
52+
Tags.DB_INSTANCE.set(span, mongoNamespaceInfo.getDatabaseName());
53+
}
54+
if (StringUtil.isNotEmpty(mongoNamespaceInfo.getCollectionName())) {
55+
span.tag(DB_COLLECTION_TAG, mongoNamespaceInfo.getCollectionName());
5656
}
57-
} else if (dynamicFieldValue != null && dynamicFieldValue instanceof String) {
58-
Tags.DB_INSTANCE.set(span, (String) dynamicFieldValue);
5957
}
6058
}
6159

@@ -64,10 +62,4 @@ public static void createExitSpan(String executeMethod, String remotePeer, Objec
6462
}
6563
}
6664

67-
private static void extractTagsFromNamespace(AbstractSpan span, MongoNamespace namespace) {
68-
Tags.DB_INSTANCE.set(span, namespace.getDatabaseName());
69-
if (StringUtil.isNotEmpty(namespace.getCollectionName())) {
70-
span.tag(DB_COLLECTION_TAG, namespace.getCollectionName());
71-
}
72-
}
7365
}

apm-sniffer/apm-sdk-plugin/mongodb-3.x-plugin/src/main/resources/skywalking-plugin.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ mongodb-3.x=org.apache.skywalking.apm.plugin.mongodb.v3.define.readOperation.Lis
3939
mongodb-3.x=org.apache.skywalking.apm.plugin.mongodb.v3.define.readOperation.ListIndexesOperationInstrumentation
4040
mongodb-3.x=org.apache.skywalking.apm.plugin.mongodb.v3.define.readOperation.MapReduceWithInlineResultsOperationInstrumentation
4141
mongodb-3.x=org.apache.skywalking.apm.plugin.mongodb.v3.define.readOperation.WrappedMapReduceReadOperationInstrumentation
42+
mongodb-3.x=org.apache.skywalking.apm.plugin.mongodb.v3.define.readOperation.GroupOperationInstrumentation
43+
mongodb-3.x=org.apache.skywalking.apm.plugin.mongodb.v3.define.readOperation.ParallelCollectionScanOperationInstrumentation
44+
mongodb-3.x=org.apache.skywalking.apm.plugin.mongodb.v3.define.readOperation.UserExistsOperationInstrumentation
4245
# writeOperation
4346
mongodb-3.x=org.apache.skywalking.apm.plugin.mongodb.v3.define.writeOperation.AggregateToCollectionOperationInstrumentation
4447
mongodb-3.x=org.apache.skywalking.apm.plugin.mongodb.v3.define.writeOperation.BaseFindAndModifyOperationInstrumentation

0 commit comments

Comments
 (0)