Skip to content

Commit 98df97a

Browse files
committed
First iteration on a better context store api
Separate context storage from actual fetching/putting
1 parent 3c80c25 commit 98df97a

8 files changed

Lines changed: 401 additions & 182 deletions

File tree

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package datadog.trace.bootstrap;
2+
3+
/**
4+
* Interface to represent context storage for instrumentations.
5+
*
6+
* <p>Context instances are weakly referenced and will be garbage collected when their corresponding
7+
* key instance is collected.
8+
*
9+
* @param <K> key type to do context lookups
10+
* @param <C> context type
11+
*/
12+
public interface ContextStore<K, C> {
13+
14+
/**
15+
* Factory interface to create context instances
16+
*
17+
* @param <C> context type
18+
*/
19+
interface Factory<C> {
20+
21+
/** @return new context instance */
22+
C create();
23+
}
24+
25+
/**
26+
* Get context given the key
27+
*
28+
* @param key the key to looup
29+
* @return context object
30+
*/
31+
C get(K key);
32+
33+
/**
34+
* Put new context instance for given key
35+
*
36+
* @param key key to use
37+
* @param context context instance to save
38+
*/
39+
void put(K key, C context);
40+
41+
/**
42+
* Put new context instance if key is absent
43+
*
44+
* @param key key to use
45+
* @param context new context instance to put
46+
* @return old instance if it was present, or new instance
47+
*/
48+
C putIfAbsent(K key, C context);
49+
50+
/**
51+
* Put new context instance if key is absent. Uses context factory to avoid creating objects if
52+
* not needed.
53+
*
54+
* @param key key to use
55+
* @param contextFactory factory instance to produce new context object
56+
* @return old instance if it was present, or new instance
57+
*/
58+
C putIfAbsent(K key, Factory<C> contextFactory);
59+
}

dd-java-agent/agent-bootstrap/src/main/java/datadog/trace/bootstrap/InstrumentationContext.java

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,22 @@ public class InstrumentationContext {
55
private InstrumentationContext() {}
66

77
/**
8-
* Fetch a context instance out of the context store.
8+
* Find a {@link ContextStore} instance for given key class and context class.
99
*
10-
* <p>
11-
*
12-
* <p>Conceptually, this can be thought of as a two pass map look up.
13-
*
14-
* <p>For example: <em>RunnableState runnableState = get(runnableImpl, Runnable.class,
15-
* RunnableState.class)</em> --> <em>RunnableState runnableState = (RunnableState)
16-
* GlobalContextMap.get(Runnable.class).get(runnableImpl)</em>
17-
*
18-
* <p>
10+
* <p>Conceptually this can be thought of as a map lookup to fetch a second level map given
11+
* keyClass.
1912
*
2013
* <p>However, the implementation is actually provided by bytecode transformation for performance
2114
* reasons.
2215
*
23-
* <p>
24-
*
25-
* <p>Context classes are weakly referenced and will be garbage collected when their corresponding
26-
* user instance is collected.
27-
*
28-
* <p>
29-
*
30-
* <p>Instrumenters making this call must define the user-context class relationship in
31-
* datadog.trace.agent.tooling.Instrumenter.Default#contextStore.
32-
*
33-
* @param userInstance The instance to store context on.
34-
* @param userClass The user class context is attached to.
16+
* @param keyClass The key class context is attached to.
3517
* @param contextClass The context class attached to the user class.
36-
* @param <K> user class
37-
* @param <V> context class
38-
* @return The context instance attached to userInstance.
18+
* @param <K> key class
19+
* @param <C> context class
20+
* @return The instance of context store for given arguments.
3921
*/
40-
public static <K, V> V get(K userInstance, Class<K> userClass, Class<V> contextClass) {
41-
throw new RuntimeException("calls to this method will be rewritten");
22+
public static <K, C> ContextStore<K, C> get(
23+
final Class<K> keyClass, final Class<C> contextClass) {
24+
throw new RuntimeException("Calls to this method will be rewritten by MapBackedProvider");
4225
}
4326
}

0 commit comments

Comments
 (0)