|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * Elastic APM Java agent |
| 4 | + * %% |
| 5 | + * Copyright (C) 2018 Elastic and contributors |
| 6 | + * %% |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + * #L% |
| 19 | + */ |
| 20 | +package co.elastic.apm.es.restclient.v5_6; |
| 21 | + |
| 22 | +import co.elastic.apm.bci.ElasticApmInstrumentation; |
| 23 | +import co.elastic.apm.bci.VisibleForAdvice; |
| 24 | +import co.elastic.apm.impl.transaction.AbstractSpan; |
| 25 | +import co.elastic.apm.impl.transaction.Span; |
| 26 | +import net.bytebuddy.asm.Advice; |
| 27 | +import net.bytebuddy.description.method.MethodDescription; |
| 28 | +import net.bytebuddy.description.type.TypeDescription; |
| 29 | +import net.bytebuddy.matcher.ElementMatcher; |
| 30 | +import org.apache.http.HttpEntity; |
| 31 | +import org.elasticsearch.client.Response; |
| 32 | +import org.elasticsearch.client.ResponseException; |
| 33 | + |
| 34 | +import javax.annotation.Nullable; |
| 35 | +import java.io.IOException; |
| 36 | +import java.util.Collection; |
| 37 | +import java.util.Collections; |
| 38 | + |
| 39 | +import static net.bytebuddy.matcher.ElementMatchers.declaresMethod; |
| 40 | +import static net.bytebuddy.matcher.ElementMatchers.named; |
| 41 | +import static net.bytebuddy.matcher.ElementMatchers.not; |
| 42 | +import static net.bytebuddy.matcher.ElementMatchers.takesArgument; |
| 43 | +import static net.bytebuddy.matcher.ElementMatchers.takesArguments; |
| 44 | + |
| 45 | +/** |
| 46 | + * Instrumentation for Elasticsearch RestClient, currently supporting only synchronized queries. |
| 47 | + * All sync operations go through org.elasticsearch.client.RestClient#performRequest(org.elasticsearch.client.Request) |
| 48 | + */ |
| 49 | +public class ElasticsearchRestClientInstrumentation extends ElasticApmInstrumentation { |
| 50 | + @VisibleForAdvice |
| 51 | + public static final String SEARCH_QUERY_PATH_SUFFIX = "_search"; |
| 52 | + @VisibleForAdvice |
| 53 | + public static final String SPAN_TYPE = "db.elasticsearch.request"; |
| 54 | + @VisibleForAdvice |
| 55 | + public static final String DB_CONTEXT_TYPE = "elasticsearch"; |
| 56 | + |
| 57 | + @Advice.OnMethodEnter |
| 58 | + private static void onBeforeExecute(@Advice.Argument(0) String method, |
| 59 | + @Advice.Argument(1) String endpoint, |
| 60 | + @Advice.Argument(3) HttpEntity entity, |
| 61 | + @Advice.Local("span") Span span) { |
| 62 | + if (tracer == null) { |
| 63 | + return; |
| 64 | + } |
| 65 | + final AbstractSpan<?> activeSpan = tracer.activeSpan(); |
| 66 | + if (activeSpan == null || !activeSpan.isSampled()) { |
| 67 | + return; |
| 68 | + } |
| 69 | + span = activeSpan.createSpan() |
| 70 | + .withType(SPAN_TYPE) |
| 71 | + .appendToName("Elasticsearch: ").appendToName(method).appendToName(" ").appendToName(endpoint); |
| 72 | + span.getContext().getDb().withType(DB_CONTEXT_TYPE); |
| 73 | + span.activate(); |
| 74 | + |
| 75 | + if (span.isSampled()) { |
| 76 | + span.getContext().getHttp().withMethod(method); |
| 77 | + if (endpoint.endsWith(SEARCH_QUERY_PATH_SUFFIX)) { |
| 78 | + if (entity != null && entity.isRepeatable()) { |
| 79 | + try { |
| 80 | + String body = ESRestClientInstrumentationHelper.readRequestBody(entity.getContent(), endpoint); |
| 81 | + if (body != null && !body.isEmpty()) { |
| 82 | + span.getContext().getDb().withStatement(body); |
| 83 | + } |
| 84 | + } catch (IOException e) { |
| 85 | + // We can't log from here |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + @Advice.OnMethodExit(onThrowable = Throwable.class) |
| 93 | + public static void onAfterExecute(@Advice.Return @Nullable Response response, |
| 94 | + @Advice.Local("span") @Nullable Span span, |
| 95 | + @Advice.Thrown @Nullable Throwable t) { |
| 96 | + if (span != null) { |
| 97 | + try { |
| 98 | + String url = null; |
| 99 | + int statusCode = -1; |
| 100 | + if(response != null) { |
| 101 | + url = response.getHost().toURI(); |
| 102 | + statusCode = response.getStatusLine().getStatusCode(); |
| 103 | + } else if(t != null) { |
| 104 | + if (t instanceof ResponseException) { |
| 105 | + ResponseException esre = (ResponseException) t; |
| 106 | + url = esre.getResponse().getHost().toURI(); |
| 107 | + statusCode = esre.getResponse().getStatusLine().getStatusCode(); |
| 108 | + |
| 109 | + /* |
| 110 | + // Add tags so that they will be copied to error capture |
| 111 | + span.addTag(QUERY_STATUS_CODE_KEY, Integer.toString(statusCode)); |
| 112 | + span.addTag(ELASTICSEARCH_NODE_URL_KEY, url); |
| 113 | + span.addTag(ERROR_REASON_KEY, esre.getResponse().getStatusLine().getReasonPhrase()); |
| 114 | + */ |
| 115 | + } |
| 116 | + span.captureException(t); |
| 117 | + } |
| 118 | + |
| 119 | + if(url != null && !url.isEmpty()) { |
| 120 | + span.getContext().getHttp().withUrl(url); |
| 121 | + } |
| 122 | + if(statusCode > 0) { |
| 123 | + span.getContext().getHttp().withStatusCode(statusCode); |
| 124 | + } |
| 125 | + } finally { |
| 126 | + span.deactivate().end(); |
| 127 | + } |
| 128 | + |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public ElementMatcher<? super TypeDescription> getTypeMatcher() { |
| 134 | + return named("org.elasticsearch.client.RestClient"). |
| 135 | + and(not( |
| 136 | + declaresMethod(named("performRequest") |
| 137 | + .and(takesArguments(1) |
| 138 | + .and(takesArgument(0, named("org.elasticsearch.client.Request"))))))); |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public ElementMatcher<? super MethodDescription> getMethodMatcher() { |
| 143 | + return named("performRequest") |
| 144 | + .and(takesArguments(6) |
| 145 | + .and(takesArgument(4, named("org.elasticsearch.client.HttpAsyncResponseConsumerFactory")))); |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public Collection<String> getInstrumentationGroupNames() { |
| 150 | + return Collections.singleton("elasticsearch-restclient"); |
| 151 | + } |
| 152 | +} |
0 commit comments