Skip to content

Commit 707148d

Browse files
authored
Merge pull request #14 from newrelic-experimental/elasticrestclient710
fix: Query Size handling by parts
2 parents be464b3 + 303d10f commit 707148d

File tree

2 files changed

+34
-14
lines changed

2 files changed

+34
-14
lines changed

esrestclient-7.10/src/main/java/com/newrelic/instrumentation/labs/esrestclient/RestClient_Instrumentation.java

+32-12
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,30 @@ public Response performRequest(Request request) {
4646
}
4747

4848
String sql = Utils.constructSql(endPoint, payload);
49+
String queryToSend = sql;
4950

51+
if (null != sql) {
52+
53+
int sqlLength = sql.length();
54+
String queryPart = "";
55+
56+
if (sqlLength > 4094) {
57+
58+
queryPart = sql.substring(0, 4093);
59+
queryToSend = queryPart;
60+
// Utils.logPayload(sql);
61+
Utils.logSqlQueryAttributes(segment, sql.substring(4093), 255); // SQL Truncation
62+
}
63+
64+
}
5065
segment.reportAsExternal(DatastoreParameters.product("Elasticsearch")
5166
.collection(Utils.extractIndex(endPoint))
5267
.operation(request.getMethod())
5368
.noInstance()
5469
.noDatabaseName()
55-
.slowQuery(sql, new ESQueryConverter())
70+
.slowQuery(queryToSend, new ESQueryConverter())
5671
.build());
5772

58-
if (null != sql) {
59-
// Utils.logPayload(sql);
60-
Utils.logSqlQueryAttributes(segment, sql, 4091); // SQL Truncation
61-
}
62-
6373
return Weaver.callOriginal();
6474
} finally {
6575
segment.end();
@@ -102,21 +112,31 @@ public Cancellable performRequestAsync(Request request, ResponseListener respons
102112

103113
// Construct SQL-like query for slow query logging
104114
String sql = Utils.constructSql(endPoint, payload);
115+
String queryToSend = sql;
105116

117+
if (null != sql) {
118+
119+
int sqlLength = sql.length();
120+
String queryPart = "";
121+
122+
if (sqlLength > 4094) {
123+
124+
queryPart = sql.substring(0, 4093);
125+
queryToSend = queryPart;
126+
// Utils.logPayload(sql);
127+
Utils.logSqlQueryAttributes(segment, sql.substring(4093), 255); // SQL Truncation
128+
}
129+
130+
}
106131
// Create datastore parameters for the request
107132
DatastoreParameters params = DatastoreParameters.product("Elasticsearch")
108133
.collection(Utils.extractIndex(endPoint))
109134
.operation(request.getMethod())
110135
.noInstance()
111136
.noDatabaseName()
112-
.slowQuery(sql, new ESQueryConverter())
137+
.slowQuery(queryToSend, new ESQueryConverter())
113138
.build();
114139

115-
if (null != sql) {
116-
// Utils.logPayload(sql);
117-
Utils.logSqlQueryAttributes(segment, sql, 4091); // SQL Truncation
118-
}
119-
120140
// Store parameters for asynchronous processing
121141
int hash = responseListener.hashCode();
122142
NRHolder.putParams(hash, params);

esrestclient-7.10/src/main/java/com/newrelic/instrumentation/labs/esrestclient/Utils.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ public static void logSqlQueryAttributes(Segment segment, String sqlQuery, int s
6868

6969
// Break the SQL query into parts if it exceeds the specified size
7070
int queryLength = sqlQuery.length();
71-
int partNumber = 1;
71+
int partNumber = 2;
7272
if (queryLength > size) {
73-
for (int i = 0; (i < queryLength && partNumber < 200); i += size) {
73+
for (int i = 0; (i < queryLength && partNumber < 64); i += size) {
7474
int end = Math.min(i + size, queryLength);
7575
String queryPart = sqlQuery.substring(i, end);
7676
// Format the part number with leading zeros

0 commit comments

Comments
 (0)