Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create traces using the SDK observability APIs #64

Merged
merged 1 commit into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/Amazon.Extensions.S3.Encryption.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFrameworks>net35;net45;netstandard2.0;netcoreapp3.1</TargetFrameworks>
<Version>2.1.2</Version>
<Version>2.2.0</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageId>Amazon.Extensions.S3.Encryption</PackageId>
<Title>Amazon S3 Encryption Client for .NET</Title>
Expand All @@ -15,8 +15,8 @@
<PackageIcon>icon.png</PackageIcon>
<RepositoryUrl>https://github.com/aws/amazon-s3-encryption-client-dotnet/</RepositoryUrl>
<Company>Amazon Web Services</Company>
<AssemblyVersion>2.1.2</AssemblyVersion>
<FileVersion>2.1.2</FileVersion>
<AssemblyVersion>2.2.0</AssemblyVersion>
<FileVersion>2.2.0</FileVersion>

<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>..\public.snk</AssemblyOriginatorKeyFile>
Expand All @@ -41,9 +41,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AWSSDK.Core" Version="3.7.303.14" />
<PackageReference Include="AWSSDK.S3" Version="3.7.307.15" />
<PackageReference Include="AWSSDK.KeyManagementService" Version="3.7.301.15" />
<PackageReference Include="AWSSDK.Core" Version="3.7.400.32" />
<PackageReference Include="AWSSDK.S3" Version="3.7.404.4" />
<PackageReference Include="AWSSDK.KeyManagementService" Version="3.7.400.32" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net35'">
Expand Down
83 changes: 45 additions & 38 deletions src/Internal/SetupDecryptionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Amazon.Runtime.Internal;
using Amazon.S3;
using GetObjectResponse = Amazon.S3.Model.GetObjectResponse;
using Amazon.Extensions.S3.Encryption.Util;

namespace Amazon.Extensions.S3.Encryption.Internal
{
Expand Down Expand Up @@ -50,41 +51,44 @@ public override void InvokeSync(IExecutionContext executionContext)
/// <param name="executionContext"></param>
protected void PostInvoke(IExecutionContext executionContext)
{
byte[] encryptedKMSEnvelopeKey;
Dictionary<string, string> encryptionContext;
byte[] decryptedEnvelopeKeyKMS = null;

if (KMSEnvelopeKeyIsPresent(executionContext, out encryptedKMSEnvelopeKey, out encryptionContext))
using (TelemetryUtilities.CreateSpan(EncryptionClient, Constants.SetupDecryptionHandlerSpanName, null, Amazon.Runtime.Telemetry.Tracing.SpanKind.CLIENT))
{
byte[] encryptedKMSEnvelopeKey;
Dictionary<string, string> encryptionContext;
byte[] decryptedEnvelopeKeyKMS = null;

if (KMSEnvelopeKeyIsPresent(executionContext, out encryptedKMSEnvelopeKey, out encryptionContext))
{
#if BCL
decryptedEnvelopeKeyKMS = DecryptedEnvelopeKeyKms(encryptedKMSEnvelopeKey, encryptionContext);
#else
decryptedEnvelopeKeyKMS = DecryptedEnvelopeKeyKmsAsync(encryptedKMSEnvelopeKey, encryptionContext).GetAwaiter().GetResult();
decryptedEnvelopeKeyKMS = DecryptedEnvelopeKeyKmsAsync(encryptedKMSEnvelopeKey, encryptionContext).GetAwaiter().GetResult();
#endif
}
}

var getObjectResponse = executionContext.ResponseContext.Response as GetObjectResponse;
if (getObjectResponse != null)
{
var getObjectResponse = executionContext.ResponseContext.Response as GetObjectResponse;
if (getObjectResponse != null)
{
#if BCL
DecryptObject(decryptedEnvelopeKeyKMS, getObjectResponse);
#else
DecryptObjectAsync(decryptedEnvelopeKeyKMS, getObjectResponse).GetAwaiter().GetResult();
DecryptObjectAsync(decryptedEnvelopeKeyKMS, getObjectResponse).GetAwaiter().GetResult();
#endif
}
}

var completeMultiPartUploadRequest = executionContext.RequestContext.Request.OriginalRequest as CompleteMultipartUploadRequest;
var completeMultipartUploadResponse = executionContext.ResponseContext.Response as CompleteMultipartUploadResponse;
if (completeMultipartUploadResponse != null)
{
var completeMultiPartUploadRequest = executionContext.RequestContext.Request.OriginalRequest as CompleteMultipartUploadRequest;
var completeMultipartUploadResponse = executionContext.ResponseContext.Response as CompleteMultipartUploadResponse;
if (completeMultipartUploadResponse != null)
{
#if BCL
CompleteMultipartUpload(completeMultiPartUploadRequest);
#else
CompleteMultipartUploadAsync(completeMultiPartUploadRequest).GetAwaiter().GetResult();
CompleteMultipartUploadAsync(completeMultiPartUploadRequest).GetAwaiter().GetResult();
#endif
}
}

PostInvokeSynchronous(executionContext, decryptedEnvelopeKeyKMS);
PostInvokeSynchronous(executionContext, decryptedEnvelopeKeyKMS);
}
}

