Skip to content

Commit 6bc49ac

Browse files
authored
Camel: add support for db.query.summary (#16026)
1 parent 810b3e9 commit 6bc49ac

3 files changed

Lines changed: 91 additions & 42 deletions

File tree

  • instrumentation/camel-2.20
    • javaagent-unit-tests/src/test/java/io/opentelemetry/javaagent/instrumentation/apachecamel/decorators
    • javaagent/src

instrumentation/camel-2.20/javaagent-unit-tests/src/test/java/io/opentelemetry/javaagent/instrumentation/apachecamel/decorators/SanitizationTest.java

Lines changed: 54 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,84 +9,119 @@
99
import static org.mockito.Mockito.mock;
1010
import static org.mockito.Mockito.when;
1111

12+
import io.opentelemetry.api.common.Attributes;
13+
import io.opentelemetry.api.common.AttributesBuilder;
14+
import io.opentelemetry.instrumentation.api.internal.SemconvStability;
15+
import io.opentelemetry.semconv.DbAttributes;
16+
import io.opentelemetry.semconv.incubating.DbIncubatingAttributes;
1217
import java.util.stream.Stream;
1318
import org.apache.camel.Exchange;
1419
import org.apache.camel.Message;
1520
import org.junit.jupiter.params.ParameterizedTest;
1621
import org.junit.jupiter.params.provider.Arguments;
1722
import org.junit.jupiter.params.provider.MethodSource;
1823

24+
@SuppressWarnings("deprecation") // using deprecated semconv
1925
class SanitizationTest {
2026

2127
@ParameterizedTest
2228
@MethodSource("sanitizeCqlArgs")
23-
void sanitizeCql(String original, String expected) {
29+
void sanitizeCql(String original, String expectedStatement, String expectedSummary) {
2430
DbSpanDecorator decorator = new DbSpanDecorator("cql", "");
2531

2632
Exchange exchange = mock(Exchange.class);
2733
Message message = mock(Message.class);
2834
when(message.getHeader("CamelCqlQuery")).thenReturn(original);
2935
when(exchange.getIn()).thenReturn(message);
3036

31-
String actualSanitized = decorator.getStatement(exchange, null);
32-
assertThat(actualSanitized).isEqualTo(expected);
37+
assertSanitizedStatement(decorator, exchange, expectedStatement, expectedSummary);
3338
}
3439

3540
static Stream<Arguments> sanitizeCqlArgs() {
3641
return Stream.of(
3742
Arguments.of(
38-
"SELECT * FROM users WHERE field>=-1234", "SELECT * FROM users WHERE field>=?"),
43+
"SELECT * FROM users WHERE field>=-1234",
44+
"SELECT * FROM users WHERE field>=?",
45+
"SELECT users"),
3946
Arguments.of(
4047
"SELECT name, phone FROM contact WHERE state = 'NY'",
41-
"SELECT name, phone FROM contact WHERE state = ?"),
42-
Arguments.of("SELECT * FROM col WHERE tag='Something'", "SELECT * FROM col WHERE tag=?"));
48+
"SELECT name, phone FROM contact WHERE state = ?",
49+
"SELECT contact"),
50+
Arguments.of(
51+
"SELECT * FROM col WHERE tag='Something'",
52+
"SELECT * FROM col WHERE tag=?",
53+
"SELECT col"));
4354
}
4455

4556
@ParameterizedTest
4657
@MethodSource("sanitizeJdbcArgs")
47-
void sanitizeJdbc(String original, String expected) {
58+
void sanitizeJdbc(String original, String expectedStatement, String expectedSummary) {
4859
DbSpanDecorator decorator = new DbSpanDecorator("jdbc", "");
4960

5061
Exchange exchange = mock(Exchange.class);
5162
Message message = mock(Message.class);
5263
when(message.getBody()).thenReturn(original);
5364
when(exchange.getIn()).thenReturn(message);
5465

55-
String actualSanitized = decorator.getStatement(exchange, null);
56-
assertThat(actualSanitized).isEqualTo(expected);
66+
assertSanitizedStatement(decorator, exchange, expectedStatement, expectedSummary);
5767
}
5868

5969
static Stream<Arguments> sanitizeJdbcArgs() {
6070
return Stream.of(
61-
Arguments.of("SELECT 3", "SELECT ?"),
71+
Arguments.of("SELECT 3", "SELECT ?", "SELECT"),
72+
Arguments.of(
73+
"SELECT * FROM TABLE WHERE FIELD = 1234",
74+
"SELECT * FROM TABLE WHERE FIELD = ?",
75+
"SELECT TABLE"),
6276
Arguments.of(
63-
"SELECT * FROM TABLE WHERE FIELD = 1234", "SELECT * FROM TABLE WHERE FIELD = ?"),
64-
Arguments.of("SELECT * FROM TABLE WHERE FIELD<-1234", "SELECT * FROM TABLE WHERE FIELD<?"),
77+
"SELECT * FROM TABLE WHERE FIELD<-1234",
78+
"SELECT * FROM TABLE WHERE FIELD<?",
79+
"SELECT TABLE"),
6580
Arguments.of(
6681
"SELECT col1 AS col2 FROM users WHERE field=1234",
67-
"SELECT col1 AS col2 FROM users WHERE field=?"));
82+
"SELECT col1 AS col2 FROM users WHERE field=?",
83+
"SELECT users"));
6884
}
6985

