|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.instrumentation.api.internal; |
| 7 | + |
| 8 | +import static io.opentelemetry.instrumentation.testing.junit.db.SemconvStabilityUtil.maybeStable; |
| 9 | +import static org.assertj.core.api.Assertions.assertThat; |
| 10 | + |
| 11 | +import io.opentelemetry.api.common.Attributes; |
| 12 | +import io.opentelemetry.api.common.AttributesBuilder; |
| 13 | +import io.opentelemetry.context.Context; |
| 14 | +import io.opentelemetry.instrumentation.api.incubator.semconv.db.DbClientSpanNameExtractor; |
| 15 | +import io.opentelemetry.instrumentation.api.incubator.semconv.db.SqlClientAttributesExtractor; |
| 16 | +import io.opentelemetry.instrumentation.api.incubator.semconv.db.SqlClientAttributesGetter; |
| 17 | +import io.opentelemetry.instrumentation.api.incubator.semconv.db.SqlStatementInfo; |
| 18 | +import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; |
| 19 | +import io.opentelemetry.instrumentation.api.instrumenter.SpanNameExtractor; |
| 20 | +import io.opentelemetry.semconv.incubating.DbIncubatingAttributes; |
| 21 | +import java.util.Collection; |
| 22 | +import java.util.Collections; |
| 23 | +import java.util.Map; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | + |
| 26 | +class InstrumenterContextTest { |
| 27 | + |
| 28 | + @SuppressWarnings({"unchecked", "deprecation"}) // using deprecated DB_SQL_TABLE |
| 29 | + @Test |
| 30 | + void testSqlSanitizer() { |
| 31 | + String testQuery = "SELECT name FROM test WHERE id = 1"; |
| 32 | + SqlClientAttributesGetter<Object> getter = |
| 33 | + new SqlClientAttributesGetter<Object>() { |
| 34 | + |
| 35 | + @Override |
| 36 | + public Collection<String> getRawQueryTexts(Object request) { |
| 37 | + return Collections.singletonList(testQuery); |
| 38 | + } |
| 39 | + }; |
| 40 | + SpanNameExtractor<Object> spanNameExtractor = DbClientSpanNameExtractor.create(getter); |
| 41 | + AttributesExtractor<Object, Void> attributesExtractor = |
| 42 | + SqlClientAttributesExtractor.create(getter); |
| 43 | + |
| 44 | + assertThat(InstrumenterContext.isActive()).isFalse(); |
| 45 | + InstrumenterContext.withContext( |
| 46 | + () -> { |
| 47 | + assertThat(InstrumenterContext.isActive()).isTrue(); |
| 48 | + assertThat(InstrumenterContext.get()).isEmpty(); |
| 49 | + assertThat(spanNameExtractor.extract(null)).isEqualTo("SELECT test"); |
| 50 | + // verify that sanitized statement was cached, see SqlStatementSanitizerUtil |
| 51 | + assertThat(InstrumenterContext.get()).containsKey("sanitized-sql-map"); |
| 52 | + Map<String, SqlStatementInfo> sanitizedMap = |
| 53 | + (Map<String, SqlStatementInfo>) InstrumenterContext.get().get("sanitized-sql-map"); |
| 54 | + assertThat(sanitizedMap).containsKey(testQuery); |
| 55 | + |
| 56 | + // replace cached sanitization result to verify it is used |
| 57 | + sanitizedMap.put( |
| 58 | + testQuery, |
| 59 | + SqlStatementInfo.create("SELECT name2 FROM test2 WHERE id = ?", "SELECT", "test2")); |
| 60 | + { |
| 61 | + AttributesBuilder builder = Attributes.builder(); |
| 62 | + attributesExtractor.onStart(builder, Context.root(), null); |
| 63 | + assertThat(builder.build().get(maybeStable(DbIncubatingAttributes.DB_SQL_TABLE))) |
| 64 | + .isEqualTo("test2"); |
| 65 | + } |
| 66 | + |
| 67 | + // clear cached value to see whether it gets recomputed correctly |
| 68 | + sanitizedMap.clear(); |
| 69 | + { |
| 70 | + AttributesBuilder builder = Attributes.builder(); |
| 71 | + attributesExtractor.onStart(builder, Context.root(), null); |
| 72 | + assertThat(builder.build().get(maybeStable(DbIncubatingAttributes.DB_SQL_TABLE))) |
| 73 | + .isEqualTo("test"); |
| 74 | + } |
| 75 | + |
| 76 | + return null; |
| 77 | + }); |
| 78 | + } |
| 79 | +} |
0 commit comments