Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance class name detection #3746

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ Use subheadings with the "=====" level for adding notes for unreleased changes:

=== Unreleased

===== Bug fixes
* Improve automatic span class name detection for Scala and nested/anonymous classes - {pull}3746[#3746]

[[release-notes-1.x]]
=== Java Agent version 1.x

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* 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.bytebuddy;

/**
* Utility class to extract the correct simple class name
*/
public class ClassNameParser {
/**
* Utility class, do not instantiate
*/
private ClassNameParser() {}
private static String getNestedClassName(String className) {
int dollarIndex = className.lastIndexOf('$');
String innerClassName = className.substring(dollarIndex + 1);
if(innerClassName.matches("\\d+")) {
// this is an anonymous inner class
// we don't want to include the number in the class name
className = className.substring(0, dollarIndex);
} else {
className = innerClassName;
}
return className;
}
/**
* Parses the simple class name from a class name
* @param className
* @return
*/
public static String parse(String className) {
//remove package name
className = className.substring(className.lastIndexOf('.') + 1);
if(className.contains("$")) {
if(className.endsWith("$")) {
// this can happen if the source code was in Scala and the object keyword was used
// https://www.toptal.com/scala/scala-bytecode-and-the-jvm
className = className.substring(0, className.length() - 1);
wolframhaussig marked this conversation as resolved.
Show resolved Hide resolved
if(className.contains("$"))
{
className = getNestedClassName(className);
}
} else {
className = getNestedClassName(className);
}
}
return className;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ public Advice.OffsetMapping make(ParameterDescription.InDefinedShape target,
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 simpleClassName = ClassNameParser.parse(className);
final String signature = String.format("%s#%s", simpleClassName, instrumentedMethod.getName());
return Target.ForStackManipulation.of(signature);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* 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.bytebuddy;

import static org.assertj.core.api.Assertions.*;

import org.junit.jupiter.api.Test;

import co.elastic.apm.agent.sdk.bytebuddy.clazzes.ParentClass.InnerClass;
import co.elastic.apm.agent.sdk.bytebuddy.clazzes.ParentObject.ChildScalaObject$;
import co.elastic.apm.agent.sdk.bytebuddy.clazzes.SimpleClass;
import co.elastic.apm.agent.sdk.bytebuddy.clazzes.AnonymousClass;
import co.elastic.apm.agent.sdk.bytebuddy.clazzes.Dollar$Class;
import co.elastic.apm.agent.sdk.bytebuddy.clazzes.ScalaObject$;

class ClassNameParserTest {

@Test
void testSimpleClassName() {
String className = ClassNameParser.parse(SimpleClass.class.getName());
assertThat(className).isEqualTo("SimpleClass");
}
@Test
void testNestedClassName() {
String className = ClassNameParser.parse(InnerClass.class.getName());
assertThat(className).isEqualTo("InnerClass");
}
@Test
void testDollarClassName() {
String className = ClassNameParser.parse(Dollar$Class.class.getName());
//We have no way to know if the class name is Dollar$Class or Class is a nested class of Dollar
//As dollar signs should not be used in names, it should be fine to detect Class instead of Dollar$Class
assertThat(className).describedAs("Dollar signs should not be used in names").isEqualTo("Class");
}
@Test
void testAnonymousClassName() {
String className = ClassNameParser.parse(AnonymousClass.getAnonymousClass().getName());
assertThat(className).isEqualTo("AnonymousClass");
}
@Test
void testScalaObjectClassName() {
String className = ClassNameParser.parse(ScalaObject$.class.getName());
assertThat(className).isEqualTo("ScalaObject");
}
@Test
void testNestedScalaObjectClassName() {
String className = ClassNameParser.parse(ChildScalaObject$.class.getName());
assertThat(className).isEqualTo("ChildScalaObject");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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.bytebuddy.clazzes;

public class AnonymousClass {
public static Class<?> getAnonymousClass() {
return new Object() {
@Override
public String toString() {
return "foo";
}
}.getClass();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* 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.bytebuddy.clazzes;

public class Dollar$Class {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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.bytebuddy.clazzes;

public class ParentClass {

public static class InnerClass {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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.bytebuddy.clazzes;

public class ParentObject {
/**
* Scala objects generate a class with a dollar at the end
* https://www.toptal.com/scala/scala-bytecode-and-the-jvm
*/
public class ChildScalaObject$ {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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.bytebuddy.clazzes;

/**
* Scala objects generate a class with a dollar at the end
* https://www.toptal.com/scala/scala-bytecode-and-the-jvm
*/
public class ScalaObject$ {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* 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.bytebuddy.clazzes;

public class SimpleClass {

}
Loading