Skip to content

Commit 1bdad8b

Browse files
committed
cosmetic changes
1 parent 740e6c0 commit 1bdad8b

20 files changed

+962
-596
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,8 @@ public override Task<TransactionalBatchResponse> ExecuteAsync(
235235
},
236236
openTelemetry: (response) => new OpenTelemetryResponse(
237237
responseMessage: response,
238-
operationFlag: this.isHomogenousOperations,
239-
operationName: this.lastItemBatchOperation.OperationType));
238+
isHomogenousOperations: this.isHomogenousOperations,
239+
batchOperation: this.lastItemBatchOperation.OperationType));
240240
}
241241

242242
/// <summary>

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

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,62 @@ namespace Microsoft.Azure.Cosmos
1111
using System.Threading.Tasks;
1212
using Microsoft.Azure.Documents;
1313

14+
/// <summary>
15+
/// Represents an internal abstract class for handling transactional batches of operations.
16+
/// </summary>
1417
internal abstract class TransactionalBatchInternal : TransactionalBatch
1518
{
19+
/// <summary>
20+
/// The list of operations in the batch.
21+
/// </summary>
1622
protected List<ItemBatchOperation> operations;
1723

18-
internal bool isHomogenousOperations = false;
24+
/// <summary>
25+
/// Indicates whether all operations in the batch are of the same type.
26+
/// </summary>
27+
internal bool isHomogenousOperations = true;
1928

29+
/// <summary>
30+
/// The last operation added to the batch.
31+
/// </summary>
2032
internal ItemBatchOperation lastItemBatchOperation = null;
2133

34+
/// <summary>
35+
/// Adds a new operation to the batch of operations and updates the homogeneity status of the operations.
36+
/// </summary>
37+
/// <param name="itemBatchOperation">The operation to be added to the batch.</param>
38+
/// <remarks>
39+
/// This method performs the following actions:
40+
/// <list type="number">
41+
/// <item>
42+
/// <description>Adds the given <paramref name="itemBatchOperation"/> to the operations list.</description>
43+
/// </item>
44+
/// <item>
45+
/// <description>If the added operation is the first operation in the batch, it sets this operation as the <see cref="lastItemBatchOperation"/>.</description>
46+
/// </item>
47+
/// <item>
48+
/// <description>If there are existing operations in the batch and the operations are currently homogeneous, it checks if the last added operation's type matches the new operation's type:
49+
/// <list type="bullet">
50+
/// <item><description>If they match, the batch remains homogeneous.</description></item>
51+
/// <item><description>If they do not match, the batch is no longer considered homogeneous.</description></item>
52+
/// </list>
53+
/// </description>
54+
/// </item>
55+
/// <item>
56+
/// <description>Updates the <see cref="lastItemBatchOperation"/> to the newly added operation.</description>
57+
/// </item>
58+
/// </list>
59+
/// </remarks>
2260
protected void AddOperation(ItemBatchOperation itemBatchOperation)
2361
{
2462
this.operations.Add(itemBatchOperation);
2563
if (this.operations.Count == 1)
2664
{
2765
this.lastItemBatchOperation = itemBatchOperation;
2866
}
29-
else
67+
else if (this.isHomogenousOperations)
3068
{
31-
this.isHomogenousOperations = this.isHomogenousOperations
32-
&& this.lastItemBatchOperation.OperationType == itemBatchOperation.OperationType;
69+
this.isHomogenousOperations = this.lastItemBatchOperation.OperationType == itemBatchOperation.OperationType;
3370
this.lastItemBatchOperation = itemBatchOperation;
3471
}
3572
}

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

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

395+
/// <summary>
396+
/// Retrieves the size of the transactional batch.
397+
/// </summary>
398+
/// <returns>
399+
/// An integer representing the number of operations in the batch.
400+
/// Returns 0 if there are no operations.
401+
/// </returns>
402+
/// <remarks>
403+
/// This method checks the <see cref="Operations"/> property to determine the number of operations in the current transactional batch.
404+
/// If the <see cref="Operations"/> property is null, it returns 0, indicating that there are no operations in the batch.
405+
/// </remarks>
395406
internal int GetBatchSize()
396407
{
397408
if (this.Operations == null)

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

Lines changed: 125 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,159 @@
44

55
namespace Microsoft.Azure.Cosmos.Telemetry
66
{
7+
/// <summary>
8+
/// Contains constant string values representing OpenTelemetry attribute keys for monitoring and tracing Cosmos DB operations.
9+
/// These keys follow the OpenTelemetry conventions and the Cosmos DB semantic conventions as outlined in the OpenTelemetry specification.
10+
/// </summary>
11+
/// <remarks>
12+
/// For more details on the semantic conventions, refer to the OpenTelemetry documentation at:
13+
/// <see href="https://opentelemetry.io/docs/specs/semconv/database/cosmosdb/"/>
14+
/// </remarks>
715
internal sealed class OpenTelemetryAttributeKeys
816
{
917
// Azure defaults
18+
19+
/// <summary>
20+
/// Represents the diagnostic namespace for Azure Cosmos.
21+
/// </summary>
1022
public const string DiagnosticNamespace = "Azure.Cosmos";
23+
24+
/// <summary>
25+
/// Represents the resource provider namespace for Azure Cosmos.
26+
/// </summary>
1127
public const string ResourceProviderNamespace = "Microsoft.DocumentDB";
28+
29+
/// <summary>
30+
/// Represents the prefix for operation names.
31+
/// </summary>
1232
public const string OperationPrefix = "Operation";
33+
34+
/// <summary>
35+
/// Represents the prefix for network-level operations.
36+
/// </summary>
1337
public const string NetworkLevelPrefix = "Request";
1438

1539
// Common database attributes
40+
41+
/// <summary>
42+
/// Represents the name of the database system.
43+
/// </summary>
1644
public const string DbSystemName = "db.system";
17-
public const string DbName = "db.name";
18-
public const string DbOperation = "db.operation";
45+
46+
/// <summary>
47+
/// Represents the namespace of the database.
48+
/// </summary>
49+
public const string DbName = "db.namespace";
50+
51+
/// <summary>
52+
/// Represents the name of the database operation.
53+
/// </summary>
54+
public const string DbOperation = "db.operation.name";
55+
56+
/// <summary>
57+
/// Represents the server address.
58+
/// </summary>
1959
public const string ServerAddress = "server.address";
2060

21-
// Cosmos Db Specific
61+
// Cosmos DB specific attributes
62+
63+
/// <summary>
64+
/// Represents the client ID for Cosmos DB.
65+
/// </summary>
2266
public const string ClientId = "db.cosmosdb.client_id";
67+
68+
/// <summary>
69+
/// Represents the machine ID for Cosmos DB.
70+
/// </summary>
2371
public const string MachineId = "db.cosmosdb.machine_id";
24-
public const string UserAgent = "user_agent.original"; // Compliant with open telemetry conventions
72+
73+
/// <summary>
74+
/// Represents the user agent, compliant with OpenTelemetry conventions.
75+
/// </summary>
76+
public const string UserAgent = "user_agent.original";
77+
78+
/// <summary>
79+
/// Represents the connection mode for Cosmos DB.
80+
/// </summary>
2581
public const string ConnectionMode = "db.cosmosdb.connection_mode";
82+
83+
/// <summary>
84+
/// Represents the type of operation for Cosmos DB.
85+
/// </summary>
2686
public const string OperationType = "db.cosmosdb.operation_type";
2787

28-
// Request/Response Specifics
29-
public const string ContainerName = "db.cosmosdb.container";
30-
public const string RequestContentLength = "db.cosmosdb.request_content_length_bytes";
31-
public const string ResponseContentLength = "db.cosmosdb.response_content_length_bytes";
88+
// Request/Response specifics
89+
90+
/// <summary>
91+
/// Represents the name of the container in Cosmos DB.
92+
/// </summary>
93+
public const string ContainerName = "db.collection.name";
94+
95+
/// <summary>
96+
/// Represents the content length of the request.
97+
/// </summary>
98+
public const string RequestContentLength = "db.cosmosdb.request_content_length";
99+
100+
/// <summary>
101+
/// Represents the content length of the response.
102+
/// </summary>
103+
public const string ResponseContentLength = "db.cosmosdb.response_content_length";
104+
105+
/// <summary>
106+
/// Represents the status code of the response.
107+
/// </summary>
32108
public const string StatusCode = "db.cosmosdb.status_code";
109+
110+
/// <summary>
111+
/// Represents the sub-status code of the response.
112+
/// </summary>
33113
public const string SubStatusCode = "db.cosmosdb.sub_status_code";
114+
115+
/// <summary>
116+
/// Represents the request charge for the operation.
117+
/// </summary>
34118
public const string RequestCharge = "db.cosmosdb.request_charge";
119+
120+
/// <summary>
121+
/// Represents the regions contacted for the operation.
122+
/// </summary>
35123
public const string Region = "db.cosmosdb.regions_contacted";
124+
125+
/// <summary>
126+
/// Represents the item count in the operation.
127+
/// </summary>
36128
public const string ItemCount = "db.cosmosdb.item_count";
129+
130+
/// <summary>
131+
/// Represents the activity ID for the operation.
132+
/// </summary>
37133
public const string ActivityId = "db.cosmosdb.activity_id";
134+
135+
/// <summary>
136+
/// Represents the correlated activity ID for the operation.
137+
/// </summary>
38138
public const string CorrelatedActivityId = "db.cosmosdb.correlated_activity_id";
39-
public const string BatchSize = "db.cosmosdb.batch_size";
139+
140+
/// <summary>
141+
/// Represents the size of the batch operation.
142+
/// </summary>
143+
public const string BatchSize = "db.operation.batch.size";
40144

41145
// Exceptions
146+
147+
/// <summary>
148+
/// Represents the type of exception.
149+
/// </summary>
42150
public const string ExceptionType = "exception.type";
151+
152+
/// <summary>
153+
/// Represents the message of the exception.
154+
/// </summary>
43155
public const string ExceptionMessage = "exception.message";
156+
157+
/// <summary>
158+
/// Represents the stack trace of the exception.
159+
/// </summary>
44160
public const string ExceptionStacktrace = "exception.stacktrace";
45161
}
46162
}

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,26 @@ internal OpenTelemetryAttributes(RequestMessage requestMessage)
7676
internal int? BatchSize { get; set; }
7777

7878
/// <summary>
79-
/// Will have value for homogeneous batch operation and will be null for heterogeneous batch operation
79+
/// Gets or sets the operation type for batch operations.
80+
/// Will have a value for homogeneous batch operations and will be null for heterogeneous batch operations.
81+
///
82+
/// Operation name should be prepended with BATCH for homogeneous operations, or be just BATCH for heterogeneous operations.
8083
/// </summary>
84+
/// <example>
85+
/// For example, if you have a batch of homogeneous operations like Read:
86+
/// <code>
87+
/// var recorder = new OpenTelemetryCoreRecorder();
88+
/// recorder.BatchOperationName = Documents.OperationType.Read; // Homogeneous batch
89+
/// string operationName = "BATCH." + recorder.BatchOperationName; // Results in "BATCH.Read"
90+
/// </code>
91+
///
92+
/// For a batch of heterogeneous operations:
93+
/// <code>
94+
/// var recorder = new OpenTelemetryCoreRecorder();
95+
/// recorder.BatchOperationName = null; // Heterogeneous batch
96+
/// string operationName = "BATCH"; // No specific operation type
97+
/// </code>
98+
/// </example>
8199
internal Documents.OperationType? BatchOperationName { get; set; }
82100
}
83101
}

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ namespace Microsoft.Azure.Cosmos.Telemetry
1212
using Microsoft.Azure.Documents;
1313

