Context Class
- java.
lang. Object - com.
azure. core. util. Context
- com.
public class Context
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 addData(Object key, Object value) method creates a new Context
object that refers to its parent, forming a linked list.
Modifier and Type | Field and Description |
---|---|
static final Context |
NONE
Signifies that no data needs to be passed to the pipeline. |
Constructor | Description |
---|---|
Context(Object key, Object value) |
Constructs a new Context object. |
Modifier and Type | Method and Description |
---|---|
Context |
add Adds a new immutable Context object with the specified key-value pair to the existing Context chain. |
Optional<Object> |
get Scans the linked-list of Context objects looking for one with the specified key. |
Map<Object,Object> |
get Scans the linked-list of Context objects populating a Map with the values of the context. |
static Context |
of(Map<Object,Object> key Creates a new immutable Context object with all the keys and values provided by the input Map. |
Methods inherited from java.lang.Object
NONE
public static final Context NONE
Signifies that no data needs to be passed to the pipeline.
Context
public Context(Object key, Object value)
Constructs a new Context object.
Code samples
// Create an empty context having no data
Context emptyContext = Context.NONE;
// OpenTelemetry context can be optionally passed using PARENT_TRACE_CONTEXT_KEY
// when OpenTelemetry context is not provided explicitly, ambient
// io.opentelemetry.context.Context.current() is used
// Context contextWithSpan = new Context(PARENT_TRACE_CONTEXT_KEY, openTelemetryContext);
Parameters:
addData
public Context addData(Object key, Object value)
Adds a new immutable Context object with the specified key-value pair to the existing Context chain.
Code samples
// Users can pass parent trace context information and additional metadata to attach to spans created by SDKs
// using the com.azure.core.util.Context object.
final String hostNameValue = "host-name-value";
final String entityPathValue = "entity-path-value";
// TraceContext represents a tracing solution context type - io.opentelemetry.context.Context for OpenTelemetry.
final TraceContext parentContext = TraceContext.root();
Context parentSpanContext = new Context(PARENT_TRACE_CONTEXT_KEY, parentContext);
// Add a new key value pair to the existing context object.
Context updatedContext = parentSpanContext.addData(HOST_NAME_KEY, hostNameValue)
.addData(ENTITY_PATH_KEY, entityPathValue);
// Both key values found on the same updated context object
System.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());
Parameters:
Returns:
getData
public Optional
Azure SDK for Java feedback
Azure SDK for Java is an open source project. Select a link to provide feedback: