-
Notifications
You must be signed in to change notification settings - Fork 826
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix elasticsearch latestDepTest (#9066)
- Loading branch information
Mateusz Rzeszutek
committed
Jul 28, 2023
1 parent
dc5d76a
commit 1773cb2
Showing
4 changed files
with
112 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
...n/java/io/opentelemetry/javaagent/instrumentation/elasticsearch/apiclient/EndpointId.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.elasticsearch.apiclient; | ||
|
||
import static io.opentelemetry.context.ContextKey.named; | ||
|
||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.context.ContextKey; | ||
import javax.annotation.Nullable; | ||
|
||
public final class EndpointId { | ||
|
||
private static final ContextKey<String> KEY = named("elasticsearch-api-client-endpoint-id"); | ||
|
||
public static Context storeInContext(Context context, String endpointId) { | ||
return context.with(KEY, endpointId); | ||
} | ||
|
||
@Nullable | ||
public static String get(Context context) { | ||
return context.get(KEY); | ||
} | ||
|
||
private EndpointId() {} | ||
} |
79 changes: 79 additions & 0 deletions
79
...avaagent/instrumentation/elasticsearch/apiclient/RestClientHttpClientInstrumentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.elasticsearch.apiclient; | ||
|
||
import static net.bytebuddy.matcher.ElementMatchers.isMethod; | ||
import static net.bytebuddy.matcher.ElementMatchers.named; | ||
import static net.bytebuddy.matcher.ElementMatchers.namedOneOf; | ||
import static net.bytebuddy.matcher.ElementMatchers.returns; | ||
import static net.bytebuddy.matcher.ElementMatchers.takesArgument; | ||
|
||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.context.Scope; | ||
import io.opentelemetry.instrumentation.api.util.VirtualField; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; | ||
import io.opentelemetry.javaagent.instrumentation.elasticsearch.rest.ElasticsearchEndpointDefinition; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
import org.elasticsearch.client.Request; | ||
|
||
// starting from 8.9 | ||
public class RestClientHttpClientInstrumentation implements TypeInstrumentation { | ||
|
||
@Override | ||
public ElementMatcher<TypeDescription> typeMatcher() { | ||
return named("co.elastic.clients.transport.rest_client.RestClientHttpClient"); | ||
} | ||
|
||
@Override | ||
public void transform(TypeTransformer transformer) { | ||
transformer.applyAdviceToMethod( | ||
isMethod() | ||
.and(namedOneOf("performRequest", "performRequestAsync")) | ||
.and(takesArgument(0, String.class)), | ||
this.getClass().getName() + "$PerformRequestAdvice"); | ||
transformer.applyAdviceToMethod( | ||
isMethod() | ||
.and(named("createRestRequest")) | ||
.and(returns(named("org.elasticsearch.client.Request"))), | ||
this.getClass().getName() + "$CreateRestRequestAdvice"); | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class PerformRequestAdvice { | ||
|
||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static Scope onEnter(@Advice.Argument(0) String endpointId) { | ||
return EndpointId.storeInContext(Context.current(), endpointId).makeCurrent(); | ||
} | ||
|
||
@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class) | ||
public static void onExit(@Advice.Enter Scope scope) { | ||
if (scope != null) { | ||
scope.close(); | ||
} | ||
} | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class CreateRestRequestAdvice { | ||
|
||
@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class) | ||
public static void onExit(@Advice.Return Request request) { | ||
String endpointId = EndpointId.get(Context.current()); | ||
if (endpointId == null) { | ||
return; | ||
} | ||
if (endpointId.startsWith("es/") && endpointId.length() > 3) { | ||
endpointId = endpointId.substring(3); | ||
} | ||
VirtualField.find(Request.class, ElasticsearchEndpointDefinition.class) | ||
.set(request, ElasticsearchEndpointMap.get(endpointId)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters