//-----------------------------------------------------------------------------
//
// 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 Amazon.XRay.Recorder.Core.Exceptions;
using Amazon.XRay.Recorder.Core.Internal.Entities;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
namespace Amazon.XRay.Recorder.Core.Internal.Context
{
///
/// This context is used in AWS Lambda environment.
///
public class LambdaContextContainer : TraceContextImpl
{
private static AsyncLocal _entityHolder = new AsyncLocal();
///
/// Get entity (segment/subsegment) from the context.
///
/// The segment get from context.
[SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate", Justification = "It's a wrapper for AsyncLocal.")]
public override Entity GetEntity()
{
Entity entity = _entityHolder.Value;
if (entity == null)
{
AWSXRayRecorder.Instance.AddFacadeSegment();
entity = _entityHolder.Value;
}
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;
}
}
}