-
Notifications
You must be signed in to change notification settings - Fork 246
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Alexander Wert <[email protected]>
- Loading branch information
1 parent
73cab12
commit d81f7dc
Showing
10 changed files
with
211 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
197 changes: 197 additions & 0 deletions
197
java-client/src/main/java/co/elastic/clients/transport/rest_client/Instrumentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package co.elastic.clients.transport.rest_client; | ||
|
||
import co.elastic.clients.transport.Endpoint; | ||
import io.opentelemetry.api.GlobalOpenTelemetry; | ||
import io.opentelemetry.api.OpenTelemetry; | ||
import io.opentelemetry.api.common.AttributeKey; | ||
import io.opentelemetry.api.trace.Span; | ||
import io.opentelemetry.api.trace.SpanKind; | ||
import io.opentelemetry.api.trace.Tracer; | ||
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes; | ||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
import org.apache.http.HttpEntity; | ||
import org.apache.http.HttpHost; | ||
import org.elasticsearch.client.Response; | ||
|
||
import javax.annotation.Nullable; | ||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.net.InetAddress; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
public class Instrumentation { | ||
|
||
private static final Set<String> SEARCH_ENDPOINTS = new HashSet<>(Arrays.asList( | ||
"render_search_template", | ||
"terms_enum", | ||
"msearch_template", | ||
"eql.search", | ||
"msearch", | ||
"search_template", | ||
"async_search.submit", | ||
"search")); | ||
|
||
|
||
// these reflect the config options in the OTel Java agent | ||
private static final boolean INSTRUMENTATION_ENABLED = | ||
Boolean.parseBoolean( | ||
ConfigUtil.getConfigOption("otel.instrumentation.elasticsearch.enabled", "true")); | ||
|
||
private static final boolean CAPTURE_SEARCH_BODY = | ||
Boolean.parseBoolean( | ||
ConfigUtil.getConfigOption("otel.instrumentation.elasticsearch.capture-search-query", "false")); | ||
|
||
private static final Log logger = LogFactory.getLog(Instrumentation.class); | ||
|
||
private final Tracer tracer; | ||
|
||
protected Instrumentation(@Nullable OpenTelemetry openTelemetry) { | ||
if (openTelemetry == null) { | ||
openTelemetry = GlobalOpenTelemetry.get(); | ||
} | ||
|
||
tracer = openTelemetry.getTracer("elasticsearch-api"); | ||
} | ||
|
||
protected <RequestT, ResponseT, ErrorT> Span createSpanForRequest(RequestT request, | ||
Endpoint<RequestT, ResponseT, ErrorT> endpoint) { | ||
if (!INSTRUMENTATION_ENABLED) { | ||
return Span.getInvalid(); | ||
} | ||
|
||
Span span = tracer.spanBuilder(endpoint.id()).setSpanKind(SpanKind.CLIENT).startSpan(); | ||
if (isInvalidSpan(span)) { | ||
span.setAttribute(OTelAttributes.DB_SYSTEM, "elasticsearch"); | ||
span.setAttribute(OTelAttributes.DB_OPERATION, endpoint.id()); | ||
span.setAttribute(OTelAttributes.HTTP_REQUEST_METHOD, endpoint.method(request)); | ||
|
||
for (Map.Entry<String, String> pathParamEntry : endpoint.pathParameters(request).entrySet()) { | ||
String attributeKey = OTelAttributes.PATH_PART_PREFIX + pathParamEntry.getKey(); | ||
span.setAttribute(AttributeKey.stringKey(attributeKey), pathParamEntry.getValue()); | ||
} | ||
} | ||
|
||
return span; | ||
} | ||
|
||
protected void captureResponseInformation(@Nullable Span span, Response response) { | ||
if (isInvalidSpan(span)) { | ||
return; | ||
} | ||
|
||
HttpHost host = response.getHost(); | ||
String uri = response.getRequestLine().getUri(); | ||
uri = uri.startsWith("/") ? uri : "/" + uri; | ||
String fullUrl = host.toURI() + uri; | ||
|
||
span.setAttribute(OTelAttributes.URL_FULL, fullUrl); | ||
span.setAttribute(OTelAttributes.SERVER_PORT, host.getPort()); | ||
|
||
InetAddress hostAddress = response.getHost().getAddress(); | ||
if (hostAddress != null) { | ||
span.setAttribute(OTelAttributes.SERVER_ADDRESS, hostAddress.getHostAddress()); | ||
} | ||
} | ||
|
||
protected <RequestT> void captureBody(@Nullable Span span, Endpoint<RequestT, ?, ?> endpoint, | ||
HttpEntity httpEntity) { | ||
try { | ||
if (shouldCaptureBody(span, endpoint, httpEntity)) { | ||
|
||
String body = new BufferedReader( | ||
new InputStreamReader(httpEntity.getContent(), StandardCharsets.UTF_8)) | ||
.lines() | ||
.collect(Collectors.joining()); | ||
|
||
span.setAttribute(OTelAttributes.DB_STATEMENT, body); | ||
} | ||
} catch (IOException e) { | ||
logger.debug("Failed reading HTTP body content.", e); | ||
} | ||
} | ||
|
||
private <RequestT> boolean shouldCaptureBody(@Nullable Span span, Endpoint<RequestT, ?, ?> endpoint, HttpEntity httpEntity) { | ||
return !isInvalidSpan(span) | ||
&& CAPTURE_SEARCH_BODY | ||
&& SEARCH_ENDPOINTS.contains(endpoint.id()) | ||
&& httpEntity != null | ||
&& httpEntity.isRepeatable(); | ||
} | ||
|
||
private boolean isInvalidSpan(@Nullable Span span) { | ||
return !INSTRUMENTATION_ENABLED || span == null || !span.isRecording(); | ||
} | ||
|
||
private static final class OTelAttributes { | ||
private static final AttributeKey<String> DB_SYSTEM = SemanticAttributes.DB_SYSTEM; | ||
private static final AttributeKey<String> DB_OPERATION = SemanticAttributes.DB_OPERATION; | ||
private static final AttributeKey<String> DB_STATEMENT = SemanticAttributes.DB_STATEMENT; | ||
private static final AttributeKey<String> HTTP_REQUEST_METHOD = AttributeKey.stringKey("http.request.method"); | ||
private static final AttributeKey<String> URL_FULL = AttributeKey.stringKey("url.full"); | ||
private static final AttributeKey<String> SERVER_ADDRESS = AttributeKey.stringKey("server.address"); | ||
private static final AttributeKey<Long> SERVER_PORT = AttributeKey.longKey("server.port"); | ||
|
||
private static final String PATH_PART_PREFIX = "db.elasticsearch.path_parts."; | ||
} | ||
|
||
private static final class ConfigUtil { | ||
private static String getConfigOption(String key, String defaultValue) { | ||
String normalizedKey = normalizePropertyKey(key); | ||
String systemProperty = | ||
System.getProperties().entrySet().stream() | ||
.filter(entry -> normalizedKey.equals(normalizePropertyKey(entry.getKey().toString()))) | ||
.map(entry -> entry.getValue().toString()) | ||
.findFirst() | ||
.orElse(null); | ||
if (systemProperty != null) { | ||
return systemProperty; | ||
} | ||
return System.getenv().entrySet().stream() | ||
.filter(entry -> normalizedKey.equals(normalizeEnvironmentVariableKey(entry.getKey()))) | ||
.map(Map.Entry::getValue) | ||
.findFirst() | ||
.orElse(defaultValue); | ||
} | ||
|
||
/** | ||
* Normalize an environment variable key by converting to lower case and replacing "_" with ".". | ||
*/ | ||
private static String normalizeEnvironmentVariableKey(String key) { | ||
return key.toLowerCase(Locale.ROOT).replace("_", "."); | ||
} | ||
|
||
/** | ||
* Normalize a property key by converting to lower case and replacing "-" with ".". | ||
*/ | ||
private static String normalizePropertyKey(String key) { | ||
return key.toLowerCase(Locale.ROOT).replace("-", "."); | ||
} | ||
} | ||
} |
Oops, something went wrong.