1414
/// <summary>
15-
/// This class is used to add information in an Activity tags ref. https://github.com/Azure/azure-cosmos-dotnet-v3/issues/3058
15+
/// This class is used to add information in an Activity tags for OpenTelemetry.
16+
/// Refer to <see href="https://github.com/Azure/azure-cosmos-dotnet-v3/issues/3058"/> for more details.
1617
/// </summary>
1718
internal struct OpenTelemetryCoreRecorder : IDisposable
1819
{
@@ -27,6 +28,9 @@ internal struct OpenTelemetryCoreRecorder : IDisposable
2728

2829
private OpenTelemetryAttributes response = null;
2930

31+
/// <summary>
32+
/// Maps exception types to actions that record their OpenTelemetry attributes.
33+
/// </summary>
3034
internal static IDictionary<Type, Action<Exception, DiagnosticScope>> OTelCompatibleExceptions = new Dictionary<Type, Action<Exception, DiagnosticScope>>()
3135
{
3236
{ typeof(CosmosNullReferenceException), (exception, scope) => CosmosNullReferenceException.RecordOtelAttributes((CosmosNullReferenceException)exception, scope)},
@@ -76,9 +80,10 @@ private OpenTelemetryCoreRecorder(
7680
}
7781

7882
/// <summary>
79-
/// Used for creating parent activity in scenario where there are no listeners at operation level
80-
/// but they are present at network level
83+
/// Creates a parent activity for scenarios where there are no listeners at the operation level but are present at the network level.
8184
/// </summary>
85+
/// <param name="networkScope">The network-level diagnostic scope.</param>
86+
/// <returns>An instance of <see cref="OpenTelemetryCoreRecorder"/>.</returns>
8287
public static OpenTelemetryCoreRecorder CreateNetworkLevelParentActivity(DiagnosticScope networkScope)
8388
{
8489
return new OpenTelemetryCoreRecorder(networkScope);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace Microsoft.Azure.Cosmos
1313

1414
internal sealed class OpenTelemetryResponse : OpenTelemetryAttributes
1515
{
16-
internal OpenTelemetryResponse(TransactionalBatchResponse responseMessage, bool operationFlag, OperationType? operationName)
16+
internal OpenTelemetryResponse(TransactionalBatchResponse responseMessage, bool isHomogenousOperations, OperationType? batchOperation)
1717
: this(
1818
statusCode: responseMessage.StatusCode,
1919
requestCharge: OpenTelemetryResponse.GetHeader(responseMessage)?.RequestCharge,
@@ -25,7 +25,7 @@ internal OpenTelemetryResponse(TransactionalBatchResponse responseMessage, bool
2525
activityId: OpenTelemetryResponse.GetHeader(responseMessage)?.ActivityId,
2626
correlationId: OpenTelemetryResponse.GetHeader(responseMessage)?.CorrelatedActivityId,
2727
batchSize: responseMessage.GetBatchSize(),
28-
batchOperationName: operationFlag ? operationName : null )
28+
batchOperationName: isHomogenousOperations ? batchOperation : null )
2929
{
3030
}
3131

0 commit comments

Comments
 (0)