|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.instrumentation.geode; |
| 7 | + |
| 8 | +import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; |
| 9 | +import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; |
| 10 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 11 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 12 | + |
| 13 | +import io.opentelemetry.api.trace.SpanKind; |
| 14 | +import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; |
| 15 | +import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; |
| 16 | +import io.opentelemetry.semconv.trace.attributes.SemanticAttributes; |
| 17 | +import java.io.DataInput; |
| 18 | +import java.io.DataOutput; |
| 19 | +import java.io.IOException; |
| 20 | +import java.util.stream.Stream; |
| 21 | +import org.apache.geode.DataSerializable; |
| 22 | +import org.apache.geode.cache.Region; |
| 23 | +import org.apache.geode.cache.client.ClientCache; |
| 24 | +import org.apache.geode.cache.client.ClientCacheFactory; |
| 25 | +import org.apache.geode.cache.client.ClientRegionFactory; |
| 26 | +import org.apache.geode.cache.client.ClientRegionShortcut; |
| 27 | +import org.apache.geode.cache.query.QueryException; |
| 28 | +import org.apache.geode.cache.query.SelectResults; |
| 29 | +import org.junit.jupiter.api.Test; |
| 30 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 31 | +import org.junit.jupiter.params.ParameterizedTest; |
| 32 | +import org.junit.jupiter.params.provider.Arguments; |
| 33 | +import org.junit.jupiter.params.provider.MethodSource; |
| 34 | + |
| 35 | +class PutGetTest { |
| 36 | + @RegisterExtension |
| 37 | + private static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); |
| 38 | + |
| 39 | + static ClientCache cache = new ClientCacheFactory().create(); |
| 40 | + static ClientRegionFactory<Object, Object> regionFactory = |
| 41 | + cache.createClientRegionFactory(ClientRegionShortcut.LOCAL); |
| 42 | + static Region<Object, Object> region = regionFactory.create("test-region"); |
| 43 | + |
| 44 | + private static Stream<Arguments> provideParameters() { |
| 45 | + return Stream.of( |
| 46 | + Arguments.of("Hello", "World"), |
| 47 | + Arguments.of("Humpty", "Dumpty"), |
| 48 | + Arguments.of(Integer.valueOf(1), "One"), |
| 49 | + Arguments.of("One", Integer.valueOf(1))); |
| 50 | + } |
| 51 | + |
| 52 | + @ParameterizedTest |
| 53 | + @MethodSource("provideParameters") |
| 54 | + void testPutAndGet(Object key, Object value) { |
| 55 | + Object cacheValue = |
| 56 | + testing.runWithSpan( |
| 57 | + "someTrace", |
| 58 | + () -> { |
| 59 | + region.clear(); |
| 60 | + region.put(key, value); |
| 61 | + return region.get(key); |
| 62 | + }); |
| 63 | + assertEquals(value, cacheValue); |
| 64 | + assertGeodeTrace("get", null); |
| 65 | + } |
| 66 | + |
| 67 | + @ParameterizedTest |
| 68 | + @MethodSource("provideParameters") |
| 69 | + void testPutAndRemove(Object key, Object value) { |
| 70 | + testing.runWithSpan( |
| 71 | + "someTrace", |
| 72 | + () -> { |
| 73 | + region.clear(); |
| 74 | + region.put(key, value); |
| 75 | + region.remove(key); |
| 76 | + }); |
| 77 | + assertEquals(0, region.size()); |
| 78 | + assertGeodeTrace("remove", null); |
| 79 | + } |
| 80 | + |
| 81 | + @ParameterizedTest |
| 82 | + @MethodSource("provideParameters") |
| 83 | + void testQuery(Object key, Object value) throws QueryException { |
| 84 | + SelectResults<Object> cacheValue = |
| 85 | + testing.runWithSpan( |
| 86 | + "someTrace", |
| 87 | + () -> { |
| 88 | + region.clear(); |
| 89 | + region.put(key, value); |
| 90 | + return region.query("SELECT * FROM /test-region"); |
| 91 | + }); |
| 92 | + assertEquals(1, cacheValue.size()); |
| 93 | + assertGeodeTrace("query", "SELECT * FROM /test-region"); |
| 94 | + } |
| 95 | + |
| 96 | + @ParameterizedTest |
| 97 | + @MethodSource("provideParameters") |
| 98 | + void testExistsValue(Object key, Object value) throws QueryException { |
| 99 | + boolean cacheValue = |
| 100 | + testing.runWithSpan( |
| 101 | + "someTrace", |
| 102 | + () -> { |
| 103 | + region.clear(); |
| 104 | + region.put(key, value); |
| 105 | + return region.existsValue("SELECT * FROM /test-region"); |
| 106 | + }); |
| 107 | + assertTrue(cacheValue); |
| 108 | + assertGeodeTrace("existsValue", "SELECT * FROM /test-region"); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + void shouldSanitizeGeodeQuery() throws QueryException { |
| 113 | + Card value = new Card("1234432156788765", "10/2020"); |
| 114 | + SelectResults<Object> results = |
| 115 | + testing.runWithSpan( |
| 116 | + "someTrace", |
| 117 | + () -> { |
| 118 | + region.clear(); |
| 119 | + region.put(1, value); |
| 120 | + return region.query("SELECT * FROM /test-region p WHERE p.expDate = '10/2020'"); |
| 121 | + }); |
| 122 | + |
| 123 | + assertEquals(value, results.asList().get(0)); |
| 124 | + assertGeodeTrace("query", "SELECT * FROM /test-region p WHERE p.expDate = ?"); |
| 125 | + } |
| 126 | + |
| 127 | + void assertGeodeTrace(String verb, String query) { |
| 128 | + testing.waitAndAssertTraces( |
| 129 | + trace -> |
| 130 | + trace |
| 131 | + .hasSize(4) |
| 132 | + .hasSpansSatisfyingExactly( |
| 133 | + span -> span.hasName("someTrace").hasKind(SpanKind.INTERNAL), |
| 134 | + span -> |
| 135 | + span.hasName("clear test-region") |
| 136 | + .hasKind(SpanKind.CLIENT) |
| 137 | + .hasAttributesSatisfyingExactly( |
| 138 | + equalTo(SemanticAttributes.DB_SYSTEM, "geode"), |
| 139 | + equalTo(SemanticAttributes.DB_NAME, "test-region"), |
| 140 | + equalTo(SemanticAttributes.DB_OPERATION, "clear")), |
| 141 | + span -> |
| 142 | + span.hasName("put test-region") |
| 143 | + .hasKind(SpanKind.CLIENT) |
| 144 | + .hasAttributesSatisfyingExactly( |
| 145 | + equalTo(SemanticAttributes.DB_SYSTEM, "geode"), |
| 146 | + equalTo(SemanticAttributes.DB_NAME, "test-region"), |
| 147 | + equalTo(SemanticAttributes.DB_OPERATION, "put")), |
| 148 | + span -> |
| 149 | + span.hasName(verb.concat(" test-region")) |
| 150 | + .hasKind(SpanKind.CLIENT) |
| 151 | + .hasAttributesSatisfying( |
| 152 | + attributes -> { |
| 153 | + assertThat(attributes) |
| 154 | + .containsEntry(SemanticAttributes.DB_SYSTEM, "geode") |
| 155 | + .containsEntry(SemanticAttributes.DB_NAME, "test-region") |
| 156 | + .containsEntry(SemanticAttributes.DB_OPERATION, verb); |
| 157 | + if (query != null) { |
| 158 | + assertThat(attributes) |
| 159 | + .containsEntry(SemanticAttributes.DB_STATEMENT, query); |
| 160 | + } |
| 161 | + }))); |
| 162 | + } |
| 163 | + |
| 164 | + static class Card implements DataSerializable { |
| 165 | + String cardNumber; |
| 166 | + String expDate; |
| 167 | + |
| 168 | + public Card(String cardNumber, String expDate) { |
| 169 | + this.cardNumber = cardNumber; |
| 170 | + this.expDate = expDate; |
| 171 | + } |
| 172 | + |
| 173 | + public String getCardNumber() { |
| 174 | + return cardNumber; |
| 175 | + } |
| 176 | + |
| 177 | + public void setCardNumber(String cardNumber) { |
| 178 | + this.cardNumber = cardNumber; |
| 179 | + } |
| 180 | + |
| 181 | + public String getExpDate() { |
| 182 | + return expDate; |
| 183 | + } |
| 184 | + |
| 185 | + public void setExpDate(String expDate) { |
| 186 | + this.expDate = expDate; |
| 187 | + } |
| 188 | + |
| 189 | + @Override |
| 190 | + public void toData(DataOutput dataOutput) throws IOException { |
| 191 | + dataOutput.writeUTF(cardNumber); |
| 192 | + dataOutput.writeUTF(expDate); |
| 193 | + } |
| 194 | + |
| 195 | + @Override |
| 196 | + public void fromData(DataInput dataInput) throws IOException, ClassNotFoundException { |
| 197 | + cardNumber = dataInput.readUTF(); |
| 198 | + expDate = dataInput.readUTF(); |
| 199 | + } |
| 200 | + } |
| 201 | +} |
0 commit comments