Skip to content

Commit 394dc0a

Browse files
committed
[TEMP - REVERT] temporarily reverting otel update for testing
1 parent 2bbd679 commit 394dc0a

3 files changed

Lines changed: 39 additions & 35 deletions

File tree

java-client/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ dependencies {
179179
val elasticsearchVersion = "9.2.0"
180180
val jacksonVersion = "2.18.3"
181181
val jackson3Version = "3.0.0"
182-
val openTelemetryVersion = "1.37.0"
182+
val openTelemetryVersion = "1.32.0"
183183

184184
api(project(":rest5-client"))
185185

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

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +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.AttributeKeyTemplate;
34+
import io.opentelemetry.semconv.HttpAttributes;
35+
import io.opentelemetry.semconv.ServerAttributes;
36+
import io.opentelemetry.semconv.UrlAttributes;
3537
import org.apache.commons.logging.Log;
3638
import org.apache.commons.logging.LogFactory;
3739

@@ -50,15 +52,6 @@
5052
import java.util.Set;
5153
import java.util.concurrent.ConcurrentHashMap;
5254

53-
import static io.opentelemetry.semconv.DbAttributes.DB_OPERATION_NAME;
54-
import static io.opentelemetry.semconv.DbAttributes.DB_QUERY_TEXT;
55-
import static io.opentelemetry.semconv.DbAttributes.DB_SYSTEM_NAME;
56-
import static io.opentelemetry.semconv.HttpAttributes.HTTP_REQUEST_METHOD;
57-
import static io.opentelemetry.semconv.HttpAttributes.HTTP_RESPONSE_STATUS_CODE;
58-
import static io.opentelemetry.semconv.ServerAttributes.SERVER_ADDRESS;
59-
import static io.opentelemetry.semconv.ServerAttributes.SERVER_PORT;
60-
import static io.opentelemetry.semconv.UrlAttributes.URL_FULL;
61-
6255
/**
6356
* An OpenTelemetry instrumentation for the Elasticsearch client.
6457
*
@@ -79,11 +72,19 @@ public class OpenTelemetryForElasticsearch implements Instrumentation {
7972
"search"
8073
));
8174

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");
78+
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;
8283

8384
// Caching attributes keys to avoid unnecessary memory allocation
8485
private static final Map<String, AttributeKey<String>> attributesKeyCache = new ConcurrentHashMap<>();
8586

86-
AttributeKeyTemplate<String> PATH_PART_PREFIX = AttributeKeyTemplate.stringKeyTemplate("db.elasticsearch.path_parts");
87+
private static final String PATH_PART_PREFIX = "db.elasticsearch.path_parts.";
8788

8889
// these reflect the config options in the OTel Java agent
8990
private static final boolean INSTRUMENTATION_ENABLED = Boolean.parseBoolean(
@@ -185,16 +186,17 @@ <TRequest> OTelContext(TRequest request, Endpoint<TRequest, ?, ?> endpoint) {
185186
this.endpointId = endpointId;
186187

187188
span = tracer.spanBuilder(endpointId).setSpanKind(SpanKind.CLIENT)
188-
.setAttribute(DB_SYSTEM_NAME, "elasticsearch")
189-
.setAttribute(DB_OPERATION_NAME, endpointId)
189+
.setAttribute(ATTR_DB_SYSTEM, "elasticsearch")
190+
.setAttribute(ATTR_DB_OPERATION, endpointId)
190191
.startSpan();
191192

192193
if (span.isRecording()) {
193194

194-
span.setAttribute(HTTP_REQUEST_METHOD, endpoint.method(request));
195+
span.setAttribute(ATTR_HTTP_REQUEST_METHOD, endpoint.method(request));
195196

196197
for (Map.Entry<String, String> pathParamEntry : endpoint.pathParameters(request).entrySet()) {
197-
AttributeKey<String> attributeKey = PATH_PART_PREFIX.getAttributeKey(pathParamEntry.getKey());
198+
AttributeKey<String> attributeKey = attributesKeyCache.computeIfAbsent(pathParamEntry.getKey(),
199+
(key) -> AttributeKey.stringKey(PATH_PART_PREFIX + key));
198200
span.setAttribute(attributeKey, pathParamEntry.getValue());
199201
}
200202
}
@@ -213,7 +215,7 @@ public void beforeSendingHttpRequest(TransportHttpClient.Request httpRequest, Tr
213215

214216
this.pathAndQuery = pathAndQuery(httpRequest, options);
215217

216-
span.setAttribute(HTTP_REQUEST_METHOD, httpRequest.method());
218+
span.setAttribute(ATTR_HTTP_REQUEST_METHOD, httpRequest.method());
217219
Iterable<ByteBuffer> body = httpRequest.body();
218220
if (body != null && shouldCaptureBody(span, endpointId)) {
219221
StringBuilder sb = new StringBuilder();
@@ -222,7 +224,7 @@ public void beforeSendingHttpRequest(TransportHttpClient.Request httpRequest, Tr
222224
sb.append(StandardCharsets.UTF_8.decode(buf));
223225
buf.reset();
224226
}
225-
span.setAttribute(DB_QUERY_TEXT, sb.toString());
227+
span.setAttribute(ATTR_DB_QUERY, sb.toString());
226228
}
227229
} catch (Exception e) {
228230
logger.debug("Failed reading HTTP body content for an OpenTelemetry span.", e);
@@ -237,10 +239,9 @@ public void afterReceivingHttpResponse(TransportHttpClient.Response httpResponse
237239
URI uri = httpResponse.node().uri();
238240
String fullUrl = uri.resolve(pathAndQuery).toString();
239241

240-
span.setAttribute(URL_FULL, fullUrl);
241-
span.setAttribute(SERVER_PORT, uri.getPort());
242-
span.setAttribute(SERVER_ADDRESS, uri.getHost());
243-
span.setAttribute(HTTP_RESPONSE_STATUS_CODE, httpResponse.statusCode());
242+
span.setAttribute(ATTR_URL_FULL, fullUrl);
243+
span.setAttribute(ATTR_SERVER_PORT, uri.getPort());
244+
span.setAttribute(ATTR_SERVER_ADDRESS, uri.getHost());
244245
}
245246
} catch (RuntimeException e) {
246247
logger.debug("Failed capturing response information for the OpenTelemetry span.", e);

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

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +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.DbAttributes;
42-
import io.opentelemetry.semconv.HttpAttributes;
43-
import io.opentelemetry.semconv.ServerAttributes;
44-
import io.opentelemetry.semconv.UrlAttributes;
4541
import org.junit.jupiter.api.AfterAll;
4642
import org.junit.jupiter.api.Assertions;
4743
import org.junit.jupiter.api.BeforeAll;
@@ -104,6 +100,14 @@ public class OpenTelemetryForElasticsearchTest {
104100
" ]\n" +
105101
" }\n" +
106102
"}";
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";
106+
public static final String URL_FULL = "url.full";
107+
public static final String SERVER_ADDRESS = "server.address";
108+
public static final String SERVER_PORT = "server.port";
109+
// has been renamed in 1.21 from http.method - see https://github.com/open-telemetry/semantic-conventions/blob/main/schemas/1.21.0
110+
public static final String HTTP_REQUEST_METHOD = "http.request.method";
107111
private static HttpServer httpServer;
108112
private static MockSpanExporter spanExporter;
109113
private static OpenTelemetry openTelemetry;
@@ -185,16 +189,15 @@ public void testGetRequest() throws IOException, InterruptedException {
185189
Assertions.assertEquals(spanExporter.getSpans().size(), 1);
186190
SpanData span = spanExporter.getSpans().get(0);
187191
Assertions.assertEquals("get", span.getName());
188-
Assertions.assertEquals("get", span.getAttributes().get(DbAttributes.DB_OPERATION_NAME));
189-
Assertions.assertEquals("GET", span.getAttributes().get(HttpAttributes.HTTP_REQUEST_METHOD));
190-
Assertions.assertEquals("elasticsearch", span.getAttributes().get(DbAttributes.DB_SYSTEM_NAME));
192+
Assertions.assertEquals("get", span.getAttributes().get(AttributeKey.stringKey(DB_OPERATION)));
193+
Assertions.assertEquals("GET", span.getAttributes().get(AttributeKey.stringKey(HTTP_REQUEST_METHOD)));
194+
Assertions.assertEquals("elasticsearch", span.getAttributes().get(AttributeKey.stringKey(DB_SYSTEM)));
191195

192196
String url = "http://" + httpServer.getAddress().getHostString() + ":" + httpServer.getAddress().getPort() +
193197
"/" + INDEX + "/_doc/" + DOC_ID + "?refresh=true";
194-
Assertions.assertEquals(url, span.getAttributes().get(UrlAttributes.URL_FULL));
195-
Assertions.assertEquals(200, span.getAttributes().get(HttpAttributes.HTTP_RESPONSE_STATUS_CODE));
196-
Assertions.assertEquals(httpServer.getAddress().getHostString(), span.getAttributes().get(ServerAttributes.SERVER_ADDRESS));
197-
Assertions.assertEquals(httpServer.getAddress().getPort(), span.getAttributes().get(ServerAttributes.SERVER_PORT));
198+
Assertions.assertEquals(url, span.getAttributes().get(AttributeKey.stringKey(URL_FULL)));
199+
Assertions.assertEquals(httpServer.getAddress().getHostString(), span.getAttributes().get(AttributeKey.stringKey(SERVER_ADDRESS)));
200+
Assertions.assertEquals(httpServer.getAddress().getPort(), span.getAttributes().get(AttributeKey.longKey(SERVER_PORT)));
198201

199202
// Path parts
200203
Assertions.assertEquals(DOC_ID, span.getAttributes().get(AttributeKey.stringKey("db.elasticsearch.path_parts.id")));
@@ -213,7 +216,7 @@ public void testSearchRequest() throws IOException, InterruptedException {
213216
Assertions.assertEquals(spanExporter.getSpans().size(), 1);
214217
SpanData span = spanExporter.getSpans().get(0);
215218
Assertions.assertEquals("search", span.getName());
216-
Assertions.assertEquals(queryAsString, span.getAttributes().get(DbAttributes.DB_QUERY_TEXT));
219+
Assertions.assertEquals(queryAsString, span.getAttributes().get(AttributeKey.stringKey(DB_QUERY)));
217220
}
218221

219222
@Test
@@ -227,7 +230,7 @@ public void testAsyncSearchRequest() throws IOException, InterruptedException, T
227230
Assertions.assertEquals("search", span.getName());
228231

229232
// We're not capturing bodies by default
230-
Assertions.assertNull(span.getAttributes().get(DbAttributes.DB_QUERY_TEXT));
233+
Assertions.assertNull(span.getAttributes().get(AttributeKey.stringKey(DB_QUERY)));
231234
}
232235

233236
private static class MockSpanExporter implements SpanExporter {

0 commit comments

Comments
 (0)