|
| 1 | +/* |
| 2 | + * Copyright (C) 2021 ScyllaDB |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.datastax.driver.core.tracing; |
| 18 | + |
| 19 | +import static org.junit.Assert.assertFalse; |
| 20 | +import static org.junit.Assert.assertNotNull; |
| 21 | +import static org.testng.Assert.assertEquals; |
| 22 | +import static org.testng.Assert.assertNotEquals; |
| 23 | +import static org.testng.Assert.assertNull; |
| 24 | +import static org.testng.Assert.assertTrue; |
| 25 | + |
| 26 | +import com.datastax.driver.core.BoundStatement; |
| 27 | +import com.datastax.driver.core.CCMTestsSupport; |
| 28 | +import com.datastax.driver.core.ConsistencyLevel; |
| 29 | +import com.datastax.driver.core.PreparedStatement; |
| 30 | +import com.datastax.driver.core.Session; |
| 31 | +import com.datastax.driver.core.policies.DefaultRetryPolicy; |
| 32 | +import com.datastax.driver.core.policies.PagingOptimizingLoadBalancingPolicy; |
| 33 | +import java.util.ArrayList; |
| 34 | +import java.util.Collection; |
| 35 | +import org.testng.annotations.Test; |
| 36 | + |
| 37 | +public class BasicTracingTest extends CCMTestsSupport { |
| 38 | + private static TestTracingInfoFactory testTracingInfoFactory; |
| 39 | + private Session session; |
| 40 | + |
| 41 | + @Override |
| 42 | + public void onTestContextInitialized() { |
| 43 | + initializeTestTracing(); |
| 44 | + session.execute("USE " + keyspace); |
| 45 | + session.execute("CREATE TABLE t (k int PRIMARY KEY, v int)"); |
| 46 | + |
| 47 | + Collection<TracingInfo> spans = testTracingInfoFactory.getSpans(); |
| 48 | + spans.clear(); |
| 49 | + } |
| 50 | + |
| 51 | + @Test(groups = "short") |
| 52 | + public void simpleTracingTest() { |
| 53 | + session.execute("INSERT INTO t(k, v) VALUES (1, 7)"); |
| 54 | + |
| 55 | + Collection<TracingInfo> spans = testTracingInfoFactory.getSpans(); |
| 56 | + assertNotEquals(spans.size(), 0); |
| 57 | + |
| 58 | + TracingInfo rootSpan = getRoot(spans); |
| 59 | + assertTrue(rootSpan instanceof TestTracingInfo); |
| 60 | + TestTracingInfo root = (TestTracingInfo) rootSpan; |
| 61 | + |
| 62 | + assertTrue(root.isSpanStarted()); |
| 63 | + assertTrue(root.isSpanFinished()); |
| 64 | + assertEquals(root.getStatusCode(), TracingInfo.StatusCode.OK); |
| 65 | + |
| 66 | + spans.clear(); |
| 67 | + } |
| 68 | + |
| 69 | + @Test(groups = "short") |
| 70 | + public void tagsTest() { |
| 71 | + PreparedStatement prepared = session.prepare("INSERT INTO t(k, v) VALUES (?, ?)"); |
| 72 | + |
| 73 | + Collection<TracingInfo> prepareSpans = testTracingInfoFactory.getSpans(); |
| 74 | + assertNotEquals(prepareSpans.size(), 0); |
| 75 | + assertTrue(getRoot(prepareSpans) instanceof TestTracingInfo); |
| 76 | + prepareSpans.clear(); |
| 77 | + |
| 78 | + BoundStatement bound = prepared.bind(1, 7); |
| 79 | + session.execute(bound); |
| 80 | + |
| 81 | + Collection<TracingInfo> spans = testTracingInfoFactory.getSpans(); |
| 82 | + assertNotEquals(spans.size(), 0); |
| 83 | + |
| 84 | + TracingInfo rootSpan = getRoot(spans); |
| 85 | + assertTrue(rootSpan instanceof TestTracingInfo); |
| 86 | + TestTracingInfo root = (TestTracingInfo) rootSpan; |
| 87 | + |
| 88 | + assertTrue(root.isSpanStarted()); |
| 89 | + assertTrue(root.isSpanFinished()); |
| 90 | + assertEquals(root.getStatusCode(), TracingInfo.StatusCode.OK); |
| 91 | + |
| 92 | + // these tags should be set for request span |
| 93 | + assertEquals(root.getStatementType(), "prepared"); |
| 94 | + assertEquals(root.getBatchSize(), new Integer(1)); |
| 95 | + assertEquals(root.getConsistencyLevel(), ConsistencyLevel.ONE); |
| 96 | + assertNull(root.getRowsCount()); // no rows are returned in insert |
| 97 | + assertTrue(root.getLoadBalancingPolicy() instanceof PagingOptimizingLoadBalancingPolicy); |
| 98 | + assertTrue(root.getRetryPolicy() instanceof DefaultRetryPolicy); |
| 99 | + assertFalse(root.getQueryPaged()); |
| 100 | + assertNull(root.getStatement()); // because of precision level NORMAL |
| 101 | + // these are tags specific to bound statement |
| 102 | + assertEquals(root.getKeyspace(), keyspace); |
| 103 | + assertEquals(root.getPartitionKey(), "k=1"); |
| 104 | + assertEquals(root.getTable(), "t"); |
| 105 | + |
| 106 | + // these tags should not be set for request span |
| 107 | + assertNull(root.getPeerName()); |
| 108 | + assertNull(root.getPeerIP()); |
| 109 | + assertNull(root.getPeerPort()); |
| 110 | + assertNull(root.getRetryCount()); |
| 111 | + |
| 112 | + ArrayList<TracingInfo> speculativeExecutions = getChildren(spans, root); |
| 113 | + assertTrue(speculativeExecutions.size() > 0); |
| 114 | + |
| 115 | + for (TracingInfo speculativeExecutionSpan : speculativeExecutions) { |
| 116 | + assertTrue(speculativeExecutionSpan instanceof TestTracingInfo); |
| 117 | + TestTracingInfo tracingInfo = (TestTracingInfo) speculativeExecutionSpan; |
| 118 | + |
| 119 | + // these tags should not be set for speculative execution span |
| 120 | + assertNull(tracingInfo.getStatementType()); |
| 121 | + assertNull(tracingInfo.getBatchSize()); |
| 122 | + assertNull(tracingInfo.getConsistencyLevel()); |
| 123 | + assertNull(tracingInfo.getRowsCount()); |
| 124 | + assertNull(tracingInfo.getLoadBalancingPolicy()); |
| 125 | + assertNull(tracingInfo.getRetryPolicy()); |
| 126 | + assertNull(tracingInfo.getQueryPaged()); |
| 127 | + assertNull(tracingInfo.getStatement()); |
| 128 | + assertNull(tracingInfo.getPeerName()); |
| 129 | + assertNull(tracingInfo.getPeerIP()); |
| 130 | + assertNull(tracingInfo.getPeerPort()); |
| 131 | + // these are tags specific to bound statement |
| 132 | + assertNull(tracingInfo.getKeyspace()); |
| 133 | + assertNull(tracingInfo.getPartitionKey()); |
| 134 | + assertNull(tracingInfo.getTable()); |
| 135 | + |
| 136 | + // this tag should be set for speculative execution span |
| 137 | + assertTrue(tracingInfo.getRetryCount() >= 0); |
| 138 | + } |
| 139 | + |
| 140 | + ArrayList<TracingInfo> queries = new ArrayList<TracingInfo>(); |
| 141 | + for (TracingInfo tracingInfo : speculativeExecutions) { |
| 142 | + queries.addAll(getChildren(spans, tracingInfo)); |
| 143 | + } |
| 144 | + assertTrue(queries.size() > 0); |
| 145 | + |
| 146 | + for (TracingInfo querySpan : queries) { |
| 147 | + assertTrue(querySpan instanceof TestTracingInfo); |
| 148 | + TestTracingInfo tracingInfo = (TestTracingInfo) querySpan; |
| 149 | + |
| 150 | + // these tags should not be set for query span |
| 151 | + assertNull(tracingInfo.getStatementType()); |
| 152 | + assertNull(tracingInfo.getBatchSize()); |
| 153 | + assertNull(tracingInfo.getConsistencyLevel()); |
| 154 | + assertNull(tracingInfo.getRowsCount()); |
| 155 | + assertNull(tracingInfo.getLoadBalancingPolicy()); |
| 156 | + assertNull(tracingInfo.getRetryPolicy()); |
| 157 | + assertNull(tracingInfo.getQueryPaged()); |
| 158 | + assertNull(tracingInfo.getStatement()); |
| 159 | + assertNull(tracingInfo.getRetryCount()); |
| 160 | + // these are tags specific to bound statement |
| 161 | + assertNull(tracingInfo.getKeyspace()); |
| 162 | + assertNull(tracingInfo.getPartitionKey()); |
| 163 | + assertNull(tracingInfo.getTable()); |
| 164 | + |
| 165 | + // these tags should be set for query span |
| 166 | + assertNotNull(tracingInfo.getPeerName()); |
| 167 | + assertNotNull(tracingInfo.getPeerIP()); |
| 168 | + assertNotNull(tracingInfo.getPeerPort()); |
| 169 | + assertTrue(tracingInfo.getPeerPort() >= 0 && tracingInfo.getPeerPort() <= 65535); |
| 170 | + } |
| 171 | + |
| 172 | + spans.clear(); |
| 173 | + } |
| 174 | + |
| 175 | + private void initializeTestTracing() { |
| 176 | + testTracingInfoFactory = new TestTracingInfoFactory(PrecisionLevel.NORMAL); |
| 177 | + cluster().setTracingInfoFactory(testTracingInfoFactory); |
| 178 | + session = cluster().connect(); |
| 179 | + } |
| 180 | + |
| 181 | + private TracingInfo getRoot(Collection<TracingInfo> spans) { |
| 182 | + TracingInfo root = null; |
| 183 | + for (TracingInfo tracingInfo : spans) { |
| 184 | + if (tracingInfo instanceof TestTracingInfo |
| 185 | + && ((TestTracingInfo) tracingInfo).getParent() == null) { |
| 186 | + assertNull(root); // There should be only one root. |
| 187 | + root = tracingInfo; |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + return root; |
| 192 | + } |
| 193 | + |
| 194 | + private ArrayList<TracingInfo> getChildren(Collection<TracingInfo> spans, TracingInfo parent) { |
| 195 | + ArrayList<TracingInfo> children = new ArrayList<TracingInfo>(); |
| 196 | + for (TracingInfo tracingInfo : spans) { |
| 197 | + if (tracingInfo instanceof TestTracingInfo |
| 198 | + && ((TestTracingInfo) tracingInfo).getParent() == parent) { |
| 199 | + children.add(tracingInfo); |
| 200 | + } |
| 201 | + } |
| 202 | + return children; |
| 203 | + } |
| 204 | +} |
0 commit comments