Skip to content

Commit b181999

Browse files
committed
Add db.statement tag with DSL tracing support
- Add ElasticsearchPluginConfig with TRACE_DSL and ELASTICSEARCH_DSL_LENGTH_THRESHOLD (same config keys as existing elasticsearch plugins: plugin.elasticsearch.trace_dsl) - Capture request body via RequestBase.toString() which serializes to JSON, same approach as existing ES 6.x/7.x plugins - Guarded by TRACE_DSL config (default false), with length threshold - Update all three scenario expectedData.yaml files
1 parent 5a7c89b commit b181999

6 files changed

Lines changed: 77 additions & 1 deletion

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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.elasticsearch.java;
20+
21+
import org.apache.skywalking.apm.agent.core.boot.PluginConfig;
22+
23+
public class ElasticsearchPluginConfig {
24+
public static class Plugin {
25+
@PluginConfig(root = ElasticsearchPluginConfig.class)
26+
public static class Elasticsearch {
27+
/**
28+
* If true, trace all the DSL(Domain Specific Language) in ElasticSearch access, default is false
29+
*/
30+
public static boolean TRACE_DSL = false;
31+
32+
public static int ELASTICSEARCH_DSL_LENGTH_THRESHOLD = 1024;
33+
}
34+
}
35+
}

apm-sniffer/apm-sdk-plugin/elasticsearch-java-plugin/src/main/java/org/apache/skywalking/apm/plugin/elasticsearch/java/interceptor/TransportPerformRequestInterceptor.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
2828
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
2929
import org.apache.skywalking.apm.network.trace.component.ComponentsDefine;
30+
import org.apache.skywalking.apm.plugin.elasticsearch.java.ElasticsearchPluginConfig;
3031

3132
import java.lang.reflect.Method;
3233

@@ -57,11 +58,22 @@ public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allAr
5758
Tags.DB_TYPE.set(span, DB_TYPE);
5859
SpanLayer.asDB(span);
5960

60-
String requestUrl = endpoint.requestUrl(allArguments[0]);
61+
Object request = allArguments[0];
62+
String requestUrl = endpoint.requestUrl(request);
6163
String index = extractIndex(requestUrl);
6264
if (index != null) {
6365
span.tag(Tags.ofKey("db.instance"), index);
6466
}
67+
if (ElasticsearchPluginConfig.Plugin.Elasticsearch.TRACE_DSL) {
68+
String dsl = request.toString();
69+
if (dsl != null && !dsl.isEmpty()) {
70+
int maxLen = ElasticsearchPluginConfig.Plugin.Elasticsearch.ELASTICSEARCH_DSL_LENGTH_THRESHOLD;
71+
if (maxLen > 0 && dsl.length() > maxLen) {
72+
dsl = dsl.substring(0, maxLen) + "...";
73+
}
74+
Tags.DB_STATEMENT.set(span, dsl);
75+
}
76+
}
6577
}
6678

6779
@Override

apm-sniffer/apm-sdk-plugin/elasticsearch-java-plugin/src/main/java/org/apache/skywalking/apm/plugin/elasticsearch/java/interceptor/TransportPerformRequestV1Interceptor.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
2929
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
3030
import org.apache.skywalking.apm.network.trace.component.ComponentsDefine;
31+
import org.apache.skywalking.apm.plugin.elasticsearch.java.ElasticsearchPluginConfig;
3132

3233
import java.lang.reflect.Method;
3334

@@ -75,6 +76,16 @@ public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allAr
7576
} catch (Exception e) {
7677
LOGGER.warn("Failed to extract index from request URL", e);
7778
}
79+
if (ElasticsearchPluginConfig.Plugin.Elasticsearch.TRACE_DSL) {
80+
String dsl = request.toString();
81+
if (dsl != null && !dsl.isEmpty()) {
82+
int maxLen = ElasticsearchPluginConfig.Plugin.Elasticsearch.ELASTICSEARCH_DSL_LENGTH_THRESHOLD;
83+
if (maxLen > 0 && dsl.length() > maxLen) {
84+
dsl = dsl.substring(0, maxLen) + "...";
85+
}
86+
Tags.DB_STATEMENT.set(span, dsl);
87+
}
88+
}
7889
}
7990

8091
@Override

