Skip to content

Commit

Permalink
elastic#3030: Remove dependency to core module and use SDK dependency…
Browse files Browse the repository at this point in the history
… instead.
  • Loading branch information
raphw committed Feb 22, 2023
1 parent b28ae3d commit 90d3348
Show file tree
Hide file tree
Showing 172 changed files with 3,162 additions and 351 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@ private static AgentBuilder.Transformer.ForAdvice getTransformer(final ElasticAp
.withCustomMapping()
.with(new Advice.AssignReturned.Factory().withSuppressed(ClassCastException.class))
.bind(new SimpleMethodSignatureOffsetMappingFactory())
.bind(new co.elastic.apm.agent.sdk.bindings.SimpleMethodSignatureOffsetMappingFactory())
.bind(new AnnotationValueOffsetMappingFactory());
Advice.OffsetMapping.Factory<?> offsetMapping = instrumentation.getOffsetMapping();
if (offsetMapping != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
/**
* This class must be provided at most once per {@linkplain #getPluginPackage() plugin package}.
*/
public abstract class PluginClassLoaderRootPackageCustomizer {
public abstract class PluginClassLoaderRootPackageCustomizer implements co.elastic.apm.agent.sdk.PluginClassLoaderRootPackageCustomizer {

/**
* The root package name prefix that all embedded plugins classes must start with
Expand All @@ -48,6 +48,11 @@ public static String getPluginPackageFromClassName(String className) {
return className.substring(0, className.indexOf('.', EMBEDDED_PLUGINS_PACKAGE_PREFIX.length()));
}

@Override
public boolean isIncludePluginPackage() {
return false;
}

public final String getPluginPackage() {
return pluginPackage;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ public <V> WeakSet<V> buildSet() {
return new NullSafeWeakConcurrentSet<V>(this.<V, Boolean>weakMapBuilder().build());
}

@Override
public <K, V extends AbstractSpan<?>> WeakMap<K, V> weakSpanMap() {
return createWeakSpanMap();
}

/**
* Calls {@link AbstractWeakConcurrentMap#expungeStaleEntries()} on all registered maps,
* causing the entries of already collected keys to be removed.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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.apm.agent.configuration;

import org.stagemonitor.configuration.ConfigurationOption;
import org.stagemonitor.configuration.ConfigurationOptionProvider;

/**
* Configuration provider for the apm jax-rs plugin
*/
public class JaxRsConfiguration extends ConfigurationOptionProvider {
private static final String JAXRS_CATEGORY = "JAX-RS";

private final ConfigurationOption<Boolean> enableJaxrsAnnotationInheritance = ConfigurationOption.booleanOption()
.key("enable_jaxrs_annotation_inheritance")
.tags("added[1.5.0]")
.configurationCategory(JAXRS_CATEGORY)
.tags("performance")
.description(
"By default, the agent will scan for @Path annotations on the whole class hierarchy, recognizing a class as a JAX-RS resource if the class or any of its superclasses/interfaces has a class level @Path annotation.\n" +
"If your application does not use @Path annotation inheritance, set this property to 'false' to only scan for direct @Path annotations. This can improve the startup time of the agent.\n")
.dynamic(false)
.buildWithDefault(true);

private final ConfigurationOption<Boolean> useAnnotationValueForTransactionName = ConfigurationOption.booleanOption()
.key("use_jaxrs_path_as_transaction_name")
.tags("added[1.8.0]")
.configurationCategory(JAXRS_CATEGORY)
.description("By default, the agent will use `ClassName#methodName` for the transaction name of JAX-RS requests.\n" +
"If you want to use the URI template from the `@Path` annotation, set the value to `true`.")
.dynamic(false)
.buildWithDefault(false);

/**
* @return if true, the jax-rs plugin must scan for @Path annotations in the class hierarchy of classes.
* if false, only @Path annotations on implementation classes are considered.
*/
public boolean isEnableJaxrsAnnotationInheritance() {
return enableJaxrsAnnotationInheritance.get();
}

public boolean isUseJaxRsPathForTransactionName() {
return useAnnotationValueForTransactionName.get();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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.apm.agent.configuration;

import co.elastic.apm.agent.configuration.converter.WildcardMatcher;
import co.elastic.apm.agent.matcher.WildcardMatcherValueConverter;
import org.stagemonitor.configuration.ConfigurationOption;
import org.stagemonitor.configuration.ConfigurationOptionProvider;
import org.stagemonitor.configuration.converter.ListValueConverter;

import java.util.Arrays;
import java.util.List;

public class MongoConfiguration extends ConfigurationOptionProvider {

private final ConfigurationOption<List<WildcardMatcher>> captureStatementCommands = ConfigurationOption
.builder(new ListValueConverter<>(new WildcardMatcherValueConverter()), List.class)
.key("mongodb_capture_statement_commands")
.configurationCategory("MongoDB")
.description("MongoDB command names for which the command document will be captured, limited to common read-only operations by default.\n" +
"Set to ` \"\"` (empty) to disable capture, and `\"*\"` to capture all (which is discouraged as it may lead to sensitive information capture).\n" +
"\n" +
WildcardMatcher.DOCUMENTATION
)
.dynamic(true)
.buildWithDefault(Arrays.asList(
WildcardMatcher.valueOf("find"),
WildcardMatcher.valueOf("aggregate"),
WildcardMatcher.valueOf("count"),
WildcardMatcher.valueOf("distinct"),
WildcardMatcher.valueOf("mapReduce")
));

public List<WildcardMatcher> getCaptureStatementCommands() {
return captureStatementCommands.get();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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.apm.agent.sdk;

import javax.annotation.Nullable;
import java.util.Collection;
import java.util.Map;

public interface PluginClassLoaderRootPackageCustomizer {

boolean isIncludePluginPackage();

Collection<String> pluginClassLoaderRootPackages();

Map<String, ? extends Collection<String>> requiredModuleOpens();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* 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.apm.agent.sdk;

import java.util.Arrays;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Version implements Comparable<Version> {

private static final Version INVALID = new Version(new int[0], "");

private static final Pattern VERSION_REGEX = Pattern.compile("^" +
"(?<prefix>.*?)" +
"(?<version>(\\d+)(\\.\\d+)*)" +
"(?<suffix>.*?)" +
"$");

private final int[] numbers;
private final String suffix;

public static Version of(String version) {
Matcher matcher = VERSION_REGEX.matcher(version);
if (!matcher.find()) {
return INVALID;
}
final String[] parts = matcher.group("version").split("\\.");
int[] numbers = new int[parts.length];
for (int i = 0; i < parts.length; i++) {
numbers[i] = Integer.parseInt(parts[i]);
}

String suffixTmp = matcher.group("suffix");
if (suffixTmp == null) {
suffixTmp = "";
}
String suffix = suffixTmp;
return new Version(numbers, suffix);
}

private Version(int[] numbers, String suffix) {
this.numbers = numbers;
this.suffix = suffix;
}

public boolean hasSuffix() {
return suffix.length() > 0;
}

public Version withoutSuffix() {
return new Version(numbers, "");
}

@Override
public int compareTo(Version another) {
final int maxLength = Math.max(numbers.length, another.numbers.length);
for (int i = 0; i < maxLength; i++) {
final int left = i < numbers.length ? numbers[i] : 0;
final int right = i < another.numbers.length ? another.numbers[i] : 0;
if (left != right) {
return left < right ? -1 : 1;
}
}
if (suffix.isEmpty() || another.suffix.isEmpty()) {
// no suffix is greater than any suffix (1.0.0 > 1.0.0-SNAPSHOT)
return another.suffix.compareTo(suffix);
} else {
// if both have a suffix, sort in natural order (1.0.0-RC2 > 1.0.0-RC1)
return suffix.compareTo(another.suffix);
}
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < numbers.length; i++) {
sb.append(numbers[i]);
if (i < numbers.length - 1) {
sb.append('.');
}
}
sb.append(suffix);
return sb.toString();
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Version version = (Version) o;
return Arrays.equals(numbers, version.numbers) && suffix.equals(version.suffix);
}

@Override
public int hashCode() {
int result = Objects.hash(suffix);
result = 31 * result + Arrays.hashCode(numbers);
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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.apm.agent.sdk.bindings;

import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.annotation.AnnotationDescription;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.method.ParameterDescription;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.implementation.bytecode.assign.Assigner;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;

public class SimpleMethodSignatureOffsetMappingFactory implements Advice.OffsetMapping.Factory<SimpleMethodSignatureOffsetMappingFactory.SimpleMethodSignature> {

@Override
public Class<SimpleMethodSignature> getAnnotationType() {
return SimpleMethodSignature.class;
}

@Override
public Advice.OffsetMapping make(ParameterDescription.InDefinedShape target,
AnnotationDescription.Loadable<SimpleMethodSignature> annotation,
AdviceType adviceType) {
return new Advice.OffsetMapping() {
@Override
public Target resolve(TypeDescription instrumentedType, MethodDescription instrumentedMethod, Assigner assigner,
Advice.ArgumentHandler argumentHandler, Sort sort) {
final String className = instrumentedMethod.getDeclaringType().getTypeName();
String simpleClassName = className.substring(className.lastIndexOf('$') + 1);
simpleClassName = simpleClassName.substring(simpleClassName.lastIndexOf('.') + 1);
final String signature = String.format("%s#%s", simpleClassName, instrumentedMethod.getName());
return Target.ForStackManipulation.of(signature);
}
};
}

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface SimpleMethodSignature {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* 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.
*/
@NonnullApi
package co.elastic.apm.agent.sdk.bindings;

import co.elastic.apm.agent.sdk.NonnullApi;
Loading

0 comments on commit 90d3348

Please sign in to comment.