Skip to content

Commit

Permalink
Fixing AWS SDK handler for registering all clients using custom AWS S…
Browse files Browse the repository at this point in the history
…ervice manifest file (#75)
  • Loading branch information
Yogiraj Awati committed Apr 23, 2019
1 parent 78ffdf7 commit ff4a741
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 37 deletions.
10 changes: 5 additions & 5 deletions sdk/src/Handlers/AwsSdk/AWSSDKHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static void RegisterXRayForAllServices(String path)
{
_customizer = GetCustomizer();
_customizer.RegisterAll = true;
_customizer.XRayPipelineHandler = new XRayPipelineHandler(path);
_customizer.AWSServiceHandlerManifest = XRayPipelineHandler.GetAWSServiceManifest(path);
}

/// <summary>
Expand All @@ -63,8 +63,8 @@ public static void RegisterXRay<T>()
/// <param name="path"> Absolute path to the file which contains the operation parameter whitelist configuration.</param>
public static void RegisterXRayManifest(String path)
{
_customizer = GetCustomizer();
_customizer.XRayPipelineHandler = new XRayPipelineHandler(path);
_customizer = GetCustomizer();
_customizer.AWSServiceHandlerManifest = XRayPipelineHandler.GetAWSServiceManifest(path);
}

/// <summary>
Expand All @@ -73,8 +73,8 @@ public static void RegisterXRayManifest(String path)
/// <param name="stream"> stream for manifest which contains the operation parameter whitelist configuration.</param>
public static void RegisterXRayManifest(Stream stream)
{
_customizer = GetCustomizer();
_customizer.XRayPipelineHandler = new XRayPipelineHandler(stream);
_customizer = GetCustomizer();
_customizer.AWSServiceHandlerManifest = XRayPipelineHandler.GetAWSServiceManifest(stream);
}

private static XRayPipelineCustomizer GetCustomizer()
Expand Down
102 changes: 70 additions & 32 deletions sdk/src/Handlers/AwsSdk/Internal/XRayPipelineHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public class XRayPipelineHandler : PipelineHandler
public XRayPipelineHandler()
{
_recorder = AWSXRayRecorder.Instance;
InitWithDefaultAWSWhitelist(_recorder);
AWSServiceHandlerManifest = GetDefaultAWSWhitelist();
}

/// <summary>
Expand All @@ -76,20 +76,10 @@ public XRayPipelineHandler(string path)
throw new ArgumentNullException("recorder");
}

if (string.IsNullOrEmpty(path))
{
_logger.DebugFormat("The path is null or empty, initializing with default AWS whitelist.");
InitWithDefaultAWSWhitelist(_recorder);
}
else
{
using (Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read))
{
Init(_recorder, stream);
}
}
AWSServiceHandlerManifest = GetAWSServiceManifest(path);
}


/// <summary>
/// Initializes a new instance of the <see cref="XRayPipelineHandler" /> class.
/// </summary>
Expand All @@ -104,17 +94,65 @@ public XRayPipelineHandler(Stream stream)
throw new ArgumentNullException("recorder");
}

if (stream == null)
AWSServiceHandlerManifest = GetAWSServiceManifest(stream);
}

/// <summary>
/// Creates instance of <see cref="XRayPipelineHandler"/> with provided AWS service manifest instance.
/// </summary>
/// <param name="awsServiceManifest">Instance of <see cref="AWSServiceHandlerManifest"/></param>
public XRayPipelineHandler(AWSServiceHandlerManifest awsServiceManifest)
{
_recorder = AWSXRayRecorder.Instance;

if (_recorder == null)
{
_logger.DebugFormat("The provided stream is null, initializing with default AWS whitelist.");
InitWithDefaultAWSWhitelist(_recorder);
throw new ArgumentNullException("recorder");
}

AWSServiceHandlerManifest = awsServiceManifest;
}

