3131import io .opentelemetry .api .trace .StatusCode ;
3232import io .opentelemetry .api .trace .Tracer ;
3333import 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 ;
3537import org .apache .commons .logging .Log ;
3638import org .apache .commons .logging .LogFactory ;
3739
5052import java .util .Set ;
5153import 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 );
0 commit comments