7086
@ParameterizedTest
7187
@MethodSource("sanitizeSqlArgs")
72-
void sanitizeSql(String original, String expected) {
88+
void sanitizeSql(String original, String expectedStatement, String expectedSummary) {
7389
DbSpanDecorator decorator = new DbSpanDecorator("sql", "");
7490

7591
Exchange exchange = mock(Exchange.class);
7692
Message message = mock(Message.class);
7793
when(message.getHeader("CamelSqlQuery")).thenReturn(original);
7894
when(exchange.getIn()).thenReturn(message);
7995

80-
String actualSanitized = decorator.getStatement(exchange, null);
81-
assertThat(actualSanitized).isEqualTo(expected);
96+
assertSanitizedStatement(decorator, exchange, expectedStatement, expectedSummary);
8297
}
8398

8499
static Stream<Arguments> sanitizeSqlArgs() {
85100
return Stream.of(
86101
Arguments.of(
87102
"SELECT * FROM table WHERE col1=1234 AND col2>3",
88-
"SELECT * FROM table WHERE col1=? AND col2>?"),
89-
Arguments.of("UPDATE table SET col=12", "UPDATE table SET col=?"),
90-
Arguments.of("insert into table where col=321", "insert into table where col=?"));
103+
"SELECT * FROM table WHERE col1=? AND col2>?",
104+
"SELECT table"),
105+
Arguments.of("UPDATE table SET col=12", "UPDATE table SET col=?", "UPDATE table"),
106+
Arguments.of(
107+
"insert into table where col=321", "insert into table where col=?", "INSERT table"));
108+
}
109+
110+
private static void assertSanitizedStatement(
111+
DbSpanDecorator decorator,
112+
Exchange exchange,
113+
String expectedStatement,
114+
String expectedSummary) {
115+
AttributesBuilder attributesBuilder = Attributes.builder();
116+
decorator.setQueryAttributes(attributesBuilder, exchange);
117+
Attributes attributes = attributesBuilder.build();
118+
119+
if (SemconvStability.emitStableDatabaseSemconv()) {
120+
assertThat(attributes.get(DbAttributes.DB_QUERY_TEXT)).isEqualTo(expectedStatement);
121+
assertThat(attributes.get(DbAttributes.DB_QUERY_SUMMARY)).isEqualTo(expectedSummary);
122+
}
123+
if (SemconvStability.emitOldDatabaseSemconv()) {
124+
assertThat(attributes.get(DbIncubatingAttributes.DB_STATEMENT)).isEqualTo(expectedStatement);
125+
}
91126
}
92127
}

instrumentation/camel-2.20/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachecamel/decorators/DbSpanDecorator.java

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
package io.opentelemetry.javaagent.instrumentation.apachecamel.decorators;
2525

2626
import io.opentelemetry.api.common.AttributesBuilder;
27+
import io.opentelemetry.instrumentation.api.incubator.semconv.db.SqlStatementInfo;
2728
import io.opentelemetry.instrumentation.api.incubator.semconv.db.SqlStatementSanitizer;
2829
import io.opentelemetry.instrumentation.api.internal.SemconvStability;
2930
import io.opentelemetry.javaagent.bootstrap.internal.AgentCommonConfig;
@@ -32,6 +33,7 @@
3233
import io.opentelemetry.semconv.incubating.DbIncubatingAttributes;
3334
import java.net.URI;
3435
import java.util.Map;
36+
import javax.annotation.Nullable;
3537
import org.apache.camel.Endpoint;
3638
import org.apache.camel.Exchange;
3739

@@ -66,27 +68,18 @@ public String getOperationName(
6668
}
6769
}
6870

