Skip to content

Commit

Permalink
Added batchsize and batchioperation name info
Browse files Browse the repository at this point in the history
  • Loading branch information
sourabh1007 committed Aug 2, 2024
1 parent aba2b2c commit 3ed23f5
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 3 deletions.
21 changes: 21 additions & 0 deletions Microsoft.Azure.Cosmos/src/Batch/TransactionalBatchResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,27 @@ private static async Task<TransactionalBatchResponse> PopulateFromContentAsync(
return response;
}

internal int GetBatchSize()
{
return this.Operations.Count;
}

internal OperationType? GetBatchOperationName()
{
HashSet<OperationType> operationNames = new ();
foreach (ItemBatchOperation operation in this.Operations)
{
operationNames.Add(operation.OperationType);
}

if (operationNames.Count == 1)
{
return this.Operations[0].OperationType;
}

return null;
}

/// <summary>
/// Disposes the disposable members held by this class.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ internal sealed class OpenTelemetryAttributeKeys
public const string ItemCount = "db.cosmosdb.item_count";
public const string ActivityId = "db.cosmosdb.activity_id";
public const string CorrelatedActivityId = "db.cosmosdb.correlated_activity_id";
public const string BatchSize = "db.cosmosdb.batch_size";

// Exceptions
public const string ExceptionType = "exception.type";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,15 @@ internal OpenTelemetryAttributes(RequestMessage requestMessage)
/// OperationType
/// </summary>
internal Documents.OperationType OperationType { get; set; }

/// <summary>
/// Batch Size
/// </summary>
internal int? BatchSize { get; set; }

/// <summary>
/// Will have value for homogeneous batch operation and will be null for heterogeneous batch operation
/// </summary>
internal Documents.OperationType? BatchOperationName { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,20 @@ public void Dispose()
OperationType operationType
= (this.response == null || this.response?.OperationType == OperationType.Invalid) ? this.operationType : this.response.OperationType;

this.scope.AddAttribute(OpenTelemetryAttributeKeys.OperationType, Enum.GetName(typeof(OperationType), operationType));
string operationName = Enum.GetName(typeof(OperationType), operationType);
if (this.response.BatchOperationName != null)
{
string batchOpsName = Enum.GetName(typeof(OperationType), this.response.BatchOperationName);
operationName = $"{operationName}.{batchOpsName}";
}
this.scope.AddAttribute(OpenTelemetryAttributeKeys.OperationType, operationName);

if (this.response != null)
{
if (this.response.BatchSize is not null)
{
this.scope.AddIntegerAttribute(OpenTelemetryAttributeKeys.BatchSize, (int)this.response.BatchSize);
}
this.scope.AddAttribute(OpenTelemetryAttributeKeys.RequestContentLength, this.response.RequestContentLength);
this.scope.AddAttribute(OpenTelemetryAttributeKeys.ResponseContentLength, this.response.ResponseContentLength);
this.scope.AddIntegerAttribute(OpenTelemetryAttributeKeys.StatusCode, (int)this.response.StatusCode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ internal OpenTelemetryResponse(TransactionalBatchResponse responseMessage)
requestMessage: null,
subStatusCode: OpenTelemetryResponse.GetHeader(responseMessage)?.SubStatusCode,
activityId: OpenTelemetryResponse.GetHeader(responseMessage)?.ActivityId,
correlationId: OpenTelemetryResponse.GetHeader(responseMessage)?.CorrelatedActivityId)
correlationId: OpenTelemetryResponse.GetHeader(responseMessage)?.CorrelatedActivityId,
batchSize: responseMessage.GetBatchSize(),
batchOperationName: responseMessage.GetBatchOperationName())
{
}

Expand Down Expand Up @@ -52,7 +54,9 @@ private OpenTelemetryResponse(
Documents.SubStatusCodes? subStatusCode,
string activityId,
string correlationId,
Documents.OperationType operationType = Documents.OperationType.Invalid)
Documents.OperationType operationType = Documents.OperationType.Invalid,
int? batchSize = null,
Documents.OperationType? batchOperationName = null)
: base(requestMessage)
{
this.StatusCode = statusCode;
Expand All @@ -64,6 +68,8 @@ private OpenTelemetryResponse(
this.ActivityId = activityId;
this.CorrelatedActivityId = correlationId;
this.OperationType = operationType;
this.BatchSize = batchSize;
this.BatchOperationName = batchOperationName;
}

private static string GetPayloadSize(ResponseMessage response)
Expand Down

0 comments on commit 3ed23f5

Please sign in to comment.