-
Notifications
You must be signed in to change notification settings - Fork 302
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- incoming requests: implemented parent extraction for incoming SQS and SNS events (getters) - outgoing requests: implemented enrichment of outgoing message attributes by trace data (setters)
- Loading branch information
Showing
16 changed files
with
638 additions
and
17 deletions.
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
src/OpenTelemetry.Contrib.Instrumentation.AWS/Implementation/AWSMessageAttributeHelper.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,82 @@ | ||
// <copyright file="AWSMessageAttributeHelper.cs" company="OpenTelemetry Authors"> | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// </copyright> | ||
|
||
using System; | ||
using System.Linq; | ||
using Amazon.Runtime; | ||
using Amazon.Runtime.Internal; | ||
|
||
namespace OpenTelemetry.Contrib.Instrumentation.AWS.Implementation; | ||
|
||
internal class AWSMessageAttributeHelper | ||
{ | ||
// SQS/SNS message attributes collection size limit according to | ||
// https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-message-metadata.html and | ||
// https://docs.aws.amazon.com/sns/latest/dg/sns-message-attributes.html | ||
private const int MaxMessageAttributes = 10; | ||
|
||
private readonly IAWSMessageAttributeFormatter attributeFormatter; | ||
|
||
internal AWSMessageAttributeHelper(IAWSMessageAttributeFormatter attributeFormatter) | ||
{ | ||
this.attributeFormatter = attributeFormatter ?? throw new ArgumentNullException(nameof(attributeFormatter)); | ||
} | ||
|
||
internal bool TryAddParameter(ParameterCollection parameters, string name, string value) | ||
{ | ||
var index = this.GetNextAttributeIndex(parameters, name); | ||
if (!index.HasValue) | ||
{ | ||
return false; | ||
} | ||
else if (index >= MaxMessageAttributes) | ||
{ | ||
// TODO: Add logging (event source). | ||
return false; | ||
} | ||
|
||
var attributePrefix = this.attributeFormatter.AttributeNamePrefix + $".{index.Value}"; | ||
parameters.Add(attributePrefix + ".Name", name.Trim()); | ||
parameters.Add(attributePrefix + ".Value.DataType", "String"); | ||
parameters.Add(attributePrefix + ".Value.StringValue", value.Trim()); | ||
|
||
return true; | ||
} | ||
|
||
private int? GetNextAttributeIndex(ParameterCollection parameters, string name) | ||
{ | ||
var names = parameters.Where(a => this.attributeFormatter.AttributeNameRegex.IsMatch(a.Key)); | ||
if (!names.Any()) | ||
{ | ||
return 1; | ||
} | ||
|
||
int? index = 0; | ||
foreach (var nameAttribute in names) | ||
{ | ||
if (nameAttribute.Value is StringParameterValue param && param.Value == name) | ||
{ | ||
index = null; | ||
break; | ||
} | ||
|
||
var currentIndex = this.attributeFormatter.GetAttributeIndex(nameAttribute.Key); | ||
index = (currentIndex ?? 0) > index ? currentIndex : index; | ||
} | ||
|
||
return ++index; | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
src/OpenTelemetry.Contrib.Instrumentation.AWS/Implementation/AWSMessagingUtils.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,74 @@ | ||
// <copyright file="AWSMessagingUtils.cs" company="OpenTelemetry Authors"> | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// </copyright> | ||
|
||
using Amazon.Runtime; | ||
using SNS = Amazon.SimpleNotificationService.Model; | ||
using SQS = Amazon.SQS.Model; | ||
|
||
namespace OpenTelemetry.Contrib.Instrumentation.AWS.Implementation; | ||
|
||
internal static class AWSMessagingUtils | ||
{ | ||
private static readonly AWSMessageAttributeHelper SqsAttributeHelper = new(new SqsMessageAttributeFormatter()); | ||
private static readonly AWSMessageAttributeHelper SnsAttributeHelper = new(new SnsMessageAttributeFormatter()); | ||
|
||
internal static void SqsMessageAttributeSetter(IRequestContext context, string name, string value) | ||
{ | ||
var parameters = context.Request?.ParameterCollection; | ||
if (parameters == null || | ||
!parameters.ContainsKey("MessageBody") || | ||
context.OriginalRequest is not SQS::SendMessageRequest originalRequest) | ||
{ | ||
return; | ||
} | ||
|
||
// Add trace data to parameters collection of the request. | ||
if (SqsAttributeHelper.TryAddParameter(parameters, name, value)) | ||
{ | ||
// Add trace data to message attributes dictionary of the original request. | ||
// This dictionary must be in sync with parameters collection to pass through the MD5 hash matching check. | ||
if (!originalRequest.MessageAttributes.ContainsKey(name)) | ||
{ | ||
originalRequest.MessageAttributes.Add( | ||
name, new SQS::MessageAttributeValue | ||
{ DataType = "String", StringValue = value }); | ||
} | ||
} | ||
} | ||
|
||
internal static void SnsMessageAttributeSetter(IRequestContext context, string name, string value) | ||
{ | ||
var parameters = context.Request?.ParameterCollection; | ||
if (parameters == null || | ||
!parameters.ContainsKey("Message") || | ||
context.OriginalRequest is not SNS::PublishRequest originalRequest) | ||
{ | ||
return; | ||
} | ||
|
||
if (SnsAttributeHelper.TryAddParameter(parameters, name, value)) | ||
{ | ||
// Add trace data to message attributes dictionary of the original request. | ||
// This dictionary must be in sync with parameters collection to pass through the MD5 hash matching check. | ||
if (!originalRequest.MessageAttributes.ContainsKey(name)) | ||
{ | ||
originalRequest.MessageAttributes.Add( | ||
name, new SNS::MessageAttributeValue | ||
{ DataType = "String", StringValue = value }); | ||
} | ||
} | ||
} | ||
} |
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
35 changes: 35 additions & 0 deletions
35
src/OpenTelemetry.Contrib.Instrumentation.AWS/Implementation/AWSServiceType.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,35 @@ | ||
// <copyright file="AWSServiceType.cs" company="OpenTelemetry Authors"> | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// </copyright> | ||
|
||
using System; | ||
|
||
namespace OpenTelemetry.Contrib.Instrumentation.AWS.Implementation; | ||
|
||
internal class AWSServiceType | ||
{ | ||
internal const string DynamoDbService = "DynamoDBv2"; | ||
internal const string SQSService = "SQS"; | ||
internal const string SNSService = "SimpleNotificationService"; | ||
|
||
internal static bool IsDynamoDbService(string service) | ||
=> DynamoDbService.Equals(service, StringComparison.OrdinalIgnoreCase); | ||
|
||
internal static bool IsSqsService(string service) | ||
=> SQSService.Equals(service, StringComparison.OrdinalIgnoreCase); | ||
|
||
internal static bool IsSnsService(string service) | ||
=> SNSService.Equals(service, StringComparison.OrdinalIgnoreCase); | ||
} |
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
27 changes: 27 additions & 0 deletions
27
...OpenTelemetry.Contrib.Instrumentation.AWS/Implementation/IAWSMessageAttributeFormatter.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,27 @@ | ||
// <copyright file="IAWSMessageAttributeFormatter.cs" company="OpenTelemetry Authors"> | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// </copyright> | ||
|
||
using System.Text.RegularExpressions; | ||
|
||
namespace OpenTelemetry.Contrib.Instrumentation.AWS.Implementation; | ||
internal interface IAWSMessageAttributeFormatter | ||
{ | ||
Regex AttributeNameRegex { get; } | ||
|
||
string AttributeNamePrefix { get; } | ||
|
||
int? GetAttributeIndex(string attributeName); | ||
} |
34 changes: 34 additions & 0 deletions
34
src/OpenTelemetry.Contrib.Instrumentation.AWS/Implementation/SnsMessageAttributeFormatter.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,34 @@ | ||
// <copyright file="SnsMessageAttributeHelper.cs" company="OpenTelemetry Authors"> | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// </copyright> | ||
|
||
using System.Text.RegularExpressions; | ||
|
||
namespace OpenTelemetry.Contrib.Instrumentation.AWS.Implementation; | ||
|
||
internal class SnsMessageAttributeFormatter : IAWSMessageAttributeFormatter | ||
{ | ||
Regex IAWSMessageAttributeFormatter.AttributeNameRegex => new(@"MessageAttributes\.entry\.\d+\.Name"); | ||
|
||
string IAWSMessageAttributeFormatter.AttributeNamePrefix => "MessageAttributes.entry"; | ||
|
||
int? IAWSMessageAttributeFormatter.GetAttributeIndex(string attributeName) | ||
{ | ||
var parts = attributeName.Split('.'); | ||
return (parts.Length >= 3 && int.TryParse(parts[2], out int index)) ? | ||
index : | ||
null; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/OpenTelemetry.Contrib.Instrumentation.AWS/Implementation/SqsMessageAttributeFormatter.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,34 @@ | ||
// <copyright file="SqsMessageAttributeHelper.cs" company="OpenTelemetry Authors"> | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// </copyright> | ||
|
||
using System.Text.RegularExpressions; | ||
|
||
namespace OpenTelemetry.Contrib.Instrumentation.AWS.Implementation; | ||
|
||
internal class SqsMessageAttributeFormatter : IAWSMessageAttributeFormatter | ||
{ | ||
Regex IAWSMessageAttributeFormatter.AttributeNameRegex => new(@"MessageAttribute\.\d+\.Name"); | ||
|
||
string IAWSMessageAttributeFormatter.AttributeNamePrefix => "MessageAttribute"; | ||
|
||
int? IAWSMessageAttributeFormatter.GetAttributeIndex(string attributeName) | ||
{ | ||
var parts = attributeName.Split('.'); | ||
return (parts.Length >= 2 && int.TryParse(parts[1], out int index)) ? | ||
index : | ||
null; | ||
} | ||
} |
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
Oops, something went wrong.