Skip to content

Commit

Permalink
feat: Add observability to V4 DynamoDB HLL
Browse files Browse the repository at this point in the history
  • Loading branch information
96malhar authored Nov 1, 2024
1 parent e34641f commit 15400fa
Show file tree
Hide file tree
Showing 36 changed files with 1,337 additions and 389 deletions.
5 changes: 5 additions & 0 deletions sdk/src/Services/DynamoDBv2/Custom/DataModel/AsyncSearch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/

using Amazon.DynamoDBv2.DocumentModel;
using Amazon.Runtime.Telemetry.Tracing;

namespace Amazon.DynamoDBv2.DataModel
{
Expand Down Expand Up @@ -46,6 +47,8 @@ public partial class AsyncSearch<T> : IAsyncSearch<T>
private DynamoDBContext _sourceContext { get; set; }
private DynamoDBFlatConfig _config { get; set; }

internal TracerProvider TracerProvider { get; set; }

/// <summary>
/// This constructor is used for mocking. Users that want to mock AsyncSearch can create a subclass of AsyncSearch and make a public parameterless constructor.
/// </summary>
Expand All @@ -59,6 +62,8 @@ internal AsyncSearch(DynamoDBContext source, DynamoDBContext.ContextSearch conte
_sourceContext = source;
_documentSearch = contextSearch.Search;
_config = contextSearch.FlatConfig;
TracerProvider = source?.Client?.Config?.TelemetryProvider?.TracerProvider
?? AWSConfigs.TelemetryProvider.TracerProvider;
}

/// <inheritdoc/>
Expand Down
24 changes: 24 additions & 0 deletions sdk/src/Services/DynamoDBv2/Custom/DataModel/BatchGet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
using System;
using System.Collections.Generic;
using Amazon.DynamoDBv2.DocumentModel;
using Amazon.Runtime.Telemetry.Tracing;


#if AWS_ASYNC_API
using System.Threading.Tasks;
Expand Down Expand Up @@ -105,6 +107,8 @@ public abstract partial class BatchGet : IBatchGet
{
internal DocumentBatchGet DocumentBatch { get; set; }

internal TracerProvider TracerProvider { get; set; }

internal abstract void CreateDocumentBatch();

internal abstract void PopulateResults(List<Document> items);
Expand Down Expand Up @@ -167,6 +171,8 @@ internal BatchGet(DynamoDBContext context, DynamoDBFlatConfig config)
_context = context;
_config = config;
_itemStorageConfig = context.StorageConfigCache.GetConfig<T>(config);
TracerProvider = context?.Client?.Config?.TelemetryProvider?.TracerProvider
?? AWSConfigs.TelemetryProvider.TracerProvider;
}

private void ExecuteHelper()
Expand Down Expand Up @@ -239,6 +245,8 @@ public partial class MultiTableBatchGet : IMultiTableBatchGet
{
private List<IBatchGet> allBatches = new List<IBatchGet>();

internal TracerProvider TracerProvider { get; set; }

/// <summary>
/// Constructs a MultiTableBatchGet object from a number of
/// BatchGet objects
Expand All @@ -247,13 +255,15 @@ public partial class MultiTableBatchGet : IMultiTableBatchGet
public MultiTableBatchGet(params IBatchGet[] batches)
{
allBatches = new List<IBatchGet>(batches);
TracerProvider = GetTracerProvider(allBatches);
}

internal MultiTableBatchGet(IBatchGet first, params IBatchGet[] rest)
{
allBatches = new List<IBatchGet>();
allBatches.Add(first);
allBatches.AddRange(rest);
TracerProvider = GetTracerProvider(allBatches);
}

/// <inheritdoc/>
Expand Down Expand Up @@ -317,5 +327,19 @@ private async Task ExecuteHelperAsync(CancellationToken cancellationToken)
}
}
#endif

