Skip to content

Commit

Permalink
Refactored ES 7.16+ instrumentation
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Wert <alexander.wert@elastic.co>
  • Loading branch information
AlexanderWert committed Jul 3, 2023
1 parent befc456 commit d9c5117
Show file tree
Hide file tree
Showing 14 changed files with 1,064 additions and 975 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
plugins {
id("otel.java-conventions")
}

dependencies {
testImplementation(project(":instrumentation:elasticsearch:elasticsearch-rest-common:javaagent"))
testImplementation(project(":instrumentation:elasticsearch:elasticsearch-api-client-7.16:javaagent"))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.elasticsearch.rest;

import static org.junit.jupiter.api.Assertions.assertEquals;

import io.opentelemetry.javaagent.instrumentation.elasticsearch.apiclient.ElasticsearchEndpointMap;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Test;

public class ElasticsearchEndpointMapTest {

private static final Set<String> SEARCH_ENDPOINTS =
new HashSet<>(
Arrays.asList(
"search",
"async_search.submit",
"msearch",
"eql.search",
"terms_enum",
"search_template",
"msearch_template",
"render_search_template"));

private static List<String> getPathParts(String route) {
List<String> pathParts = new ArrayList<>();
String routeFragment = route;
int paramStartIndex = routeFragment.indexOf('{');
while (paramStartIndex >= 0) {
int paramEndIndex = routeFragment.indexOf('}');
if (paramEndIndex < 0 || paramEndIndex <= paramStartIndex + 1) {
throw new IllegalStateException("Invalid route syntax!");
}
pathParts.add(routeFragment.substring(paramStartIndex + 1, paramEndIndex));

int nextIdx = paramEndIndex + 1;
if (nextIdx >= routeFragment.length()) {
break;
}

routeFragment = routeFragment.substring(nextIdx);
paramStartIndex = routeFragment.indexOf('{');
}
return pathParts;
}

@Test
public void testIsSearchEndpoint() {
for (ElasticsearchEndpointDefinition esEndpointDefinition :
ElasticsearchEndpointMap.getAllEndpoints()) {
String endpointId = esEndpointDefinition.getEndpointName();
assertEquals(SEARCH_ENDPOINTS.contains(endpointId), esEndpointDefinition.isSearchEndpoint());
}
}

@Test
public void testProcessPathParts() {
for (ElasticsearchEndpointDefinition esEndpointDefinition :
ElasticsearchEndpointMap.getAllEndpoints()) {
for (String route :
esEndpointDefinition.getRoutes().stream()
.map(ElasticsearchEndpointDefinition.Route::getName)
.collect(Collectors.toList())) {
List<String> pathParts = getPathParts(route);
String resolvedRoute = route.replace("{", "").replace("}", "");
Map<String, String> observedParams = new HashMap<>();
esEndpointDefinition.processPathParts(resolvedRoute, (k, v) -> observedParams.put(k, v));

Map<String, String> expectedMap = new HashMap<>();
pathParts.forEach(part -> expectedMap.put(part, part));

assertEquals(expectedMap, observedParams);
}
}
}

@Test
public void testSearchEndpoint() {
ElasticsearchEndpointDefinition esEndpoint = ElasticsearchEndpointMap.get("search");
Map<String, String> observedParams = new HashMap<>();
esEndpoint.processPathParts(
"/test-index-1,test-index-2/_search", (k, v) -> observedParams.put(k, v));

assertEquals("test-index-1,test-index-2", observedParams.get("index"));
}

@Test
public void testBuildRegexPattern() {
Pattern pattern =
ElasticsearchEndpointDefinition.EndpointPattern.buildRegexPattern(
"/_nodes/{node_id}/shutdown");
assertEquals("^/_nodes/(?<node0id>[^/]+)/shutdown$", pattern.pattern());

pattern =
ElasticsearchEndpointDefinition.EndpointPattern.buildRegexPattern(
"/_snapshot/{repository}/{snapshot}/_mount");
assertEquals("^/_snapshot/(?<repository>[^/]+)/(?<snapshot>[^/]+)/_mount$", pattern.pattern());

pattern =
ElasticsearchEndpointDefinition.EndpointPattern.buildRegexPattern(
"/_security/profile/_suggest");
assertEquals("^/_security/profile/_suggest$", pattern.pattern());

pattern =
ElasticsearchEndpointDefinition.EndpointPattern.buildRegexPattern(
"/_application/search_application/{name}");
assertEquals("^/_application/search_application/(?<name>[^/]+)$", pattern.pattern());

pattern = ElasticsearchEndpointDefinition.EndpointPattern.buildRegexPattern("/");
assertEquals("^/$", pattern.pattern());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ muzzle {
}

dependencies {
library("co.elastic.clients:elasticsearch-java:8.0.0")
library("co.elastic.clients:elasticsearch-java:7.16.0")

implementation(project(":instrumentation:elasticsearch:elasticsearch-rest-common:javaagent"))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.elasticsearch.apiclient.v8_0;
package io.opentelemetry.javaagent.instrumentation.elasticsearch.apiclient;

import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.named;
Expand Down Expand Up @@ -49,10 +49,7 @@ public static void onPrepareLowLevelRequest(
if (endpointId.startsWith("es/") && endpointId.length() > 3) {
endpointId = endpointId.substring(3);
}
virtualField.set(
request,
new ElasticsearchEndpointDefinition(
endpointId, ElasticsearchEndpointMap.get().get(endpointId)));
virtualField.set(request, ElasticsearchEndpointMap.get(endpointId));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.elasticsearch.apiclient.v8_0;
package io.opentelemetry.javaagent.instrumentation.elasticsearch.apiclient;

import static java.util.Collections.singletonList;

Expand All @@ -15,7 +15,7 @@
@AutoService(InstrumentationModule.class)
public class ElasticsearchApiClientInstrumentationModule extends InstrumentationModule {
public ElasticsearchApiClientInstrumentationModule() {
super("elasticsearch-client", "elasticsearch-api-client", "elasticsearch");
super("elasticsearch-api-client-7.16", "elasticsearch");
}

@Override
Expand Down

0 comments on commit d9c5117

Please sign in to comment.