Skip to content
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
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
/*-
* #%L
* Elastic APM Java agent
* %%
* Copyright (C) 2018 Elastic and contributors
* %%
* Licensed 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.
* #L%
*/
package co.elastic.apm.plugin.api;

import co.elastic.apm.bci.ElasticApmInstrumentation;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<artifactId>apm-es-restclient-plugin</artifactId>
<groupId>co.elastic.apm</groupId>
<version>0.9.0-SNAPSHOT</version>
</parent>

<artifactId>apm-es-restclient-plugin-5_6</artifactId>
<name>${project.groupId}:${project.artifactId}</name>

<properties>
<version.elasticsearch>5.6.0</version.elasticsearch>
</properties>

<dependencies>
<!-- Elasticsearch rest client -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>${version.elasticsearch}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>${version.elasticsearch}</version>
<scope>provided</scope>
</dependency>

<!-- Elasticsearch testcontainer -->
<dependency>
<groupId>fr.pilato.elasticsearch.testcontainers</groupId>
<artifactId>testcontainers-elasticsearch</artifactId>
<version>0.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* limitations under the License.
* #L%
*/
package co.elastic.apm.es.restclient;
package co.elastic.apm.es.restclient.v5_6;

import co.elastic.apm.bci.VisibleForAdvice;
import co.elastic.apm.report.serialize.DslJsonSerializer;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/*-
* #%L
* Elastic APM Java agent
* %%
* Copyright (C) 2018 Elastic and contributors
* %%
* Licensed 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.
* #L%
*/
package co.elastic.apm.es.restclient.v5_6;

import co.elastic.apm.bci.ElasticApmInstrumentation;
import co.elastic.apm.bci.VisibleForAdvice;
import co.elastic.apm.impl.transaction.AbstractSpan;
import co.elastic.apm.impl.transaction.Span;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
import org.apache.http.HttpEntity;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.ResponseException;

import javax.annotation.Nullable;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;

import static net.bytebuddy.matcher.ElementMatchers.declaresMethod;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.not;
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
import static net.bytebuddy.matcher.ElementMatchers.takesArguments;

/**
* Instrumentation for Elasticsearch RestClient, currently supporting only synchronized queries.
* All sync operations go through org.elasticsearch.client.RestClient#performRequest(org.elasticsearch.client.Request)
*/
public class ElasticsearchRestClientInstrumentation extends ElasticApmInstrumentation {
@VisibleForAdvice
public static final String SEARCH_QUERY_PATH_SUFFIX = "_search";
@VisibleForAdvice
public static final String SPAN_TYPE = "db.elasticsearch.request";
@VisibleForAdvice
public static final String DB_CONTEXT_TYPE = "elasticsearch";

@Advice.OnMethodEnter
private static void onBeforeExecute(@Advice.Argument(0) String method,
@Advice.Argument(1) String endpoint,
@Advice.Argument(3) HttpEntity entity,
@Advice.Local("span") Span span) {
if (tracer == null) {
return;
}
final AbstractSpan<?> activeSpan = tracer.activeSpan();
if (activeSpan == null || !activeSpan.isSampled()) {
return;
}
span = activeSpan.createSpan()
.withType(SPAN_TYPE)
.appendToName("Elasticsearch: ").appendToName(method).appendToName(" ").appendToName(endpoint);
span.getContext().getDb().withType(DB_CONTEXT_TYPE);
span.activate();

if (span.isSampled()) {
span.getContext().getHttp().withMethod(method);
if (endpoint.endsWith(SEARCH_QUERY_PATH_SUFFIX)) {
if (entity != null && entity.isRepeatable()) {
try {
String body = ESRestClientInstrumentationHelper.readRequestBody(entity.getContent(), endpoint);
if (body != null && !body.isEmpty()) {
span.getContext().getDb().withStatement(body);
}
} catch (IOException e) {
// We can't log from here
}
}
}
}
}

@Advice.OnMethodExit(onThrowable = Throwable.class)
public static void onAfterExecute(@Advice.Return @Nullable Response response,
@Advice.Local("span") @Nullable Span span,
@Advice.Thrown @Nullable Throwable t) {
if (span != null) {
try {
String url = null;
int statusCode = -1;
if(response != null) {
url = response.getHost().toURI();
statusCode = response.getStatusLine().getStatusCode();
} else if(t != null) {
if (t instanceof ResponseException) {
ResponseException esre = (ResponseException) t;
url = esre.getResponse().getHost().toURI();
statusCode = esre.getResponse().getStatusLine().getStatusCode();

/*
// Add tags so that they will be copied to error capture
span.addTag(QUERY_STATUS_CODE_KEY, Integer.toString(statusCode));
span.addTag(ELASTICSEARCH_NODE_URL_KEY, url);
span.addTag(ERROR_REASON_KEY, esre.getResponse().getStatusLine().getReasonPhrase());
*/
}
span.captureException(t);
}

if(url != null && !url.isEmpty()) {
span.getContext().getHttp().withUrl(url);
}
if(statusCode > 0) {
span.getContext().getHttp().withStatusCode(statusCode);
}
} finally {
span.deactivate().end();
}

}
}

@Override
public ElementMatcher<? super TypeDescription> getTypeMatcher() {
return named("org.elasticsearch.client.RestClient").
and(not(
declaresMethod(named("performRequest")
.and(takesArguments(1)
.and(takesArgument(0, named("org.elasticsearch.client.Request")))))));
}

@Override
public ElementMatcher<? super MethodDescription> getMethodMatcher() {
return named("performRequest")
.and(takesArguments(6)
.and(takesArgument(4, named("org.elasticsearch.client.HttpAsyncResponseConsumerFactory"))));
}

@Override
public Collection<String> getInstrumentationGroupNames() {
return Collections.singleton("elasticsearch-restclient");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@
* #L%
*/
@NonnullApi
package co.elastic.apm.es.restclient;
package co.elastic.apm.es.restclient.v5_6;

import co.elastic.apm.annotation.NonnullApi;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
co.elastic.apm.es.restclient.v5_6.ElasticsearchRestClientInstrumentation
Loading