private TracerProvider GetTracerProvider(List<IBatchGet> allBatches)
{
var tracerProvider = AWSConfigs.TelemetryProvider.TracerProvider;
if (allBatches.Count > 0)
{
var firstBatch = allBatches[0];
if (firstBatch is BatchGet batchGet)
{
tracerProvider = batchGet.TracerProvider;
}
}
return tracerProvider;
}
}
}
25 changes: 25 additions & 0 deletions sdk/src/Services/DynamoDBv2/Custom/DataModel/BatchWrite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
using System.Collections.Generic;
using Amazon.DynamoDBv2.DocumentModel;
using System.Globalization;
using Amazon.Runtime.Telemetry.Tracing;

#if AWS_ASYNC_API
using System.Threading.Tasks;
#endif
Expand Down Expand Up @@ -96,6 +98,8 @@ public interface IBatchWrite<T> : IBatchWrite
public abstract partial class BatchWrite : IBatchWrite
{
internal DocumentBatchWrite DocumentBatch { get; set; }

internal TracerProvider TracerProvider { get; set; }
}

/// <summary>
Expand Down Expand Up @@ -132,6 +136,9 @@ internal BatchWrite(DynamoDBContext context, Type valuesType, DynamoDBFlatConfig
// Table.CreateBatchWrite() returns the IDocumentBatchWrite interface.
// But since we rely on the internal behavior of DocumentBatchWrite, we instantiate it via the constructor.
DocumentBatch = new DocumentBatchWrite(table);

TracerProvider = context?.Client?.Config?.TelemetryProvider?.TracerProvider
?? AWSConfigs.TelemetryProvider.TracerProvider;
}

/// <inheritdoc/>
Expand Down Expand Up @@ -228,6 +235,8 @@ public partial class MultiTableBatchWrite : IMultiTableBatchWrite
{
private List<IBatchWrite> allBatches = new();

internal TracerProvider TracerProvider { get; set; }

/// <summary>
/// Constructs a MultiTableBatchWrite object from a number of
/// BatchWrite objects
Expand All @@ -236,13 +245,15 @@ public partial class MultiTableBatchWrite : IMultiTableBatchWrite
public MultiTableBatchWrite(params IBatchWrite[] batches)
{
allBatches = new List<IBatchWrite>(batches);
TracerProvider = GetTracerProvider(allBatches);
}

internal MultiTableBatchWrite(IBatchWrite first, params IBatchWrite[] rest)
{
allBatches = new List<IBatchWrite>();
allBatches.Add(first);
allBatches.AddRange(rest);
TracerProvider = GetTracerProvider(allBatches);
}

/// <inheritdoc/>
Expand Down Expand Up @@ -276,5 +287,19 @@ private Task ExecuteHelperAsync(CancellationToken cancellationToken)
return superBatch.ExecuteHelperAsync(cancellationToken);
}
#endif

private TracerProvider GetTracerProvider(List<IBatchWrite> allBatches)
{
var tracerProvider = AWSConfigs.TelemetryProvider.TracerProvider;
if (allBatches.Count > 0)
{
var firstBatch = allBatches[0];
if (firstBatch is BatchWrite batchWrite)
{
tracerProvider = batchWrite.TracerProvider;
}
}
return tracerProvider;
}
}
}
23 changes: 23 additions & 0 deletions sdk/src/Services/DynamoDBv2/Custom/DataModel/TransactGet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using System.Threading.Tasks;
#endif
using Amazon.DynamoDBv2.DocumentModel;
using Amazon.Runtime.Telemetry.Tracing;

