|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.skywalking.apm.plugin.jdbc.clickhouse; |
| 20 | + |
| 21 | +import java.util.List; |
| 22 | +import org.apache.skywalking.apm.agent.core.context.trace.AbstractTracingSpan; |
| 23 | +import org.apache.skywalking.apm.agent.core.context.trace.TraceSegment; |
| 24 | +import org.apache.skywalking.apm.agent.core.context.util.TagValuePair; |
| 25 | +import org.apache.skywalking.apm.agent.test.helper.SegmentHelper; |
| 26 | +import org.apache.skywalking.apm.agent.test.helper.SpanHelper; |
| 27 | +import org.apache.skywalking.apm.agent.test.tools.AgentServiceRule; |
| 28 | +import org.apache.skywalking.apm.agent.test.tools.SegmentStorage; |
| 29 | +import org.apache.skywalking.apm.agent.test.tools.SegmentStoragePoint; |
| 30 | +import org.apache.skywalking.apm.agent.test.tools.TracingSegmentRunner; |
| 31 | +import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; |
| 32 | +import org.apache.skywalking.apm.plugin.jdbc.JDBCPluginConfig; |
| 33 | +import org.apache.skywalking.apm.plugin.jdbc.clickhouse.v32.ClickHousePrepareStatementTracing; |
| 34 | +import org.apache.skywalking.apm.plugin.jdbc.trace.ConnectionInfo; |
| 35 | +import org.junit.After; |
| 36 | +import org.junit.Before; |
| 37 | +import org.junit.Rule; |
| 38 | +import org.junit.Test; |
| 39 | +import org.junit.runner.RunWith; |
| 40 | + |
| 41 | +import static org.hamcrest.CoreMatchers.is; |
| 42 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 43 | + |
| 44 | +/** |
| 45 | + * Verify that {@link ClickHousePrepareStatementTracing} truncates the SQL body |
| 46 | + * recorded on the exit span according to {@link JDBCPluginConfig.Plugin.JDBC#SQL_BODY_MAX_LENGTH}. |
| 47 | + */ |
| 48 | +@RunWith(TracingSegmentRunner.class) |
| 49 | +public class ClickHousePrepareStatementTracingTest { |
| 50 | + |
| 51 | + @SegmentStoragePoint |
| 52 | + private SegmentStorage segmentStorage; |
| 53 | + |
| 54 | + @Rule |
| 55 | + public AgentServiceRule serviceRule = new AgentServiceRule(); |
| 56 | + |
| 57 | + private ConnectionInfo connectionInfo; |
| 58 | + |
| 59 | + private int originalLimit; |
| 60 | + |
| 61 | + @Before |
| 62 | + public void setUp() { |
| 63 | + connectionInfo = new ConnectionInfo( |
| 64 | + ComponentsDefine.CLICKHOUSE_JDBC_DRIVER, "ClickHouse", "127.0.0.1", 8123, "test"); |
| 65 | + originalLimit = JDBCPluginConfig.Plugin.JDBC.SQL_BODY_MAX_LENGTH; |
| 66 | + } |
| 67 | + |
| 68 | + @After |
| 69 | + public void tearDown() { |
| 70 | + JDBCPluginConfig.Plugin.JDBC.SQL_BODY_MAX_LENGTH = originalLimit; |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void shortSqlIsRecordedAsIs() throws Exception { |
| 75 | + JDBCPluginConfig.Plugin.JDBC.SQL_BODY_MAX_LENGTH = 2048; |
| 76 | + String sql = "SELECT 1"; |
| 77 | + |
| 78 | + ClickHousePrepareStatementTracing.of(connectionInfo, "execute", sql, () -> Boolean.TRUE); |
| 79 | + |
| 80 | + assertThat(dbStatementTagValue(), is(sql)); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void longSqlIsTruncatedToConfiguredLimit() throws Exception { |
| 85 | + JDBCPluginConfig.Plugin.JDBC.SQL_BODY_MAX_LENGTH = 16; |
| 86 | + // Longer than the configured limit, so the helper must truncate to the first 16 chars and append "..." |
| 87 | + String sql = "SELECT * FROM table_with_many_cols"; |
| 88 | + |
| 89 | + ClickHousePrepareStatementTracing.of(connectionInfo, "execute", sql, () -> Boolean.TRUE); |
| 90 | + |
| 91 | + assertThat(dbStatementTagValue(), is(sql.substring(0, 16) + "...")); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void negativeLimitDisablesTruncation() throws Exception { |
| 96 | + JDBCPluginConfig.Plugin.JDBC.SQL_BODY_MAX_LENGTH = -1; |
| 97 | + StringBuilder builder = new StringBuilder("SELECT "); |
| 98 | + for (int i = 0; i < 1000; i++) { |
| 99 | + builder.append("a, "); |
| 100 | + } |
| 101 | + builder.append("a FROM t"); |
| 102 | + String sql = builder.toString(); |
| 103 | + |
| 104 | + ClickHousePrepareStatementTracing.of(connectionInfo, "execute", sql, () -> Boolean.TRUE); |
| 105 | + |
| 106 | + assertThat(dbStatementTagValue(), is(sql)); |
| 107 | + } |
| 108 | + |
| 109 | + private String dbStatementTagValue() { |
| 110 | + List<TraceSegment> traceSegments = segmentStorage.getTraceSegments(); |
| 111 | + assertThat(traceSegments.size(), is(1)); |
| 112 | + List<AbstractTracingSpan> spans = SegmentHelper.getSpans(traceSegments.get(0)); |
| 113 | + assertThat(spans.size(), is(1)); |
| 114 | + List<TagValuePair> tags = SpanHelper.getTags(spans.get(0)); |
| 115 | + // tag order: db.type, db.instance, db.statement |
| 116 | + return (String) tags.get(2).getValue(); |
| 117 | + } |
| 118 | +} |
0 commit comments