Skip to content

Commit 099f470

Browse files
committed
using stable conventions
1 parent 4ddc663 commit 099f470

File tree

3 files changed

+22
-22
lines changed

3 files changed

+22
-22
lines changed

java-client/build.gradle.kts

+2-3
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ dependencies {
179179
// the Java API client coexists with a 8.x HLRC work fine
180180
val elasticsearchVersion = "9.0.0"
181181
val jacksonVersion = "2.18.3"
182-
val openTelemetryVersion = "1.29.0"
182+
val openTelemetryVersion = "1.32.0"
183183

184184
// Apache 2.0
185185
// https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-low.html
@@ -211,8 +211,7 @@ dependencies {
211211
// Apache 2.0
212212
// https://github.com/open-telemetry/opentelemetry-java
213213
implementation("io.opentelemetry", "opentelemetry-api", openTelemetryVersion)
214-
// Use it once it's stable (see Instrumentation.java). Limited to tests for now.
215-
testImplementation("io.opentelemetry", "opentelemetry-semconv", "$openTelemetryVersion-alpha")
214+
implementation("io.opentelemetry.semconv","opentelemetry-semconv", openTelemetryVersion)
216215
testImplementation("io.opentelemetry", "opentelemetry-sdk", openTelemetryVersion)
217216

218217
// EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0

java-client/src/main/java/co/elastic/clients/transport/instrumentation/OpenTelemetryForElasticsearch.java

+11-12
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
import io.opentelemetry.api.trace.StatusCode;
3232
import io.opentelemetry.api.trace.Tracer;
3333
import io.opentelemetry.context.Scope;
34+
import io.opentelemetry.semconv.HttpAttributes;
35+
import io.opentelemetry.semconv.ServerAttributes;
36+
import io.opentelemetry.semconv.UrlAttributes;
3437
import org.apache.commons.logging.Log;
3538
import org.apache.commons.logging.LogFactory;
3639

@@ -69,18 +72,14 @@ public class OpenTelemetryForElasticsearch implements Instrumentation {
6972
"search"
7073
));
7174

72-
private static final AttributeKey<String> ATTR_DB_SYSTEM = AttributeKey.stringKey("db.system");
73-
private static final AttributeKey<String> ATTR_DB_OPERATION = AttributeKey.stringKey("db.operation");
74-
private static final AttributeKey<String> ATTR_DB_STATEMENT = AttributeKey.stringKey("db.statement");
75-
// Use Semantic Convention keys once opentelemetry-semconv is stable
76-
//private static final AttributeKey<String> ATTR_DB_SYSTEM = SemanticAttributes.DB_SYSTEM;
77-
//private static final AttributeKey<String> ATTR_DB_OPERATION = SemanticAttributes.DB_OPERATION;
78-
//private static final AttributeKey<String> ATTR_DB_STATEMENT = SemanticAttributes.DB_STATEMENT;
75+
private static final AttributeKey<String> ATTR_DB_SYSTEM = AttributeKey.stringKey("db.system.name");
76+
private static final AttributeKey<String> ATTR_DB_OPERATION = AttributeKey.stringKey("db.operation.name");
77+
private static final AttributeKey<String> ATTR_DB_QUERY = AttributeKey.stringKey("db.query.text");
7978

80-
private static final AttributeKey<String> ATTR_HTTP_REQUEST_METHOD = AttributeKey.stringKey("http.request.method");
81-
private static final AttributeKey<String> ATTR_URL_FULL = AttributeKey.stringKey("url.full");
82-
private static final AttributeKey<String> ATTR_SERVER_ADDRESS = AttributeKey.stringKey("server.address");
83-
private static final AttributeKey<Long> ATTR_SERVER_PORT = AttributeKey.longKey("server.port");
79+
private static final AttributeKey<String> ATTR_HTTP_REQUEST_METHOD = HttpAttributes.HTTP_REQUEST_METHOD;
80+
private static final AttributeKey<String> ATTR_URL_FULL = UrlAttributes.URL_FULL;
81+
private static final AttributeKey<String> ATTR_SERVER_ADDRESS = ServerAttributes.SERVER_ADDRESS;
82+
private static final AttributeKey<Long> ATTR_SERVER_PORT = ServerAttributes.SERVER_PORT;
8483

8584
// Caching attributes keys to avoid unnecessary memory allocation
8685
private static final Map<String, AttributeKey<String>> attributesKeyCache = new ConcurrentHashMap<>();
@@ -222,7 +221,7 @@ public void beforeSendingHttpRequest(TransportHttpClient.Request httpRequest, Tr
222221
sb.append(StandardCharsets.UTF_8.decode(buf));
223222
buf.reset();
224223
}
225-
span.setAttribute(ATTR_DB_STATEMENT, sb.toString());
224+
span.setAttribute(ATTR_DB_QUERY, sb.toString());
226225
}
227226
} catch (Exception e) {
228227
logger.debug("Failed reading HTTP body content for an OpenTelemetry span.", e);

java-client/src/test/java/co/elastic/clients/transport/instrumentation/OpenTelemetryForElasticsearchTest.java

+9-7
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@
3838
import io.opentelemetry.sdk.trace.data.SpanData;
3939
import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor;
4040
import io.opentelemetry.sdk.trace.export.SpanExporter;
41-
import io.opentelemetry.semconv.resource.attributes.ResourceAttributes;
42-
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
4341
import org.junit.jupiter.api.AfterAll;
4442
import org.junit.jupiter.api.Assertions;
4543
import org.junit.jupiter.api.BeforeAll;
@@ -57,6 +55,8 @@
5755
import java.util.concurrent.TimeUnit;
5856
import java.util.concurrent.TimeoutException;
5957

58+
import static io.opentelemetry.semconv.ServiceAttributes.SERVICE_NAME;
59+
6060
public class OpenTelemetryForElasticsearchTest {
6161
private static final String INDEX = "test-index";
6262
private static final String DOC_ID = "1234567";
@@ -100,7 +100,9 @@ public class OpenTelemetryForElasticsearchTest {
100100
" ]\n" +
101101
" }\n" +
102102
"}";
103-
public static final String DB_OPERATION = "db.operation";
103+
public static final String DB_SYSTEM = "db.system.name";
104+
public static final String DB_OPERATION = "db.operation.name";
105+
public static final String DB_QUERY = "db.query.text";
104106
public static final String URL_FULL = "url.full";
105107
public static final String SERVER_ADDRESS = "server.address";
106108
public static final String SERVER_PORT = "server.port";
@@ -162,7 +164,7 @@ private static void setupHttpServer() throws IOException {
162164

163165
private static void setupOTel() {
164166
Resource resource = Resource.getDefault()
165-
.merge(Resource.create(Attributes.of(ResourceAttributes.SERVICE_NAME, "es-api-test")));
167+
.merge(Resource.create(Attributes.of(SERVICE_NAME, "es-api-test")));
166168

167169
spanExporter = new MockSpanExporter();
168170

@@ -189,7 +191,7 @@ public void testGetRequest() throws IOException, InterruptedException {
189191
Assertions.assertEquals("get", span.getName());
190192
Assertions.assertEquals("get", span.getAttributes().get(AttributeKey.stringKey(DB_OPERATION)));
191193
Assertions.assertEquals("GET", span.getAttributes().get(AttributeKey.stringKey(HTTP_REQUEST_METHOD)));
192-
Assertions.assertEquals("elasticsearch", span.getAttributes().get(SemanticAttributes.DB_SYSTEM));
194+
Assertions.assertEquals("elasticsearch", span.getAttributes().get(AttributeKey.stringKey(DB_SYSTEM)));
193195

194196
String url = "http://" + httpServer.getAddress().getHostString() + ":" + httpServer.getAddress().getPort() +
195197
"/" + INDEX + "/_doc/" + DOC_ID + "?refresh=true";
@@ -214,7 +216,7 @@ public void testSearchRequest() throws IOException, InterruptedException {
214216
Assertions.assertEquals(spanExporter.getSpans().size(), 1);
215217
SpanData span = spanExporter.getSpans().get(0);
216218
Assertions.assertEquals("search", span.getName());
217-
Assertions.assertEquals(queryAsString, span.getAttributes().get(SemanticAttributes.DB_STATEMENT));
219+
Assertions.assertEquals(queryAsString, span.getAttributes().get(AttributeKey.stringKey(DB_QUERY)));
218220
}
219221

220222
@Test
@@ -228,7 +230,7 @@ public void testAsyncSearchRequest() throws IOException, InterruptedException, T
228230
Assertions.assertEquals("search", span.getName());
229231

230232
// We're not capturing bodies by default
231-
Assertions.assertNull(span.getAttributes().get(SemanticAttributes.DB_STATEMENT));
233+
Assertions.assertNull(span.getAttributes().get(AttributeKey.stringKey(DB_QUERY)));
232234
}
233235

234236
private static class MockSpanExporter implements SpanExporter {

0 commit comments

Comments
 (0)