From 348aa9e23a03d63ff2b51a44eb63afa8c64991dc Mon Sep 17 00:00:00 2001 From: Marty Tippin <120425148+tippmar-nr@users.noreply.github.com> Date: Thu, 11 Jul 2024 15:14:11 -0500 Subject: [PATCH 1/2] chore: Refactor AwsSDKPipelineWrapper to encapsulate SQS handling --- .../Wrapper/AwsSdk/AwsSdkPipelineWrapper.cs | 27 ++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/Agent/NewRelic/Agent/Extensions/Providers/Wrapper/AwsSdk/AwsSdkPipelineWrapper.cs b/src/Agent/NewRelic/Agent/Extensions/Providers/Wrapper/AwsSdk/AwsSdkPipelineWrapper.cs index 92e80fd19..8f0c037d2 100644 --- a/src/Agent/NewRelic/Agent/Extensions/Providers/Wrapper/AwsSdk/AwsSdkPipelineWrapper.cs +++ b/src/Agent/NewRelic/Agent/Extensions/Providers/Wrapper/AwsSdk/AwsSdkPipelineWrapper.cs @@ -54,12 +54,25 @@ public AfterWrappedMethodDelegate BeforeWrappedMethod(InstrumentedMethodCall ins return Delegates.NoOp; } dynamic request = requestContext.OriginalRequest; - string requestType = request.GetType().Name; + string requestType = request.GetType().FullName; + agent.Logger.Finest($"AwsSdkPipelineWrapper: Request type is {requestType}"); - agent.Logger.Finest("AwsSdkPipelineWrapper: Request type is " + requestType); + if (requestType.StartsWith("Amazon.SQS")) + { + return HandleSQSRequest(instrumentedMethodCall, agent, transaction, request, isAsync, executionContext); + } + + agent.Logger.Debug($"AwsSdkPipelineWrapper: Unsupported request type: {requestType}. Returning NoOp delegate."); + return Delegates.NoOp; + } + + private static AfterWrappedMethodDelegate HandleSQSRequest(InstrumentedMethodCall instrumentedMethodCall, IAgent agent, + ITransaction transaction, dynamic request, bool isAsync, dynamic executionContext) + { + var requestType = request.GetType().Name; MessageBrokerAction action; - switch (requestType) + switch (requestType ) { case "SendMessageRequest": case "SendMessageBatchRequest": @@ -72,7 +85,7 @@ public AfterWrappedMethodDelegate BeforeWrappedMethod(InstrumentedMethodCall ins action = MessageBrokerAction.Purge; break; default: - agent.Logger.Finest($"AwsSdkPipelineWrapper: Request type {requestType} is not supported. Returning NoOp delegate."); + agent.Logger.Finest($"AwsSdkPipelineWrapper: SQS Request type {requestType } is not supported. Returning NoOp delegate."); return Delegates.NoOp; } @@ -82,7 +95,7 @@ public AfterWrappedMethodDelegate BeforeWrappedMethod(InstrumentedMethodCall ins ISegment segment = SqsHelper.GenerateSegment(transaction, instrumentedMethodCall.MethodCall, requestQueueUrl, action); if (action == MessageBrokerAction.Produce) { - if (requestType == "SendMessageRequest") + if (requestType == "SendMessageRequest") { if (request.MessageAttributes == null) { @@ -93,7 +106,7 @@ public AfterWrappedMethodDelegate BeforeWrappedMethod(InstrumentedMethodCall ins SqsHelper.InsertDistributedTraceHeaders(transaction, request, dtHeaders.Count); } } - else if (requestType == "SendMessageBatchRequest") + else if (requestType == "SendMessageBatchRequest") { // loop through each message in the batch and insert distributed trace headers foreach (var message in request.Entries) @@ -116,7 +129,7 @@ public AfterWrappedMethodDelegate BeforeWrappedMethod(InstrumentedMethodCall ins if (request.MessageAttributeNames == null) request.MessageAttributeNames = new List(); - foreach(var header in dtHeaders) + foreach (var header in dtHeaders) request.MessageAttributeNames.Add(header); } From d2a95f149e86236f4ccdc7bb6b6a6d523f1876eb Mon Sep 17 00:00:00 2001 From: Marty Tippin <120425148+tippmar-nr@users.noreply.github.com> Date: Thu, 11 Jul 2024 15:48:07 -0500 Subject: [PATCH 2/2] Log unsupported request types one time only --- .../Wrapper/AwsSdk/AwsSdkPipelineWrapper.cs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/Agent/NewRelic/Agent/Extensions/Providers/Wrapper/AwsSdk/AwsSdkPipelineWrapper.cs b/src/Agent/NewRelic/Agent/Extensions/Providers/Wrapper/AwsSdk/AwsSdkPipelineWrapper.cs index 8f0c037d2..128fa381b 100644 --- a/src/Agent/NewRelic/Agent/Extensions/Providers/Wrapper/AwsSdk/AwsSdkPipelineWrapper.cs +++ b/src/Agent/NewRelic/Agent/Extensions/Providers/Wrapper/AwsSdk/AwsSdkPipelineWrapper.cs @@ -19,6 +19,8 @@ public class AwsSdkPipelineWrapper : IWrapper private const string WrapperName = "AwsSdkPipelineWrapper"; private static readonly ConcurrentDictionary> _getRequestResponseFromGeneric = new(); + private static HashSet _unsupportedRequestTypes = new(); + private static HashSet _unsupportedSQSRequestTypes = new(); public CanWrapResponse CanWrap(InstrumentedMethodInfo methodInfo) { @@ -55,14 +57,15 @@ public AfterWrappedMethodDelegate BeforeWrappedMethod(InstrumentedMethodCall ins } dynamic request = requestContext.OriginalRequest; string requestType = request.GetType().FullName; - agent.Logger.Finest($"AwsSdkPipelineWrapper: Request type is {requestType}"); if (requestType.StartsWith("Amazon.SQS")) { return HandleSQSRequest(instrumentedMethodCall, agent, transaction, request, isAsync, executionContext); } - agent.Logger.Debug($"AwsSdkPipelineWrapper: Unsupported request type: {requestType}. Returning NoOp delegate."); + if (_unsupportedRequestTypes.Add(requestType)) // log once per unsupported request type + agent.Logger.Debug($"AwsSdkPipelineWrapper: Unsupported request type: {requestType}. Returning NoOp delegate."); + return Delegates.NoOp; } @@ -72,7 +75,7 @@ private static AfterWrappedMethodDelegate HandleSQSRequest(InstrumentedMethodCal var requestType = request.GetType().Name; MessageBrokerAction action; - switch (requestType ) + switch (requestType) { case "SendMessageRequest": case "SendMessageBatchRequest": @@ -85,7 +88,9 @@ private static AfterWrappedMethodDelegate HandleSQSRequest(InstrumentedMethodCal action = MessageBrokerAction.Purge; break; default: - agent.Logger.Finest($"AwsSdkPipelineWrapper: SQS Request type {requestType } is not supported. Returning NoOp delegate."); + if (_unsupportedSQSRequestTypes.Add(requestType)) // log once per unsupported request type + agent.Logger.Debug($"AwsSdkPipelineWrapper: SQS Request type {requestType} is not supported. Returning NoOp delegate."); + return Delegates.NoOp; } @@ -95,7 +100,7 @@ private static AfterWrappedMethodDelegate HandleSQSRequest(InstrumentedMethodCal ISegment segment = SqsHelper.GenerateSegment(transaction, instrumentedMethodCall.MethodCall, requestQueueUrl, action); if (action == MessageBrokerAction.Produce) { - if (requestType == "SendMessageRequest") + if (requestType == "SendMessageRequest") { if (request.MessageAttributes == null) { @@ -106,7 +111,7 @@ private static AfterWrappedMethodDelegate HandleSQSRequest(InstrumentedMethodCal SqsHelper.InsertDistributedTraceHeaders(transaction, request, dtHeaders.Count); } } - else if (requestType == "SendMessageBatchRequest") + else if (requestType == "SendMessageBatchRequest") { // loop through each message in the batch and insert distributed trace headers foreach (var message in request.Entries)