Skip to content

Add option elasticsearch capture body urls #3091

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ communication - {pull}2996[#2996]
* Add the <<config-long-field-max-length>> config to enable capturing larger values for specific fields - {pull}3027[#3027]
* Provide fallback correlation when `ecs-logging-java` is used - {pull}3064[#3064]
* Added separate Java 8 build with updated log4j2 - {pull}3076[#3076]
* Add <<config-elasticsearch-capture-body-urls, elasticsearch_capture_body_urls>> option to customize which Elasticsearch request bodies are captured - {pull}3091[#3091]

[float]
===== Bug fixes
Expand Down
5 changes: 5 additions & 0 deletions apm-agent-builds/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@
<artifactId>apm-dubbo-plugin</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>apm-es-restclient-plugin-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>apm-es-restclient-plugin-5_6</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package co.elastic.apm.agent.esrestclient;

import co.elastic.apm.agent.common.util.WildcardMatcher;
import co.elastic.apm.agent.matcher.WildcardMatcherValueConverter;
import org.stagemonitor.configuration.ConfigurationOption;
import org.stagemonitor.configuration.ConfigurationOptionProvider;
import org.stagemonitor.configuration.converter.ListValueConverter;

import java.util.Arrays;
import java.util.List;

public class ElasticsearchConfiguration extends ConfigurationOptionProvider {
private final ConfigurationOption<List<WildcardMatcher>> captureBodyUrls = ConfigurationOption
.builder(new ListValueConverter<>(new WildcardMatcherValueConverter()), List.class)
.key("elasticsearch_capture_body_urls")
.configurationCategory("Datastore")
.description("The URL path patterns for which the APM agent will capture the request body of outgoing requests to Elasticsearch made with the `elasticsearch-restclient` instrumentation. The default setting captures the body for Elasticsearch REST APIs searches and counts.\n" +
"\n" +
"The captured request body (if any) is stored on the `span.db.statement` field. Captured request bodies are truncated to a maximum length defined by <<config-long-field-max-length>>." +
"\n" +
WildcardMatcher.DOCUMENTATION
)
.tags("added[1.37.0]")
.dynamic(true)
.buildWithDefault(Arrays.asList(
WildcardMatcher.valueOf("*_search"),
WildcardMatcher.valueOf("*_msearch"),
WildcardMatcher.valueOf("*_msearch/template"),
WildcardMatcher.valueOf("*_search/template"),
WildcardMatcher.valueOf("*_count"),
WildcardMatcher.valueOf("*_sql"),
WildcardMatcher.valueOf("*_eql/search"),
WildcardMatcher.valueOf("*_async_search")
));

public List<WildcardMatcher> getCaptureBodyUrls() {
return captureBodyUrls.get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,27 +48,24 @@ public class ElasticsearchRestClientInstrumentationHelper {
private static final Logger unsupportedOperationOnceLogger = LoggerUtils.logOnce(logger);
private static final ElasticsearchRestClientInstrumentationHelper INSTANCE = new ElasticsearchRestClientInstrumentationHelper(GlobalTracer.get());

public static final List<WildcardMatcher> QUERY_WILDCARD_MATCHERS = Arrays.asList(
WildcardMatcher.valueOf("*_search"),
WildcardMatcher.valueOf("*_msearch"),
WildcardMatcher.valueOf("*_msearch/template"),
WildcardMatcher.valueOf("*_search/template"),
WildcardMatcher.valueOf("*_count"));
public static final String SPAN_TYPE = "db";
public static final String ELASTICSEARCH = "elasticsearch";
public static final String SPAN_ACTION = "request";
private static final int MAX_POOLED_ELEMENTS = 256;
private final Tracer tracer;
private final ElasticsearchConfiguration config;

private final ObjectPool<ResponseListenerWrapper> responseListenerObjectPool;

public static ElasticsearchRestClientInstrumentationHelper get() {
return INSTANCE;
}


private ElasticsearchRestClientInstrumentationHelper(Tracer tracer) {
this.tracer = tracer;
this.responseListenerObjectPool = tracer.getObjectPoolFactory().createRecyclableObjectPool(MAX_POOLED_ELEMENTS, new ResponseListenerAllocator());
this.config = tracer.getConfig(ElasticsearchConfiguration.class);
}

private class ResponseListenerAllocator implements Allocator<ResponseListenerWrapper> {
Expand Down Expand Up @@ -99,10 +96,9 @@ public Span<?> createClientSpan(String method, String endpoint, @Nullable HttpEn
span.getContext().getDb().withType(ELASTICSEARCH);
span.getContext().getServiceTarget().withType(ELASTICSEARCH);
span.activate();

if (span.isSampled()) {
span.getContext().getHttp().withMethod(method);
if (WildcardMatcher.isAnyMatch(QUERY_WILDCARD_MATCHERS, endpoint)) {
if (WildcardMatcher.isAnyMatch(config.getCaptureBodyUrls(), endpoint)) {
if (httpEntity != null && httpEntity.isRepeatable()) {
try {
IOUtils.readUtf8Stream(httpEntity.getContent(), span.getContext().getDb().withStatementBuffer());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
co.elastic.apm.agent.esrestclient.ElasticsearchConfiguration
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,25 @@
package co.elastic.apm.agent.esrestclient;

import co.elastic.apm.agent.AbstractInstrumentationTest;
import co.elastic.apm.agent.common.util.WildcardMatcher;
import co.elastic.apm.agent.impl.context.Db;
import co.elastic.apm.agent.impl.transaction.Span;
import co.elastic.apm.agent.impl.transaction.Transaction;
import co.elastic.apm.agent.impl.transaction.TransactionTest;
import org.apache.http.HttpHost;
import org.apache.http.HttpVersion;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.message.BasicStatusLine;
import org.assertj.core.api.Assertions;
import org.elasticsearch.client.Response;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.Map;

import static co.elastic.apm.agent.esrestclient.ElasticsearchRestClientInstrumentationHelper.ELASTICSEARCH;
import static co.elastic.apm.agent.testutils.assertions.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doAnswer;
Expand Down Expand Up @@ -132,4 +138,45 @@ void testNonSampledSpan() {
esSpan.deactivate().end();
}
}


@Test
public void testCaptureBodyUrls() throws Exception {
testCaptureBodyUrls(false);
testCaptureBodyUrls(true);
}

public void testCaptureBodyUrls(boolean captureEverything) throws Exception {
if (captureEverything) {
doReturn(List.of(WildcardMatcher.valueOf("*")))
.when(config.getConfig(ElasticsearchConfiguration.class))
.getCaptureBodyUrls();
assertThat(config.getConfig(ElasticsearchConfiguration.class).getCaptureBodyUrls()).hasSize(1);
} else {
assertThat(config.getConfig(ElasticsearchConfiguration.class).getCaptureBodyUrls()).hasSizeGreaterThan(5);
}

Span span = (Span) helper.createClientSpan("GET", "/_test",
new ByteArrayEntity(new byte[0]));
assertThat(span).isNotNull();
assertThat(tracer.getActive()).isEqualTo(span);

Response response = mockResponse(Map.of());
helper.finishClientSpan(response, span, null);
span.deactivate();

assertThat(tracer.getActive()).isEqualTo(transaction);
assertThat(span).hasType("db").hasSubType("elasticsearch");
assertThat(span.getContext().getServiceTarget())
.hasType("elasticsearch")
.hasNoName();

Db db = span.getContext().getDb();
Assertions.assertThat(db.getType()).isEqualTo(ELASTICSEARCH);
if (captureEverything) {
assertThat((CharSequence) db.getStatementBuffer()).isNotNull();
} else {
assertThat((CharSequence) db.getStatementBuffer()).isNull();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class MongoConfiguration extends ConfigurationOptionProvider {
private final ConfigurationOption<List<WildcardMatcher>> captureStatementCommands = ConfigurationOption
.builder(new ListValueConverter<>(new WildcardMatcherValueConverter()), List.class)
.key("mongodb_capture_statement_commands")
.configurationCategory("MongoDB")
.configurationCategory("Datastore")
.description("MongoDB command names for which the command document will be captured, limited to common read-only operations by default.\n" +
"Set to ` \"\"` (empty) to disable capture, and `\"*\"` to capture all (which is discouraged as it may lead to sensitive information capture).\n" +
"\n" +
Expand Down
Loading