Package com.amazonaws.xray
Class AWSXRayRecorder
- java.lang.Object
-
- com.amazonaws.xray.AWSXRayRecorder
-
public class AWSXRayRecorder extends java.lang.Object
-
-
Constructor Summary
Constructors Constructor Description AWSXRayRecorder()
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidaddAllLogReferences(java.util.Set<AWSLogReference> logReferences)voidaddAllSegmentListeners(java.util.Collection<SegmentListener> segmentListeners)Adds a Collection of SegmentListeners to the recordervoidaddSegmentListener(SegmentListener segmentListener)Adds a single SegmentListener to the recorderSegmentbeginDummySegment()Deprecated.UsebeginNoOpSegment().SegmentbeginDummySegment(TraceID traceId)Deprecated.SegmentbeginDummySegment(java.lang.String name, TraceID traceId)Deprecated.SegmentbeginNoOpSegment()Sets the currentSegmentto a no-op which will not record any information or be emitted.SegmentbeginNoOpSegment(TraceID traceID)Sets the currentSegmentto a no-op which will not record any information or be emitted.SegmentbeginSegment(java.lang.String name)SegmentbeginSegment(java.lang.String name, TraceID traceId, @Nullable java.lang.String parentId)SegmentbeginSegmentWithSampling(java.lang.String name)Begins a new segment after applying the configured sampling strategy.SubsegmentbeginSubsegment(java.lang.String name)Begins a subsegment.SubsegmentbeginSubsegmentWithoutSampling(java.lang.String name)Begins a subsegment.voidclearThreadLocal()Deprecated.useclearTraceEntity()insteadvoidclearTraceEntity()Clears the current trace entity value using the implementation provided by the SegmentContext resolved from the segmentContextResolverChain.voidcreateSegment(java.lang.String name, java.lang.Runnable runnable)Begins a segment, runs the provided runnable, and ends the segment before returning the supplier's result.voidcreateSegment(java.lang.String name, java.util.function.Consumer<Segment> consumer)Begins a segment and passes it to the supplied consumer, and ends the segment before returning the consumer's result.<R> @Nullable RcreateSegment(java.lang.String name, java.util.function.Function<Segment,@Nullable R> function)Begins a segment, passes it to the supplied function, and ends the segment before returning the supplied function's result.<R> @Nullable RcreateSegment(java.lang.String name, java.util.function.Supplier<R> supplier)Begins a segment, invokes the provided supplier, and ends the segment before returning the supplier's result.voidcreateSubsegment(java.lang.String name, java.lang.Runnable runnable)Begins a subsegment, runs the provided runnable, and ends the subsegment once complete.voidcreateSubsegment(java.lang.String name, java.util.function.Consumer<Subsegment> consumer)Begins a subsegment and passes it to the supplied consumer, and ends the subsegment before returning the consumer's result.<R> @Nullable RcreateSubsegment(java.lang.String name, java.util.function.Function<Subsegment,@Nullable R> function)Begins a subsegment, passes it to the supplied function, and ends the subsegment before returning the supplied function's result.<R> @Nullable RcreateSubsegment(java.lang.String name, java.util.function.Supplier<R> supplier)Begins a subsegment, passes it to the provided supplier, and ends the subsegment before returning the supplier's result.@Nullable java.lang.StringcurrentEntityId()@Nullable java.lang.StringcurrentFormattedId()@Nullable TraceIDcurrentTraceId()voidendSegment()Ends a segment.voidendSubsegment()Ends a subsegment.voidendSubsegment(@Nullable Subsegment subsegment)Ends the provided subsegment.booleanforceSamplingOfCurrentSegment()Checks whether the currentSamplingStrategysupports forced sampling.java.util.Map<java.lang.String,java.lang.Object>getAwsRuntimeContext()ContextMissingStrategygetContextMissingStrategy()@Nullable SegmentgetCurrentSegment()java.util.Optional<Segment>getCurrentSegmentOptional()@Nullable SubsegmentgetCurrentSubsegment()java.util.Optional<Subsegment>getCurrentSubsegmentOptional()EmittergetEmitter()IdGeneratorgetIdGenerator()Gets thisAWSXRayRecorderinstance's ID generator.@Nullable java.lang.StringgetOrigin()PrioritizationStrategygetPrioritizationStrategy()SamplingStrategygetSamplingStrategy()SegmentContextResolverChaingetSegmentContextResolverChain()java.util.ArrayList<SegmentListener>getSegmentListeners()Returns the list of SegmentListeners attached to the recorderjava.util.Map<java.lang.String,java.lang.Object>getServiceRuntimeContext()StreamingStrategygetStreamingStrategy()@Nullable EntitygetThreadLocal()Deprecated.usegetTraceEntity()insteadThrowableSerializationStrategygetThrowableSerializationStrategy()@Nullable EntitygetTraceEntity()Gets the current trace entity value using the implementation provided by the SegmentContext resolved from the segmentContextResolverChain.voidinjectThreadLocal(Entity entity)Deprecated.usesetTraceEntity(Entity entity)insteadvoidputRuntimeContext(java.lang.String key, java.lang.Object value)booleansendSegment(Segment segment)Sends a segment to the emitter if the segment is marked as sampled.booleansendSubsegment(Subsegment subsegment)Sends a subsegment to the emitter if the subsegment's parent segment is marked as sampled.voidsetContextMissingStrategy(ContextMissingStrategy contextMissingStrategy)voidsetEmitter(Emitter emitter)voidsetForcedTraceIdGeneration(boolean alwaysCreateTraceId)Configures thisAWSXRayRecorderto add valid TraceId in all segments even NoOp ones that usually have a fixed value.voidsetOrigin(java.lang.String origin)voidsetPrioritizationStrategy(PrioritizationStrategy prioritizationStrategy)voidsetSamplingStrategy(SamplingStrategy samplingStrategy)voidsetSegmentContextResolverChain(SegmentContextResolverChain segmentContextResolverChain)voidsetStreamingStrategy(StreamingStrategy streamingStrategy)voidsetThrowableSerializationStrategy(ThrowableSerializationStrategy throwableSerializationStrategy)voidsetTraceEntity(@Nullable Entity entity)Deprecated.UseEntity.run(Runnable)or methods inSegmentContextExecutorsinstead of directly setting the trace entity so it can be restored correctly.voiduseFastIdGenerator()Configures thisAWSXRayRecorderto use a fast but cryptographically insecure random number generator for generating random IDs.voiduseSecureIdGenerator()Configures thisAWSXRayRecorderto use a cryptographically secure random generator for generating random IDs.
-
-
-
Method Detail
-
sendSegment
public boolean sendSegment(Segment segment)
Sends a segment to the emitter if the segment is marked as sampled.- Parameters:
segment- the segment to send- Returns:
- true if the segment was emitted succesfully.
-
sendSubsegment
public boolean sendSubsegment(Subsegment subsegment)
Sends a subsegment to the emitter if the subsegment's parent segment is marked as sampled.- Parameters:
subsegment- the subsegment to send- Returns:
- true if the subsegment was emitted succesfully.
-
createSegment
public <R> @Nullable R createSegment(java.lang.String name, java.util.function.Function<Segment,@Nullable R> function)Begins a segment, passes it to the supplied function, and ends the segment before returning the supplied function's result. Intercepts exceptions, adds them to the segment, and re-throws them.- Type Parameters:
R- the type of the value returned byfunction- Parameters:
name- the name to use for the created segmentfunction- the function to invoke- Returns:
- the value returned by the supplied function
-
createSegment
public void createSegment(java.lang.String name, java.util.function.Consumer<Segment> consumer)Begins a segment and passes it to the supplied consumer, and ends the segment before returning the consumer's result. Intercepts exceptions, adds them to the segment, and re-throws them.- Parameters:
name- the name to use for the created segmentconsumer- the function to invoke
-
createSegment
public <R> @Nullable R createSegment(java.lang.String name, java.util.function.Supplier<R> supplier)Begins a segment, invokes the provided supplier, and ends the segment before returning the supplier's result. Intercepts exceptions, adds them to the segment, and re-throws them.- Type Parameters:
R- the type of the value returned bysupplier- Parameters:
name- the name to use for the created segmentsupplier- the supplier to invoke- Returns:
- the value returned by the provided supplier
-
createSegment
public void createSegment(java.lang.String name, java.lang.Runnable runnable)Begins a segment, runs the provided runnable, and ends the segment before returning the supplier's result. Intercepts exceptions, adds them to the segment, and re-throws them.- Parameters:
name- the name to use for the created segmentrunnable- the runnable to run
-
createSubsegment
public <R> @Nullable R createSubsegment(java.lang.String name, java.util.function.Function<Subsegment,@Nullable R> function)Begins a subsegment, passes it to the supplied function, and ends the subsegment before returning the supplied function's result. Intercepts exceptions, adds them to the subsegment, and re-throws them.- Type Parameters:
R- the type of the value returned byfunction- Parameters:
name- the name to use for the created subsegmentfunction- the function to invoke- Returns:
- the value returned by the supplied function
-
createSubsegment
public void createSubsegment(java.lang.String name, java.util.function.Consumer<Subsegment> consumer)Begins a subsegment and passes it to the supplied consumer, and ends the subsegment before returning the consumer's result. Intercepts exceptions, adds them to the subsegment, and re-throws them.- Parameters:
name- the name to use for the created subsegmentconsumer- the function to invoke
-
createSubsegment
public <R> @Nullable R createSubsegment(java.lang.String name, java.util.function.Supplier<R> supplier)Begins a subsegment, passes it to the provided supplier, and ends the subsegment before returning the supplier's result. Intercepts exceptions, adds them to the subsegment, and re-throws them.- Type Parameters:
R- the type of the value returned byfunction- Parameters:
name- the name to use for the created subsegmentsupplier- the supplier to invoke- Returns:
- the value returned by the provided supplier
-
createSubsegment
public void createSubsegment(java.lang.String name, java.lang.Runnable runnable)Begins a subsegment, runs the provided runnable, and ends the subsegment once complete. Intercepts exceptions, adds them to the subsegment, and re-throws them.- Parameters:
name- the name to use for the created subsegmentrunnable- the runnable to run
-
beginSegment
public Segment beginSegment(java.lang.String name)
-
beginSegmentWithSampling
public Segment beginSegmentWithSampling(java.lang.String name)
Begins a new segment after applying the configured sampling strategy. This method only uses the segment name and origin (if defined) to compute a sampling decision.- Parameters:
name- the segment name, to be used for the sampling decision- Returns:
- Returns a proper segment if a sampled decision is made, and a no-op segment otherwise.
-
beginSegment
public Segment beginSegment(java.lang.String name, TraceID traceId, @Nullable java.lang.String parentId)
-
beginNoOpSegment
public Segment beginNoOpSegment()
-
beginDummySegment
@Deprecated public Segment beginDummySegment()
Deprecated.UsebeginNoOpSegment().Sets the current segment to a new instance ofDummySegment.- Returns:
- the newly created
DummySegment.
-
beginDummySegment
@Deprecated public Segment beginDummySegment(java.lang.String name, TraceID traceId)
Deprecated.
-
endSegment
public void endSegment()
Ends a segment.- Throws:
SegmentNotFoundException- ifcontextMissingStrategythrows exceptions and no segment is currently in progress
-
endSubsegment
public void endSubsegment(@Nullable Subsegment subsegment)
Ends the provided subsegment. This method doesn't touch context storage and should be used when ending custom subsegments in asynchronous methods or other threads.- Parameters:
subsegment- the subsegment to close.
-
beginSubsegment
public Subsegment beginSubsegment(java.lang.String name)
Begins a subsegment.- Parameters:
name- the name to use for the created subsegment- Returns:
- the newly created subsegment, or
nullifcontextMissingStrategysuppresses and no segment is currently in progress - Throws:
SegmentNotFoundException- ifcontextMissingStrategythrows exceptions and no segment is currently in progress
-
beginSubsegmentWithoutSampling
public Subsegment beginSubsegmentWithoutSampling(java.lang.String name)
Begins a subsegment.- Parameters:
name- the name to use for the created subsegment- Returns:
- the newly created subsegment, or
nullifcontextMissingStrategysuppresses and no segment is currently in progress. The subsegment will not be sampled regardless of the SamplingStrategy. - Throws:
SegmentNotFoundException- ifcontextMissingStrategythrows exceptions and no segment is currently in progress
-
endSubsegment
public void endSubsegment()
Ends a subsegment.- Throws:
SegmentNotFoundException- ifcontextMissingStrategythrows exceptions and no segment is currently in progressSubsegmentNotFoundException- ifcontextMissingStrategythrows exceptions and no subsegment is currently in progress
-
getCurrentSegment
public @Nullable Segment getCurrentSegment()
- Returns:
- the current segment, or
nullifcontextMissingStrategysuppresses exceptions and there is no segment in progress - Throws:
SegmentNotFoundException- ifcontextMissingStrategythrows exceptions and there is no segment in progress
-
getCurrentSegmentOptional
public java.util.Optional<Segment> getCurrentSegmentOptional()
- Returns:
- the current segment, or
Optional.empty()if there is no segment
-
getCurrentSubsegment
public @Nullable Subsegment getCurrentSubsegment()
- Returns:
- the current subsegment, or
nullifcontextMissingStrategysuppresses exceptions and the segment context cannot be found or the segment has no subsegments in progress - Throws:
SegmentNotFoundException- ifcontextMissingStrategythrows exceptions and the segment context cannot be foundSubsegmentNotFoundException- ifcontextMissingStrategythrows exceptions and the current segment has no subsegments in progress
-
getCurrentSubsegmentOptional
public java.util.Optional<Subsegment> getCurrentSubsegmentOptional()
- Returns:
- the current subsegment, or
Optional.empty()if there is no subsegment
-
injectThreadLocal
@Deprecated public void injectThreadLocal(Entity entity)
Deprecated.usesetTraceEntity(Entity entity)insteadInjects the providedEntityinto the current thread's thread local context.- Parameters:
entity- theSegmentorSubsegmentto inject into the current thread
-
getThreadLocal
@Deprecated public @Nullable Entity getThreadLocal()
Deprecated.usegetTraceEntity()instead- Returns:
- the Entity object currently stored in the thread's ThreadLocalStorage
-
clearThreadLocal
@Deprecated public void clearThreadLocal()
Deprecated.useclearTraceEntity()instead
-
setTraceEntity
@Deprecated public void setTraceEntity(@Nullable Entity entity)
Deprecated.UseEntity.run(Runnable)or methods inSegmentContextExecutorsinstead of directly setting the trace entity so it can be restored correctly.Sets the trace entity value using the implementation provided by the SegmentContext resolved from the segmentContextResolverChain.- Parameters:
entity- the trace entity to set
-
getTraceEntity
public @Nullable Entity getTraceEntity()
Gets the current trace entity value using the implementation provided by the SegmentContext resolved from the segmentContextResolverChain.- Returns:
- the current trace entity
-
clearTraceEntity
public void clearTraceEntity()
Clears the current trace entity value using the implementation provided by the SegmentContext resolved from the segmentContextResolverChain.
-
putRuntimeContext
public void putRuntimeContext(java.lang.String key, java.lang.Object value)
-
addAllLogReferences
public void addAllLogReferences(java.util.Set<AWSLogReference> logReferences)
-
getSamplingStrategy
public SamplingStrategy getSamplingStrategy()
- Returns:
- the samplingStrategy
-
setSamplingStrategy
public void setSamplingStrategy(SamplingStrategy samplingStrategy)
- Parameters:
samplingStrategy- the samplingStrategy to set
-
getStreamingStrategy
public StreamingStrategy getStreamingStrategy()
- Returns:
- the streamingStrategy
-
setStreamingStrategy
public void setStreamingStrategy(StreamingStrategy streamingStrategy)
- Parameters:
streamingStrategy- the streamingStrategy to set
-
getPrioritizationStrategy
public PrioritizationStrategy getPrioritizationStrategy()
- Returns:
- the prioritizationStrategy
-
setPrioritizationStrategy
public void setPrioritizationStrategy(PrioritizationStrategy prioritizationStrategy)
- Parameters:
prioritizationStrategy- the prioritizationStrategy to set
-
getThrowableSerializationStrategy
public ThrowableSerializationStrategy getThrowableSerializationStrategy()
- Returns:
- the throwableSerializationStrategy
-
setThrowableSerializationStrategy
public void setThrowableSerializationStrategy(ThrowableSerializationStrategy throwableSerializationStrategy)
- Parameters:
throwableSerializationStrategy- the throwableSerializationStrategy to set
-
getContextMissingStrategy
public ContextMissingStrategy getContextMissingStrategy()
- Returns:
- the contextMissingStrategy
-
setContextMissingStrategy
public void setContextMissingStrategy(ContextMissingStrategy contextMissingStrategy)
- Parameters:
contextMissingStrategy- the contextMissingStrategy to set
-
getSegmentContextResolverChain
public SegmentContextResolverChain getSegmentContextResolverChain()
- Returns:
- the segmentContextResolverChain
-
setSegmentContextResolverChain
public void setSegmentContextResolverChain(SegmentContextResolverChain segmentContextResolverChain)
- Parameters:
segmentContextResolverChain- the segmentContextResolverChain to set
-
getEmitter
public Emitter getEmitter()
- Returns:
- the emitter
-
setEmitter
public void setEmitter(Emitter emitter)
- Parameters:
emitter- the emitter to set
-
getSegmentListeners
public java.util.ArrayList<SegmentListener> getSegmentListeners()
Returns the list of SegmentListeners attached to the recorder- Returns:
- the SegmentListeners
-
addSegmentListener
public void addSegmentListener(SegmentListener segmentListener)
Adds a single SegmentListener to the recorder- Parameters:
segmentListener- a SegmentListener to add
-
addAllSegmentListeners
public void addAllSegmentListeners(java.util.Collection<SegmentListener> segmentListeners)
Adds a Collection of SegmentListeners to the recorder- Parameters:
segmentListeners- a Collection of SegmentListeners to add
-
getAwsRuntimeContext
public java.util.Map<java.lang.String,java.lang.Object> getAwsRuntimeContext()
- Returns:
- the awsRuntimeContext
-
getServiceRuntimeContext
public java.util.Map<java.lang.String,java.lang.Object> getServiceRuntimeContext()
- Returns:
- the serviceRuntimeContext
-
getOrigin
public @Nullable java.lang.String getOrigin()
- Returns:
- the origin
-
setOrigin
public void setOrigin(java.lang.String origin)
- Parameters:
origin- the origin to set
-
useFastIdGenerator
public final void useFastIdGenerator()
Configures thisAWSXRayRecorderto use a fast but cryptographically insecure random number generator for generating random IDs. This option should be preferred if your application does not rely on AWS X-Ray Trace IDs being generated from a cryptographically secure random number generator.- See Also:
useSecureIdGenerator()
-
useSecureIdGenerator
public final void useSecureIdGenerator()
Configures thisAWSXRayRecorderto use a cryptographically secure random generator for generating random IDs. Unless your application in some way relies on AWS X-Ray trace IDs being generated from a cryptographically secure random number source, you should prefer to use the fast ID generator.- See Also:
useFastIdGenerator()
-
getIdGenerator
public final IdGenerator getIdGenerator()
Gets thisAWSXRayRecorderinstance's ID generator. This method is intended for internal use only.- Returns:
- the configured ID generator
-
forceSamplingOfCurrentSegment
public boolean forceSamplingOfCurrentSegment()
Checks whether the currentSamplingStrategysupports forced sampling. Use with caution, since segments sampled in this manner will not count towards your sampling statistic counts.- Returns:
- true if forced sampling is supported and the current segment was changed from not sampled to sampled.
-
currentEntityId
public @Nullable java.lang.String currentEntityId()
- Returns:
- the ID of the
SegmentorSubsegmentcurrently in progress, ornullifcontextMissingStrategysuppresses exceptions and no segment or subsegment is currently in progress - Throws:
SegmentNotFoundException- ifcontextMissingStrategythrows exceptions and no segment or subsegment is currently in progress
-
currentTraceId
public @Nullable TraceID currentTraceId()
- Returns:
- the trace ID of the
Segmentcurrently in progress, ornullifcontextMissingStrategysuppresses exceptions and no segment or subsegment is currently in progress - Throws:
SegmentNotFoundException- ifcontextMissingStrategythrows exceptions and no segment or subsegment is currently in progress
-
currentFormattedId
public @Nullable java.lang.String currentFormattedId()
- Returns:
- the trace ID of the
Segmentcurrently in progress and the ID of theSegmentorSubsegmentin progress, joined with@, ornullifcontextMissingStrategysuppresses exceptions and no segment or subsegment is currently in progress - Throws:
SegmentNotFoundException- ifcontextMissingStrategythrows exceptions and no segment or subsegment is currently in progress
-
setForcedTraceIdGeneration
public void setForcedTraceIdGeneration(boolean alwaysCreateTraceId)
Configures thisAWSXRayRecorderto add valid TraceId in all segments even NoOp ones that usually have a fixed value.
-
-