#if BCL
Expand Down Expand Up @@ -130,29 +134,32 @@ public override async System.Threading.Tasks.Task<T> InvokeAsync<T>(IExecutionCo
/// <param name="executionContext">The execution context, it contains the request and response context.</param>
protected async System.Threading.Tasks.Task PostInvokeAsync(IExecutionContext executionContext)
{
byte[] encryptedKMSEnvelopeKey;
Dictionary<string, string> encryptionContext;
byte[] decryptedEnvelopeKeyKMS = null;

if (KMSEnvelopeKeyIsPresent(executionContext, out encryptedKMSEnvelopeKey, out encryptionContext))
using (TelemetryUtilities.CreateSpan(EncryptionClient, Constants.SetupDecryptionHandlerSpanName, null, Amazon.Runtime.Telemetry.Tracing.SpanKind.CLIENT))
{
decryptedEnvelopeKeyKMS = await DecryptedEnvelopeKeyKmsAsync(encryptedKMSEnvelopeKey, encryptionContext).ConfigureAwait(false);
}
byte[] encryptedKMSEnvelopeKey;
Dictionary<string, string> encryptionContext;
byte[] decryptedEnvelopeKeyKMS = null;

var getObjectResponse = executionContext.ResponseContext.Response as GetObjectResponse;
if (getObjectResponse != null)
{
await DecryptObjectAsync(decryptedEnvelopeKeyKMS, getObjectResponse).ConfigureAwait(false);
}
if (KMSEnvelopeKeyIsPresent(executionContext, out encryptedKMSEnvelopeKey, out encryptionContext))
{
decryptedEnvelopeKeyKMS = await DecryptedEnvelopeKeyKmsAsync(encryptedKMSEnvelopeKey, encryptionContext).ConfigureAwait(false);
}

var completeMultiPartUploadRequest = executionContext.RequestContext.Request.OriginalRequest as CompleteMultipartUploadRequest;
var completeMultipartUploadResponse = executionContext.ResponseContext.Response as CompleteMultipartUploadResponse;
if (completeMultipartUploadResponse != null)
{
await CompleteMultipartUploadAsync(completeMultiPartUploadRequest).ConfigureAwait(false);
}
var getObjectResponse = executionContext.ResponseContext.Response as GetObjectResponse;
if (getObjectResponse != null)
{
await DecryptObjectAsync(decryptedEnvelopeKeyKMS, getObjectResponse).ConfigureAwait(false);
}

PostInvokeSynchronous(executionContext, decryptedEnvelopeKeyKMS);
var completeMultiPartUploadRequest = executionContext.RequestContext.Request.OriginalRequest as CompleteMultipartUploadRequest;
var completeMultipartUploadResponse = executionContext.ResponseContext.Response as CompleteMultipartUploadResponse;
if (completeMultipartUploadResponse != null)
{
await CompleteMultipartUploadAsync(completeMultiPartUploadRequest).ConfigureAwait(false);
}

PostInvokeSynchronous(executionContext, decryptedEnvelopeKeyKMS);
}
}

/// <summary>
Expand Down
38 changes: 22 additions & 16 deletions src/Internal/SetupEncryptionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Amazon.Runtime;
using Amazon.S3.Model;
using Amazon.Runtime.Internal;
using Amazon.Extensions.S3.Encryption.Util;

