public class Context extends Object
Context offers a means of passing arbitrary data (key-value pairs) to pipeline policies.
Most applications do not need to pass arbitrary data to the pipeline and can pass Context.NONE or
null.
Each context object is immutable. The Context.addData(Object, Object) method creates a new
Context object that refers to its parent, forming a linked list.
| Modifier and Type | Field and Description |
|---|---|
static Context |
NONE
Signifies that no data needs to be passed to the pipeline.
|
| Constructor and Description |
|---|
Context(Object key,
Object value)
Constructs a new
Context object. |
| Modifier and Type | Method and Description |
|---|---|
Context |
addData(Object key,
Object value)
|
Optional<Object> |
getData(Object key)
Scans the linked-list of
Context objects looking for one with the specified key. |
static Context |
of(Map<Object,Object> keyValues)
|
public static final Context NONE
public Context(Object key, Object value)
Context object.
Code samples
// Create an empty context having no dataContextemptyContext =Context.NONE; // Tracing spans created by users can be passed to calling methods in sdk clients using Context object finalStringuserParentSpan = "user-parent-span"; // Create a context using the provided key and user parent spanContextkeyValueContext = newContext(PARENT_SPAN_KEY, userParentSpan);
key - The key with which the specified value should be associated.value - The value to be associated with the specified key.IllegalArgumentException - If key is null.public Context addData(Object key, Object value)
Context object with the specified key-value pair to
the existing Context chain.
Code samples
// Users can send parent span information and pass additional metadata to attach to spans of the calling methods // using the Context object finalStringhostNameValue = "host-name-value"; finalStringentityPathValue = "entity-path-value"; finalStringuserParentSpan = "user-parent-span";ContextparentSpanContext = newContext(PARENT_SPAN_KEY, userParentSpan); // Add a new key value pair to the existing context object.ContextupdatedContext = parentSpanContext.addData(HOST_NAME_KEY, hostNameValue) .addData(ENTITY_PATH_KEY, entityPathValue); // Both key values found on the same updated context objectSystem.out.printf("Hostname value: %s%n", updatedContext.getData(HOST_NAME_KEY).get());System.out.printf("Entity Path value: %s%n", updatedContext.getData(ENTITY_PATH_KEY).get());
key - The key with which the specified value should be associated.value - The value to be associated with the specified key.Context object containing the specified pair added to the set of pairs.IllegalArgumentException - If key is null.public static Context of(Map<Object,Object> keyValues)
Context object with all the keys and values provided by
the input Map.
Code samples
finalStringkey1 = "Key1"; finalStringvalue1 = "first-value";Map<Object,Object> keyValueMap = newHashMap<>(); keyValueMap.put(key1, value1); // Create a context using the provided key value pair mapContextkeyValueContext =Context.of(keyValueMap);System.out.printf("Key1 value %s%n", keyValueContext.getData(key1).get());
keyValues - The input key value pairs that will be added to this context.IllegalArgumentException - If keyValues is null or emptypublic Optional<Object> getData(Object key)
Context objects looking for one with the specified key.
Note that the first key found, i.e. the most recently added, will be returned.
Code samples
finalStringkey1 = "Key1"; finalStringvalue1 = "first-value"; // Create a context object with given key and valueContextcontext = newContext(key1, value1); // Look for the specified key in the returned context objectOptional<Object> optionalObject = context.getData(key1); if (optionalObject.isPresent()) {System.out.printf("Key1 value: %s%n", optionalObject.get()); } else {System.out.println("Key1 does not exist or have data."); }
key - The key to search for.IllegalArgumentException - If key is null.Copyright © 2019 Microsoft Corporation. All rights reserved.