|
19 | 19 | package co.elastic.apm.agent.esrestclient;
|
20 | 20 |
|
21 | 21 | import co.elastic.apm.agent.AbstractInstrumentationTest;
|
| 22 | +import co.elastic.apm.agent.common.util.WildcardMatcher; |
| 23 | +import co.elastic.apm.agent.impl.context.Db; |
22 | 24 | import co.elastic.apm.agent.impl.transaction.Span;
|
23 | 25 | import co.elastic.apm.agent.impl.transaction.Transaction;
|
24 | 26 | import co.elastic.apm.agent.impl.transaction.TransactionTest;
|
25 | 27 | import org.apache.http.HttpHost;
|
26 | 28 | import org.apache.http.HttpVersion;
|
| 29 | +import org.apache.http.entity.ByteArrayEntity; |
27 | 30 | import org.apache.http.message.BasicStatusLine;
|
| 31 | +import org.assertj.core.api.Assertions; |
28 | 32 | import org.elasticsearch.client.Response;
|
29 | 33 | import org.junit.jupiter.api.AfterEach;
|
30 | 34 | import org.junit.jupiter.api.BeforeEach;
|
31 | 35 | import org.junit.jupiter.api.Test;
|
32 | 36 |
|
| 37 | +import java.util.List; |
33 | 38 | import java.util.Map;
|
34 | 39 |
|
| 40 | +import static co.elastic.apm.agent.esrestclient.ElasticsearchRestClientInstrumentationHelper.ELASTICSEARCH; |
35 | 41 | import static co.elastic.apm.agent.testutils.assertions.Assertions.assertThat;
|
36 | 42 | import static org.mockito.ArgumentMatchers.any;
|
37 | 43 | import static org.mockito.Mockito.doAnswer;
|
@@ -132,4 +138,45 @@ void testNonSampledSpan() {
|
132 | 138 | esSpan.deactivate().end();
|
133 | 139 | }
|
134 | 140 | }
|
| 141 | + |
| 142 | + |
| 143 | + @Test |
| 144 | + public void testCaptureBodyUrls() throws Exception { |
| 145 | + testCaptureBodyUrls(false); |
| 146 | + testCaptureBodyUrls(true); |
| 147 | + } |
| 148 | + |
| 149 | + public void testCaptureBodyUrls(boolean captureEverything) throws Exception { |
| 150 | + if (captureEverything) { |
| 151 | + doReturn(List.of(WildcardMatcher.valueOf("*"))) |
| 152 | + .when(config.getConfig(ElasticsearchConfiguration.class)) |
| 153 | + .getCaptureBodyUrls(); |
| 154 | + assertThat(config.getConfig(ElasticsearchConfiguration.class).getCaptureBodyUrls()).hasSize(1); |
| 155 | + } else { |
| 156 | + assertThat(config.getConfig(ElasticsearchConfiguration.class).getCaptureBodyUrls()).hasSizeGreaterThan(5); |
| 157 | + } |
| 158 | + |
| 159 | + Span span = (Span) helper.createClientSpan("GET", "/_test", |
| 160 | + new ByteArrayEntity(new byte[0])); |
| 161 | + assertThat(span).isNotNull(); |
| 162 | + assertThat(tracer.getActive()).isEqualTo(span); |
| 163 | + |
| 164 | + Response response = mockResponse(Map.of()); |
| 165 | + helper.finishClientSpan(response, span, null); |
| 166 | + span.deactivate(); |
| 167 | + |
| 168 | + assertThat(tracer.getActive()).isEqualTo(transaction); |
| 169 | + assertThat(span).hasType("db").hasSubType("elasticsearch"); |
| 170 | + assertThat(span.getContext().getServiceTarget()) |
| 171 | + .hasType("elasticsearch") |
| 172 | + .hasNoName(); |
| 173 | + |
| 174 | + Db db = span.getContext().getDb(); |
| 175 | + Assertions.assertThat(db.getType()).isEqualTo(ELASTICSEARCH); |
| 176 | + if (captureEverything) { |
| 177 | + assertThat((CharSequence) db.getStatementBuffer()).isNotNull(); |
| 178 | + } else { |
| 179 | + assertThat((CharSequence) db.getStatementBuffer()).isNull(); |
| 180 | + } |
| 181 | + } |
135 | 182 | }
|
0 commit comments