Skip to content

Commit 5a7c89b

Browse files
committed
Add db.instance tag with index name to align with existing ES plugins
- Extract index name from Endpoint.requestUrl() and set as db.instance tag - Aligns with existing elasticsearch-5.x/6.x/7.x plugins that capture db.type, db.instance, and db.statement tags - Update all three scenario expectedData.yaml files
1 parent 5ee6f14 commit 5a7c89b

5 files changed

Lines changed: 64 additions & 1 deletion

File tree

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@ public class TransportPerformRequestInterceptor implements InstanceMethodsAround
4141
private static final String DB_TYPE = "Elasticsearch";
4242

4343
@Override
44+
@SuppressWarnings("unchecked")
4445
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments,
4546
Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {
46-
Endpoint<?, ?, ?> endpoint = (Endpoint<?, ?, ?>) allArguments[1];
47+
Endpoint endpoint = (Endpoint) allArguments[1];
4748
String operationName = "Elasticsearch/" + endpoint.id();
4849

4950
String peers = (String) objInst.getSkyWalkingDynamicField();
@@ -55,6 +56,12 @@ public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allAr
5556
span.setComponent(ComponentsDefine.REST_HIGH_LEVEL_CLIENT);
5657
Tags.DB_TYPE.set(span, DB_TYPE);
5758
SpanLayer.asDB(span);
59+
60+
String requestUrl = endpoint.requestUrl(allArguments[0]);
61+
String index = extractIndex(requestUrl);
62+
if (index != null) {
63+
span.tag(Tags.ofKey("db.instance"), index);
64+
}
5865
}
5966

