//----------------------------------------------------------------------------- // // Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"). // You may not use this file except in compliance with the License. // A copy of the License is located at // // http://aws.amazon.com/apache2.0 // // or in the "license" file accompanying this file. This file is distributed // on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either // express or implied. See the License for the specific language governing // permissions and limitations under the License. // //----------------------------------------------------------------------------- using System; using System.Diagnostics.CodeAnalysis; using Amazon.XRay.Recorder.Core.Exceptions; using Amazon.XRay.Recorder.Core.Internal.Entities; using System.Threading; using Amazon.XRay.Recorder.Core.Strategies; namespace Amazon.XRay.Recorder.Core.Internal.Context { /// /// Context to save trace segment which will be preserved across thread. /// public class AsyncLocalContextContainer : TraceContextImpl { private static AsyncLocal _entityHolder = new AsyncLocal(); /// /// Get entity (segment/subsegment) from the context. /// /// The segment get from context. /// Thrown when the entity is not available to get. [SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate", Justification = "It's a wrapper for AsyncLocal.")] public override Entity GetEntity() { Entity entity = _entityHolder.Value; if (entity == null) { throw new EntityNotAvailableException("Entity doesn't exist in AsyncLocal"); } return entity; } /// /// Set the specified entity (segment/subsegment) into context. /// /// The segment to be set. /// Thrown when the entity is not available to set. public override void SetEntity(Entity entity) { _entityHolder.Value = entity; } /// /// Clear entity from trace context for cleanup. /// public override void ClearEntity() { _entityHolder.Value = null; } /// /// Checks whether enity is present in . /// /// True if entity is present in else false. public override Boolean IsEntityPresent() { Entity entity = _entityHolder.Value; if (entity == null) { return false; } return true; } } }