|
| 1 | +// Copyright 2020 New Relic, Inc. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections; |
| 6 | +using System.Collections.Concurrent; |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.Linq; |
| 9 | +using NewRelic.Agent.Api; |
| 10 | +using NewRelic.Agent.Api.Experimental; |
| 11 | +using NewRelic.Agent.Extensions.Providers.Wrapper; |
| 12 | +using NewRelic.Reflection; |
| 13 | + |
| 14 | +namespace NewRelic.Agent.Extensions.AwsSdk |
| 15 | +{ |
| 16 | + public static class SqsHelper |
| 17 | + { |
| 18 | + private static ConcurrentDictionary<Type, Func<object, IDictionary>> _getMessageAttributes = new(); |
| 19 | + private static Func<object> _messageAttributeValueTypeFactory; |
| 20 | + |
| 21 | + public const string VendorName = "SQS"; |
| 22 | + |
| 23 | + private class SqsAttributes |
| 24 | + { |
| 25 | + public string QueueName { get; } |
| 26 | + public string CloudId { get; } |
| 27 | + public string Region { get; } |
| 28 | + |
| 29 | + // https://sqs.us-east-2.amazonaws.com/123456789012/MyQueue |
| 30 | + public SqsAttributes(string url) |
| 31 | + { |
| 32 | + if (string.IsNullOrEmpty(url)) |
| 33 | + { |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + var parts = url.Split('/'); |
| 38 | + if (parts.Length < 5) |
| 39 | + { |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + CloudId = parts[3]; |
| 44 | + QueueName = parts[4]; |
| 45 | + |
| 46 | + var subdomain = parts[2].Split('.'); |
| 47 | + if (subdomain.Length < 2) |
| 48 | + { |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + // subdomain[0] should always be "sqs" |
| 53 | + Region = subdomain[1]; |
| 54 | + } |
| 55 | + } |
| 56 | + public static ISegment GenerateSegment(ITransaction transaction, MethodCall methodCall, string url, MessageBrokerAction action) |
| 57 | + { |
| 58 | + var attr = new SqsAttributes(url); |
| 59 | + var segment = transaction.StartMessageBrokerSegment(methodCall, MessageBrokerDestinationType.Queue, action, VendorName, attr.QueueName); |
| 60 | + segment.GetExperimentalApi().MakeLeaf(); |
| 61 | + |
| 62 | + return segment; |
| 63 | + } |
| 64 | + |
| 65 | + // SQS allows a maximum of 10 message attributes |
| 66 | + private const int MaxSQSMessageAttributes = 10; |
| 67 | + |
| 68 | + public static void InsertDistributedTraceHeaders(ITransaction transaction, object sendMessageRequest, int dtHeaderCount) |
| 69 | + { |
| 70 | + var headersInserted = 0; |
| 71 | + |
| 72 | + var setHeaders = new Action<object, string, string>((smr, key, value) => |
| 73 | + { |
| 74 | + var getMessageAttributes = _getMessageAttributes.GetOrAdd(smr.GetType(), t => VisibilityBypasser.Instance.GeneratePropertyAccessor<IDictionary>(t, "MessageAttributes")); |
| 75 | + var messageAttributes = getMessageAttributes(smr); |
| 76 | + |
| 77 | + // if we can't add all DT headers, don't add any |
| 78 | + if ((messageAttributes.Count + dtHeaderCount - headersInserted) > MaxSQSMessageAttributes) |
| 79 | + return; |
| 80 | + |
| 81 | + // create a new MessageAttributeValue instance |
| 82 | + var messageAttributeValueTypeFactory = _messageAttributeValueTypeFactory ??= VisibilityBypasser.Instance.GenerateTypeFactory(smr.GetType().Assembly.FullName, "Amazon.SQS.Model.MessageAttributeValue"); |
| 83 | + object newMessageAttributeValue = messageAttributeValueTypeFactory.Invoke(); |
| 84 | + |
| 85 | + var dataTypePropertySetter = VisibilityBypasser.Instance.GeneratePropertySetter<string>(newMessageAttributeValue, "DataType"); |
| 86 | + dataTypePropertySetter("String"); |
| 87 | + |
| 88 | + var stringValuePropertySetter = VisibilityBypasser.Instance.GeneratePropertySetter<string>(newMessageAttributeValue, "StringValue"); |
| 89 | + stringValuePropertySetter(value); |
| 90 | + |
| 91 | + messageAttributes.Add(key, newMessageAttributeValue); |
| 92 | + |
| 93 | + ++headersInserted; |
| 94 | + }); |
| 95 | + |
| 96 | + transaction.InsertDistributedTraceHeaders(sendMessageRequest, setHeaders); |
| 97 | + |
| 98 | + } |
| 99 | + public static void AcceptDistributedTraceHeaders(ITransaction transaction, dynamic messageAttributes) |
| 100 | + { |
| 101 | + var getHeaders = new Func<IDictionary, string, IEnumerable<string>>((maDict, key) => |
| 102 | + { |
| 103 | + if (!maDict.Contains(key)) |
| 104 | + return []; |
| 105 | + |
| 106 | + return [(string)((dynamic)maDict[key]).StringValue]; |
| 107 | + }); |
| 108 | + |
| 109 | + transaction.AcceptDistributedTraceHeaders((IDictionary)messageAttributes, getHeaders, TransportType.Queue); |
| 110 | + |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments