-
Notifications
You must be signed in to change notification settings - Fork 292
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
21 changed files
with
825 additions
and
21 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
30 changes: 30 additions & 0 deletions
30
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,30 @@ | ||
// <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 System.Collections.Generic; | ||
using OpenTelemetry.Context.Propagation; | ||
|
||
namespace OpenTelemetry.Contrib.Instrumentation.AWS.Implementation; | ||
|
||
internal static class AWSMessagingUtils | ||
{ | ||
internal static IReadOnlyDictionary<string, string> InjectIntoDictionary(PropagationContext propagationContext) | ||
{ | ||
var carrier = new Dictionary<string, string>(); | ||
Propagators.DefaultTextMapPropagator.Inject(propagationContext, carrier, (c, k, v) => c[k] = v); | ||
return carrier; | ||
} | ||
} |
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"; // SNS | ||
|
||
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
72 changes: 72 additions & 0 deletions
72
src/OpenTelemetry.Contrib.Instrumentation.AWS/Implementation/SnsRequestContextHelper.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,72 @@ | ||
// <copyright file="SnsRequestContextHelper.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.Collections.Generic; | ||
using System.Linq; | ||
using Amazon.Runtime; | ||
using Amazon.Runtime.Internal; | ||
using Amazon.SimpleNotificationService.Model; | ||
|
||
namespace OpenTelemetry.Contrib.Instrumentation.AWS.Implementation; | ||
internal class SnsRequestContextHelper | ||
{ | ||
// 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; | ||
|
||
internal static void AddAttributes(IRequestContext context, IReadOnlyDictionary<string, string> attributes) | ||
{ | ||
var parameters = context.Request?.ParameterCollection; | ||
var originalRequest = context.OriginalRequest as PublishRequest; | ||
if (originalRequest?.MessageAttributes == null || parameters == null) | ||
{ | ||
return; | ||
} | ||
|
||
if (attributes.Keys.Any(k => originalRequest.MessageAttributes.ContainsKey(k))) | ||
{ | ||
// If at least one attribute is already present in the request then we skip the injection. | ||
return; | ||
} | ||
|
||
int attributesCount = originalRequest.MessageAttributes.Count; | ||
if (attributes.Count + attributesCount > MaxMessageAttributes) | ||
{ | ||
// TODO: add logging (event source). | ||
return; | ||
} | ||
|
||
int nextAttributeIndex = attributesCount + 1; | ||
foreach (var param in attributes) | ||
{ | ||
AddAttribute(parameters, originalRequest, param.Key, param.Value, nextAttributeIndex); | ||
nextAttributeIndex++; | ||
} | ||
} | ||
|
||
private static void AddAttribute(ParameterCollection parameters, PublishRequest originalRequest, string name, string value, int attributeIndex) | ||
{ | ||
var prefix = "MessageAttributes.entry." + attributeIndex; | ||
parameters.Add(prefix + ".Name", name); | ||
parameters.Add(prefix + ".Value.DataType", "String"); | ||
parameters.Add(prefix + ".Value.StringValue", value); | ||
|
||
// Add injected attributes to the original request as well. | ||
// This dictionary must be in sync with parameters collection to pass through the MD5 hash matching check. | ||
originalRequest.MessageAttributes.Add(name, new MessageAttributeValue { DataType = "String", StringValue = value }); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
src/OpenTelemetry.Contrib.Instrumentation.AWS/Implementation/SqsRequestContextHelper.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,72 @@ | ||
// <copyright file="SqsRequestContextHelper.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.Collections.Generic; | ||
using System.Linq; | ||
using Amazon.Runtime; | ||
using Amazon.Runtime.Internal; | ||
using Amazon.SQS.Model; | ||
|
||
namespace OpenTelemetry.Contrib.Instrumentation.AWS.Implementation; | ||
internal class SqsRequestContextHelper | ||
{ | ||
// 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; | ||
|
||
internal static void AddAttributes(IRequestContext context, IReadOnlyDictionary<string, string> attributes) | ||
{ | ||
var parameters = context.Request?.ParameterCollection; | ||
var originalRequest = context.OriginalRequest as SendMessageRequest; | ||
if (originalRequest?.MessageAttributes == null || parameters == null) | ||
{ | ||
return; | ||
} | ||
|
||
if (attributes.Keys.Any(k => originalRequest.MessageAttributes.ContainsKey(k))) | ||
{ | ||
// If at least one attribute is already present in the request then we skip the injection. | ||
return; | ||
} | ||
|
||
int attributesCount = originalRequest.MessageAttributes.Count; | ||
if (attributes.Count + attributesCount > MaxMessageAttributes) | ||
{ | ||
// TODO: add logging (event source). | ||
return; | ||
} | ||
|
||
int nextAttributeIndex = attributesCount + 1; | ||
foreach (var param in attributes) | ||
{ | ||
AddAttribute(parameters, originalRequest, param.Key, param.Value, nextAttributeIndex); | ||
nextAttributeIndex++; | ||
} | ||
} | ||
|
||
private static void AddAttribute(ParameterCollection parameters, SendMessageRequest originalRequest, string name, string value, int attributeIndex) | ||
{ | ||
var prefix = "MessageAttribute." + attributeIndex; | ||
parameters.Add(prefix + ".Name", name); | ||
parameters.Add(prefix + ".Value.DataType", "String"); | ||
parameters.Add(prefix + ".Value.StringValue", value); | ||
|
||
// Add injected attributes to the original request as well. | ||
// This dictionary must be in sync with parameters collection to pass through the MD5 hash matching check. | ||
originalRequest.MessageAttributes.Add(name, new 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
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
Oops, something went wrong.