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 c2b9be8
Show file tree
Hide file tree
Showing 6 changed files with 63 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,22 @@ 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);
this.scope.AddAttribute(OpenTelemetryAttributeKeys.OperationType, operationName);

if (this.response != null)
{
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.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
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@
<EVENT name="ThresholdViolation" />
<EVENT name="ThresholdViolation" />
<EVENT name="ThresholdViolation" />
<EVENT name="FailedRequest" />
</OTelActivities></Output>
</Result>
<Result>
Expand Down Expand Up @@ -690,6 +691,7 @@
<EVENT name="ThresholdViolation" />
<EVENT name="ThresholdViolation" />
<EVENT name="ThresholdViolation" />
<EVENT name="FailedRequest" />
</OTelActivities></Output>
</Result>
<Result>
Expand Down Expand Up @@ -1027,6 +1029,7 @@
<EVENT name="ThresholdViolation" />
<EVENT name="ThresholdViolation" />
<EVENT name="ThresholdViolation" />
<EVENT name="FailedRequest" />
</OTelActivities></Output>
</Result>
<Result>
Expand Down Expand Up @@ -1364,6 +1367,7 @@
<EVENT name="ThresholdViolation" />
<EVENT name="ThresholdViolation" />
<EVENT name="ThresholdViolation" />
<EVENT name="FailedRequest" />
</OTelActivities></Output>
</Result>
<Result>
Expand Down Expand Up @@ -1701,6 +1705,7 @@
<EVENT name="ThresholdViolation" />
<EVENT name="ThresholdViolation" />
<EVENT name="ThresholdViolation" />
<EVENT name="FailedRequest" />
</OTelActivities></Output>
</Result>
<Result>
Expand Down Expand Up @@ -2038,6 +2043,7 @@
<EVENT name="ThresholdViolation" />
<EVENT name="ThresholdViolation" />
<EVENT name="ThresholdViolation" />
<EVENT name="FailedRequest" />
</OTelActivities></Output>
</Result>
<Result>
Expand Down Expand Up @@ -2375,6 +2381,7 @@
<EVENT name="ThresholdViolation" />
<EVENT name="ThresholdViolation" />
<EVENT name="ThresholdViolation" />
<EVENT name="FailedRequest" />
</OTelActivities></Output>
</Result>
<Result>
Expand Down Expand Up @@ -2712,6 +2719,7 @@
<EVENT name="ThresholdViolation" />
<EVENT name="ThresholdViolation" />
<EVENT name="ThresholdViolation" />
<EVENT name="FailedRequest" />
</OTelActivities></Output>
</Result>
<Result>
Expand Down Expand Up @@ -3049,6 +3057,7 @@
<EVENT name="ThresholdViolation" />
<EVENT name="ThresholdViolation" />
<EVENT name="ThresholdViolation" />
<EVENT name="FailedRequest" />
</OTelActivities></Output>
</Result>
<Result>
Expand Down Expand Up @@ -3386,6 +3395,7 @@
<EVENT name="ThresholdViolation" />
<EVENT name="ThresholdViolation" />
<EVENT name="ThresholdViolation" />
<EVENT name="FailedRequest" />
</OTelActivities></Output>
</Result>
<Result>
Expand Down

0 comments on commit c2b9be8

Please sign in to comment.