com.catchpoint.trace.instrument.integrations.elasticsearch.advice.ElasticsearchHelper Maven / Gradle / Ivy
package com.catchpoint.trace.instrument.integrations.elasticsearch.advice;
import com.catchpoint.trace.common.logger.LoggerFactory;
import com.catchpoint.trace.common.util.PropertyUtils;
import com.catchpoint.trace.core.CatchpointTracer;
import com.catchpoint.trace.core.TraceSupport;
import com.catchpoint.trace.instrument.integrations.elasticsearch.ElasticsearchTags;
import com.catchpoint.trace.core.scope.CatchpointScope;
import com.catchpoint.trace.core.span.CommonProperties;
import com.catchpoint.trace.core.span.CommonTags;
import com.catchpoint.trace.core.span.CatchpointSpan;
import com.catchpoint.trace.integrations.elasticsearch.intercept.ElasticsearchInterceptorManager;
import org.slf4j.Logger;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
/**
* Helper class for Elasticsearch related tracing stuff
*
* @author serkan
*/
public final class ElasticsearchHelper {
public static final String ELASTICSEARCH_DOMAIN_NAME = "DB";
public static final String ELASTICSEARCH_CLASS_NAME = "ELASTICSEARCH";
public static final String ELASTICSEARCH_OPERATION_NAME = "ElasticsearchRequest";
public static final String ELASTICSEARCH_DB_TYPE = "elasticsearch";
public static final String ES_BODY_PROP_NAME = "CATCHPOINT::ES_BODY";
public static final boolean MASK_ES_BODY =
PropertyUtils.getBooleanProperty(
"catchpoint.trace.integrations.elasticsearch.body.mask", false);
public static final int PATH_DEPTH =
PropertyUtils.getIntegerProperty("catchpoint.trace.integrations.elasticsearch.path.depth", 1);
public static final Boolean ES_URL_AS_RESOURCE_NAME =
PropertyUtils.getBooleanProperty("catchpoint.trace.integrations.elasticsearch.urlresource.enable", false);
private static final Logger LOGGER = LoggerFactory.getLogger(ElasticsearchHelper.class);
private ElasticsearchHelper() {
}
public static boolean isEnabled() {
Boolean disabled =
PropertyUtils.getBooleanProperty("catchpoint.trace.integrations.elasticsearch.disable");
// Disabled flag is explicitly set
if (disabled != null) {
return !disabled;
}
// If disabled flag is not set explicitly for this integration, check on global flag
return !PropertyUtils.getBooleanProperty(
"catchpoint.trace.integrations.disable", false);
}
private static String getNormalizedPath(String path) {
StringBuilder normalizedPathBuilder = new StringBuilder();
int pathSeparatorCount = 0;
char prevC = (char) -1;
for (int i = 0; i < path.length(); i++) {
char c = path.charAt(i);
if (c == '/' && prevC != '/') {
pathSeparatorCount++;
}
if (pathSeparatorCount > PATH_DEPTH) {
break;
}
normalizedPathBuilder.append(c);
prevC = c;
}
return normalizedPathBuilder.toString();
}
public static CatchpointScope createAndStartSpan(String endpoint, String method, String body) {
String normalizedUri = endpoint != null ? getNormalizedPath(endpoint) : null;
String operationName = ELASTICSEARCH_OPERATION_NAME;
if (normalizedUri != null) {
operationName = normalizedUri;
}
CatchpointTracer tracer = TraceSupport.getTracer();
CatchpointScope scope =
tracer.buildSpan(operationName).
withDomainName(ELASTICSEARCH_DOMAIN_NAME).
withClassName(ELASTICSEARCH_CLASS_NAME).
startActive(false);
CatchpointSpan span = scope.span();
span.setTag(ElasticsearchTags.ES_URI, endpoint);
if (normalizedUri != null) {
span.setTag(ElasticsearchTags.ES_NORMALIZED_URI, normalizedUri);
if (!ES_URL_AS_RESOURCE_NAME) {
List indexNames = Arrays.stream(normalizedUri.split(",")).map(indexName -> {
if (indexName.startsWith("/")) {
return indexName;
}
return "/" + indexName;
}).collect(Collectors.toList());
span.putProperty(CommonProperties.RESOURCE_NAMES, indexNames);
}
}
span.setTag(ElasticsearchTags.ES_METHOD, method);
if (body != null) {
span.putProperty(ES_BODY_PROP_NAME, body);
if (!MASK_ES_BODY) {
span.setTag(ElasticsearchTags.ES_BODY, body);
}
}
span.setTag(ElasticsearchTags.DB_TYPE, ELASTICSEARCH_DB_TYPE);
span.setTag(CommonTags.OPERATION_TYPE, method);
span.setTag(CommonTags.TOPOLOGY_VERTEX, true);
ElasticsearchInterceptorManager.onBeforeRequest(endpoint, method, body);
return scope;
}
}