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 d8450d8
Show file tree
Hide file tree
Showing 182 changed files with 3,222 additions and 361 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,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;
}
}
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 d8450d8

Please sign in to comment.