Skip to content

Commit

Permalink
fixed review comments round 2
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 4, 2023
1 parent 273a785 commit 240f218
Show file tree
Hide file tree
Showing 7 changed files with 588 additions and 403 deletions.

Large diffs are not rendered by default.

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

import static io.opentelemetry.instrumentation.testing.GlobalTraceUtil.runWithSpan;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.satisfies;

import co.elastic.clients.elasticsearch.ElasticsearchAsyncClient;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.core.InfoResponse;
Expand All @@ -15,6 +19,9 @@
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.apache.http.HttpHost;
import org.assertj.core.api.AbstractLongAssert;
import org.elasticsearch.client.RestClient;
Expand All @@ -24,15 +31,8 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.testcontainers.elasticsearch.ElasticsearchContainer;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import static io.opentelemetry.instrumentation.testing.GlobalTraceUtil.runWithSpan;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.satisfies;

public class ElasticsearchClientTest {
class ElasticsearchClientTest {
@RegisterExtension
static final InstrumentationExtension testing = AgentInstrumentationExtension.create();

Expand Down Expand Up @@ -180,8 +180,8 @@ public void elasticsearchStatusAsync() throws Exception {
runWithSpan(
"callback",
() -> {
countDownLatch.countDown();
request.setResponse(infoResponse);
countDownLatch.countDown();
})));
//noinspection ResultOfMethodCallIgnored
countDownLatch.await(10, TimeUnit.SECONDS);
Expand Down Expand Up @@ -226,7 +226,7 @@ public void elasticsearchStatusAsync() throws Exception {
}

private static class AsyncRequest {
InfoResponse response = null;
volatile InfoResponse response = null;

public InfoResponse getResponse() {
return response;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.junit.jupiter.api.extension.RegisterExtension;
import org.testcontainers.elasticsearch.ElasticsearchContainer;

public class ElasticsearchRest5Test {
class ElasticsearchRest5Test {

@RegisterExtension
static final InstrumentationExtension testing = AgentInstrumentationExtension.create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.junit.jupiter.api.extension.RegisterExtension;
import org.testcontainers.elasticsearch.ElasticsearchContainer;

public class ElasticsearchRest6Test {
class ElasticsearchRest6Test {
@RegisterExtension
static final InstrumentationExtension testing = AgentInstrumentationExtension.create();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import org.testcontainers.elasticsearch.ElasticsearchContainer;
import org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper;

public class ElasticsearchRest7Test {
class ElasticsearchRest7Test {
@RegisterExtension
static final InstrumentationExtension testing = AgentInstrumentationExtension.create();

Expand Down Expand Up @@ -189,8 +189,8 @@ public void onFailure(Exception e) {
}

private static class AsyncRequest {
Response requestResponse = null;
Exception exception = null;
volatile Response requestResponse = null;
volatile Exception exception = null;

public Response getRequestResponse() {
return requestResponse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io.opentelemetry.instrumentation.api.instrumenter.network.internal.NetworkAttributes;
import io.opentelemetry.instrumentation.api.instrumenter.url.internal.UrlAttributes;
import io.opentelemetry.instrumentation.api.internal.SemconvStability;
import io.opentelemetry.instrumentation.api.internal.cache.Cache;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import javax.annotation.Nullable;
import org.apache.http.HttpHost;
Expand All @@ -24,6 +25,8 @@ public class ElasticsearchClientAttributeExtractor

private static final String PATH_PARTS_ATTRIBUTE_PREFIX = "db.elasticsearch.path_parts.";

private static final Cache<String, AttributeKey<String>> pathPartKeysCache = Cache.weak();

private static void setServerAttributes(AttributesBuilder attributes, Response response) {
HttpHost host = response.getHost();
if (host != null) {
Expand All @@ -33,8 +36,7 @@ private static void setServerAttributes(AttributesBuilder attributes, Response r
}
if (SemconvStability.emitOldHttpSemconv()) {
internalSet(attributes, SemanticAttributes.NET_PEER_NAME, host.getHostName());
internalSet(
attributes, SemanticAttributes.NET_PEER_PORT, (long) host.getPort());
internalSet(attributes, SemanticAttributes.NET_PEER_PORT, (long) host.getPort());
}
}
}
Expand Down Expand Up @@ -63,8 +65,10 @@ private static void setPathPartsAttributes(
endpointDef.processPathParts(
request.getEndpoint(),
(key, value) -> {
String attributeKey = PATH_PARTS_ATTRIBUTE_PREFIX + key;
internalSet(attributes, AttributeKey.stringKey(attributeKey), value);
AttributeKey<String> attributeKey =
pathPartKeysCache.computeIfAbsent(
key, k -> AttributeKey.stringKey(PATH_PARTS_ATTRIBUTE_PREFIX + k));
internalSet(attributes, attributeKey, value);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

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

import static java.util.Collections.unmodifiableList;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand All @@ -15,7 +17,7 @@
import java.util.stream.Collectors;
import javax.annotation.Nullable;

public class ElasticsearchEndpointDefinition {
public final class ElasticsearchEndpointDefinition {

private static final String UNDERSCORE_REPLACEMENT = "0";

Expand All @@ -27,7 +29,8 @@ public class ElasticsearchEndpointDefinition {
public ElasticsearchEndpointDefinition(
String endpointName, String[] routes, boolean isSearchEndpoint) {
this.endpointName = endpointName;
this.routes = Arrays.stream(routes).map(Route::new).collect(Collectors.toList());
this.routes =
unmodifiableList(Arrays.stream(routes).map(Route::new).collect(Collectors.toList()));
this.isSearchEndpoint = isSearchEndpoint;
}

Expand Down Expand Up @@ -66,6 +69,7 @@ List<Route> getRoutes() {
static final class Route {
private final String name;
private final boolean hasParameters;

private volatile EndpointPattern epPattern;

public Route(String name) {
Expand All @@ -90,6 +94,8 @@ Matcher createMatcher(String urlPath) {
}

private EndpointPattern getEndpointPattern() {
// Intentionally NOT synchronizing here to avoid synchronization overhead.
// Main purpose here is to cache the pattern without the need for strict thread-safety.
if (epPattern == null) {
epPattern = new EndpointPattern(this);
}
Expand Down

0 comments on commit 240f218

Please sign in to comment.