69-
// visible for testing
70-
String getStatement(Exchange exchange, Endpoint endpoint) {
71+
@Nullable
72+
private String getRawQueryText(Exchange exchange) {
7173
switch (component) {
7274
case "cql":
7375
Object cqlObj = exchange.getIn().getHeader("CamelCqlQuery");
74-
if (cqlObj != null) {
75-
return sanitizer.sanitize(cqlObj.toString()).getQueryText();
76-
}
77-
return null;
76+
return cqlObj != null ? cqlObj.toString() : null;
7877
case "jdbc":
7978
Object body = exchange.getIn().getBody();
80-
if (body instanceof String) {
81-
return sanitizer.sanitize((String) body).getQueryText();
82-
}
83-
return null;
79+
return body instanceof String ? (String) body : null;
8480
case "sql":
8581
Object sqlquery = exchange.getIn().getHeader("CamelSqlQuery");
86-
if (sqlquery instanceof String) {
87-
return sanitizer.sanitize((String) sqlquery).getQueryText();
88-
}
89-
return null;
82+
return sqlquery instanceof String ? (String) sqlquery : null;
9083
default:
9184
return null;
9285
}
@@ -131,15 +124,9 @@ public void pre(
131124
if (SemconvStability.emitOldDatabaseSemconv()) {
132125
attributes.put(DbIncubatingAttributes.DB_SYSTEM, system);
133126
}
134-
String statement = getStatement(exchange, endpoint);
135-
if (statement != null) {
136-
if (SemconvStability.emitStableDatabaseSemconv()) {
137-
attributes.put(DbAttributes.DB_QUERY_TEXT, statement);
138-
}
139-
if (SemconvStability.emitOldDatabaseSemconv()) {
140-
attributes.put(DbIncubatingAttributes.DB_STATEMENT, statement);
141-
}
142-
}
127+
128+
setQueryAttributes(attributes, exchange);
129+
143130
String dbName = getDbName(endpoint);
144131
if (dbName != null) {
145132
if (SemconvStability.emitStableDatabaseSemconv()) {
@@ -150,4 +137,26 @@ public void pre(
150137
}
151138
}
152139
}
140+
141+
@SuppressWarnings("deprecation") // using deprecated semconv
142+
void setQueryAttributes(AttributesBuilder attributes, Exchange exchange) {
143+
String rawQueryText = getRawQueryText(exchange);
144+
if (rawQueryText != null) {
145+
SqlStatementInfo sqlStatementInfo =
146+
SemconvStability.emitOldDatabaseSemconv() ? sanitizer.sanitize(rawQueryText) : null;
147+
SqlStatementInfo sqlStatementInfoWithSummary =
148+
SemconvStability.emitStableDatabaseSemconv()
149+
? sanitizer.sanitizeWithSummary(rawQueryText)
150+
: null;
151+
152+
if (sqlStatementInfoWithSummary != null) {
153+
attributes.put(DbAttributes.DB_QUERY_TEXT, sqlStatementInfoWithSummary.getQueryText());
154+
attributes.put(
155+
DbAttributes.DB_QUERY_SUMMARY, sqlStatementInfoWithSummary.getQuerySummary());
156+
}
157+
if (sqlStatementInfo != null) {
158+
attributes.put(DbIncubatingAttributes.DB_STATEMENT, sqlStatementInfo.getQueryText());
159+
}
160+
}
161+
}
153162
}

instrumentation/camel-2.20/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/apachecamel/decorators/CassandraTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
package io.opentelemetry.javaagent.instrumentation.apachecamel.decorators;
77

88
import static io.opentelemetry.api.common.AttributeKey.stringKey;
9+
import static io.opentelemetry.instrumentation.api.internal.SemconvStability.emitStableDatabaseSemconv;
910
import static io.opentelemetry.instrumentation.testing.junit.db.SemconvStabilityUtil.maybeStable;
1011
import static io.opentelemetry.javaagent.instrumentation.apachecamel.ExperimentalTest.experimental;
1112
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
13+
import static io.opentelemetry.semconv.DbAttributes.DB_QUERY_SUMMARY;
1214
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_NAME;
1315
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_STATEMENT;
1416
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_SYSTEM;
@@ -124,6 +126,9 @@ void testCassandra() {
124126
equalTo(
125127
maybeStable(DB_STATEMENT),
126128
"select * from test.users where id=? ALLOW FILTERING"),
129+
equalTo(
130+
DB_QUERY_SUMMARY,
131+
emitStableDatabaseSemconv() ? "SELECT test.users" : null),
127132
equalTo(maybeStable(DB_SYSTEM), "cassandra"))));
128133
}
129134
}

0 commit comments

Comments
 (0)