-
Notifications
You must be signed in to change notification settings - Fork 362
Support instrumentations that make structural changes and tolerate already-loaded target classes #12610
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
base: master
Are you sure you want to change the base?
Support instrumentations that make structural changes and tolerate already-loaded target classes #12610
Changes from all commits
15a04a1
735fc2e
f77bda7
8f75a21
2251a6c
a0dddd6
174ed6b
57e838f
e5b225f
6e51a81
058842a
cf801e9
2b6ccd5
e0fe52c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -152,6 +152,16 @@ private void prepareInstrumentation(InstrumenterModule module, int instrumentati | |
| muzzle = new MuzzleCheck(module, instrumentationId); | ||
| } | ||
|
|
||
| /** Allocates a fresh transformation id not known at build-time, growing storage as needed. */ | ||
| private int allocateRuntimeTransformationId() { | ||
| int transformationId = nextRuntimeTransformationId++; | ||
| if (transformers.length <= transformationId) { | ||
| int newLen = Math.max(transformationId + 1, transformers.length + (transformers.length >> 1)); | ||
| transformers = Arrays.copyOf(transformers, newLen); | ||
| } | ||
| return transformationId; | ||
| } | ||
|
|
||
| /** Builds a type-specific transformer, controlled by one or more matchers. */ | ||
| private void buildTypeInstrumentation(Instrumenter member) { | ||
|
|
||
|
|
@@ -162,10 +172,7 @@ private void buildTypeInstrumentation(Instrumenter member) { | |
| if (transformationId < 0) { | ||
| // this is a non-indexed transformation configured at runtime, e.g. "dd.trace.methods" | ||
| // allocate a distinct runtime id to each extra transformation for matching purposes | ||
| transformationId = nextRuntimeTransformationId++; | ||
| if (transformers.length <= transformationId) { | ||
| transformers = Arrays.copyOf(transformers, transformationId + 1); | ||
| } | ||
| transformationId = allocateRuntimeTransformationId(); | ||
| } | ||
|
|
||
| buildTypeMatcher(member, transformationId); | ||
|
|
@@ -222,30 +229,63 @@ private void buildTypeMatcher(Instrumenter member, int transformationId) { | |
| } | ||
|
|
||
| matchers.add(new MatchRecorder.NarrowLocation(transformationId, muzzle)); | ||
|
|
||
| // preserve structural change, unless we're going to split it out in buildTypeAdvice | ||
| if (member instanceof Instrumenter.WithStructuralChange | ||
| && !(member instanceof Instrumenter.HasMethodAdvice)) { | ||
| addStructuralNarrowing((Instrumenter.WithStructuralChange) member, transformationId); | ||
| } | ||
| } | ||
|
|
||
| private void buildTypeAdvice(Instrumenter member, int transformationId) { | ||
|
|
||
| if (null != helperTransformer) { | ||
| advice.add(helperTransformer); | ||
| } | ||
|
|
||
| if (null != contextRequestRewriter) { | ||
| registerContextStoreInjection(member, contextStore); | ||
| // rewrite context store access to call FieldBackedContextStores with assigned store-id | ||
| advice.add(contextRequestRewriter); | ||
| } | ||
|
|
||
| if (member instanceof Instrumenter.HasTypeAdvice) { | ||
| // a structural change may contain method advice that works with and without the change | ||
| // we split out the structural change to its own transformation so it can't accidentally | ||
| // turn off the method advice when it's skipped | ||
| boolean splitOutStructuralChange = | ||
| member instanceof Instrumenter.WithStructuralChange | ||
| && member instanceof Instrumenter.HasMethodAdvice; | ||
|
|
||
| if (member instanceof Instrumenter.HasTypeAdvice && !splitOutStructuralChange) { | ||
| ((Instrumenter.HasTypeAdvice) member).typeAdvice(this); | ||
| } | ||
| if (member instanceof Instrumenter.HasMethodAdvice) { | ||
| ((Instrumenter.HasMethodAdvice) member).methodAdvice(this); | ||
| } | ||
| finishAdviceStack(transformationId); | ||
|
|
||
| if (splitOutStructuralChange) { | ||
| Instrumenter.WithStructuralChange structuralChange = | ||
| (Instrumenter.WithStructuralChange) member; | ||
|
|
||
| // reuse the type match already computed for transformationId instead of rebuilding it | ||
| int structuralTransformationId = allocateRuntimeTransformationId(); | ||
| matchers.add(new MatchRecorder.CopyMatch(transformationId, structuralTransformationId)); | ||
| addStructuralNarrowing(structuralChange, structuralTransformationId); | ||
| structuralChange.typeAdvice(this); | ||
|
Contributor
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. When an instrumentation implements both None of the three instrumentations migrated in this PR trip this (their
Contributor
Author
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. Type advice cannot rely on helpers or context rewriting because it acts at a different level - those concepts are specific to method advice. I'll add a small note to make this clear.
Contributor
Author
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. documented in e0fe52c
Contributor
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. That was Claude's analysis. I wasn't sure myself, so I passed it along. |
||
| finishAdviceStack(structuralTransformationId); | ||
| } | ||
| } | ||
|
|
||
| // record the advice collected for this transformationId | ||
| transformers[transformationId] = new AdviceStack(advice); | ||
| /** Narrows a transformation away from already-loaded types missing the structural marker. */ | ||
| private void addStructuralNarrowing( | ||
| Instrumenter.WithStructuralChange member, int transformationId) { | ||
| matchers.add( | ||
| new MatchRecorder.PreserveLoadedStructure( | ||
| transformationId, member.structuralChangeMarker())); | ||
| } | ||
|
|
||
| /** Records the advice collected so far as the stack for this transformationId. */ | ||
| private void finishAdviceStack(int transformationId) { | ||
| transformers[transformationId] = new AdviceStack(advice); | ||
| advice.clear(); // reset for next transformationId | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.