namespace Amazon.Extensions.S3.Encryption.Internal
{
Expand Down Expand Up @@ -49,19 +50,22 @@ protected void PreInvoke(IExecutionContext executionContext)
{
ThrowIfRangeGet(executionContext);

var instructions = GenerateInstructions(executionContext);

var putObjectRequest = executionContext.RequestContext.OriginalRequest as PutObjectRequest;
if (putObjectRequest != null)
using (TelemetryUtilities.CreateSpan(EncryptionClient, Constants.SetupEncryptionHandlerSpanName, null, Amazon.Runtime.Telemetry.Tracing.SpanKind.CLIENT))
{
var instructions = GenerateInstructions(executionContext);

var putObjectRequest = executionContext.RequestContext.OriginalRequest as PutObjectRequest;
if (putObjectRequest != null)
{
#if BCL
EncryptObject(instructions, putObjectRequest);
EncryptObject(instructions, putObjectRequest);
#else
EncryptObjectAsync(instructions, putObjectRequest).GetAwaiter().GetResult();
EncryptObjectAsync(instructions, putObjectRequest).GetAwaiter().GetResult();
#endif
}
}

PreInvokeSynchronous(executionContext, instructions);
PreInvokeSynchronous(executionContext, instructions);
}
}

#if BCL
Expand Down Expand Up @@ -125,18 +129,20 @@ public override async System.Threading.Tasks.Task<T> InvokeAsync<T>(IExecutionCo
protected async System.Threading.Tasks.Task PreInvokeAsync(IExecutionContext executionContext)
{
ThrowIfRangeGet(executionContext);
using (TelemetryUtilities.CreateSpan(EncryptionClient, Constants.SetupEncryptionHandlerSpanName, null, Amazon.Runtime.Telemetry.Tracing.SpanKind.CLIENT))
{
EncryptionInstructions instructions = await GenerateInstructionsAsync(executionContext).ConfigureAwait(false);

EncryptionInstructions instructions = await GenerateInstructionsAsync(executionContext).ConfigureAwait(false);
var request = executionContext.RequestContext.OriginalRequest;

var request = executionContext.RequestContext.OriginalRequest;
var putObjectRequest = request as PutObjectRequest;
if (putObjectRequest != null)
{
await EncryptObjectAsync(instructions, putObjectRequest).ConfigureAwait(false);
}

var putObjectRequest = request as PutObjectRequest;
if (putObjectRequest != null)
{
await EncryptObjectAsync(instructions, putObjectRequest).ConfigureAwait(false);
PreInvokeSynchronous(executionContext, instructions);
}

PreInvokeSynchronous(executionContext, instructions);
}

private async System.Threading.Tasks.Task EncryptObjectAsync(EncryptionInstructions instructions, PutObjectRequest putObjectRequest)
Expand Down
5 changes: 5 additions & 0 deletions src/Util/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,10 @@ namespace Amazon.Extensions.S3.Encryption.Util
internal class Constants
{
internal const string S3CryptoStreamRequestState = "S3-Crypto-Stream";


internal const string S3TransferTracerScope = "S3.Encryption";
internal const string SetupEncryptionHandlerSpanName = "EncryptionHandler";
internal const string SetupDecryptionHandlerSpanName = "DecryptionHandler";
}
}
43 changes: 43 additions & 0 deletions src/Util/TelemetryUtilities.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Amazon.S3.Transfer;
using Amazon.Runtime.Telemetry.Tracing;
using Amazon.Runtime.Telemetry;
using Attributes = Amazon.Runtime.Telemetry.Attributes;
using Amazon.S3;

namespace Amazon.Extensions.S3.Encryption.Util
{
internal static class TelemetryUtilities
{
/// <summary>
/// Creates a new span with the required attributes.
/// </summary>
/// <param name="operationName">The name of the operation from which to create the span name.</param>
/// <param name="initialAttributes">Optional initial set of attributes for the span.</param>
/// <param name="spanKind">Optional type of span to create.</param>
/// <param name="parentContext">Optional parent context for the span.</param>
/// <returns>A <see cref="TraceSpan"/> instance representing the created span.</returns>
internal static TraceSpan CreateSpan(
AmazonS3Client client,
string operationName,
Attributes initialAttributes = null,
SpanKind spanKind = SpanKind.INTERNAL,
SpanContext parentContext = null)
{
if (initialAttributes == null)
initialAttributes = new Attributes();

initialAttributes.Set(TelemetryConstants.MethodAttributeKey, operationName);

initialAttributes.Set(TelemetryConstants.SystemAttributeKey, TelemetryConstants.SystemAttributeValue);
initialAttributes.Set(TelemetryConstants.ServiceAttributeKey, Constants.S3TransferTracerScope);

var spanName = $"{nameof(TransferUtility)}.{operationName}";

var tracerProvider = client.Config.TelemetryProvider.TracerProvider;

var tracer = tracerProvider.GetTracer($"{TelemetryConstants.TelemetryScopePrefix}.{Constants.S3TransferTracerScope}");

return tracer.CreateSpan(spanName, initialAttributes, spanKind, parentContext);
}
}
}