namespace Amazon.DynamoDBv2.DataModel
{
Expand Down Expand Up @@ -97,6 +98,8 @@ public abstract partial class TransactGet : ITransactGet
{
internal DocumentTransactGet DocumentTransaction { get; set; }

internal TracerProvider TracerProvider { get; set; }

internal abstract void PopulateResults();

/// <inheritdoc/>
Expand Down Expand Up @@ -161,6 +164,9 @@ internal TransactGet(DynamoDBContext context, DynamoDBFlatConfig config)
// Table.CreateTransactGet() returns the IDocumentTransactGet interface.
// But since we rely on the internal behavior of DocumentTransactGet, we instantiate it via the constructor.
DocumentTransaction = new DocumentTransactGet(table);

TracerProvider = context?.Client?.Config?.TelemetryProvider?.TracerProvider
?? AWSConfigs.TelemetryProvider.TracerProvider;
}

private void ExecuteHelper()
Expand Down Expand Up @@ -211,6 +217,8 @@ public partial class MultiTableTransactGet : IMultiTableTransactGet
{
private readonly List<ITransactGet> allTransactionParts;

internal TracerProvider TracerProvider { get; set; }

/// <summary>
/// Constructs a MultiTableTransactGet object from a number of
/// TransactGet objects.
Expand All @@ -219,13 +227,15 @@ public partial class MultiTableTransactGet : IMultiTableTransactGet
public MultiTableTransactGet(params ITransactGet[] transactionParts)
{
allTransactionParts = new List<ITransactGet>(transactionParts);
TracerProvider = GetTracerProvider(allTransactionParts);
}

internal MultiTableTransactGet(ITransactGet first, params ITransactGet[] rest)
{
allTransactionParts = new List<ITransactGet>();
allTransactionParts.Add(first);
allTransactionParts.AddRange(rest);
TracerProvider = GetTracerProvider(allTransactionParts);
}

/// <inheritdoc/>
Expand Down Expand Up @@ -269,5 +279,18 @@ private async Task ExecuteHelperAsync(CancellationToken cancellationToken)
}
}
#endif
private TracerProvider GetTracerProvider(List<ITransactGet> allTransactionParts)
{
var tracerProvider = AWSConfigs.TelemetryProvider.TracerProvider;
if (allTransactionParts.Count > 0)
{
var firstTransactionPart = allTransactionParts[0];
if (firstTransactionPart is TransactGet transactGet)
{
tracerProvider = transactGet.TracerProvider;
}
}
return tracerProvider;
}
}
}
24 changes: 24 additions & 0 deletions sdk/src/Services/DynamoDBv2/Custom/DataModel/TransactWrite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using System.Threading.Tasks;
#endif
using Amazon.DynamoDBv2.DocumentModel;
using Amazon.Runtime.Telemetry.Tracing;

namespace Amazon.DynamoDBv2.DataModel
{
Expand Down Expand Up @@ -165,6 +166,8 @@ public abstract partial class TransactWrite : ITransactWrite
{
internal DocumentTransactWrite DocumentTransaction { get; set; }

internal TracerProvider TracerProvider { get; set; }

internal abstract void PopulateObjects();
}

Expand Down Expand Up @@ -192,6 +195,9 @@ internal TransactWrite(DynamoDBContext context, DynamoDBFlatConfig config)
// table.CreateTransactWrite() return the IDocumentTransactWrite interface.
// But since we rely on the internal behavior of DocumentTransactWrite, we instatiate it via the constructor.
DocumentTransaction = new DocumentTransactWrite(table);

TracerProvider = context?.Client?.Config?.TelemetryProvider?.TracerProvider
?? AWSConfigs.TelemetryProvider.TracerProvider;
}

/// <inheritdoc/>
Expand Down Expand Up @@ -457,6 +463,8 @@ public partial class MultiTableTransactWrite : IMultiTableTransactWrite
{
private readonly List<ITransactWrite> allTransactionParts;

internal TracerProvider TracerProvider { get; set; }

/// <summary>
/// Constructs a MultiTableTransactWrite object from a number of
/// TransactWrite objects
Expand All @@ -465,13 +473,15 @@ public partial class MultiTableTransactWrite : IMultiTableTransactWrite
public MultiTableTransactWrite(params ITransactWrite[] transactionParts)
{
allTransactionParts = new List<ITransactWrite>(transactionParts);
TracerProvider = GetTracerProvider(allTransactionParts);
}

internal MultiTableTransactWrite(ITransactWrite first, params ITransactWrite[] rest)
{
allTransactionParts = new List<ITransactWrite>();
allTransactionParts.Add(first);
allTransactionParts.AddRange(rest);
TracerProvider = GetTracerProvider(allTransactionParts);
}

/// <inheritdoc/>
Expand Down Expand Up @@ -514,6 +524,20 @@ private async Task ExecuteHelperAsync(CancellationToken cancellationToken)
abstractTransactWrite.PopulateObjects();
}
}

