Skip to content

Commit 3ed23f5

Browse files
committed
Added batchsize and batchioperation name info
1 parent aba2b2c commit 3ed23f5

File tree

5 files changed

+51
-3
lines changed

5 files changed

+51
-3
lines changed

Microsoft.Azure.Cosmos/src/Batch/TransactionalBatchResponse.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,27 @@ private static async Task<TransactionalBatchResponse> PopulateFromContentAsync(
392392
return response;
393393
}
394394

395+
internal int GetBatchSize()
396+
{
397+
return this.Operations.Count;
398+
}
399+
400+
internal OperationType? GetBatchOperationName()
401+
{
402+
HashSet<OperationType> operationNames = new ();
403+
foreach (ItemBatchOperation operation in this.Operations)
404+
{
405+
operationNames.Add(operation.OperationType);
406+
}
407+
408+
if (operationNames.Count == 1)
409+
{
410+
return this.Operations[0].OperationType;
411+
}
412+
413+
return null;
414+
}
415+
395416
/// <summary>
396417
/// Disposes the disposable members held by this class.
397418
/// </summary>

Microsoft.Azure.Cosmos/src/Telemetry/OpenTelemetry/OpenTelemetryAttributeKeys.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ internal sealed class OpenTelemetryAttributeKeys
3636
public const string ItemCount = "db.cosmosdb.item_count";
3737
public const string ActivityId = "db.cosmosdb.activity_id";
3838
public const string CorrelatedActivityId = "db.cosmosdb.correlated_activity_id";
39+
public const string BatchSize = "db.cosmosdb.batch_size";
3940

4041
// Exceptions
4142
public const string ExceptionType = "exception.type";

Microsoft.Azure.Cosmos/src/Telemetry/OpenTelemetry/OpenTelemetryAttributes.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,15 @@ internal OpenTelemetryAttributes(RequestMessage requestMessage)
6969
/// OperationType
7070
/// </summary>
7171
internal Documents.OperationType OperationType { get; set; }
72+
73+
/// <summary>
74+
/// Batch Size
75+
/// </summary>
76+
internal int? BatchSize { get; set; }
77+
78+
/// <summary>
79+
/// Will have value for homogeneous batch operation and will be null for heterogeneous batch operation
80+
/// </summary>
81+
internal Documents.OperationType? BatchOperationName { get; set; }
7282
}
7383
}

Microsoft.Azure.Cosmos/src/Telemetry/OpenTelemetry/OpenTelemetryCoreRecorder.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,20 @@ public void Dispose()
222222
OperationType operationType
223223
= (this.response == null || this.response?.OperationType == OperationType.Invalid) ? this.operationType : this.response.OperationType;
224224

225-
this.scope.AddAttribute(OpenTelemetryAttributeKeys.OperationType, Enum.GetName(typeof(OperationType), operationType));
225+
string operationName = Enum.GetName(typeof(OperationType), operationType);
226+
if (this.response.BatchOperationName != null)
227+
{
228+
string batchOpsName = Enum.GetName(typeof(OperationType), this.response.BatchOperationName);
229+
operationName = $"{operationName}.{batchOpsName}";
230+
}
231+
this.scope.AddAttribute(OpenTelemetryAttributeKeys.OperationType, operationName);
226232

227233
if (this.response != null)
228234
{
235+
if (this.response.BatchSize is not null)
236+
{
237+
this.scope.AddIntegerAttribute(OpenTelemetryAttributeKeys.BatchSize, (int)this.response.BatchSize);
238+
}
229239
this.scope.AddAttribute(OpenTelemetryAttributeKeys.RequestContentLength, this.response.RequestContentLength);
230240
this.scope.AddAttribute(OpenTelemetryAttributeKeys.ResponseContentLength, this.response.ResponseContentLength);
231241
this.scope.AddIntegerAttribute(OpenTelemetryAttributeKeys.StatusCode, (int)this.response.StatusCode);

Microsoft.Azure.Cosmos/src/Telemetry/OpenTelemetry/OpenTelemetryResponse.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ internal OpenTelemetryResponse(TransactionalBatchResponse responseMessage)
2222
requestMessage: null,
2323
subStatusCode: OpenTelemetryResponse.GetHeader(responseMessage)?.SubStatusCode,
2424
activityId: OpenTelemetryResponse.GetHeader(responseMessage)?.ActivityId,
25-
correlationId: OpenTelemetryResponse.GetHeader(responseMessage)?.CorrelatedActivityId)
25+
correlationId: OpenTelemetryResponse.GetHeader(responseMessage)?.CorrelatedActivityId,
26+
batchSize: responseMessage.GetBatchSize(),
27+
batchOperationName: responseMessage.GetBatchOperationName())
2628
{
2729
}
2830

@@ -52,7 +54,9 @@ private OpenTelemetryResponse(
5254
Documents.SubStatusCodes? subStatusCode,
5355
string activityId,
5456
string correlationId,
55-
Documents.OperationType operationType = Documents.OperationType.Invalid)
57+
Documents.OperationType operationType = Documents.OperationType.Invalid,
58+
int? batchSize = null,
59+
Documents.OperationType? batchOperationName = null)
5660
: base(requestMessage)
5761
{
5862
this.StatusCode = statusCode;
@@ -64,6 +68,8 @@ private OpenTelemetryResponse(
6468
this.ActivityId = activityId;
6569
this.CorrelatedActivityId = correlationId;
6670
this.OperationType = operationType;
71+
this.BatchSize = batchSize;
72+
this.BatchOperationName = batchOperationName;
6773
}
6874

6975
private static string GetPayloadSize(ResponseMessage response)

0 commit comments

Comments
 (0)