Skip to content

Commit 039aa40

Browse files
committed
Ability to capture database query parameters
1 parent 25f901b commit 039aa40

File tree

8 files changed

+77
-14
lines changed

8 files changed

+77
-14
lines changed

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/configuration/Configuration.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,8 @@ public static class PreviewConfiguration {
346346

347347
public boolean captureLog4jMarker;
348348

349+
public boolean captureJdbcQueryParameters;
350+
349351
// this is to support interoperability with other systems
350352
// intentionally not allowing the removal of w3c propagator since that is key to many Azure
351353
// integrated experiences

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/init/AiConfigCustomizer.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,9 @@ private static void enableInstrumentations(
283283
properties.put(
284284
"otel.instrumentation.log4j-appender.experimental.capture-marker-attribute", "true");
285285
}
286+
if (config.preview.captureJdbcQueryParameters) {
287+
properties.put("otel.instrumentation.jdbc.experimental.capture-query-parameters", "true");
288+
}
286289
if (config.preview.instrumentation.akka.enabled) {
287290
properties.put("otel.instrumentation.akka-actor.enabled", "true");
288291
properties.put("otel.instrumentation.akka-http.enabled", "true");

smoke-tests/apps/Jdbc/src/main/java/com/microsoft/applicationinsights/smoketestapp/JdbcServlet.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,10 @@ private void oracleStatement() throws Exception {
172172
}
173173

174174
private static void executePreparedStatement(Connection connection) throws SQLException {
175-
PreparedStatement ps = connection.prepareStatement("select * from abc where xyz = ?");
176-
ps.setString(1, "y");
175+
PreparedStatement ps =
176+
connection.prepareStatement("select * from abc where uvw = ? and xyz = ?");
177+
ps.setString(1, "v");
178+
ps.setString(2, "y");
177179
ResultSet rs = ps.executeQuery();
178180
while (rs.next()) {}
179181
rs.close();
@@ -332,10 +334,10 @@ private static void testConnection(Connection connection, String sql) throws SQL
332334

333335
private static void setup(Connection connection) throws SQLException {
334336
try (Statement statement = connection.createStatement()) {
335-
statement.execute("create table abc (xyz varchar(10))");
336-
statement.execute("insert into abc (xyz) values ('x')");
337-
statement.execute("insert into abc (xyz) values ('y')");
338-
statement.execute("insert into abc (xyz) values ('z')");
337+
statement.execute("create table abc (uvw varchar(10), xyz varchar(10))");
338+
statement.execute("insert into abc (uvw, xyz) values ('u', 'x')");
339+
statement.execute("insert into abc (uvw, xyz) values ('v', 'y')");
340+
statement.execute("insert into abc (uvw, xyz) values ('w', 'z')");
339341
}
340342
}
341343
}

smoke-tests/apps/Jdbc/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/JdbcUnmaskedTest.java renamed to smoke-tests/apps/Jdbc/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/AbstractJdbcUnmasked.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,13 @@
33

44
package com.microsoft.applicationinsights.smoketest;
55

6-
import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.TOMCAT_8_JAVA_8;
76
import static org.assertj.core.api.Assertions.assertThat;
87
import static org.assertj.core.data.MapEntry.entry;
98

109
import org.junit.jupiter.api.Test;
1110
import org.junit.jupiter.api.extension.RegisterExtension;
1211

13-
@Environment(TOMCAT_8_JAVA_8)
14-
@UseAgent("unmasked_applicationinsights.json")
15-
class JdbcUnmaskedTest {
12+
abstract class AbstractJdbcUnmasked {
1613

1714
@RegisterExtension static final SmokeTestExtension testing = SmokeTestExtension.create();
1815

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.microsoft.applicationinsights.smoketest;
5+
6+
import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.TOMCAT_8_JAVA_8;
7+
import static org.assertj.core.api.Assertions.assertThat;
8+
import static org.assertj.core.data.MapEntry.entry;
9+
10+
import org.junit.jupiter.api.Test;
11+
12+
@Environment(TOMCAT_8_JAVA_8)
13+
@UseAgent("capture_params_applicationinsights.json")
14+
class JdbcCaptureParameters extends AbstractJdbcUnmasked {
15+
16+
@Test
17+
@TargetUri("/hsqldbPreparedStatement")
18+
void hsqldbPreparedStatementCapturesParameters() throws Exception {
19+
Telemetry telemetry = testing.getTelemetry(1);
20+
21+
assertThat(telemetry.rd.getProperties())
22+
.containsExactly(entry("_MS.ProcessedByMetricExtractors", "True"));
23+
assertThat(telemetry.rd.getSuccess()).isTrue();
24+
25+
assertThat(telemetry.rdd1.getName()).isEqualTo("SELECT testdb.abc");
26+
assertThat(telemetry.rdd1.getData()).isEqualTo("select * from abc where uvw = ? and xyz = ?");
27+
assertThat(telemetry.rdd1.getType()).isEqualTo("SQL");
28+
assertThat(telemetry.rdd1.getTarget()).isEqualTo("hsqldb | testdb");
29+
assertThat(telemetry.rdd1.getSuccess()).isTrue();
30+
31+
assertThat(telemetry.rdd1.getProperties())
32+
.containsExactly(entry("db.query.parameter.0", "v"), entry("db.query.parameter.1", "y"));
33+
34+
SmokeTestExtension.assertParentChild(
35+
telemetry.rd, telemetry.rdEnvelope, telemetry.rddEnvelope1, "GET /Jdbc/*");
36+
}
37+
}

smoke-tests/apps/Jdbc/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/JdbcTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ void hsqldbPreparedStatement() throws Exception {
5454
assertThat(telemetry.rd.getSuccess()).isTrue();
5555

5656
assertThat(telemetry.rdd1.getName()).isEqualTo("SELECT testdb.abc");
57-
assertThat(telemetry.rdd1.getData()).isEqualTo("select * from abc where xyz = ?");
57+
assertThat(telemetry.rdd1.getData()).isEqualTo("select * from abc where uvw = ? and xyz = ?");
5858
assertThat(telemetry.rdd1.getType()).isEqualTo("SQL");
5959
assertThat(telemetry.rdd1.getTarget()).isEqualTo("hsqldb | testdb");
6060
assertThat(telemetry.rdd1.getProperties()).isEmpty();
@@ -170,7 +170,7 @@ void mysqlPreparedStatement() throws Exception {
170170
assertThat(telemetry.rd.getSuccess()).isTrue();
171171

172172
assertThat(telemetry.rdd1.getName()).isEqualTo("SELECT mysql.abc");
173-
assertThat(telemetry.rdd1.getData()).isEqualTo("select * from abc where xyz = ?");
173+
assertThat(telemetry.rdd1.getData()).isEqualTo("select * from abc where uvw = ? and xyz = ?");
174174
assertThat(telemetry.rdd1.getType()).isEqualTo("mysql");
175175
// not the best test, because this is both the db.name and db.system
176176
assertThat(telemetry.rdd1.getTarget()).matches("dependency[0-9]+ \\| mysql");
@@ -214,7 +214,7 @@ void postgresPreparedStatement() throws Exception {
214214
assertThat(telemetry.rd.getSuccess()).isTrue();
215215

216216
assertThat(telemetry.rdd1.getName()).isEqualTo("SELECT postgres.abc");
217-
assertThat(telemetry.rdd1.getData()).isEqualTo("select * from abc where xyz = ?");
217+
assertThat(telemetry.rdd1.getData()).isEqualTo("select * from abc where uvw = ? and xyz = ?");
218218
assertThat(telemetry.rdd1.getType()).isEqualTo("postgresql");
219219
// not the best test, because this is both the db.name and db.system
220220
assertThat(telemetry.rdd1.getTarget()).matches("dependency[0-9]+ \\| postgres");
@@ -256,7 +256,7 @@ void sqlServerPreparedStatement() throws Exception {
256256
assertThat(telemetry.rd.getSuccess()).isTrue();
257257

258258
assertThat(telemetry.rdd1.getName()).isEqualTo("SELECT abc");
259-
assertThat(telemetry.rdd1.getData()).isEqualTo("select * from abc where xyz = ?");
259+
assertThat(telemetry.rdd1.getData()).isEqualTo("select * from abc where uvw = ? and xyz = ?");
260260
assertThat(telemetry.rdd1.getType()).isEqualTo("SQL");
261261
assertThat(telemetry.rdd1.getTarget()).matches("dependency[0-9]+");
262262
assertThat(telemetry.rdd1.getProperties()).isEmpty();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.microsoft.applicationinsights.smoketest;
5+
6+
import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.TOMCAT_8_JAVA_8;
7+
8+
@UseAgent("unmasked_applicationinsights.json")
9+
@Environment(TOMCAT_8_JAVA_8)
10+
class JdbcUnmaskedWithUnmakingFeature extends AbstractJdbcUnmasked {}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"role": {
3+
"name": "testrolename",
4+
"instance": "testroleinstance"
5+
},
6+
"sampling": {
7+
"percentage": 100
8+
},
9+
"preview": {
10+
"captureJdbcQueryParameters": true
11+
}
12+
}

0 commit comments

Comments
 (0)