-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Instrumentation for Amazon Simple Queuing Service (AWSSDK.SQS) (#…
…2620) Adds instrumentation for Amazon SQS, including distributed tracing support (Cross-Application Tracing (CAT) is not supported). --------- Co-authored-by: chynesNR <chynes@newrelic.com> Co-authored-by: Alex Hemsath <ahemsath@newrelic.com>
- Loading branch information
1 parent
b24d2c9
commit ac738ba
Showing
37 changed files
with
1,490 additions
and
10 deletions.
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
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
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
113 changes: 113 additions & 0 deletions
113
src/Agent/NewRelic/Agent/Extensions/NewRelic.Agent.Extensions/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,113 @@ | ||
// Copyright 2020 New Relic, Inc. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
using System; | ||
using System.Collections; | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using NewRelic.Agent.Api; | ||
using NewRelic.Agent.Api.Experimental; | ||
using NewRelic.Agent.Extensions.Providers.Wrapper; | ||
using NewRelic.Reflection; | ||
|
||
namespace NewRelic.Agent.Extensions.AwsSdk | ||
{ | ||
public static class SqsHelper | ||
{ | ||
private static ConcurrentDictionary<Type, Func<object, IDictionary>> _getMessageAttributes = new(); | ||
private static Func<object> _messageAttributeValueTypeFactory; | ||
|
||
public const string VendorName = "SQS"; | ||
|
||
private class SqsAttributes | ||
{ | ||
public string QueueName { get; } | ||
public string CloudId { get; } | ||
public string Region { get; } | ||
|
||
// https://sqs.us-east-2.amazonaws.com/123456789012/MyQueue | ||
public SqsAttributes(string url) | ||
{ | ||
if (string.IsNullOrEmpty(url)) | ||
{ | ||
return; | ||
} | ||
|
||
var parts = url.Split('/'); | ||
if (parts.Length < 5) | ||
{ | ||
return; | ||
} | ||
|
||
CloudId = parts[3]; | ||
QueueName = parts[4]; | ||
|
||
var subdomain = parts[2].Split('.'); | ||
if (subdomain.Length < 2) | ||
{ | ||
return; | ||
} | ||
|
||
// subdomain[0] should always be "sqs" | ||
Region = subdomain[1]; | ||
} | ||
} | ||
public static ISegment GenerateSegment(ITransaction transaction, MethodCall methodCall, string url, MessageBrokerAction action) | ||
{ | ||
var attr = new SqsAttributes(url); | ||
var segment = transaction.StartMessageBrokerSegment(methodCall, MessageBrokerDestinationType.Queue, action, VendorName, attr.QueueName); | ||
segment.GetExperimentalApi().MakeLeaf(); | ||
|
||
return segment; | ||
} | ||
|
||
// SQS allows a maximum of 10 message attributes | ||
private const int MaxSQSMessageAttributes = 10; | ||
|
||
public static void InsertDistributedTraceHeaders(ITransaction transaction, object sendMessageRequest, int dtHeaderCount) | ||
{ | ||
var headersInserted = 0; | ||
|
||
var setHeaders = new Action<object, string, string>((smr, key, value) => | ||
{ | ||
var getMessageAttributes = _getMessageAttributes.GetOrAdd(smr.GetType(), t => VisibilityBypasser.Instance.GeneratePropertyAccessor<IDictionary>(t, "MessageAttributes")); | ||
var messageAttributes = getMessageAttributes(smr); | ||
|
||
// if we can't add all DT headers, don't add any | ||
if ((messageAttributes.Count + dtHeaderCount - headersInserted) > MaxSQSMessageAttributes) | ||
return; | ||
|
||
// create a new MessageAttributeValue instance | ||
var messageAttributeValueTypeFactory = _messageAttributeValueTypeFactory ??= VisibilityBypasser.Instance.GenerateTypeFactory(smr.GetType().Assembly.FullName, "Amazon.SQS.Model.MessageAttributeValue"); | ||
object newMessageAttributeValue = messageAttributeValueTypeFactory.Invoke(); | ||
|
||
var dataTypePropertySetter = VisibilityBypasser.Instance.GeneratePropertySetter<string>(newMessageAttributeValue, "DataType"); | ||
dataTypePropertySetter("String"); | ||
|
||
var stringValuePropertySetter = VisibilityBypasser.Instance.GeneratePropertySetter<string>(newMessageAttributeValue, "StringValue"); | ||
stringValuePropertySetter(value); | ||
|
||
messageAttributes.Add(key, newMessageAttributeValue); | ||
|
||
++headersInserted; | ||
}); | ||
|
||
transaction.InsertDistributedTraceHeaders(sendMessageRequest, setHeaders); | ||
|
||
} | ||
public static void AcceptDistributedTraceHeaders(ITransaction transaction, dynamic messageAttributes) | ||
{ | ||
var getHeaders = new Func<IDictionary, string, IEnumerable<string>>((maDict, key) => | ||
{ | ||
if (!maDict.Contains(key)) | ||
return []; | ||
|
||
return [(string)((dynamic)maDict[key]).StringValue]; | ||
}); | ||
|
||
transaction.AcceptDistributedTraceHeaders((IDictionary)messageAttributes, getHeaders, TransportType.Queue); | ||
|
||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
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,19 @@ | ||
<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="..\..\..\NewRelic.Agent.Extensions\NewRelic.Agent.Extensions.csproj" /> | ||
</ItemGroup> | ||
</Project> |
Oops, something went wrong.