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
Migrate trace_methods to indy plugin #2094
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6d6ca26
Migrate trace_methods to indy plugin
felixbarny b71886a
Update PR number in changelog
felixbarny 1000ebf
Move MethodMatcher out of tracemethods package
felixbarny 954b4f6
Merge remote-tracking branch 'origin/master' into trace-methods-indy
felixbarny File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,15 +16,16 @@ | |
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package co.elastic.apm.agent.bci.methodmatching; | ||
package co.elastic.apm.agent.tracemethods; | ||
|
||
import co.elastic.apm.agent.bci.TracerAwareInstrumentation; | ||
import co.elastic.apm.agent.bci.bytebuddy.SimpleMethodSignatureOffsetMappingFactory; | ||
import co.elastic.apm.agent.configuration.CoreConfiguration; | ||
import co.elastic.apm.agent.impl.ElasticApmTracer; | ||
import co.elastic.apm.agent.impl.GlobalTracer; | ||
import co.elastic.apm.agent.impl.transaction.AbstractSpan; | ||
import co.elastic.apm.agent.impl.transaction.Span; | ||
import co.elastic.apm.agent.matcher.WildcardMatcher; | ||
import co.elastic.apm.agent.sdk.ElasticApmInstrumentation; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.description.method.MethodDescription; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
|
@@ -45,55 +46,18 @@ | |
import static net.bytebuddy.matcher.ElementMatchers.isNative; | ||
import static net.bytebuddy.matcher.ElementMatchers.isSynthetic; | ||
import static net.bytebuddy.matcher.ElementMatchers.isTypeInitializer; | ||
import static net.bytebuddy.matcher.ElementMatchers.nameContains; | ||
import static net.bytebuddy.matcher.ElementMatchers.not; | ||
import static net.bytebuddy.matcher.ElementMatchers.takesArgument; | ||
import static net.bytebuddy.matcher.ElementMatchers.takesArguments; | ||
|
||
public class TraceMethodInstrumentation extends TracerAwareInstrumentation { | ||
public class TraceMethodInstrumentation extends ElasticApmInstrumentation { | ||
|
||
public static long traceMethodThresholdMicros; | ||
|
||
protected final MethodMatcher methodMatcher; | ||
private final MethodMatcher methodMatcher; | ||
private final CoreConfiguration config; | ||
|
||
public TraceMethodInstrumentation(ElasticApmTracer tracer, MethodMatcher methodMatcher) { | ||
this.methodMatcher = methodMatcher; | ||
config = tracer.getConfig(CoreConfiguration.class); | ||
traceMethodThresholdMicros = config.getTraceMethodsDurationThreshold().getMillis() * 1000; | ||
} | ||
|
||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static void onMethodEnter(@Advice.Origin Class<?> clazz, | ||
@SimpleMethodSignatureOffsetMappingFactory.SimpleMethodSignature String signature, | ||
@Advice.Local("span") AbstractSpan<?> span) { | ||
final AbstractSpan<?> parent = tracer.getActive(); | ||
if (parent == null) { | ||
span = tracer.startRootTransaction(clazz.getClassLoader()); | ||
if (span != null) { | ||
span.withName(signature).activate(); | ||
} | ||
} else if (parent.isSampled()) { | ||
span = parent.createSpan() | ||
.withName(signature) | ||
.activate(); | ||
} | ||
} | ||
|
||
@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class) | ||
public static void onMethodExit(@Advice.Local("span") @Nullable AbstractSpan<?> span, | ||
@Advice.Thrown @Nullable Throwable t) { | ||
if (span != null) { | ||
span.captureException(t); | ||
final long endTime = span.getTraceContext().getClock().getEpochMicros(); | ||
if (span instanceof Span) { | ||
long durationMicros = endTime - span.getTimestamp(); | ||
if (traceMethodThresholdMicros > 0 && durationMicros < traceMethodThresholdMicros && t == null) { | ||
span.requestDiscarding(); | ||
} | ||
} | ||
span.deactivate().end(endTime); | ||
} | ||
} | ||
|
||
@Override | ||
|
@@ -153,7 +117,55 @@ public Collection<String> getInstrumentationGroupNames() { | |
} | ||
|
||
@Override | ||
public boolean indyPlugin() { | ||
return false; | ||
public String getAdviceClassName() { | ||
return getClass().getName() + "$TraceMethodAdvice"; | ||
} | ||
|
||
public static class TraceMethodAdvice { | ||
|
||
private static final ElasticApmTracer tracer = GlobalTracer.requireTracerImpl(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [minor] moving tracer init to the start of the |
||
private static final long traceMethodThresholdMicros; | ||
|
||
static { | ||
CoreConfiguration config = tracer.getConfig(CoreConfiguration.class); | ||
traceMethodThresholdMicros = config.getTraceMethodsDurationThreshold().getMillis() * 1000; | ||
} | ||
|
||
@Nullable | ||
@Advice.OnMethodEnter(suppress = Throwable.class, inline = false) | ||
public static Object onMethodEnter(@Advice.Origin Class<?> clazz, | ||
@SimpleMethodSignatureOffsetMappingFactory.SimpleMethodSignature String signature) { | ||
AbstractSpan<?> span = null; | ||
final AbstractSpan<?> parent = tracer.getActive(); | ||
if (parent == null) { | ||
span = tracer.startRootTransaction(clazz.getClassLoader()); | ||
if (span != null) { | ||
span.withName(signature).activate(); | ||
} | ||
} else if (parent.isSampled()) { | ||
span = parent.createSpan() | ||
.withName(signature) | ||
.activate(); | ||
} | ||
return span; | ||
} | ||
|
||
@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class, inline = false) | ||
public static void onMethodExit(@Advice.Enter @Nullable Object spanObj, | ||
@Advice.Thrown @Nullable Throwable t) { | ||
AbstractSpan<?> span = (AbstractSpan<?>) spanObj; | ||
if (span != null) { | ||
span.captureException(t); | ||
final long endTime = span.getTraceContext().getClock().getEpochMicros(); | ||
if (span instanceof Span) { | ||
long durationMicros = endTime - span.getTimestamp(); | ||
if (traceMethodThresholdMicros > 0 && durationMicros < traceMethodThresholdMicros && t == null) { | ||
span.requestDiscarding(); | ||
} | ||
} | ||
span.deactivate().end(endTime); | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[question] If I understand it correctly, we only need global state here as it relates to configuration like
MethodMatcherValueConverter
, and thus needs to be loaded in the main agent classloader and not in the plugin classloader, is that correct ? If so, maybe a small comment could make that slightly more obvious.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct. Agree on adding an explanatory comment.