make elasticsearch indy-ready (#14672)
This commit is contained in:
		
							parent
							
								
									626c296066
								
							
						
					
					
						commit
						75a342995c
					
				| 
						 | 
					@ -42,4 +42,9 @@ public class ElasticsearchApiClientInstrumentationModule extends Instrumentation
 | 
				
			||||||
    return asList(
 | 
					    return asList(
 | 
				
			||||||
        new RestClientTransportInstrumentation(), new RestClientHttpClientInstrumentation());
 | 
					        new RestClientTransportInstrumentation(), new RestClientHttpClientInstrumentation());
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  @Override
 | 
				
			||||||
 | 
					  public boolean isIndyReady() {
 | 
				
			||||||
 | 
					    return true;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,18 @@
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * Copyright The OpenTelemetry Authors
 | 
				
			||||||
 | 
					 * SPDX-License-Identifier: Apache-2.0
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					package io.opentelemetry.javaagent.instrumentation.elasticsearch.apiclient;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import io.opentelemetry.instrumentation.api.util.VirtualField;
 | 
				
			||||||
 | 
					import io.opentelemetry.instrumentation.elasticsearch.rest.common.v5_0.internal.ElasticsearchEndpointDefinition;
 | 
				
			||||||
 | 
					import org.elasticsearch.client.Request;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					public class ElasticsearchApiClientSingletons {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  public static final VirtualField<Request, ElasticsearchEndpointDefinition> ENDPOINT_DEFINITION =
 | 
				
			||||||
 | 
					      VirtualField.find(Request.class, ElasticsearchEndpointDefinition.class);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  private ElasticsearchApiClientSingletons() {}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -5,6 +5,7 @@
 | 
				
			||||||
 | 
					
 | 
				
			||||||
package io.opentelemetry.javaagent.instrumentation.elasticsearch.apiclient;
 | 
					package io.opentelemetry.javaagent.instrumentation.elasticsearch.apiclient;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import static io.opentelemetry.javaagent.instrumentation.elasticsearch.apiclient.ElasticsearchApiClientSingletons.ENDPOINT_DEFINITION;
 | 
				
			||||||
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
 | 
					import static net.bytebuddy.matcher.ElementMatchers.isMethod;
 | 
				
			||||||
import static net.bytebuddy.matcher.ElementMatchers.named;
 | 
					import static net.bytebuddy.matcher.ElementMatchers.named;
 | 
				
			||||||
import static net.bytebuddy.matcher.ElementMatchers.namedOneOf;
 | 
					import static net.bytebuddy.matcher.ElementMatchers.namedOneOf;
 | 
				
			||||||
| 
						 | 
					@ -13,8 +14,6 @@ import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import io.opentelemetry.context.Context;
 | 
					import io.opentelemetry.context.Context;
 | 
				
			||||||
import io.opentelemetry.context.Scope;
 | 
					import io.opentelemetry.context.Scope;
 | 
				
			||||||
import io.opentelemetry.instrumentation.api.util.VirtualField;
 | 
					 | 
				
			||||||
import io.opentelemetry.instrumentation.elasticsearch.rest.common.v5_0.internal.ElasticsearchEndpointDefinition;
 | 
					 | 
				
			||||||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
 | 
					import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
 | 
				
			||||||
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
 | 
					import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
 | 
				
			||||||
import net.bytebuddy.asm.Advice;
 | 
					import net.bytebuddy.asm.Advice;
 | 
				
			||||||
| 
						 | 
					@ -72,8 +71,7 @@ public class RestClientHttpClientInstrumentation implements TypeInstrumentation
 | 
				
			||||||
      if (endpointId.startsWith("es/") && endpointId.length() > 3) {
 | 
					      if (endpointId.startsWith("es/") && endpointId.length() > 3) {
 | 
				
			||||||
        endpointId = endpointId.substring(3);
 | 
					        endpointId = endpointId.substring(3);
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
      VirtualField.find(Request.class, ElasticsearchEndpointDefinition.class)
 | 
					      ENDPOINT_DEFINITION.set(request, ElasticsearchEndpointMap.get(endpointId));
 | 
				
			||||||
          .set(request, ElasticsearchEndpointMap.get(endpointId));
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -5,14 +5,13 @@
 | 
				
			||||||
 | 
					
 | 
				
			||||||
package io.opentelemetry.javaagent.instrumentation.elasticsearch.apiclient;
 | 
					package io.opentelemetry.javaagent.instrumentation.elasticsearch.apiclient;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import static io.opentelemetry.javaagent.instrumentation.elasticsearch.apiclient.ElasticsearchApiClientSingletons.ENDPOINT_DEFINITION;
 | 
				
			||||||
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
 | 
					import static net.bytebuddy.matcher.ElementMatchers.isMethod;
 | 
				
			||||||
import static net.bytebuddy.matcher.ElementMatchers.named;
 | 
					import static net.bytebuddy.matcher.ElementMatchers.named;
 | 
				
			||||||
import static net.bytebuddy.matcher.ElementMatchers.returns;
 | 
					import static net.bytebuddy.matcher.ElementMatchers.returns;
 | 
				
			||||||
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
 | 
					import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import co.elastic.clients.transport.Endpoint;
 | 
					import co.elastic.clients.transport.Endpoint;
 | 
				
			||||||
import io.opentelemetry.instrumentation.api.util.VirtualField;
 | 
					 | 
				
			||||||
import io.opentelemetry.instrumentation.elasticsearch.rest.common.v5_0.internal.ElasticsearchEndpointDefinition;
 | 
					 | 
				
			||||||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
 | 
					import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
 | 
				
			||||||
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
 | 
					import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
 | 
				
			||||||
import net.bytebuddy.asm.Advice;
 | 
					import net.bytebuddy.asm.Advice;
 | 
				
			||||||
| 
						 | 
					@ -44,13 +43,11 @@ public class RestClientTransportInstrumentation implements TypeInstrumentation {
 | 
				
			||||||
    @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
 | 
					    @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
 | 
				
			||||||
    public static void onPrepareLowLevelRequest(
 | 
					    public static void onPrepareLowLevelRequest(
 | 
				
			||||||
        @Advice.Argument(1) Endpoint<?, ?, ?> endpoint, @Advice.Return Request request) {
 | 
					        @Advice.Argument(1) Endpoint<?, ?, ?> endpoint, @Advice.Return Request request) {
 | 
				
			||||||
      VirtualField<Request, ElasticsearchEndpointDefinition> virtualField =
 | 
					 | 
				
			||||||
          VirtualField.find(Request.class, ElasticsearchEndpointDefinition.class);
 | 
					 | 
				
			||||||
      String endpointId = endpoint.id();
 | 
					      String endpointId = endpoint.id();
 | 
				
			||||||
      if (endpointId.startsWith("es/") && endpointId.length() > 3) {
 | 
					      if (endpointId.startsWith("es/") && endpointId.length() > 3) {
 | 
				
			||||||
        endpointId = endpointId.substring(3);
 | 
					        endpointId = endpointId.substring(3);
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
      virtualField.set(request, ElasticsearchEndpointMap.get(endpointId));
 | 
					      ENDPOINT_DEFINITION.set(request, ElasticsearchEndpointMap.get(endpointId));
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -10,10 +10,12 @@ import static java.util.Collections.singletonList;
 | 
				
			||||||
import com.google.auto.service.AutoService;
 | 
					import com.google.auto.service.AutoService;
 | 
				
			||||||
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
 | 
					import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
 | 
				
			||||||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
 | 
					import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
 | 
				
			||||||
 | 
					import io.opentelemetry.javaagent.extension.instrumentation.internal.ExperimentalInstrumentationModule;
 | 
				
			||||||
import java.util.List;
 | 
					import java.util.List;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@AutoService(InstrumentationModule.class)
 | 
					@AutoService(InstrumentationModule.class)
 | 
				
			||||||
public class ElasticsearchRest5InstrumentationModule extends InstrumentationModule {
 | 
					public class ElasticsearchRest5InstrumentationModule extends InstrumentationModule
 | 
				
			||||||
 | 
					    implements ExperimentalInstrumentationModule {
 | 
				
			||||||
  public ElasticsearchRest5InstrumentationModule() {
 | 
					  public ElasticsearchRest5InstrumentationModule() {
 | 
				
			||||||
    super("elasticsearch-rest", "elasticsearch-rest-5.0", "elasticsearch");
 | 
					    super("elasticsearch-rest", "elasticsearch-rest-5.0", "elasticsearch");
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
| 
						 | 
					@ -22,4 +24,9 @@ public class ElasticsearchRest5InstrumentationModule extends InstrumentationModu
 | 
				
			||||||
  public List<TypeInstrumentation> typeInstrumentations() {
 | 
					  public List<TypeInstrumentation> typeInstrumentations() {
 | 
				
			||||||
    return singletonList(new RestClientInstrumentation());
 | 
					    return singletonList(new RestClientInstrumentation());
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  @Override
 | 
				
			||||||
 | 
					  public boolean isIndyReady() {
 | 
				
			||||||
 | 
					    return true;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -19,7 +19,10 @@ import io.opentelemetry.instrumentation.elasticsearch.rest.common.v5_0.internal.
 | 
				
			||||||
import io.opentelemetry.instrumentation.elasticsearch.rest.common.v5_0.internal.RestResponseListener;
 | 
					import io.opentelemetry.instrumentation.elasticsearch.rest.common.v5_0.internal.RestResponseListener;
 | 
				
			||||||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
 | 
					import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
 | 
				
			||||||
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
 | 
					import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
 | 
				
			||||||
 | 
					import javax.annotation.Nullable;
 | 
				
			||||||
import net.bytebuddy.asm.Advice;
 | 
					import net.bytebuddy.asm.Advice;
 | 
				
			||||||
 | 
					import net.bytebuddy.asm.Advice.AssignReturned;
 | 
				
			||||||
 | 
					import net.bytebuddy.asm.Advice.AssignReturned.ToArguments.ToArgument;
 | 
				
			||||||
import net.bytebuddy.description.type.TypeDescription;
 | 
					import net.bytebuddy.description.type.TypeDescription;
 | 
				
			||||||
import net.bytebuddy.matcher.ElementMatcher;
 | 
					import net.bytebuddy.matcher.ElementMatcher;
 | 
				
			||||||
import org.elasticsearch.client.ResponseListener;
 | 
					import org.elasticsearch.client.ResponseListener;
 | 
				
			||||||
| 
						 | 
					@ -45,45 +48,67 @@ public class RestClientInstrumentation implements TypeInstrumentation {
 | 
				
			||||||
  @SuppressWarnings("unused")
 | 
					  @SuppressWarnings("unused")
 | 
				
			||||||
  public static class PerformRequestAsyncAdvice {
 | 
					  public static class PerformRequestAsyncAdvice {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Advice.OnMethodEnter(suppress = Throwable.class)
 | 
					    public static class AdviceScope {
 | 
				
			||||||
    public static void onEnter(
 | 
					      private final ElasticsearchRestRequest request;
 | 
				
			||||||
        @Advice.Argument(0) String method,
 | 
					      private final Context parentContext;
 | 
				
			||||||
        @Advice.Argument(1) String endpoint,
 | 
					      private final Context context;
 | 
				
			||||||
        @Advice.Local("otelRequest") ElasticsearchRestRequest request,
 | 
					      private final Scope scope;
 | 
				
			||||||
        @Advice.Local("otelContext") Context context,
 | 
					 | 
				
			||||||
        @Advice.Local("otelScope") Scope scope,
 | 
					 | 
				
			||||||
        @Advice.Argument(value = 5, readOnly = false) ResponseListener responseListener) {
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
      Context parentContext = currentContext();
 | 
					      private AdviceScope(
 | 
				
			||||||
      request = ElasticsearchRestRequest.create(method, endpoint);
 | 
					          ElasticsearchRestRequest request, Context parentContext, Context context, Scope scope) {
 | 
				
			||||||
      if (!instrumenter().shouldStart(parentContext, request)) {
 | 
					        this.request = request;
 | 
				
			||||||
        return;
 | 
					        this.parentContext = parentContext;
 | 
				
			||||||
 | 
					        this.context = context;
 | 
				
			||||||
 | 
					        this.scope = scope;
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      context = instrumenter().start(parentContext, request);
 | 
					      @Nullable
 | 
				
			||||||
      scope = context.makeCurrent();
 | 
					      public static AdviceScope start(ElasticsearchRestRequest request) {
 | 
				
			||||||
 | 
					        Context parentContext = currentContext();
 | 
				
			||||||
 | 
					        if (!instrumenter().shouldStart(parentContext, request)) {
 | 
				
			||||||
 | 
					          return null;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        Context context = instrumenter().start(parentContext, request);
 | 
				
			||||||
 | 
					        return new AdviceScope(request, parentContext, context, context.makeCurrent());
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      responseListener =
 | 
					      public RestResponseListener wrapListener(ResponseListener listener) {
 | 
				
			||||||
          new RestResponseListener(
 | 
					        return new RestResponseListener(listener, parentContext, instrumenter(), context, request);
 | 
				
			||||||
              responseListener, parentContext, instrumenter(), context, request);
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      public void end(@Nullable Throwable throwable) {
 | 
				
			||||||
 | 
					        scope.close();
 | 
				
			||||||
 | 
					        if (throwable != null) {
 | 
				
			||||||
 | 
					          instrumenter().end(context, request, null, throwable);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        // span ended in RestResponseListener
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @AssignReturned.ToArguments(@ToArgument(value = 5, index = 1))
 | 
				
			||||||
 | 
					    @Advice.OnMethodEnter(suppress = Throwable.class)
 | 
				
			||||||
 | 
					    public static Object[] onEnter(
 | 
				
			||||||
 | 
					        @Advice.Argument(0) String method,
 | 
				
			||||||
 | 
					        @Advice.Argument(1) String endpoint,
 | 
				
			||||||
 | 
					        @Advice.Argument(5) ResponseListener originalResponseListener) {
 | 
				
			||||||
 | 
					      ResponseListener responseListener = originalResponseListener;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      ElasticsearchRestRequest request = ElasticsearchRestRequest.create(method, endpoint);
 | 
				
			||||||
 | 
					      AdviceScope adviceScope = AdviceScope.start(request);
 | 
				
			||||||
 | 
					      if (adviceScope == null) {
 | 
				
			||||||
 | 
					        return new Object[] {null, responseListener};
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					      responseListener = adviceScope.wrapListener(responseListener);
 | 
				
			||||||
 | 
					      return new Object[] {adviceScope, responseListener};
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
 | 
					    @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
 | 
				
			||||||
    public static void stopSpan(
 | 
					    public static void stopSpan(
 | 
				
			||||||
        @Advice.Thrown Throwable throwable,
 | 
					        @Advice.Thrown Throwable throwable, @Advice.Enter Object[] enterResult) {
 | 
				
			||||||
        @Advice.Local("otelRequest") ElasticsearchRestRequest request,
 | 
					      AdviceScope adviceScope = (AdviceScope) enterResult[0];
 | 
				
			||||||
        @Advice.Local("otelContext") Context context,
 | 
					      if (adviceScope != null) {
 | 
				
			||||||
        @Advice.Local("otelScope") Scope scope) {
 | 
					        adviceScope.end(throwable);
 | 
				
			||||||
 | 
					 | 
				
			||||||
      if (scope == null) {
 | 
					 | 
				
			||||||
        return;
 | 
					 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
      scope.close();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      if (throwable != null) {
 | 
					 | 
				
			||||||
        instrumenter().end(context, request, null, throwable);
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
      // span ended in RestResponseListener
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -12,11 +12,13 @@ import static net.bytebuddy.matcher.ElementMatchers.not;
 | 
				
			||||||
import com.google.auto.service.AutoService;
 | 
					import com.google.auto.service.AutoService;
 | 
				
			||||||
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
 | 
					import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
 | 
				
			||||||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
 | 
					import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
 | 
				
			||||||
 | 
					import io.opentelemetry.javaagent.extension.instrumentation.internal.ExperimentalInstrumentationModule;
 | 
				
			||||||
import java.util.List;
 | 
					import java.util.List;
 | 
				
			||||||
import net.bytebuddy.matcher.ElementMatcher;
 | 
					import net.bytebuddy.matcher.ElementMatcher;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@AutoService(InstrumentationModule.class)
 | 
					@AutoService(InstrumentationModule.class)
 | 
				
			||||||
public class ElasticsearchRest6InstrumentationModule extends InstrumentationModule {
 | 
					public class ElasticsearchRest6InstrumentationModule extends InstrumentationModule
 | 
				
			||||||
 | 
					    implements ExperimentalInstrumentationModule {
 | 
				
			||||||
  public ElasticsearchRest6InstrumentationModule() {
 | 
					  public ElasticsearchRest6InstrumentationModule() {
 | 
				
			||||||
    super("elasticsearch-rest", "elasticsearch-rest-6.4", "elasticsearch");
 | 
					    super("elasticsearch-rest", "elasticsearch-rest-6.4", "elasticsearch");
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
| 
						 | 
					@ -31,4 +33,9 @@ public class ElasticsearchRest6InstrumentationModule extends InstrumentationModu
 | 
				
			||||||
  public List<TypeInstrumentation> typeInstrumentations() {
 | 
					  public List<TypeInstrumentation> typeInstrumentations() {
 | 
				
			||||||
    return singletonList(new RestClientInstrumentation());
 | 
					    return singletonList(new RestClientInstrumentation());
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  @Override
 | 
				
			||||||
 | 
					  public boolean isIndyReady() {
 | 
				
			||||||
 | 
					    return true;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -18,7 +18,10 @@ import io.opentelemetry.instrumentation.elasticsearch.rest.common.v5_0.internal.
 | 
				
			||||||
import io.opentelemetry.instrumentation.elasticsearch.rest.common.v5_0.internal.RestResponseListener;
 | 
					import io.opentelemetry.instrumentation.elasticsearch.rest.common.v5_0.internal.RestResponseListener;
 | 
				
			||||||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
 | 
					import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
 | 
				
			||||||
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
 | 
					import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
 | 
				
			||||||
 | 
					import javax.annotation.Nullable;
 | 
				
			||||||
import net.bytebuddy.asm.Advice;
 | 
					import net.bytebuddy.asm.Advice;
 | 
				
			||||||
 | 
					import net.bytebuddy.asm.Advice.AssignReturned;
 | 
				
			||||||
 | 
					import net.bytebuddy.asm.Advice.AssignReturned.ToArguments.ToArgument;
 | 
				
			||||||
import net.bytebuddy.description.type.TypeDescription;
 | 
					import net.bytebuddy.description.type.TypeDescription;
 | 
				
			||||||
import net.bytebuddy.matcher.ElementMatcher;
 | 
					import net.bytebuddy.matcher.ElementMatcher;
 | 
				
			||||||
import org.elasticsearch.client.Request;
 | 
					import org.elasticsearch.client.Request;
 | 
				
			||||||
| 
						 | 
					@ -44,44 +47,67 @@ public class RestClientInstrumentation implements TypeInstrumentation {
 | 
				
			||||||
  @SuppressWarnings("unused")
 | 
					  @SuppressWarnings("unused")
 | 
				
			||||||
  public static class PerformRequestAsyncAdvice {
 | 
					  public static class PerformRequestAsyncAdvice {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Advice.OnMethodEnter(suppress = Throwable.class)
 | 
					    public static class AdviceScope {
 | 
				
			||||||
    public static void onEnter(
 | 
					      private final ElasticsearchRestRequest request;
 | 
				
			||||||
        @Advice.Argument(0) Request request,
 | 
					      private final Context parentContext;
 | 
				
			||||||
        @Advice.Argument(value = 1, readOnly = false) ResponseListener responseListener,
 | 
					      private final Context context;
 | 
				
			||||||
        @Advice.Local("otelRequest") ElasticsearchRestRequest otelRequest,
 | 
					      private final Scope scope;
 | 
				
			||||||
        @Advice.Local("otelContext") Context context,
 | 
					 | 
				
			||||||
        @Advice.Local("otelScope") Scope scope) {
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
      Context parentContext = currentContext();
 | 
					      private AdviceScope(
 | 
				
			||||||
      otelRequest = ElasticsearchRestRequest.create(request.getMethod(), request.getEndpoint());
 | 
					          ElasticsearchRestRequest request, Context parentContext, Context context, Scope scope) {
 | 
				
			||||||
      if (!instrumenter().shouldStart(parentContext, otelRequest)) {
 | 
					        this.request = request;
 | 
				
			||||||
        return;
 | 
					        this.parentContext = parentContext;
 | 
				
			||||||
 | 
					        this.context = context;
 | 
				
			||||||
 | 
					        this.scope = scope;
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      context = instrumenter().start(parentContext, otelRequest);
 | 
					      @Nullable
 | 
				
			||||||
      scope = context.makeCurrent();
 | 
					      public static AdviceScope start(ElasticsearchRestRequest request) {
 | 
				
			||||||
 | 
					        Context parentContext = currentContext();
 | 
				
			||||||
 | 
					        if (!instrumenter().shouldStart(parentContext, request)) {
 | 
				
			||||||
 | 
					          return null;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        Context context = instrumenter().start(parentContext, request);
 | 
				
			||||||
 | 
					        return new AdviceScope(request, parentContext, context, context.makeCurrent());
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      responseListener =
 | 
					      public RestResponseListener wrapListener(ResponseListener listener) {
 | 
				
			||||||
          new RestResponseListener(
 | 
					        return new RestResponseListener(listener, parentContext, instrumenter(), context, request);
 | 
				
			||||||
              responseListener, parentContext, instrumenter(), context, otelRequest);
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      public void end(@Nullable Throwable throwable) {
 | 
				
			||||||
 | 
					        scope.close();
 | 
				
			||||||
 | 
					        if (throwable != null) {
 | 
				
			||||||
 | 
					          instrumenter().end(context, request, null, throwable);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        // span ended in RestResponseListener
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @AssignReturned.ToArguments(@ToArgument(value = 1, index = 1))
 | 
				
			||||||
 | 
					    @Advice.OnMethodEnter(suppress = Throwable.class)
 | 
				
			||||||
 | 
					    public static Object[] onEnter(
 | 
				
			||||||
 | 
					        @Advice.Argument(0) Request request,
 | 
				
			||||||
 | 
					        @Advice.Argument(1) ResponseListener originalResponseListener) {
 | 
				
			||||||
 | 
					      ResponseListener responseListener = originalResponseListener;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      ElasticsearchRestRequest otelRequest =
 | 
				
			||||||
 | 
					          ElasticsearchRestRequest.create(request.getMethod(), request.getEndpoint());
 | 
				
			||||||
 | 
					      AdviceScope adviceScope = AdviceScope.start(otelRequest);
 | 
				
			||||||
 | 
					      if (adviceScope == null) {
 | 
				
			||||||
 | 
					        return new Object[] {null, responseListener};
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					      responseListener = adviceScope.wrapListener(responseListener);
 | 
				
			||||||
 | 
					      return new Object[] {adviceScope, responseListener};
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
 | 
					    @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
 | 
				
			||||||
    public static void stopSpan(
 | 
					    public static void stopSpan(
 | 
				
			||||||
        @Advice.Thrown Throwable throwable,
 | 
					        @Advice.Thrown @Nullable Throwable throwable, @Advice.Enter Object[] enterResult) {
 | 
				
			||||||
        @Advice.Local("otelRequest") ElasticsearchRestRequest otelRequest,
 | 
					      AdviceScope adviceScope = (AdviceScope) enterResult[0];
 | 
				
			||||||
        @Advice.Local("otelContext") Context context,
 | 
					      if (adviceScope != null) {
 | 
				
			||||||
        @Advice.Local("otelScope") Scope scope) {
 | 
					        adviceScope.end(throwable);
 | 
				
			||||||
 | 
					 | 
				
			||||||
      if (scope == null) {
 | 
					 | 
				
			||||||
        return;
 | 
					 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
      scope.close();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      if (throwable != null) {
 | 
					 | 
				
			||||||
        instrumenter().end(context, otelRequest, null, throwable);
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
      // span ended in RestResponseListener
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -43,4 +43,9 @@ public class ElasticsearchRest7InstrumentationModule extends InstrumentationModu
 | 
				
			||||||
  public List<TypeInstrumentation> typeInstrumentations() {
 | 
					  public List<TypeInstrumentation> typeInstrumentations() {
 | 
				
			||||||
    return singletonList(new RestClientInstrumentation());
 | 
					    return singletonList(new RestClientInstrumentation());
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  @Override
 | 
				
			||||||
 | 
					  public boolean isIndyReady() {
 | 
				
			||||||
 | 
					    return true;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -6,8 +6,11 @@
 | 
				
			||||||
package io.opentelemetry.javaagent.instrumentation.elasticsearch.rest.v7_0;
 | 
					package io.opentelemetry.javaagent.instrumentation.elasticsearch.rest.v7_0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
 | 
					import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
 | 
				
			||||||
 | 
					import io.opentelemetry.instrumentation.api.util.VirtualField;
 | 
				
			||||||
 | 
					import io.opentelemetry.instrumentation.elasticsearch.rest.common.v5_0.internal.ElasticsearchEndpointDefinition;
 | 
				
			||||||
import io.opentelemetry.instrumentation.elasticsearch.rest.common.v5_0.internal.ElasticsearchRestRequest;
 | 
					import io.opentelemetry.instrumentation.elasticsearch.rest.common.v5_0.internal.ElasticsearchRestRequest;
 | 
				
			||||||
import io.opentelemetry.javaagent.instrumentation.elasticsearch.rest.ElasticsearchRestJavaagentInstrumenterFactory;
 | 
					import io.opentelemetry.javaagent.instrumentation.elasticsearch.rest.ElasticsearchRestJavaagentInstrumenterFactory;
 | 
				
			||||||
 | 
					import org.elasticsearch.client.Request;
 | 
				
			||||||
import org.elasticsearch.client.Response;
 | 
					import org.elasticsearch.client.Response;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
public final class ElasticsearchRest7Singletons {
 | 
					public final class ElasticsearchRest7Singletons {
 | 
				
			||||||
| 
						 | 
					@ -20,5 +23,8 @@ public final class ElasticsearchRest7Singletons {
 | 
				
			||||||
    return INSTRUMENTER;
 | 
					    return INSTRUMENTER;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  public static final VirtualField<Request, ElasticsearchEndpointDefinition> ENDPOINT_DEFINITION =
 | 
				
			||||||
 | 
					      VirtualField.find(Request.class, ElasticsearchEndpointDefinition.class);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  private ElasticsearchRest7Singletons() {}
 | 
					  private ElasticsearchRest7Singletons() {}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -6,6 +6,7 @@
 | 
				
			||||||
package io.opentelemetry.javaagent.instrumentation.elasticsearch.rest.v7_0;
 | 
					package io.opentelemetry.javaagent.instrumentation.elasticsearch.rest.v7_0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import static io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge.currentContext;
 | 
					import static io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge.currentContext;
 | 
				
			||||||
 | 
					import static io.opentelemetry.javaagent.instrumentation.elasticsearch.rest.v7_0.ElasticsearchRest7Singletons.ENDPOINT_DEFINITION;
 | 
				
			||||||
import static io.opentelemetry.javaagent.instrumentation.elasticsearch.rest.v7_0.ElasticsearchRest7Singletons.instrumenter;
 | 
					import static io.opentelemetry.javaagent.instrumentation.elasticsearch.rest.v7_0.ElasticsearchRest7Singletons.instrumenter;
 | 
				
			||||||
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
 | 
					import static net.bytebuddy.matcher.ElementMatchers.isMethod;
 | 
				
			||||||
import static net.bytebuddy.matcher.ElementMatchers.named;
 | 
					import static net.bytebuddy.matcher.ElementMatchers.named;
 | 
				
			||||||
| 
						 | 
					@ -14,13 +15,14 @@ import static net.bytebuddy.matcher.ElementMatchers.takesArguments;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import io.opentelemetry.context.Context;
 | 
					import io.opentelemetry.context.Context;
 | 
				
			||||||
import io.opentelemetry.context.Scope;
 | 
					import io.opentelemetry.context.Scope;
 | 
				
			||||||
import io.opentelemetry.instrumentation.api.util.VirtualField;
 | 
					 | 
				
			||||||
import io.opentelemetry.instrumentation.elasticsearch.rest.common.v5_0.internal.ElasticsearchEndpointDefinition;
 | 
					 | 
				
			||||||
import io.opentelemetry.instrumentation.elasticsearch.rest.common.v5_0.internal.ElasticsearchRestRequest;
 | 
					import io.opentelemetry.instrumentation.elasticsearch.rest.common.v5_0.internal.ElasticsearchRestRequest;
 | 
				
			||||||
import io.opentelemetry.instrumentation.elasticsearch.rest.common.v5_0.internal.RestResponseListener;
 | 
					import io.opentelemetry.instrumentation.elasticsearch.rest.common.v5_0.internal.RestResponseListener;
 | 
				
			||||||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
 | 
					import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
 | 
				
			||||||
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
 | 
					import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
 | 
				
			||||||
 | 
					import javax.annotation.Nullable;
 | 
				
			||||||
import net.bytebuddy.asm.Advice;
 | 
					import net.bytebuddy.asm.Advice;
 | 
				
			||||||
 | 
					import net.bytebuddy.asm.Advice.AssignReturned;
 | 
				
			||||||
 | 
					import net.bytebuddy.asm.Advice.AssignReturned.ToArguments.ToArgument;
 | 
				
			||||||
import net.bytebuddy.description.type.TypeDescription;
 | 
					import net.bytebuddy.description.type.TypeDescription;
 | 
				
			||||||
import net.bytebuddy.matcher.ElementMatcher;
 | 
					import net.bytebuddy.matcher.ElementMatcher;
 | 
				
			||||||
import org.elasticsearch.client.Request;
 | 
					import org.elasticsearch.client.Request;
 | 
				
			||||||
| 
						 | 
					@ -50,101 +52,107 @@ public class RestClientInstrumentation implements TypeInstrumentation {
 | 
				
			||||||
        this.getClass().getName() + "$PerformRequestAsyncAdvice");
 | 
					        this.getClass().getName() + "$PerformRequestAsyncAdvice");
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  public static class AdviceScope {
 | 
				
			||||||
 | 
					    private final ElasticsearchRestRequest request;
 | 
				
			||||||
 | 
					    private final Context context;
 | 
				
			||||||
 | 
					    private final Context parentContext;
 | 
				
			||||||
 | 
					    private final Scope scope;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private AdviceScope(
 | 
				
			||||||
 | 
					        ElasticsearchRestRequest request, Context parentContext, Context context, Scope scope) {
 | 
				
			||||||
 | 
					      this.request = request;
 | 
				
			||||||
 | 
					      this.parentContext = parentContext;
 | 
				
			||||||
 | 
					      this.context = context;
 | 
				
			||||||
 | 
					      this.scope = scope;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Nullable
 | 
				
			||||||
 | 
					    public static AdviceScope start(ElasticsearchRestRequest request) {
 | 
				
			||||||
 | 
					      Context parentContext = currentContext();
 | 
				
			||||||
 | 
					      if (!instrumenter().shouldStart(parentContext, request)) {
 | 
				
			||||||
 | 
					        return null;
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					      Context context = instrumenter().start(parentContext, request);
 | 
				
			||||||
 | 
					      return new AdviceScope(request, parentContext, context, context.makeCurrent());
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public ResponseListener wrapListener(ResponseListener responseListener) {
 | 
				
			||||||
 | 
					      return new RestResponseListener(
 | 
				
			||||||
 | 
					          responseListener, parentContext, instrumenter(), context, request);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public void endWithListener(@Nullable Throwable throwable) {
 | 
				
			||||||
 | 
					      scope.close();
 | 
				
			||||||
 | 
					      if (throwable != null) {
 | 
				
			||||||
 | 
					        instrumenter().end(context, request, null, throwable);
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					      // span ended in RestResponseListener
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public void endWithResponse(@Nullable Throwable throwable, @Nullable Response response) {
 | 
				
			||||||
 | 
					      scope.close();
 | 
				
			||||||
 | 
					      instrumenter().end(context, request, response, throwable);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @SuppressWarnings("unused")
 | 
					  @SuppressWarnings("unused")
 | 
				
			||||||
  public static class PerformRequestAdvice {
 | 
					  public static class PerformRequestAdvice {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Nullable
 | 
				
			||||||
    @Advice.OnMethodEnter(suppress = Throwable.class)
 | 
					    @Advice.OnMethodEnter(suppress = Throwable.class)
 | 
				
			||||||
    public static void onEnter(
 | 
					    public static AdviceScope onEnter(@Advice.Argument(0) Request request) {
 | 
				
			||||||
        @Advice.Argument(0) Request request,
 | 
					      ElasticsearchRestRequest otelRequest =
 | 
				
			||||||
        @Advice.Local("otelRequest") ElasticsearchRestRequest otelRequest,
 | 
					 | 
				
			||||||
        @Advice.Local("otelContext") Context context,
 | 
					 | 
				
			||||||
        @Advice.Local("otelScope") Scope scope) {
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      Context parentContext = currentContext();
 | 
					 | 
				
			||||||
      VirtualField<Request, ElasticsearchEndpointDefinition> virtualField =
 | 
					 | 
				
			||||||
          VirtualField.find(Request.class, ElasticsearchEndpointDefinition.class);
 | 
					 | 
				
			||||||
      otelRequest =
 | 
					 | 
				
			||||||
          ElasticsearchRestRequest.create(
 | 
					          ElasticsearchRestRequest.create(
 | 
				
			||||||
              request.getMethod(),
 | 
					              request.getMethod(),
 | 
				
			||||||
              request.getEndpoint(),
 | 
					              request.getEndpoint(),
 | 
				
			||||||
              // set by elasticsearch-api-client instrumentation
 | 
					              // set by elasticsearch-api-client instrumentation
 | 
				
			||||||
              virtualField.get(request),
 | 
					              ENDPOINT_DEFINITION.get(request),
 | 
				
			||||||
              request.getEntity());
 | 
					              request.getEntity());
 | 
				
			||||||
      if (!instrumenter().shouldStart(parentContext, otelRequest)) {
 | 
					      return AdviceScope.start(otelRequest);
 | 
				
			||||||
        return;
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      context = instrumenter().start(parentContext, otelRequest);
 | 
					 | 
				
			||||||
      scope = context.makeCurrent();
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
 | 
					    @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
 | 
				
			||||||
    public static void stopSpan(
 | 
					    public static void stopSpan(
 | 
				
			||||||
        @Advice.Return Response response,
 | 
					        @Advice.Return @Nullable Response response,
 | 
				
			||||||
        @Advice.Thrown Throwable throwable,
 | 
					        @Advice.Thrown @Nullable Throwable throwable,
 | 
				
			||||||
        @Advice.Local("otelRequest") ElasticsearchRestRequest otelRequest,
 | 
					        @Advice.Enter @Nullable AdviceScope adviceScope) {
 | 
				
			||||||
        @Advice.Local("otelContext") Context context,
 | 
					      if (adviceScope != null) {
 | 
				
			||||||
        @Advice.Local("otelScope") Scope scope) {
 | 
					        adviceScope.endWithResponse(throwable, response);
 | 
				
			||||||
 | 
					 | 
				
			||||||
      if (scope == null) {
 | 
					 | 
				
			||||||
        return;
 | 
					 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
      scope.close();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      instrumenter().end(context, otelRequest, response, throwable);
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @SuppressWarnings("unused")
 | 
					  @SuppressWarnings("unused")
 | 
				
			||||||
  public static class PerformRequestAsyncAdvice {
 | 
					  public static class PerformRequestAsyncAdvice {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @AssignReturned.ToArguments(@ToArgument(value = 1, index = 1))
 | 
				
			||||||
    @Advice.OnMethodEnter(suppress = Throwable.class)
 | 
					    @Advice.OnMethodEnter(suppress = Throwable.class)
 | 
				
			||||||
    public static void onEnter(
 | 
					    public static Object[] onEnter(
 | 
				
			||||||
        @Advice.Argument(0) Request request,
 | 
					        @Advice.Argument(0) Request request,
 | 
				
			||||||
        @Advice.Argument(value = 1, readOnly = false) ResponseListener responseListener,
 | 
					        @Advice.Argument(1) ResponseListener originalResponseListener) {
 | 
				
			||||||
        @Advice.Local("otelRequest") ElasticsearchRestRequest otelRequest,
 | 
					      ResponseListener responseListener = originalResponseListener;
 | 
				
			||||||
        @Advice.Local("otelContext") Context context,
 | 
					      ElasticsearchRestRequest otelRequest =
 | 
				
			||||||
        @Advice.Local("otelScope") Scope scope) {
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      Context parentContext = currentContext();
 | 
					 | 
				
			||||||
      VirtualField<Request, ElasticsearchEndpointDefinition> virtualField =
 | 
					 | 
				
			||||||
          VirtualField.find(Request.class, ElasticsearchEndpointDefinition.class);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      otelRequest =
 | 
					 | 
				
			||||||
          ElasticsearchRestRequest.create(
 | 
					          ElasticsearchRestRequest.create(
 | 
				
			||||||
              request.getMethod(),
 | 
					              request.getMethod(),
 | 
				
			||||||
              request.getEndpoint(),
 | 
					              request.getEndpoint(),
 | 
				
			||||||
              // set by elasticsearch-api-client instrumentation
 | 
					              // set by elasticsearch-api-client instrumentation
 | 
				
			||||||
              virtualField.get(request),
 | 
					              ENDPOINT_DEFINITION.get(request),
 | 
				
			||||||
              request.getEntity());
 | 
					              request.getEntity());
 | 
				
			||||||
      if (!instrumenter().shouldStart(parentContext, otelRequest)) {
 | 
					      AdviceScope adviceScope = AdviceScope.start(otelRequest);
 | 
				
			||||||
        return;
 | 
					      if (adviceScope == null) {
 | 
				
			||||||
 | 
					        return new Object[] {null, responseListener};
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
 | 
					      responseListener = adviceScope.wrapListener(responseListener);
 | 
				
			||||||
      context = instrumenter().start(parentContext, otelRequest);
 | 
					      return new Object[] {adviceScope, responseListener};
 | 
				
			||||||
      scope = context.makeCurrent();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      responseListener =
 | 
					 | 
				
			||||||
          new RestResponseListener(
 | 
					 | 
				
			||||||
              responseListener, parentContext, instrumenter(), context, otelRequest);
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
 | 
					    @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
 | 
				
			||||||
    public static void stopSpan(
 | 
					    public static void stopSpan(
 | 
				
			||||||
        @Advice.Thrown Throwable throwable,
 | 
					        @Advice.Thrown @Nullable Throwable throwable, @Advice.Enter Object[] enterResult) {
 | 
				
			||||||
        @Advice.Local("otelRequest") ElasticsearchRestRequest otelRequest,
 | 
					      AdviceScope adviceScope = (AdviceScope) enterResult[0];
 | 
				
			||||||
        @Advice.Local("otelContext") Context context,
 | 
					      if (adviceScope != null) {
 | 
				
			||||||
        @Advice.Local("otelScope") Scope scope) {
 | 
					        adviceScope.endWithListener(throwable);
 | 
				
			||||||
 | 
					 | 
				
			||||||
      if (scope == null) {
 | 
					 | 
				
			||||||
        return;
 | 
					 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
      scope.close();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      if (throwable != null) {
 | 
					 | 
				
			||||||
        instrumenter().end(context, otelRequest, null, throwable);
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
      // span ended in RestResponseListener
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -17,7 +17,10 @@ import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
 | 
				
			||||||
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
 | 
					import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
 | 
				
			||||||
import io.opentelemetry.javaagent.instrumentation.elasticsearch.transport.ElasticTransportRequest;
 | 
					import io.opentelemetry.javaagent.instrumentation.elasticsearch.transport.ElasticTransportRequest;
 | 
				
			||||||
import io.opentelemetry.javaagent.instrumentation.elasticsearch.transport.TransportActionListener;
 | 
					import io.opentelemetry.javaagent.instrumentation.elasticsearch.transport.TransportActionListener;
 | 
				
			||||||
 | 
					import javax.annotation.Nullable;
 | 
				
			||||||
import net.bytebuddy.asm.Advice;
 | 
					import net.bytebuddy.asm.Advice;
 | 
				
			||||||
 | 
					import net.bytebuddy.asm.Advice.AssignReturned;
 | 
				
			||||||
 | 
					import net.bytebuddy.asm.Advice.AssignReturned.ToArguments.ToArgument;
 | 
				
			||||||
import net.bytebuddy.description.type.TypeDescription;
 | 
					import net.bytebuddy.description.type.TypeDescription;
 | 
				
			||||||
import net.bytebuddy.matcher.ElementMatcher;
 | 
					import net.bytebuddy.matcher.ElementMatcher;
 | 
				
			||||||
import org.elasticsearch.action.Action;
 | 
					import org.elasticsearch.action.Action;
 | 
				
			||||||
| 
						 | 
					@ -47,44 +50,68 @@ public class AbstractClientInstrumentation implements TypeInstrumentation {
 | 
				
			||||||
  @SuppressWarnings("unused")
 | 
					  @SuppressWarnings("unused")
 | 
				
			||||||
  public static class ExecuteAdvice {
 | 
					  public static class ExecuteAdvice {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Advice.OnMethodEnter(suppress = Throwable.class)
 | 
					    public static class AdviceScope {
 | 
				
			||||||
    public static void onEnter(
 | 
					      private final ElasticTransportRequest request;
 | 
				
			||||||
        @Advice.Argument(0) Action<?, ?, ?> action,
 | 
					      private final Context parentContext;
 | 
				
			||||||
        @Advice.Argument(1) ActionRequest<?> actionRequest,
 | 
					      private final Context context;
 | 
				
			||||||
        @Advice.Local("otelContext") Context context,
 | 
					      private final Scope scope;
 | 
				
			||||||
        @Advice.Local("otelScope") Scope scope,
 | 
					 | 
				
			||||||
        @Advice.Local("otelRequest") ElasticTransportRequest transportRequest,
 | 
					 | 
				
			||||||
        @Advice.Argument(value = 2, readOnly = false)
 | 
					 | 
				
			||||||
            ActionListener<ActionResponse> actionListener) {
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
      transportRequest = ElasticTransportRequest.create(action, actionRequest);
 | 
					      private AdviceScope(
 | 
				
			||||||
      Context parentContext = currentContext();
 | 
					          ElasticTransportRequest request, Context parentContext, Context context, Scope scope) {
 | 
				
			||||||
      if (!instrumenter().shouldStart(parentContext, transportRequest)) {
 | 
					        this.request = request;
 | 
				
			||||||
        return;
 | 
					        this.parentContext = parentContext;
 | 
				
			||||||
 | 
					        this.context = context;
 | 
				
			||||||
 | 
					        this.scope = scope;
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      context = instrumenter().start(parentContext, transportRequest);
 | 
					      @Nullable
 | 
				
			||||||
      scope = context.makeCurrent();
 | 
					      public static AdviceScope start(ElasticTransportRequest request) {
 | 
				
			||||||
 | 
					        Context parentContext = currentContext();
 | 
				
			||||||
 | 
					        if (!instrumenter().shouldStart(parentContext, request)) {
 | 
				
			||||||
 | 
					          return null;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        Context context = instrumenter().start(parentContext, request);
 | 
				
			||||||
 | 
					        return new AdviceScope(request, parentContext, context, context.makeCurrent());
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      actionListener =
 | 
					      public ActionListener<ActionResponse> wrapListener(
 | 
				
			||||||
          new TransportActionListener<>(
 | 
					          ActionListener<ActionResponse> actionListener) {
 | 
				
			||||||
              instrumenter(), transportRequest, actionListener, context, parentContext);
 | 
					        return new TransportActionListener<>(
 | 
				
			||||||
 | 
					            instrumenter(), request, actionListener, context, parentContext);
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      public void end(@Nullable Throwable throwable) {
 | 
				
			||||||
 | 
					        scope.close();
 | 
				
			||||||
 | 
					        if (throwable != null) {
 | 
				
			||||||
 | 
					          instrumenter().end(context, request, null, throwable);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @AssignReturned.ToArguments(@ToArgument(value = 2, index = 1))
 | 
				
			||||||
 | 
					    @Advice.OnMethodEnter(suppress = Throwable.class)
 | 
				
			||||||
 | 
					    public static Object[] onEnter(
 | 
				
			||||||
 | 
					        @Advice.Argument(0) Action<?, ?, ?> action,
 | 
				
			||||||
 | 
					        @Advice.Argument(1) ActionRequest<?> actionRequest,
 | 
				
			||||||
 | 
					        @Advice.Argument(2) ActionListener<ActionResponse> originalActionListener) {
 | 
				
			||||||
 | 
					      ActionListener<ActionResponse> actionListener = originalActionListener;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      ElasticTransportRequest request = ElasticTransportRequest.create(action, actionRequest);
 | 
				
			||||||
 | 
					      AdviceScope adviceScope = AdviceScope.start(request);
 | 
				
			||||||
 | 
					      if (adviceScope == null) {
 | 
				
			||||||
 | 
					        return new Object[] {null, actionListener};
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      actionListener = adviceScope.wrapListener(actionListener);
 | 
				
			||||||
 | 
					      return new Object[] {adviceScope, actionListener};
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
 | 
					    @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
 | 
				
			||||||
    public static void stopSpan(
 | 
					    public static void stopSpan(
 | 
				
			||||||
        @Advice.Thrown Throwable throwable,
 | 
					        @Advice.Thrown @Nullable Throwable throwable, @Advice.Enter Object[] enterResult) {
 | 
				
			||||||
        @Advice.Local("otelContext") Context context,
 | 
					      AdviceScope adviceScope = (AdviceScope) enterResult[0];
 | 
				
			||||||
        @Advice.Local("otelScope") Scope scope,
 | 
					      if (adviceScope != null) {
 | 
				
			||||||
        @Advice.Local("otelRequest") ElasticTransportRequest transportRequest) {
 | 
					        adviceScope.end(throwable);
 | 
				
			||||||
      if (scope == null) {
 | 
					 | 
				
			||||||
        return;
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      scope.close();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      if (throwable != null) {
 | 
					 | 
				
			||||||
        instrumenter().end(context, transportRequest, null, throwable);
 | 
					 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -10,10 +10,12 @@ import static java.util.Collections.singletonList;
 | 
				
			||||||
import com.google.auto.service.AutoService;
 | 
					import com.google.auto.service.AutoService;
 | 
				
			||||||
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
 | 
					import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
 | 
				
			||||||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
 | 
					import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
 | 
				
			||||||
 | 
					import io.opentelemetry.javaagent.extension.instrumentation.internal.ExperimentalInstrumentationModule;
 | 
				
			||||||
import java.util.List;
 | 
					import java.util.List;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@AutoService(InstrumentationModule.class)
 | 
					@AutoService(InstrumentationModule.class)
 | 
				
			||||||
public class Elasticsearch5TransportClientInstrumentationModule extends InstrumentationModule {
 | 
					public class Elasticsearch5TransportClientInstrumentationModule extends InstrumentationModule
 | 
				
			||||||
 | 
					    implements ExperimentalInstrumentationModule {
 | 
				
			||||||
  public Elasticsearch5TransportClientInstrumentationModule() {
 | 
					  public Elasticsearch5TransportClientInstrumentationModule() {
 | 
				
			||||||
    super("elasticsearch-transport", "elasticsearch-transport-5.0", "elasticsearch");
 | 
					    super("elasticsearch-transport", "elasticsearch-transport-5.0", "elasticsearch");
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
| 
						 | 
					@ -22,4 +24,9 @@ public class Elasticsearch5TransportClientInstrumentationModule extends Instrume
 | 
				
			||||||
  public List<TypeInstrumentation> typeInstrumentations() {
 | 
					  public List<TypeInstrumentation> typeInstrumentations() {
 | 
				
			||||||
    return singletonList(new AbstractClientInstrumentation());
 | 
					    return singletonList(new AbstractClientInstrumentation());
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  @Override
 | 
				
			||||||
 | 
					  public boolean isIndyReady() {
 | 
				
			||||||
 | 
					    return true;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -17,7 +17,10 @@ import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
 | 
				
			||||||
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
 | 
					import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
 | 
				
			||||||
import io.opentelemetry.javaagent.instrumentation.elasticsearch.transport.ElasticTransportRequest;
 | 
					import io.opentelemetry.javaagent.instrumentation.elasticsearch.transport.ElasticTransportRequest;
 | 
				
			||||||
import io.opentelemetry.javaagent.instrumentation.elasticsearch.transport.TransportActionListener;
 | 
					import io.opentelemetry.javaagent.instrumentation.elasticsearch.transport.TransportActionListener;
 | 
				
			||||||
 | 
					import javax.annotation.Nullable;
 | 
				
			||||||
import net.bytebuddy.asm.Advice;
 | 
					import net.bytebuddy.asm.Advice;
 | 
				
			||||||
 | 
					import net.bytebuddy.asm.Advice.AssignReturned;
 | 
				
			||||||
 | 
					import net.bytebuddy.asm.Advice.AssignReturned.ToArguments.ToArgument;
 | 
				
			||||||
import net.bytebuddy.description.type.TypeDescription;
 | 
					import net.bytebuddy.description.type.TypeDescription;
 | 
				
			||||||
import net.bytebuddy.matcher.ElementMatcher;
 | 
					import net.bytebuddy.matcher.ElementMatcher;
 | 
				
			||||||
import org.elasticsearch.action.Action;
 | 
					import org.elasticsearch.action.Action;
 | 
				
			||||||
| 
						 | 
					@ -47,44 +50,68 @@ public class AbstractClientInstrumentation implements TypeInstrumentation {
 | 
				
			||||||
  @SuppressWarnings("unused")
 | 
					  @SuppressWarnings("unused")
 | 
				
			||||||
  public static class ExecuteAdvice {
 | 
					  public static class ExecuteAdvice {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Advice.OnMethodEnter(suppress = Throwable.class)
 | 
					    public static class AdviceScope {
 | 
				
			||||||
    public static void onEnter(
 | 
					      private final ElasticTransportRequest request;
 | 
				
			||||||
        @Advice.Argument(0) Action<?, ?, ?> action,
 | 
					      private final Context parentContext;
 | 
				
			||||||
        @Advice.Argument(1) ActionRequest actionRequest,
 | 
					      private final Context context;
 | 
				
			||||||
        @Advice.Local("otelContext") Context context,
 | 
					      private final Scope scope;
 | 
				
			||||||
        @Advice.Local("otelScope") Scope scope,
 | 
					 | 
				
			||||||
        @Advice.Local("otelRequest") ElasticTransportRequest transportRequest,
 | 
					 | 
				
			||||||
        @Advice.Argument(value = 2, readOnly = false)
 | 
					 | 
				
			||||||
            ActionListener<ActionResponse> actionListener) {
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
      transportRequest = ElasticTransportRequest.create(action, actionRequest);
 | 
					      private AdviceScope(
 | 
				
			||||||
      Context parentContext = currentContext();
 | 
					          ElasticTransportRequest request, Context parentContext, Context context, Scope scope) {
 | 
				
			||||||
      if (!instrumenter().shouldStart(parentContext, transportRequest)) {
 | 
					        this.request = request;
 | 
				
			||||||
        return;
 | 
					        this.parentContext = parentContext;
 | 
				
			||||||
 | 
					        this.context = context;
 | 
				
			||||||
 | 
					        this.scope = scope;
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      context = instrumenter().start(parentContext, transportRequest);
 | 
					      @Nullable
 | 
				
			||||||
      scope = context.makeCurrent();
 | 
					      public static AdviceScope start(ElasticTransportRequest request) {
 | 
				
			||||||
 | 
					        Context parentContext = currentContext();
 | 
				
			||||||
 | 
					        if (!instrumenter().shouldStart(parentContext, request)) {
 | 
				
			||||||
 | 
					          return null;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        Context context = instrumenter().start(parentContext, request);
 | 
				
			||||||
 | 
					        return new AdviceScope(request, parentContext, context, context.makeCurrent());
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      actionListener =
 | 
					      public ActionListener<ActionResponse> wrapListener(
 | 
				
			||||||
          new TransportActionListener<>(
 | 
					          ActionListener<ActionResponse> actionListener) {
 | 
				
			||||||
              instrumenter(), transportRequest, actionListener, context, parentContext);
 | 
					        return new TransportActionListener<>(
 | 
				
			||||||
 | 
					            instrumenter(), request, actionListener, context, parentContext);
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      public void end(@Nullable Throwable throwable) {
 | 
				
			||||||
 | 
					        scope.close();
 | 
				
			||||||
 | 
					        if (throwable != null) {
 | 
				
			||||||
 | 
					          instrumenter().end(context, request, null, throwable);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @AssignReturned.ToArguments(@ToArgument(value = 2, index = 1))
 | 
				
			||||||
 | 
					    @Advice.OnMethodEnter(suppress = Throwable.class)
 | 
				
			||||||
 | 
					    public static Object[] onEnter(
 | 
				
			||||||
 | 
					        @Advice.Argument(0) Action<?, ?, ?> action,
 | 
				
			||||||
 | 
					        @Advice.Argument(1) ActionRequest actionRequest,
 | 
				
			||||||
 | 
					        @Advice.Argument(2) ActionListener<ActionResponse> originalActionListener) {
 | 
				
			||||||
 | 
					      ActionListener<ActionResponse> actionListener = originalActionListener;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      ElasticTransportRequest request = ElasticTransportRequest.create(action, actionRequest);
 | 
				
			||||||
 | 
					      AdviceScope adviceScope = AdviceScope.start(request);
 | 
				
			||||||
 | 
					      if (adviceScope == null) {
 | 
				
			||||||
 | 
					        return new Object[] {null, actionListener};
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      actionListener = adviceScope.wrapListener(actionListener);
 | 
				
			||||||
 | 
					      return new Object[] {adviceScope, actionListener};
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
 | 
					    @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
 | 
				
			||||||
    public static void stopSpan(
 | 
					    public static void stopSpan(
 | 
				
			||||||
        @Advice.Thrown Throwable throwable,
 | 
					        @Advice.Thrown @Nullable Throwable throwable, @Advice.Enter Object[] enterResult) {
 | 
				
			||||||
        @Advice.Local("otelContext") Context context,
 | 
					      AdviceScope adviceScope = (AdviceScope) enterResult[0];
 | 
				
			||||||
        @Advice.Local("otelScope") Scope scope,
 | 
					      if (adviceScope != null) {
 | 
				
			||||||
        @Advice.Local("otelRequest") ElasticTransportRequest transportRequest) {
 | 
					        adviceScope.end(throwable);
 | 
				
			||||||
      if (scope == null) {
 | 
					 | 
				
			||||||
        return;
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      scope.close();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      if (throwable != null) {
 | 
					 | 
				
			||||||
        instrumenter().end(context, transportRequest, null, throwable);
 | 
					 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -10,11 +10,13 @@ import static java.util.Collections.singletonList;
 | 
				
			||||||
import com.google.auto.service.AutoService;
 | 
					import com.google.auto.service.AutoService;
 | 
				
			||||||
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
 | 
					import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
 | 
				
			||||||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
 | 
					import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
 | 
				
			||||||
 | 
					import io.opentelemetry.javaagent.extension.instrumentation.internal.ExperimentalInstrumentationModule;
 | 
				
			||||||
import java.util.List;
 | 
					import java.util.List;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/** Beginning in version 5.3.0, DocumentRequest was renamed to DocWriteRequest. */
 | 
					/** Beginning in version 5.3.0, DocumentRequest was renamed to DocWriteRequest. */
 | 
				
			||||||
@AutoService(InstrumentationModule.class)
 | 
					@AutoService(InstrumentationModule.class)
 | 
				
			||||||
public class Elasticsearch53TransportClientInstrumentationModule extends InstrumentationModule {
 | 
					public class Elasticsearch53TransportClientInstrumentationModule extends InstrumentationModule
 | 
				
			||||||
 | 
					    implements ExperimentalInstrumentationModule {
 | 
				
			||||||
  public Elasticsearch53TransportClientInstrumentationModule() {
 | 
					  public Elasticsearch53TransportClientInstrumentationModule() {
 | 
				
			||||||
    super("elasticsearch-transport", "elasticsearch-transport-5.3", "elasticsearch");
 | 
					    super("elasticsearch-transport", "elasticsearch-transport-5.3", "elasticsearch");
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
| 
						 | 
					@ -23,4 +25,9 @@ public class Elasticsearch53TransportClientInstrumentationModule extends Instrum
 | 
				
			||||||
  public List<TypeInstrumentation> typeInstrumentations() {
 | 
					  public List<TypeInstrumentation> typeInstrumentations() {
 | 
				
			||||||
    return singletonList(new AbstractClientInstrumentation());
 | 
					    return singletonList(new AbstractClientInstrumentation());
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  @Override
 | 
				
			||||||
 | 
					  public boolean isIndyReady() {
 | 
				
			||||||
 | 
					    return true;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -18,7 +18,10 @@ import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
 | 
				
			||||||
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
 | 
					import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
 | 
				
			||||||
import io.opentelemetry.javaagent.instrumentation.elasticsearch.transport.ElasticTransportRequest;
 | 
					import io.opentelemetry.javaagent.instrumentation.elasticsearch.transport.ElasticTransportRequest;
 | 
				
			||||||
import io.opentelemetry.javaagent.instrumentation.elasticsearch.transport.TransportActionListener;
 | 
					import io.opentelemetry.javaagent.instrumentation.elasticsearch.transport.TransportActionListener;
 | 
				
			||||||
 | 
					import javax.annotation.Nullable;
 | 
				
			||||||
import net.bytebuddy.asm.Advice;
 | 
					import net.bytebuddy.asm.Advice;
 | 
				
			||||||
 | 
					import net.bytebuddy.asm.Advice.AssignReturned;
 | 
				
			||||||
 | 
					import net.bytebuddy.asm.Advice.AssignReturned.ToArguments.ToArgument;
 | 
				
			||||||
import net.bytebuddy.description.type.TypeDescription;
 | 
					import net.bytebuddy.description.type.TypeDescription;
 | 
				
			||||||
import net.bytebuddy.matcher.ElementMatcher;
 | 
					import net.bytebuddy.matcher.ElementMatcher;
 | 
				
			||||||
import org.elasticsearch.action.ActionListener;
 | 
					import org.elasticsearch.action.ActionListener;
 | 
				
			||||||
| 
						 | 
					@ -51,44 +54,68 @@ public class AbstractClientInstrumentation implements TypeInstrumentation {
 | 
				
			||||||
  @SuppressWarnings("unused")
 | 
					  @SuppressWarnings("unused")
 | 
				
			||||||
  public static class ExecuteAdvice {
 | 
					  public static class ExecuteAdvice {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Advice.OnMethodEnter(suppress = Throwable.class)
 | 
					    public static class AdviceScope {
 | 
				
			||||||
    public static void onEnter(
 | 
					      private final ElasticTransportRequest request;
 | 
				
			||||||
        @Advice.Argument(0) Object action,
 | 
					      private final Context parentContext;
 | 
				
			||||||
        @Advice.Argument(1) ActionRequest actionRequest,
 | 
					      private final Context context;
 | 
				
			||||||
        @Advice.Local("otelContext") Context context,
 | 
					      private final Scope scope;
 | 
				
			||||||
        @Advice.Local("otelScope") Scope scope,
 | 
					 | 
				
			||||||
        @Advice.Local("otelRequest") ElasticTransportRequest transportRequest,
 | 
					 | 
				
			||||||
        @Advice.Argument(value = 2, readOnly = false)
 | 
					 | 
				
			||||||
            ActionListener<ActionResponse> actionListener) {
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
      transportRequest = ElasticTransportRequest.create(action, actionRequest);
 | 
					      private AdviceScope(
 | 
				
			||||||
      Context parentContext = currentContext();
 | 
					          ElasticTransportRequest request, Context parentContext, Context context, Scope scope) {
 | 
				
			||||||
      if (!instrumenter().shouldStart(parentContext, transportRequest)) {
 | 
					        this.request = request;
 | 
				
			||||||
        return;
 | 
					        this.parentContext = parentContext;
 | 
				
			||||||
 | 
					        this.context = context;
 | 
				
			||||||
 | 
					        this.scope = scope;
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      context = instrumenter().start(parentContext, transportRequest);
 | 
					      @Nullable
 | 
				
			||||||
      scope = context.makeCurrent();
 | 
					      public static AdviceScope start(ElasticTransportRequest request) {
 | 
				
			||||||
 | 
					        Context parentContext = currentContext();
 | 
				
			||||||
 | 
					        if (!instrumenter().shouldStart(parentContext, request)) {
 | 
				
			||||||
 | 
					          return null;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        Context context = instrumenter().start(parentContext, request);
 | 
				
			||||||
 | 
					        return new AdviceScope(request, parentContext, context, context.makeCurrent());
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      actionListener =
 | 
					      public ActionListener<ActionResponse> wrapListener(
 | 
				
			||||||
          new TransportActionListener<>(
 | 
					          ActionListener<ActionResponse> actionListener) {
 | 
				
			||||||
              instrumenter(), transportRequest, actionListener, context, parentContext);
 | 
					        return new TransportActionListener<>(
 | 
				
			||||||
 | 
					            instrumenter(), request, actionListener, context, parentContext);
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      public void end(@Nullable Throwable throwable) {
 | 
				
			||||||
 | 
					        scope.close();
 | 
				
			||||||
 | 
					        if (throwable != null) {
 | 
				
			||||||
 | 
					          instrumenter().end(context, request, null, throwable);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @AssignReturned.ToArguments(@ToArgument(value = 2, index = 1))
 | 
				
			||||||
 | 
					    @Advice.OnMethodEnter(suppress = Throwable.class)
 | 
				
			||||||
 | 
					    public static Object[] onEnter(
 | 
				
			||||||
 | 
					        @Advice.Argument(0) Object action,
 | 
				
			||||||
 | 
					        @Advice.Argument(1) ActionRequest actionRequest,
 | 
				
			||||||
 | 
					        @Advice.Argument(2) ActionListener<ActionResponse> originalActionListener) {
 | 
				
			||||||
 | 
					      ActionListener<ActionResponse> actionListener = originalActionListener;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      ElasticTransportRequest request = ElasticTransportRequest.create(action, actionRequest);
 | 
				
			||||||
 | 
					      AdviceScope adviceScope = AdviceScope.start(request);
 | 
				
			||||||
 | 
					      if (adviceScope == null) {
 | 
				
			||||||
 | 
					        return new Object[] {null, actionListener};
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      actionListener = adviceScope.wrapListener(actionListener);
 | 
				
			||||||
 | 
					      return new Object[] {adviceScope, actionListener};
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
 | 
					    @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
 | 
				
			||||||
    public static void stopSpan(
 | 
					    public static void stopSpan(
 | 
				
			||||||
        @Advice.Thrown Throwable throwable,
 | 
					        @Advice.Thrown @Nullable Throwable throwable, @Advice.Enter Object[] enterResult) {
 | 
				
			||||||
        @Advice.Local("otelContext") Context context,
 | 
					      AdviceScope adviceScope = (AdviceScope) enterResult[0];
 | 
				
			||||||
        @Advice.Local("otelScope") Scope scope,
 | 
					      if (adviceScope != null) {
 | 
				
			||||||
        @Advice.Local("otelRequest") ElasticTransportRequest transportRequest) {
 | 
					        adviceScope.end(throwable);
 | 
				
			||||||
      if (scope == null) {
 | 
					 | 
				
			||||||
        return;
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      scope.close();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      if (throwable != null) {
 | 
					 | 
				
			||||||
        instrumenter().end(context, transportRequest, null, throwable);
 | 
					 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -10,6 +10,7 @@ import static java.util.Collections.singletonList;
 | 
				
			||||||
import com.google.auto.service.AutoService;
 | 
					import com.google.auto.service.AutoService;
 | 
				
			||||||
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
 | 
					import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
 | 
				
			||||||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
 | 
					import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
 | 
				
			||||||
 | 
					import io.opentelemetry.javaagent.extension.instrumentation.internal.ExperimentalInstrumentationModule;
 | 
				
			||||||
import java.util.List;
 | 
					import java.util.List;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
| 
						 | 
					@ -17,7 +18,8 @@ import java.util.List;
 | 
				
			||||||
 * an abstract class, so the bytecode isn't directly compatible.
 | 
					 * an abstract class, so the bytecode isn't directly compatible.
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
@AutoService(InstrumentationModule.class)
 | 
					@AutoService(InstrumentationModule.class)
 | 
				
			||||||
public class Elasticsearch6TransportClientInstrumentationModule extends InstrumentationModule {
 | 
					public class Elasticsearch6TransportClientInstrumentationModule extends InstrumentationModule
 | 
				
			||||||
 | 
					    implements ExperimentalInstrumentationModule {
 | 
				
			||||||
  public Elasticsearch6TransportClientInstrumentationModule() {
 | 
					  public Elasticsearch6TransportClientInstrumentationModule() {
 | 
				
			||||||
    super("elasticsearch-transport", "elasticsearch-transport-6.0", "elasticsearch");
 | 
					    super("elasticsearch-transport", "elasticsearch-transport-6.0", "elasticsearch");
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
| 
						 | 
					@ -26,4 +28,9 @@ public class Elasticsearch6TransportClientInstrumentationModule extends Instrume
 | 
				
			||||||
  public List<TypeInstrumentation> typeInstrumentations() {
 | 
					  public List<TypeInstrumentation> typeInstrumentations() {
 | 
				
			||||||
    return singletonList(new AbstractClientInstrumentation());
 | 
					    return singletonList(new AbstractClientInstrumentation());
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  @Override
 | 
				
			||||||
 | 
					  public boolean isIndyReady() {
 | 
				
			||||||
 | 
					    return true;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue