-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
179 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/Agent/NewRelic/Agent/Extensions/Providers/Wrapper/AwsSdk/AwsSdk.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFrameworks>net462;netstandard2.0</TargetFrameworks> | ||
<AssemblyName>NewRelic.Providers.Wrapper.AwsSdk</AssemblyName> | ||
<RootNamespace>NewRelic.Providers.Wrapper.AwsSdk</RootNamespace> | ||
<Description>AWS SDK Wrapper Provider for New Relic .NET Agent</Description> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Content Include="Instrumentation.xml"> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
</Content> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="$(RootProjectDirectory)\src\NewRelic.Core\NewRelic.Core.csproj" /> | ||
<ProjectReference Include="..\..\..\NewRelic.Agent.Extensions\NewRelic.Agent.Extensions.csproj" /> | ||
</ItemGroup> | ||
</Project> |
63 changes: 63 additions & 0 deletions
63
src/Agent/NewRelic/Agent/Extensions/Providers/Wrapper/AwsSdk/AwsSdkPipelineWrapper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright 2020 New Relic, Inc. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
using System; | ||
using System.Linq; | ||
using NewRelic.Agent.Api; | ||
using NewRelic.Agent.Extensions.Providers.Wrapper; | ||
|
||
namespace NewRelic.Providers.Wrapper.AwsSdk | ||
{ | ||
public class AwsSdkPipelinekWrapper : IWrapper | ||
{ | ||
public bool IsTransactionRequired => true; | ||
|
||
private const string WrapperName = "AwsSdkPipelineWrapper"; | ||
|
||
public CanWrapResponse CanWrap(InstrumentedMethodInfo methodInfo) | ||
{ | ||
return new CanWrapResponse(WrapperName.Equals(methodInfo.RequestedWrapperName)); | ||
} | ||
|
||
public AfterWrappedMethodDelegate BeforeWrappedMethod(InstrumentedMethodCall instrumentedMethodCall, IAgent agent, ITransaction transaction) | ||
{ | ||
// Get the IExecutionContext (the only parameter) | ||
dynamic executionContext = instrumentedMethodCall.MethodCall.MethodArguments[0]; | ||
|
||
// Get the IRequestContext | ||
dynamic requestContext = executionContext.RequestContext; | ||
|
||
dynamic metadata = requestContext.ServiceMetaData; | ||
string requestId = metadata.ServiceId; // SQS? | ||
|
||
// Get the AmazonWebServiceRequest being invoked. The name will tell us the type of request | ||
dynamic request = requestContext.OriginalRequest; | ||
string requestType = request.GetType().Name; | ||
|
||
// Get the web request object (IRequest). This can be used to get the headers | ||
dynamic webRequest = requestContext.Request; | ||
|
||
ISegment segment; | ||
|
||
switch (requestType) | ||
{ | ||
case "SendMessageRequest": | ||
case "SendMessageBatchRequest": | ||
segment = SqsHelper.GenerateSegment(transaction, instrumentedMethodCall.MethodCall, request.QueueUrl, MessageBrokerAction.Produce); | ||
// This needs to happen at the end | ||
//SqsHelper.InsertDistributedTraceHeaders(transaction, webRequest); | ||
break; | ||
case "ReceiveMessageRequest": | ||
segment = SqsHelper.GenerateSegment(transaction, instrumentedMethodCall.MethodCall, request.QueueUrl, MessageBrokerAction.Consume); | ||
break; | ||
case "PurgeQueueRequest": | ||
segment = SqsHelper.GenerateSegment(transaction, instrumentedMethodCall.MethodCall, request.QueueUrl, MessageBrokerAction.Purge); | ||
break; | ||
default: | ||
return Delegates.NoOp; | ||
} | ||
|
||
return Delegates.GetDelegateFor(segment); | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/Agent/NewRelic/Agent/Extensions/Providers/Wrapper/AwsSdk/Instrumentation.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<!-- | ||
Copyright 2020 New Relic Corporation. All rights reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
--> | ||
<extension xmlns="urn:newrelic-extension"> | ||
<instrumentation> | ||
<tracerFactory name="AwsSdkPipelineWrapper"> | ||
<match assemblyName="AWSSDK.Core" className="Amazon.Runtime.Internal.RuntimePipeline"> | ||
<exactMethodMatcher methodName="InvokeSync" parameters="Amazon.Runtime.IExecutionContext" /> | ||
</match> | ||
<match assemblyName="AWSSDK.Core" className="Amazon.Runtime.Internal.RuntimePipeline"> | ||
<exactMethodMatcher methodName="InvokeAsync" parameters="Amazon.Runtime.IExecutionContext" /> | ||
</match> | ||
</tracerFactory> | ||
</instrumentation> | ||
</extension> |
57 changes: 57 additions & 0 deletions
57
src/Agent/NewRelic/Agent/Extensions/Providers/Wrapper/AwsSdk/SqsHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright 2020 New Relic, Inc. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net.NetworkInformation; | ||
using NewRelic.Agent.Api; | ||
using NewRelic.Agent.Extensions.Providers.Wrapper; | ||
|
||
namespace NewRelic.Providers.Wrapper.AwsSdk | ||
{ | ||
public static class SqsHelper | ||
{ | ||
public const string VendorName = "SQS"; | ||
private class SqsAttributes | ||
{ | ||
public string QueueName; | ||
public string CloudId; | ||
public string Region; | ||
|
||
// https://sqs.us-east-2.amazonaws.com/123456789012/MyQueue | ||
public SqsAttributes(string url) | ||
{ | ||
var parts = url.Split('/'); | ||
var subdomain = parts[2].Split('.'); | ||
// subdomain[0] should always be "sqs" | ||
Region = subdomain[1]; | ||
CloudId = parts[3]; | ||
QueueName = parts[4]; | ||
} | ||
} | ||
public static ISegment GenerateSegment(ITransaction transaction, MethodCall methodCall, string url, MessageBrokerAction action) | ||
{ | ||
var attr = new SqsAttributes(url); | ||
return transaction.StartMessageBrokerSegment(methodCall, MessageBrokerDestinationType.Queue, action, VendorName, attr.QueueName); | ||
} | ||
|
||
public static void InsertDistributedTraceHeaders(ITransaction transaction, dynamic webRequest) | ||
{ | ||
var setHeaders = new Action<dynamic, string, string>((webRequest, key, value) => | ||
{ | ||
var headers = webRequest.Headers as IDictionary<string, object>; | ||
if (headers == null) | ||
{ | ||
headers = new Dictionary<string, object>(); | ||
webRequest.Headers = headers; | ||
} | ||
headers[key] = value; | ||
}); | ||
transaction.InsertDistributedTraceHeaders(webRequest, setHeaders); | ||
|
||
} | ||
} | ||
} |