private TracerProvider GetTracerProvider(List<ITransactWrite> allTransactionParts)
{
var tracerProvider = AWSConfigs.TelemetryProvider.TracerProvider;
if (allTransactionParts.Count > 0)
{
var firstTransactionPart = allTransactionParts[0];
if (firstTransactionPart is TransactWrite transactWrite)
{
tracerProvider = transactWrite.TracerProvider;
}
}
return tracerProvider;
}
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* permissions and limitations under the License.
*/

using Amazon.Runtime.Telemetry.Tracing;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
Expand Down Expand Up @@ -52,17 +53,25 @@ public partial class AsyncSearch<T> : IAsyncSearch<T>
/// <inheritdoc/>
public virtual async Task<List<T>> GetNextSetAsync(CancellationToken cancellationToken = default(CancellationToken))
{
var documents = await _documentSearch.GetNextSetHelperAsync(cancellationToken).ConfigureAwait(false);
List<T> items = _sourceContext.FromDocumentsHelper<T>(documents, this._config).ToList();
return items;
var operationName = DynamoDBTelemetry.ExtractOperationName(nameof(AsyncSearch<T>), nameof(GetNextSetAsync));
using (DynamoDBTelemetry.CreateSpan(TracerProvider, operationName, spanKind: SpanKind.CLIENT))
{
var documents = await _documentSearch.GetNextSetHelperAsync(cancellationToken).ConfigureAwait(false);
List<T> items = _sourceContext.FromDocumentsHelper<T>(documents, this._config).ToList();
return items;
}
}

/// <inheritdoc/>
public virtual async Task<List<T>> GetRemainingAsync(CancellationToken cancellationToken = default(CancellationToken))
{
var documents = await _documentSearch.GetRemainingHelperAsync(cancellationToken).ConfigureAwait(false);
List<T> items = _sourceContext.FromDocumentsHelper<T>(documents, this._config).ToList();
return items;
var operationName = DynamoDBTelemetry.ExtractOperationName(nameof(AsyncSearch<T>), nameof(GetRemainingAsync));
using (DynamoDBTelemetry.CreateSpan(TracerProvider, operationName, spanKind: SpanKind.CLIENT))
{
var documents = await _documentSearch.GetRemainingHelperAsync(cancellationToken).ConfigureAwait(false);
List<T> items = _sourceContext.FromDocumentsHelper<T>(documents, this._config).ToList();
return items;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
#pragma warning disable 1574

using Amazon.Runtime.Telemetry.Tracing;
using System.Threading;
using System.Threading.Tasks;

Expand All @@ -39,9 +40,13 @@ public abstract partial class BatchGet
public partial class BatchGet<T> : BatchGet, IBatchGet<T>
{
/// <inheritdoc/>
public override Task ExecuteAsync(CancellationToken cancellationToken = default(CancellationToken))
public override async Task ExecuteAsync(CancellationToken cancellationToken = default(CancellationToken))
{
return ExecuteHelperAsync(cancellationToken);
var operationName = DynamoDBTelemetry.ExtractOperationName(nameof(BatchGet), nameof(ExecuteAsync));
using (DynamoDBTelemetry.CreateSpan(TracerProvider, operationName, spanKind: SpanKind.CLIENT))
{
await ExecuteHelperAsync(cancellationToken).ConfigureAwait(false);
}
}
}

Expand All @@ -60,9 +65,13 @@ public partial interface IMultiTableBatchGet
public partial class MultiTableBatchGet : IMultiTableBatchGet
{
/// <inheritdoc/>
public Task ExecuteAsync(CancellationToken cancellationToken = default(CancellationToken))
public async Task ExecuteAsync(CancellationToken cancellationToken = default(CancellationToken))
{
return ExecuteHelperAsync(cancellationToken);
var operationName = DynamoDBTelemetry.ExtractOperationName(nameof(MultiTableBatchGet), nameof(ExecuteAsync));
using (DynamoDBTelemetry.CreateSpan(TracerProvider, operationName, spanKind: SpanKind.CLIENT))
{
await ExecuteHelperAsync(cancellationToken).ConfigureAwait(false);
}
}
}
}
Loading

0 comments on commit 15400fa

Please sign in to comment.