Skip to content

Commit d59909c

Browse files
committed
Replace FieldBackedContextAccessor with KeyWithValue from dd-instrument-java
This is step 1 of separating out field-injection from all the other transformations needed for tracing, etc.
1 parent 728666d commit d59909c

7 files changed

Lines changed: 40 additions & 53 deletions

File tree

‎dd-java-agent/agent-bootstrap/src/main/java/datadog/trace/bootstrap/FieldBackedContextAccessor.java‎

Lines changed: 0 additions & 12 deletions
This file was deleted.

‎dd-java-agent/agent-bootstrap/src/main/java/datadog/trace/bootstrap/FieldBackedContextStore.java‎

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package datadog.trace.bootstrap;
22

3+
import datadog.instrument.fieldinject.KeyWithValue;
34
import datadog.instrument.fieldinject.ObjectStoreDispatch;
45
import datadog.trace.api.InstrumenterConfig;
56
import java.util.function.Function;
@@ -20,8 +21,8 @@ public final class FieldBackedContextStore implements ContextStore<Object, Objec
2021

2122
@Override
2223
public Object get(final Object key) {
23-
if (key instanceof FieldBackedContextAccessor) {
24-
return ((FieldBackedContextAccessor) key).$get$__datadogContext$(storeId);
24+
if (key instanceof KeyWithValue) {
25+
return ((KeyWithValue) key).$get$__dd_instrument$(storeId);
2526
} else if (MAP_PER_STORE) {
2627
return weakStore().get(key);
2728
} else {
@@ -31,8 +32,8 @@ public Object get(final Object key) {
3132

3233
@Override
3334
public void put(final Object key, final Object context) {
34-
if (key instanceof FieldBackedContextAccessor) {
35-
((FieldBackedContextAccessor) key).$put$__datadogContext$(storeId, context);
35+
if (key instanceof KeyWithValue) {
36+
((KeyWithValue) key).$put$__dd_instrument$(storeId, context);
3637
} else if (MAP_PER_STORE) {
3738
weakStore().put(key, context);
3839
} else {
@@ -42,15 +43,15 @@ public void put(final Object key, final Object context) {
4243

4344
@Override
4445
public Object getOrPut(final Object key, final Object context) {
45-
if (key instanceof FieldBackedContextAccessor) {
46-
final FieldBackedContextAccessor accessor = (FieldBackedContextAccessor) key;
47-
Object existingContext = accessor.$get$__datadogContext$(storeId);
46+
if (key instanceof KeyWithValue) {
47+
final KeyWithValue accessor = (KeyWithValue) key;
48+
Object existingContext = accessor.$get$__dd_instrument$(storeId);
4849
if (null == existingContext) {
4950
synchronized (accessor) {
50-
existingContext = accessor.$get$__datadogContext$(storeId);
51+
existingContext = accessor.$get$__dd_instrument$(storeId);
5152
if (null == existingContext) {
5253
existingContext = context;
53-
accessor.$put$__datadogContext$(storeId, existingContext);
54+
accessor.$put$__dd_instrument$(storeId, existingContext);
5455
}
5556
}
5657
}
@@ -64,15 +65,15 @@ public Object getOrPut(final Object key, final Object context) {
6465

6566
@Override
6667
public Object getOrCompute(Object key, Function<? super Object, Object> contextFactory) {
67-
if (key instanceof FieldBackedContextAccessor) {
68-
final FieldBackedContextAccessor accessor = (FieldBackedContextAccessor) key;
69-
Object existingContext = accessor.$get$__datadogContext$(storeId);
68+
if (key instanceof KeyWithValue) {
69+
final KeyWithValue accessor = (KeyWithValue) key;
70+
Object existingContext = accessor.$get$__dd_instrument$(storeId);
7071
if (null == existingContext) {
7172
synchronized (accessor) {
72-
existingContext = accessor.$get$__datadogContext$(storeId);
73+
existingContext = accessor.$get$__dd_instrument$(storeId);
7374
if (null == existingContext) {
7475
existingContext = contextFactory.apply(key);
75-
accessor.$put$__datadogContext$(storeId, existingContext);
76+
accessor.$put$__dd_instrument$(storeId, existingContext);
7677
}
7778
}
7879
}
@@ -86,14 +87,14 @@ public Object getOrCompute(Object key, Function<? super Object, Object> contextF
8687

8788
@Override
8889
public Object remove(Object key) {
89-
if (key instanceof FieldBackedContextAccessor) {
90-
final FieldBackedContextAccessor accessor = (FieldBackedContextAccessor) key;
91-
Object existingContext = accessor.$get$__datadogContext$(storeId);
90+
if (key instanceof KeyWithValue) {
91+
final KeyWithValue accessor = (KeyWithValue) key;
92+
Object existingContext = accessor.$get$__dd_instrument$(storeId);
9293
if (null != existingContext) {
9394
synchronized (accessor) {
94-
existingContext = accessor.$get$__datadogContext$(storeId);
95+
existingContext = accessor.$get$__dd_instrument$(storeId);
9596
if (null != existingContext) {
96-
accessor.$put$__datadogContext$(storeId, null);
97+
accessor.$put$__dd_instrument$(storeId, null);
9798
}
9899
}
99100
}

‎dd-java-agent/agent-installer/src/main/java/datadog/trace/agent/tooling/AgentInstaller.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import static net.bytebuddy.matcher.ElementMatchers.isDefaultFinalizer;
77

88
import datadog.environment.SystemProperties;
9+
import datadog.instrument.fieldinject.KeyWithValue;
910
import datadog.trace.agent.tooling.bytebuddy.SharedTypePools;
1011
import datadog.trace.agent.tooling.bytebuddy.matcher.DDElementMatchers;
1112
import datadog.trace.agent.tooling.bytebuddy.memoize.MemoizedMatchers;
@@ -17,7 +18,6 @@
1718
import datadog.trace.api.ProductActivation;
1819
import datadog.trace.api.internal.VisibleForTesting;
1920
import datadog.trace.api.telemetry.IntegrationsCollector;
20-
import datadog.trace.bootstrap.FieldBackedContextAccessor;
2121
import datadog.trace.bootstrap.instrumentation.java.concurrent.ExcludeFilter;
2222
import datadog.trace.bootstrap.instrumentation.java.module.JpmsHelper;
2323
import datadog.trace.util.AgentTaskScheduler;
@@ -158,7 +158,7 @@ public static ClassFileTransformer installBytebuddyAgent(
158158
agentBuilder =
159159
agentBuilder
160160
.disableClassFormatChanges()
161-
.assureReadEdgeTo(inst, FieldBackedContextAccessor.class)
161+
.assureReadEdgeTo(inst, KeyWithValue.class)
162162
.with(AgentStrategies.transformerDecorator())
163163
.with(AgentBuilder.RedefinitionStrategy.RETRANSFORMATION)
164164
.with(AgentStrategies.rediscoveryStrategy())

‎dd-java-agent/agent-tooling/src/main/java/datadog/trace/agent/tooling/context/FieldBackedContextInjector.java‎

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
import static datadog.trace.bootstrap.FieldBackedContextStores.getContextStoreId;
44
import static datadog.trace.util.Strings.getInternalName;
55

6+
import datadog.instrument.fieldinject.KeyWithValue;
67
import datadog.instrument.fieldinject.ObjectStoreDispatch;
78
import datadog.trace.agent.tooling.bytebuddy.memoize.MemoizedMatchers;
89
import datadog.trace.api.InstrumenterConfig;
910
import datadog.trace.api.Pair;
1011
import datadog.trace.bootstrap.ContextStore;
11-
import datadog.trace.bootstrap.FieldBackedContextAccessor;
1212
import datadog.trace.bootstrap.WeakMapPerStore;
1313
import java.io.Serializable;
1414
import java.util.Arrays;
@@ -36,16 +36,15 @@ public final class FieldBackedContextInjector implements AsmVisitorWrapper {
3636

3737
private static final Logger log = LoggerFactory.getLogger(FieldBackedContextInjector.class);
3838

39-
static final String FIELD_BACKED_CONTEXT_ACCESSOR_CLASS =
40-
getInternalName(FieldBackedContextAccessor.class.getName());
39+
static final String KEY_WITH_VALUE_CLASS = getInternalName(KeyWithValue.class.getName());
4140

42-
static final String CONTEXT_STORE_ACCESS_PREFIX = "__datadogContext$";
41+
static final String CONTEXT_STORE_FIELD_PREFIX = "__datadogContext$";
4342

44-
static final String GETTER_METHOD = "$get$" + CONTEXT_STORE_ACCESS_PREFIX;
43+
static final String GETTER_METHOD = "$get$__dd_instrument$";
4544
static final String GETTER_METHOD_DESCRIPTOR =
4645
Type.getMethodDescriptor(Type.getType(Object.class), Type.INT_TYPE);
4746

48-
static final String PUTTER_METHOD = "$put$" + CONTEXT_STORE_ACCESS_PREFIX;
47+
static final String PUTTER_METHOD = "$put$__dd_instrument$";
4948
static final String PUTTER_METHOD_DESCRIPTOR =
5049
Type.getMethodDescriptor(Type.VOID_TYPE, Type.INT_TYPE, Type.getType(Object.class));
5150

@@ -70,8 +69,7 @@ public final class FieldBackedContextInjector implements AsmVisitorWrapper {
7069

7170
static final String OBJECT_DESCRIPTOR = Type.getDescriptor(Object.class);
7271

73-
public static final Type EXPECTED_SUPER_STORE_TYPE =
74-
Type.getType(FieldBackedContextAccessor.class);
72+
public static final Type EXPECTED_SUPER_STORE_TYPE = Type.getType(KeyWithValue.class);
7573

7674
/** Keeps track of injection requests for the class being transformed by the current thread. */
7775
static final ThreadLocal<Pair<String, BitSet>> INJECTED_STORE_IDS = new ThreadLocal<>();
@@ -134,25 +132,25 @@ public void visit(
134132
// keep track of all injection requests for the class currently being transformed
135133
// because we need to switch between them in the generated getter/putter methods
136134
int storeId = injectContextStore(name, keyClassName, contextClassName);
137-
storeFieldName = CONTEXT_STORE_ACCESS_PREFIX + storeId;
135+
storeFieldName = CONTEXT_STORE_FIELD_PREFIX + storeId;
138136

139137
if (interfaces == null) {
140138
interfaces = new String[] {};
141139
}
142140

143-
if (!Arrays.asList(interfaces).contains(FIELD_BACKED_CONTEXT_ACCESSOR_CLASS)) {
141+
if (!Arrays.asList(interfaces).contains(KEY_WITH_VALUE_CLASS)) {
144142
if (serialVersionUIDFieldInjection
145143
&& instrumentedType.isAssignableTo(Serializable.class)) {
146144
serialVersionUIDInjector = new SerialVersionUIDInjector();
147145
serialVersionUIDInjector.visit(version, access, name, signature, superName, interfaces);
148146
}
149147

150148
if (signature != null) {
151-
signature += 'L' + FIELD_BACKED_CONTEXT_ACCESSOR_CLASS + ';';
149+
signature += 'L' + KEY_WITH_VALUE_CLASS + ';';
152150
}
153151

154152
interfaces = Arrays.copyOf(interfaces, interfaces.length + 1);
155-
interfaces[interfaces.length - 1] = FIELD_BACKED_CONTEXT_ACCESSOR_CLASS;
153+
interfaces[interfaces.length - 1] = KEY_WITH_VALUE_CLASS;
156154
}
157155

158156
super.visit(version, access, name, signature, superName, interfaces);
@@ -165,7 +163,7 @@ public FieldVisitor visitField(
165163
final String descriptor,
166164
final String signature,
167165
final Object value) {
168-
if (name.startsWith(CONTEXT_STORE_ACCESS_PREFIX)) {
166+
if (name.startsWith(CONTEXT_STORE_FIELD_PREFIX)) {
169167
if (storeFieldName.equals(name)) {
170168
foundField = true;
171169
}
@@ -426,7 +424,7 @@ private void getStoreField(
426424
mv.visitFieldInsn(
427425
Opcodes.GETFIELD,
428426
instrumentedName,
429-
CONTEXT_STORE_ACCESS_PREFIX + injectedStoreId,
427+
CONTEXT_STORE_FIELD_PREFIX + injectedStoreId,
430428
OBJECT_DESCRIPTOR);
431429
mv.visitInsn(Opcodes.ARETURN);
432430
}
@@ -438,7 +436,7 @@ private void putStoreField(
438436
mv.visitFieldInsn(
439437
Opcodes.PUTFIELD,
440438
instrumentedName,
441-
CONTEXT_STORE_ACCESS_PREFIX + injectedStoreId,
439+
CONTEXT_STORE_FIELD_PREFIX + injectedStoreId,
442440
OBJECT_DESCRIPTOR);
443441
mv.visitInsn(Opcodes.RETURN);
444442
}

‎dd-java-agent/agent-tooling/src/main/java/datadog/trace/agent/tooling/context/FieldBackedContextMatcher.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import static datadog.trace.agent.tooling.bytebuddy.matcher.HierarchyMatchers.declaresContextField;
44

5-
import datadog.trace.bootstrap.FieldBackedContextAccessor;
5+
import datadog.instrument.fieldinject.KeyWithValue;
66
import net.bytebuddy.description.type.TypeDescription;
77
import net.bytebuddy.matcher.ElementMatcher;
88
import org.slf4j.Logger;
@@ -66,7 +66,7 @@ public String describe() {
6666
*/
6767
private static boolean implementsContextAccessor(Class<?> classBeingRedefined) {
6868
for (Class<?> intf : classBeingRedefined.getInterfaces()) {
69-
if (FieldBackedContextAccessor.class.equals(intf)) {
69+
if (KeyWithValue.class.equals(intf)) {
7070
return true;
7171
}
7272
}

‎dd-java-agent/instrumentation-testing/src/test/groovy/context/FieldInjectionForkedTest.groovy‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class FieldInjectionForkedTest extends InstrumentationSpecification {
6666

6767
boolean hasAccessorInterface = false
6868
for (Class inter : keyClass.getInterfaces()) {
69-
if (inter.getName() == 'datadog.trace.bootstrap.FieldBackedContextAccessor') {
69+
if (inter.getName() == 'datadog.instrument.fieldinject.KeyWithValue') {
7070
hasAccessorInterface = true
7171
}
7272
}
@@ -246,7 +246,7 @@ class FieldInjectionDisabledForkedTest extends InstrumentationSpecification {
246246

247247
boolean hasAccessorInterface = false
248248
for (Class inter : keyClass.getInterfaces()) {
249-
if (inter.getName() == 'datadog.trace.bootstrap.FieldBackedContextAccessor') {
249+
if (inter.getName() == 'datadog.instrument.fieldinject.KeyWithValue') {
250250
hasAccessorInterface = true
251251
}
252252
}

‎dd-java-agent/instrumentation-testing/src/test/groovy/excludefilter/ExcludeFilterForkedTest.groovy‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class ExcludeFilterForkedTest extends InstrumentationSpecification {
7676

7777
boolean hasAccessorInterface = false
7878
for (Class inter : clazz.getInterfaces()) {
79-
if (inter.name == 'datadog.trace.bootstrap.FieldBackedContextAccessor') {
79+
if (inter.name == 'datadog.instrument.fieldinject.KeyWithValue') {
8080
hasAccessorInterface = true
8181
}
8282
}

0 commit comments

Comments
 (0)