/// <summary>
/// Extracts <see cref="AWSServiceHandlerManifest"/> instance from provided path of AWS Service manifest file.
/// </summary>
/// <param name="path">Absolute path to AWS Service Manifest file</param>
/// <returns>Instance of <see cref="AWSServiceHandlerManifest"/></returns>
public static AWSServiceHandlerManifest GetAWSServiceManifest(string path)
{
if (string.IsNullOrEmpty(path))
{
_logger.DebugFormat("The path is null or empty, initializing with default AWS whitelist.");
return GetDefaultAWSWhitelist();
}
else
{
using (Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read))
{
return GetAWSServiceHandlerManifest(stream);
}
}
}

/// <summary>
/// Extracts <see cref="AWSServiceHandlerManifest"/> instance from provided aws service manifest stream.
/// </summary>
/// <param name="stream">Absolute path to AWS Service Manifest file</param>
/// <returns>Instance of <see cref="AWSServiceHandlerManifest"/></returns>
public static AWSServiceHandlerManifest GetAWSServiceManifest(Stream stream)
{
if (stream == null)
{
_logger.DebugFormat("The provided stream is null, initializing with default AWS whitelist.");
return GetDefaultAWSWhitelist();
}
else
{
Init(_recorder, stream);
}
}

return GetAWSServiceHandlerManifest(stream);
}
}


private static bool TryReadPropertyValue(object obj, string propertyName, out object value)
{
value = 0;
Expand Down Expand Up @@ -241,28 +279,27 @@ private static void AddListLengthProperty(IDictionary<string, object> aws, objec
aws[newPropertyName.FromCamelCaseToSnakeCase()] = listValue.Count;
}

private void InitWithDefaultAWSWhitelist(AWSXRayRecorder recorder)
private static AWSServiceHandlerManifest GetDefaultAWSWhitelist()
{
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(DefaultAwsWhitelistManifestResourceName))
{
Init(recorder, stream);
return GetAWSServiceHandlerManifest(stream);
}
}

private void Init(AWSXRayRecorder recorder, Stream stream)
private static AWSServiceHandlerManifest GetAWSServiceHandlerManifest(Stream stream)
{
_recorder = recorder;

using (var reader = new StreamReader(stream))
{
try
{
AWSServiceHandlerManifest = JsonMapper.ToObject<AWSServiceHandlerManifest>(reader);
return JsonMapper.ToObject<AWSServiceHandlerManifest>(reader);
}
catch (JsonException e)
{
_logger.Error(e, "Failed to load AWSServiceHandlerManifest.");
}
return null;
}
}

Expand Down Expand Up @@ -674,10 +711,11 @@ public class XRayPipelineCustomizer : IRuntimePipelineCustomizer
private Boolean registerAll;
private List<Type> types = new List<Type>();
private ReaderWriterLockSlim rwLock = new ReaderWriterLockSlim();

public bool RegisterAll { get => registerAll; set => registerAll = value; }
public string Path { get; set; }
public XRayPipelineHandler XRayPipelineHandler { get; set; } = null;
public string Path { get; set; } // TODO :: This is not used anymore, remove in next breaking change
public XRayPipelineHandler XRayPipelineHandler { get; set; } = null; // TODO :: This is not used anymore, remove in next breaking change
public AWSServiceHandlerManifest AWSServiceHandlerManifest { get; set; } = null;

public void Customize(Type serviceClientType, RuntimePipeline pipeline)
{
Expand All @@ -691,13 +729,13 @@ public void Customize(Type serviceClientType, RuntimePipeline pipeline)
addCustomization = ProcessType(serviceClientType, addCustomization);
}

if (addCustomization && XRayPipelineHandler == null)
if (addCustomization && AWSServiceHandlerManifest == null)
{
pipeline.AddHandlerAfter<EndpointResolver>(new XRayPipelineHandler());
}
else if (addCustomization && XRayPipelineHandler != null)
else if (addCustomization && AWSServiceHandlerManifest != null)
{
pipeline.AddHandlerAfter<EndpointResolver>(XRayPipelineHandler); // Custom AWS Manifest file path provided
pipeline.AddHandlerAfter<EndpointResolver>(new XRayPipelineHandler(AWSServiceHandlerManifest)); // Custom AWS Manifest file path/stream provided
}
}

Expand Down

0 comments on commit ff4a741

Please sign in to comment.