6067
@Override
@@ -70,4 +77,26 @@ public void handleMethodException(EnhancedInstance objInst, Method method, Objec
7077
ContextManager.activeSpan().log(t);
7178
ContextManager.activeSpan().errorOccurred();
7279
}
80+
81+
/**
82+
* Extract index name from request URL.
83+
* E.g., "/test-index/_doc/1" → "test-index", "/_bulk" → null
84+
*/
85+
static String extractIndex(String requestUrl) {
86+
if (requestUrl == null || requestUrl.isEmpty()) {
87+
return null;
88+
}
89+
// Remove leading slash
90+
String path = requestUrl.startsWith("/") ? requestUrl.substring(1) : requestUrl;
91+
if (path.isEmpty()) {
92+
return null;
93+
}
94+
// First segment before '/' or '_' prefix means no index
95+
int slashIdx = path.indexOf('/');
96+
String firstSegment = slashIdx > 0 ? path.substring(0, slashIdx) : path;
97+
if (firstSegment.startsWith("_")) {
98+
return null;
99+
}
100+
return firstSegment;
101+
}
73102
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import org.apache.skywalking.apm.agent.core.context.tag.Tags;
2323
import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan;
2424
import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer;
25+
import org.apache.skywalking.apm.agent.core.logging.api.ILog;
26+
import org.apache.skywalking.apm.agent.core.logging.api.LogManager;
2527
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
2628
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
2729
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
@@ -41,13 +43,15 @@
4143
*/
4244
public class TransportPerformRequestV1Interceptor implements InstanceMethodsAroundInterceptor {
4345

46+
private static final ILog LOGGER = LogManager.getLogger(TransportPerformRequestV1Interceptor.class);
4447
private static final String DB_TYPE = "Elasticsearch";
4548
private static final String REQUEST_SUFFIX = "Request";
4649

4750
@Override
4851
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments,
4952
Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {
5053
Object request = allArguments[0];
54+
Object endpoint = allArguments[1];
5155
String operationName = "Elasticsearch/" + deriveOperationName(request);
5256

5357
String peers = (String) objInst.getSkyWalkingDynamicField();
@@ -59,6 +63,18 @@ public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allAr
5963
span.setComponent(ComponentsDefine.REST_HIGH_LEVEL_CLIENT);
6064
Tags.DB_TYPE.set(span, DB_TYPE);
6165
SpanLayer.asDB(span);
66+
67+
try {
68+
Method requestUrlMethod = endpoint.getClass().getMethod("requestUrl", Object.class);
69+
requestUrlMethod.setAccessible(true);
70+
String requestUrl = (String) requestUrlMethod.invoke(endpoint, request);
71+
String index = TransportPerformRequestInterceptor.extractIndex(requestUrl);
72+
if (index != null) {
73+
span.tag(Tags.ofKey("db.instance"), index);
74+
}
75+
} catch (Exception e) {
76+
LOGGER.warn("Failed to extract index from request URL", e);
77+
}
6278
}
6379

6480
@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
@@ -32,6 +32,7 @@ segmentItems:
3232
skipAnalysis: false
3333
tags:
3434
- {key: db.type, value: Elasticsearch}
35+
- {key: db.instance, value: test-index}
3536
- operationName: Elasticsearch/index
3637
parentSpanId: 0
3738
spanId: 2
@@ -45,6 +46,7 @@ segmentItems:
4546
skipAnalysis: false
4647
tags:
4748
- {key: db.type, value: Elasticsearch}
49+
- {key: db.instance, value: test-index}
4850
- operationName: Elasticsearch/get
4951
parentSpanId: 0
5052
spanId: 3
@@ -58,6 +60,7 @@ segmentItems:
5860
skipAnalysis: false
5961
tags:
6062
- {key: db.type, value: Elasticsearch}
63+
- {key: db.instance, value: test-index}
6164
- operationName: Elasticsearch/search
6265
parentSpanId: 0
6366
spanId: 4
@@ -71,6 +74,7 @@ segmentItems:
7174
skipAnalysis: false
7275
tags:
7376
- {key: db.type, value: Elasticsearch}
77+
- {key: db.instance, value: test-index}
7478
- operationName: Elasticsearch/delete
7579
parentSpanId: 0
7680
spanId: 5
@@ -84,6 +88,7 @@ segmentItems:
8488
skipAnalysis: false
8589
tags:
8690
- {key: db.type, value: Elasticsearch}
91+
- {key: db.instance, value: test-index}
8792
- operationName: Elasticsearch/delete_index
8893
parentSpanId: 0
8994
spanId: 6
@@ -97,6 +102,7 @@ segmentItems:
97102
skipAnalysis: false
98103
tags:
99104
- {key: db.type, value: Elasticsearch}
105+
- {key: db.instance, value: test-index}
100106
- operationName: GET:/elasticsearch-java-case/case/elasticsearch
101107
parentSpanId: -1
102108
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
@@ -32,6 +32,7 @@ segmentItems:
3232
skipAnalysis: false
3333
tags:
3434
- {key: db.type, value: Elasticsearch}
35+
- {key: db.instance, value: test-index}
3536
- operationName: Elasticsearch/es/index
3637
parentSpanId: 0
3738
spanId: 2
@@ -45,6 +46,7 @@ segmentItems:
4546
skipAnalysis: false
4647
tags:
4748
- {key: db.type, value: Elasticsearch}
49+
- {key: db.instance, value: test-index}
4850
- operationName: Elasticsearch/es/get
4951
parentSpanId: 0
5052
spanId: 3
@@ -58,6 +60,7 @@ segmentItems:
5860
skipAnalysis: false
5961
tags:
6062
- {key: db.type, value: Elasticsearch}
63+
- {key: db.instance, value: test-index}
6164
- operationName: Elasticsearch/es/search
6265
parentSpanId: 0
6366
spanId: 4
@@ -71,6 +74,7 @@ segmentItems:
7174
skipAnalysis: false
7275
tags:
7376
- {key: db.type, value: Elasticsearch}
77+
- {key: db.instance, value: test-index}
7478
- operationName: Elasticsearch/es/delete
7579
parentSpanId: 0
7680
spanId: 5
@@ -84,6 +88,7 @@ segmentItems:
8488
skipAnalysis: false
8589
tags:
8690
- {key: db.type, value: Elasticsearch}
91+
- {key: db.instance, value: test-index}
8792
- operationName: Elasticsearch/es/indices.delete
8893
parentSpanId: 0
8994
spanId: 6
@@ -97,6 +102,7 @@ segmentItems:
97102
skipAnalysis: false
98103
tags:
99104
- {key: db.type, value: Elasticsearch}
105+
- {key: db.instance, value: test-index}
100106
- operationName: GET:/elasticsearch-java-case/case/elasticsearch
101107
parentSpanId: -1
102108
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
@@ -32,6 +32,7 @@ segmentItems:
3232
skipAnalysis: false
3333
tags:
3434
- {key: db.type, value: Elasticsearch}
35+
- {key: db.instance, value: test-index}
3536
- operationName: Elasticsearch/es/index
3637
parentSpanId: 0
3738
spanId: 2
@@ -45,6 +46,7 @@ segmentItems:
4546
skipAnalysis: false
4647
tags:
4748
- {key: db.type, value: Elasticsearch}
49+
- {key: db.instance, value: test-index}
4850
- operationName: Elasticsearch/es/get
4951
parentSpanId: 0
5052
spanId: 3
@@ -58,6 +60,7 @@ segmentItems:
5860
skipAnalysis: false
5961
tags:
6062
- {key: db.type, value: Elasticsearch}
63+
- {key: db.instance, value: test-index}
6164
- operationName: Elasticsearch/es/search
6265
parentSpanId: 0
6366
spanId: 4
@@ -71,6 +74,7 @@ segmentItems:
7174
skipAnalysis: false
7275
tags:
7376
- {key: db.type, value: Elasticsearch}
77+
- {key: db.instance, value: test-index}
7478
- operationName: Elasticsearch/es/delete
7579
parentSpanId: 0
7680
spanId: 5
@@ -84,6 +88,7 @@ segmentItems:
8488
skipAnalysis: false
8589
tags:
8690
- {key: db.type, value: Elasticsearch}
91+
- {key: db.instance, value: test-index}
8792
- operationName: Elasticsearch/es/indices.delete
8893
parentSpanId: 0
8994
spanId: 6
@@ -97,6 +102,7 @@ segmentItems:
97102
skipAnalysis: false
98103
tags:
99104
- {key: db.type, value: Elasticsearch}
105+
- {key: db.instance, value: test-index}
100106
- operationName: GET:/elasticsearch-java-case/case/elasticsearch
101107
parentSpanId: -1
102108
spanId: 0

0 commit comments

Comments
 (0)