diff --git a/instrumentation/elasticsearch/elasticsearch-api-client-7.16/javaagent/src/test/java/ElasticsearchClientTest.java b/instrumentation/elasticsearch/elasticsearch-api-client-7.16/javaagent/src/test/java/ElasticsearchClientTest.java index a38beba61fe8..5ce67f8ec703 100644 --- a/instrumentation/elasticsearch/elasticsearch-api-client-7.16/javaagent/src/test/java/ElasticsearchClientTest.java +++ b/instrumentation/elasticsearch/elasticsearch-api-client-7.16/javaagent/src/test/java/ElasticsearchClientTest.java @@ -3,24 +3,18 @@ * 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; import co.elastic.clients.json.jackson.JacksonJsonpMapper; import co.elastic.clients.transport.ElasticsearchTransport; +import co.elastic.clients.transport.Version; import co.elastic.clients.transport.rest_client.RestClientTransport; import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.trace.SpanKind; 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; @@ -30,6 +24,13 @@ 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 { @RegisterExtension @@ -73,7 +74,7 @@ static void cleanUp() { } private static String userAgent() { - return "elastic-java/7.16.0" + " (Java/" + System.getProperty("java.version") + ")"; + return "elastic-java/" + Version.VERSION + " (Java/" + System.getProperty("java.version") + ")"; } @Test @@ -92,7 +93,9 @@ public void elasticsearchStatus() throws IOException { equalTo(SemanticAttributes.DB_SYSTEM, "elasticsearch"), equalTo(SemanticAttributes.DB_OPERATION, "info"), equalTo(SemanticAttributes.HTTP_METHOD, "GET"), - equalTo(SemanticAttributes.HTTP_URL, httpHost.toURI() + "/")), + equalTo(SemanticAttributes.HTTP_URL, httpHost.toURI() + "/"), + equalTo(SemanticAttributes.NET_PEER_NAME, httpHost.getHostName()), + equalTo(SemanticAttributes.NET_PEER_PORT, httpHost.getPort())), span -> span.hasName("GET") .hasKind(SpanKind.CLIENT) @@ -130,6 +133,8 @@ public void elasticsearchIndex() throws IOException { .hasAttributesSatisfyingExactly( equalTo(SemanticAttributes.DB_SYSTEM, "elasticsearch"), equalTo(SemanticAttributes.DB_OPERATION, "index"), + equalTo(SemanticAttributes.NET_PEER_NAME, httpHost.getHostName()), + equalTo(SemanticAttributes.NET_PEER_PORT, httpHost.getPort()), equalTo(SemanticAttributes.HTTP_METHOD, "PUT"), equalTo( SemanticAttributes.HTTP_URL, @@ -194,6 +199,8 @@ public void elasticsearchStatusAsync() throws Exception { .hasAttributesSatisfyingExactly( equalTo(SemanticAttributes.DB_SYSTEM, "elasticsearch"), equalTo(SemanticAttributes.DB_OPERATION, "info"), + equalTo(SemanticAttributes.NET_PEER_NAME, httpHost.getHostName()), + equalTo(SemanticAttributes.NET_PEER_PORT, httpHost.getPort()), equalTo(SemanticAttributes.HTTP_METHOD, "GET"), equalTo(SemanticAttributes.HTTP_URL, httpHost.toURI() + "/")), span -> diff --git a/instrumentation/elasticsearch/elasticsearch-rest-5.0/javaagent/src/test/java/ElasticsearchRest5Test.java b/instrumentation/elasticsearch/elasticsearch-rest-5.0/javaagent/src/test/java/ElasticsearchRest5Test.java index 7a51b0bd2463..e138e82b0d46 100644 --- a/instrumentation/elasticsearch/elasticsearch-rest-5.0/javaagent/src/test/java/ElasticsearchRest5Test.java +++ b/instrumentation/elasticsearch/elasticsearch-rest-5.0/javaagent/src/test/java/ElasticsearchRest5Test.java @@ -93,6 +93,8 @@ void elasticsearchStatus() throws IOException { .hasAttributesSatisfyingExactly( equalTo(SemanticAttributes.DB_SYSTEM, "elasticsearch"), equalTo(SemanticAttributes.HTTP_METHOD, "GET"), + equalTo(SemanticAttributes.NET_PEER_NAME, httpHost.getHostName()), + equalTo(SemanticAttributes.NET_PEER_PORT, httpHost.getPort()), equalTo( SemanticAttributes.HTTP_URL, httpHost.toURI() + "/_cluster/health")); }, @@ -172,6 +174,8 @@ public void onFailure(Exception e) { .hasAttributesSatisfyingExactly( equalTo(SemanticAttributes.DB_SYSTEM, "elasticsearch"), equalTo(SemanticAttributes.HTTP_METHOD, "GET"), + equalTo(SemanticAttributes.NET_PEER_NAME, httpHost.getHostName()), + equalTo(SemanticAttributes.NET_PEER_PORT, httpHost.getPort()), equalTo( SemanticAttributes.HTTP_URL, httpHost.toURI() + "/_cluster/health")); }, diff --git a/instrumentation/elasticsearch/elasticsearch-rest-6.4/javaagent/src/test/java/ElasticsearchRest6Test.java b/instrumentation/elasticsearch/elasticsearch-rest-6.4/javaagent/src/test/java/ElasticsearchRest6Test.java index 6a995c35e5cb..ac10e225abdf 100644 --- a/instrumentation/elasticsearch/elasticsearch-rest-6.4/javaagent/src/test/java/ElasticsearchRest6Test.java +++ b/instrumentation/elasticsearch/elasticsearch-rest-6.4/javaagent/src/test/java/ElasticsearchRest6Test.java @@ -84,6 +84,8 @@ public void elasticsearchStatus() throws IOException { .hasAttributesSatisfyingExactly( equalTo(SemanticAttributes.DB_SYSTEM, "elasticsearch"), equalTo(SemanticAttributes.HTTP_METHOD, "GET"), + equalTo(SemanticAttributes.NET_PEER_NAME, httpHost.getHostName()), + equalTo(SemanticAttributes.NET_PEER_PORT, httpHost.getPort()), equalTo( SemanticAttributes.HTTP_URL, httpHost.toURI() + "/_cluster/health")); }, @@ -162,6 +164,8 @@ public void onFailure(Exception e) { .hasAttributesSatisfyingExactly( equalTo(SemanticAttributes.DB_SYSTEM, "elasticsearch"), equalTo(SemanticAttributes.HTTP_METHOD, "GET"), + equalTo(SemanticAttributes.NET_PEER_NAME, httpHost.getHostName()), + equalTo(SemanticAttributes.NET_PEER_PORT, httpHost.getPort()), equalTo( SemanticAttributes.HTTP_URL, httpHost.toURI() + "/_cluster/health")); }, diff --git a/instrumentation/elasticsearch/elasticsearch-rest-7.0/javaagent/src/test/java/ElasticsearchRest7Test.java b/instrumentation/elasticsearch/elasticsearch-rest-7.0/javaagent/src/test/java/ElasticsearchRest7Test.java index a8895fb8013f..922364245aeb 100644 --- a/instrumentation/elasticsearch/elasticsearch-rest-7.0/javaagent/src/test/java/ElasticsearchRest7Test.java +++ b/instrumentation/elasticsearch/elasticsearch-rest-7.0/javaagent/src/test/java/ElasticsearchRest7Test.java @@ -84,6 +84,8 @@ public void elasticsearchStatus() throws Exception { .hasAttributesSatisfyingExactly( equalTo(SemanticAttributes.DB_SYSTEM, "elasticsearch"), equalTo(SemanticAttributes.HTTP_METHOD, "GET"), + equalTo(SemanticAttributes.NET_PEER_NAME, httpHost.getHostName()), + equalTo(SemanticAttributes.NET_PEER_PORT, httpHost.getPort()), equalTo( SemanticAttributes.HTTP_URL, httpHost.toURI() + "/_cluster/health")), @@ -159,6 +161,8 @@ public void onFailure(Exception e) { .hasAttributesSatisfyingExactly( equalTo(SemanticAttributes.DB_SYSTEM, "elasticsearch"), equalTo(SemanticAttributes.HTTP_METHOD, "GET"), + equalTo(SemanticAttributes.NET_PEER_NAME, httpHost.getHostName()), + equalTo(SemanticAttributes.NET_PEER_PORT, httpHost.getPort()), equalTo( SemanticAttributes.HTTP_URL, httpHost.toURI() + "/_cluster/health")), diff --git a/instrumentation/elasticsearch/elasticsearch-rest-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/elasticsearch/rest/ElasticsearchClientAttributeExtractor.java b/instrumentation/elasticsearch/elasticsearch-rest-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/elasticsearch/rest/ElasticsearchClientAttributeExtractor.java index 47ea1f549fc7..1120eb070b32 100644 --- a/instrumentation/elasticsearch/elasticsearch-rest-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/elasticsearch/rest/ElasticsearchClientAttributeExtractor.java +++ b/instrumentation/elasticsearch/elasticsearch-rest-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/elasticsearch/rest/ElasticsearchClientAttributeExtractor.java @@ -15,8 +15,8 @@ import io.opentelemetry.instrumentation.api.instrumenter.url.internal.UrlAttributes; import io.opentelemetry.instrumentation.api.internal.SemconvStability; import io.opentelemetry.semconv.trace.attributes.SemanticAttributes; -import java.net.InetAddress; import javax.annotation.Nullable; +import org.apache.http.HttpHost; import org.elasticsearch.client.Response; public class ElasticsearchClientAttributeExtractor @@ -25,16 +25,16 @@ public class ElasticsearchClientAttributeExtractor private static final String PATH_PARTS_ATTRIBUTE_PREFIX = "db.elasticsearch.path_parts."; private static void setServerAttributes(AttributesBuilder attributes, Response response) { - InetAddress hostAddress = response.getHost().getAddress(); - if (hostAddress != null) { + HttpHost host = response.getHost(); + if (host != null) { if (SemconvStability.emitStableHttpSemconv()) { - internalSet(attributes, NetworkAttributes.SERVER_ADDRESS, hostAddress.getHostAddress()); - internalSet(attributes, NetworkAttributes.SERVER_PORT, (long) response.getHost().getPort()); + internalSet(attributes, NetworkAttributes.SERVER_ADDRESS, host.getHostName()); + internalSet(attributes, NetworkAttributes.SERVER_PORT, (long) host.getPort()); } if (SemconvStability.emitOldHttpSemconv()) { - internalSet(attributes, SemanticAttributes.NET_PEER_NAME, hostAddress.getHostAddress()); + internalSet(attributes, SemanticAttributes.NET_PEER_NAME, host.getHostName()); internalSet( - attributes, SemanticAttributes.NET_PEER_PORT, (long) response.getHost().getPort()); + attributes, SemanticAttributes.NET_PEER_PORT, (long) host.getPort()); } } }