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 3abd4d3
Show file tree
Hide file tree
Showing 175 changed files with 3,187 additions and 357 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,13 @@ private static synchronized void initInstrumentation(final ElasticApmTracer trac
}
}
}
final List<PluginClassLoaderRootPackageCustomizer> rootPackageCustomizers = DependencyInjectingServiceLoader.load(
PluginClassLoaderRootPackageCustomizer.class,
getAgentClassLoader());
final List<PluginClassLoaderRootPackageCustomizer> rootPackageCustomizers = new ArrayList<PluginClassLoaderRootPackageCustomizer>(
DependencyInjectingServiceLoader.load(PluginClassLoaderRootPackageCustomizer.class, getAgentClassLoader()));
for (co.elastic.apm.agent.sdk.PluginClassLoaderRootPackageCustomizer customizer : DependencyInjectingServiceLoader.load(
co.elastic.apm.agent.sdk.PluginClassLoaderRootPackageCustomizer.class,
getAgentClassLoader())) {
rootPackageCustomizers.add(PluginClassLoaderRootPackageCustomizer.of(customizer));
}
for (PluginClassLoaderRootPackageCustomizer rootPackageCustomizer : rootPackageCustomizers) {
PluginClassLoaderCustomizations customizations = new PluginClassLoaderCustomizations(
rootPackageCustomizer.pluginClassLoaderRootPackages(),
Expand Down Expand Up @@ -468,6 +472,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 @@ -19,10 +19,7 @@
package co.elastic.apm.agent.bci;

import java.lang.instrument.Instrumentation;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.*;

/**
* This class must be provided at most once per {@linkplain #getPluginPackage() plugin package}.
Expand All @@ -41,13 +38,37 @@ public PluginClassLoaderRootPackageCustomizer() {
pluginPackage = getPluginPackageFromClassName(className);
}

private PluginClassLoaderRootPackageCustomizer(Class<?> type) {
String className = type.getName();
pluginPackage = getPluginPackageFromClassName(className);
}

public static String getPluginPackageFromClassName(String className) {
if (!className.startsWith(EMBEDDED_PLUGINS_PACKAGE_PREFIX)) {
throw new IllegalArgumentException("invalid instrumentation class location : " + className);
}
return className.substring(0, className.indexOf('.', EMBEDDED_PLUGINS_PACKAGE_PREFIX.length()));
}

public static PluginClassLoaderRootPackageCustomizer of(final co.elastic.apm.agent.sdk.PluginClassLoaderRootPackageCustomizer customizer) {
return new PluginClassLoaderRootPackageCustomizer(customizer.getClass()) {

@Override
public Map<String, ? extends Collection<String>> requiredModuleOpens() {
return customizer.requiredModuleOpens();
}

@Override
public Collection<String> pluginClassLoaderRootPackages() {
List<String> packages = new ArrayList<String>(customizer.pluginClassLoaderRootPackages());
if (customizer.isIncludePluginPackage()) {
packages.add(getPluginPackage());
}
return packages;
}
};
}

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,31 @@
/*
* 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.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;
}
}
Loading

0 comments on commit 3abd4d3

Please sign in to comment.