diff --git a/Microsoft.Azure.Cosmos/src/Batch/TransactionalBatchResponse.cs b/Microsoft.Azure.Cosmos/src/Batch/TransactionalBatchResponse.cs index e594644731..fdfd40f338 100644 --- a/Microsoft.Azure.Cosmos/src/Batch/TransactionalBatchResponse.cs +++ b/Microsoft.Azure.Cosmos/src/Batch/TransactionalBatchResponse.cs @@ -392,6 +392,27 @@ private static async Task PopulateFromContentAsync( return response; } + internal int GetBatchSize() + { + return this.Operations.Count; + } + + internal OperationType? GetBatchOperationName() + { + HashSet operationNames = new (); + foreach (ItemBatchOperation operation in this.Operations) + { + operationNames.Add(operation.OperationType); + } + + if (operationNames.Count == 1) + { + return this.Operations[0].OperationType; + } + + return null; + } + /// /// Disposes the disposable members held by this class. /// diff --git a/Microsoft.Azure.Cosmos/src/Telemetry/OpenTelemetry/OpenTelemetryAttributeKeys.cs b/Microsoft.Azure.Cosmos/src/Telemetry/OpenTelemetry/OpenTelemetryAttributeKeys.cs index 9e71e5edbe..c34967420c 100644 --- a/Microsoft.Azure.Cosmos/src/Telemetry/OpenTelemetry/OpenTelemetryAttributeKeys.cs +++ b/Microsoft.Azure.Cosmos/src/Telemetry/OpenTelemetry/OpenTelemetryAttributeKeys.cs @@ -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"; diff --git a/Microsoft.Azure.Cosmos/src/Telemetry/OpenTelemetry/OpenTelemetryAttributes.cs b/Microsoft.Azure.Cosmos/src/Telemetry/OpenTelemetry/OpenTelemetryAttributes.cs index 70b6cea4ef..713f17cf1b 100644 --- a/Microsoft.Azure.Cosmos/src/Telemetry/OpenTelemetry/OpenTelemetryAttributes.cs +++ b/Microsoft.Azure.Cosmos/src/Telemetry/OpenTelemetry/OpenTelemetryAttributes.cs @@ -69,5 +69,15 @@ internal OpenTelemetryAttributes(RequestMessage requestMessage) /// OperationType /// internal Documents.OperationType OperationType { get; set; } + + /// + /// Batch Size + /// + internal int? BatchSize { get; set; } + + /// + /// Will have value for homogeneous batch operation and will be null for heterogeneous batch operation + /// + internal Documents.OperationType? BatchOperationName { get; set; } } } diff --git a/Microsoft.Azure.Cosmos/src/Telemetry/OpenTelemetry/OpenTelemetryCoreRecorder.cs b/Microsoft.Azure.Cosmos/src/Telemetry/OpenTelemetry/OpenTelemetryCoreRecorder.cs index 8659286b3d..759eb0c10e 100644 --- a/Microsoft.Azure.Cosmos/src/Telemetry/OpenTelemetry/OpenTelemetryCoreRecorder.cs +++ b/Microsoft.Azure.Cosmos/src/Telemetry/OpenTelemetry/OpenTelemetryCoreRecorder.cs @@ -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); diff --git a/Microsoft.Azure.Cosmos/src/Telemetry/OpenTelemetry/OpenTelemetryResponse.cs b/Microsoft.Azure.Cosmos/src/Telemetry/OpenTelemetry/OpenTelemetryResponse.cs index 09d43c6965..5ff75556d0 100644 --- a/Microsoft.Azure.Cosmos/src/Telemetry/OpenTelemetry/OpenTelemetryResponse.cs +++ b/Microsoft.Azure.Cosmos/src/Telemetry/OpenTelemetry/OpenTelemetryResponse.cs @@ -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()) { } @@ -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; @@ -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) diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/BaselineTest/TestBaseline/EndToEndTraceWriterBaselineTests.BulkOperationsAsync.xml b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/BaselineTest/TestBaseline/EndToEndTraceWriterBaselineTests.BulkOperationsAsync.xml index 69e7a3c52f..e28307b622 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/BaselineTest/TestBaseline/EndToEndTraceWriterBaselineTests.BulkOperationsAsync.xml +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/BaselineTest/TestBaseline/EndToEndTraceWriterBaselineTests.BulkOperationsAsync.xml @@ -353,6 +353,7 @@ + @@ -690,6 +691,7 @@ + @@ -1027,6 +1029,7 @@ + @@ -1364,6 +1367,7 @@ + @@ -1701,6 +1705,7 @@ + @@ -2038,6 +2043,7 @@ + @@ -2375,6 +2381,7 @@ + @@ -2712,6 +2719,7 @@ + @@ -3049,6 +3057,7 @@ + @@ -3386,6 +3395,7 @@ +