test/plugin/scenarios/elasticsearch-java-7.15.x-scenario/config/expectedData.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ segmentItems:
3333
tags:
3434
- {key: db.type, value: Elasticsearch}
3535
- {key: db.instance, value: test-index}
36+
- {key: db.statement, value: not null}
3637
- operationName: Elasticsearch/index
3738
parentSpanId: 0
3839
spanId: 2
@@ -47,6 +48,7 @@ segmentItems:
4748
tags:
4849
- {key: db.type, value: Elasticsearch}
4950
- {key: db.instance, value: test-index}
51+
- {key: db.statement, value: not null}
5052
- operationName: Elasticsearch/get
5153
parentSpanId: 0
5254
spanId: 3
@@ -61,6 +63,7 @@ segmentItems:
6163
tags:
6264
- {key: db.type, value: Elasticsearch}
6365
- {key: db.instance, value: test-index}
66+
- {key: db.statement, value: not null}
6467
- operationName: Elasticsearch/search
6568
parentSpanId: 0
6669
spanId: 4
@@ -75,6 +78,7 @@ segmentItems:
7578
tags:
7679
- {key: db.type, value: Elasticsearch}
7780
- {key: db.instance, value: test-index}
81+
- {key: db.statement, value: not null}
7882
- operationName: Elasticsearch/delete
7983
parentSpanId: 0
8084
spanId: 5
@@ -89,6 +93,7 @@ segmentItems:
8993
tags:
9094
- {key: db.type, value: Elasticsearch}
9195
- {key: db.instance, value: test-index}
96+
- {key: db.statement, value: not null}
9297
- operationName: Elasticsearch/delete_index
9398
parentSpanId: 0
9499
spanId: 6
@@ -103,6 +108,7 @@ segmentItems:
103108
tags:
104109
- {key: db.type, value: Elasticsearch}
105110
- {key: db.instance, value: test-index}
111+
- {key: db.statement, value: not null}
106112
- operationName: GET:/elasticsearch-java-case/case/elasticsearch
107113
parentSpanId: -1
108114
spanId: 0

test/plugin/scenarios/elasticsearch-java-9.x-scenario/config/expectedData.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ segmentItems:
3333
tags:
3434
- {key: db.type, value: Elasticsearch}
3535
- {key: db.instance, value: test-index}
36+
- {key: db.statement, value: not null}
3637
- operationName: Elasticsearch/es/index
3738
parentSpanId: 0
3839
spanId: 2
@@ -47,6 +48,7 @@ segmentItems:
4748
tags:
4849
- {key: db.type, value: Elasticsearch}
4950
- {key: db.instance, value: test-index}
51+
- {key: db.statement, value: not null}
5052
- operationName: Elasticsearch/es/get
5153
parentSpanId: 0
5254
spanId: 3
@@ -61,6 +63,7 @@ segmentItems:
6163
tags:
6264
- {key: db.type, value: Elasticsearch}
6365
- {key: db.instance, value: test-index}
66+
- {key: db.statement, value: not null}
6467
- operationName: Elasticsearch/es/search
6568
parentSpanId: 0
6669
spanId: 4
@@ -75,6 +78,7 @@ segmentItems:
7578
tags:
7679
- {key: db.type, value: Elasticsearch}
7780
- {key: db.instance, value: test-index}
81+
- {key: db.statement, value: not null}
7882
- operationName: Elasticsearch/es/delete
7983
parentSpanId: 0
8084
spanId: 5
@@ -89,6 +93,7 @@ segmentItems:
8993
tags:
9094
- {key: db.type, value: Elasticsearch}
9195
- {key: db.instance, value: test-index}
96+
- {key: db.statement, value: not null}
9297
- operationName: Elasticsearch/es/indices.delete
9398
parentSpanId: 0
9499
spanId: 6
@@ -103,6 +108,7 @@ segmentItems:
103108
tags:
104109
- {key: db.type, value: Elasticsearch}
105110
- {key: db.instance, value: test-index}
111+
- {key: db.statement, value: not null}
106112
- operationName: GET:/elasticsearch-java-case/case/elasticsearch
107113
parentSpanId: -1
108114
spanId: 0

test/plugin/scenarios/elasticsearch-java-scenario/config/expectedData.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ segmentItems:
3333
tags:
3434
- {key: db.type, value: Elasticsearch}
3535
- {key: db.instance, value: test-index}
36+
- {key: db.statement, value: not null}
3637
- operationName: Elasticsearch/es/index
3738
parentSpanId: 0
3839
spanId: 2
@@ -47,6 +48,7 @@ segmentItems:
4748
tags:
4849
- {key: db.type, value: Elasticsearch}
4950
- {key: db.instance, value: test-index}
51+
- {key: db.statement, value: not null}
5052
- operationName: Elasticsearch/es/get
5153
parentSpanId: 0
5254
spanId: 3
@@ -61,6 +63,7 @@ segmentItems:
6163
tags:
6264
- {key: db.type, value: Elasticsearch}
6365
- {key: db.instance, value: test-index}
66+
- {key: db.statement, value: not null}
6467
- operationName: Elasticsearch/es/search
6568
parentSpanId: 0
6669
spanId: 4
@@ -75,6 +78,7 @@ segmentItems:
7578
tags:
7679
- {key: db.type, value: Elasticsearch}
7780
- {key: db.instance, value: test-index}
81+
- {key: db.statement, value: not null}
7882
- operationName: Elasticsearch/es/delete
7983
parentSpanId: 0
8084
spanId: 5
@@ -89,6 +93,7 @@ segmentItems:
8993
tags:
9094
- {key: db.type, value: Elasticsearch}
9195
- {key: db.instance, value: test-index}
96+
- {key: db.statement, value: not null}
9297
- operationName: Elasticsearch/es/indices.delete
9398
parentSpanId: 0
9499
spanId: 6
@@ -103,6 +108,7 @@ segmentItems:
103108
tags:
104109
- {key: db.type, value: Elasticsearch}
105110
- {key: db.instance, value: test-index}
111+
- {key: db.statement, value: not null}
106112
- operationName: GET:/elasticsearch-java-case/case/elasticsearch
107113
parentSpanId: -1
108114
spanId: 0

0 commit comments

Comments
 (0)