Skip to content

Commit 6623de9

Browse files
authored
Open Telemetry: Fixes Operation name to follow Otel convention (#4643)
* Changed Operation name according to open telemetry convention * fix tests * updated baseline * fix tests * renamed ops name * fixed operatio name * reverted few files * append prefix * fix tests * remove commented code * fix activity name * code refatot for different operation name for diagnostics * baseline files * fix tests * refcator code * fix test * remove stream * feediterator changes * fix bugs * further fixes * typo fix * fix test
1 parent 9aafc77 commit 6623de9

50 files changed

Lines changed: 807 additions & 635 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

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

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace Microsoft.Azure.Cosmos
1010
using System.Linq;
1111
using System.Threading;
1212
using System.Threading.Tasks;
13+
using Microsoft.Azure.Cosmos.Telemetry.OpenTelemetry;
1314
using Microsoft.Azure.Documents;
1415

1516
internal class BatchCore : TransactionalBatchInternal
@@ -18,6 +19,11 @@ internal class BatchCore : TransactionalBatchInternal
1819

1920
private readonly ContainerInternal container;
2021

22+
/// <summary>
23+
/// The list of operations in the batch.
24+
/// </summary>
25+
protected List<ItemBatchOperation> operations;
26+
2127
/// <summary>
2228
/// Initializes a new instance of the <see cref="BatchCore"/> class.
2329
/// </summary>
@@ -29,6 +35,8 @@ internal BatchCore(
2935
{
3036
this.container = container;
3137
this.partitionKey = partitionKey;
38+
39+
this.operations = new List<ItemBatchOperation>();
3240
}
3341

3442
public override TransactionalBatch CreateItem<T>(
@@ -40,7 +48,7 @@ public override TransactionalBatch CreateItem<T>(
4048
throw new ArgumentNullException(nameof(item));
4149
}
4250

43-
this.AddOperation(new ItemBatchOperation<T>(
51+
this.operations.Add(new ItemBatchOperation<T>(
4452
operationType: OperationType.Create,
4553
operationIndex: this.operations.Count,
4654
resource: item,
@@ -59,7 +67,7 @@ public override TransactionalBatch CreateItemStream(
5967
throw new ArgumentNullException(nameof(streamPayload));
6068
}
6169

62-
this.AddOperation(new ItemBatchOperation(
70+
this.operations.Add(new ItemBatchOperation(
6371
operationType: OperationType.Create,
6472
operationIndex: this.operations.Count,
6573
resourceStream: streamPayload,
@@ -78,7 +86,7 @@ public override TransactionalBatch ReadItem(
7886
throw new ArgumentNullException(nameof(id));
7987
}
8088

81-
this.AddOperation(new ItemBatchOperation(
89+
this.operations.Add(new ItemBatchOperation(
8290
operationType: OperationType.Read,
8391
operationIndex: this.operations.Count,
8492
id: id,
@@ -97,7 +105,7 @@ public override TransactionalBatch UpsertItem<T>(
97105
throw new ArgumentNullException(nameof(item));
98106
}
99107

100-
this.AddOperation(new ItemBatchOperation<T>(
108+
this.operations.Add(new ItemBatchOperation<T>(
101109
operationType: OperationType.Upsert,
102110
operationIndex: this.operations.Count,
103111
resource: item,
@@ -116,7 +124,7 @@ public override TransactionalBatch UpsertItemStream(
116124
throw new ArgumentNullException(nameof(streamPayload));
117125
}
118126

119-
this.AddOperation(new ItemBatchOperation(
127+
this.operations.Add(new ItemBatchOperation(
120128
operationType: OperationType.Upsert,
121129
operationIndex: this.operations.Count,
122130
resourceStream: streamPayload,
@@ -141,7 +149,7 @@ public override TransactionalBatch ReplaceItem<T>(
141149
throw new ArgumentNullException(nameof(item));
142150
}
143151

144-
this.AddOperation(new ItemBatchOperation<T>(
152+
this.operations.Add(new ItemBatchOperation<T>(
145153
operationType: OperationType.Replace,
146154
operationIndex: this.operations.Count,
147155
id: id,
@@ -167,7 +175,7 @@ public override TransactionalBatch ReplaceItemStream(
167175
throw new ArgumentNullException(nameof(streamPayload));
168176
}
169177

170-
this.AddOperation(new ItemBatchOperation(
178+
this.operations.Add(new ItemBatchOperation(
171179
operationType: OperationType.Replace,
172180
operationIndex: this.operations.Count,
173181
id: id,
@@ -187,7 +195,7 @@ public override TransactionalBatch DeleteItem(
187195
throw new ArgumentNullException(nameof(id));
188196
}
189197

190-
this.AddOperation(new ItemBatchOperation(
198+
this.operations.Add(new ItemBatchOperation(
191199
operationType: OperationType.Delete,
192200
operationIndex: this.operations.Count,
193201
id: id,
@@ -232,10 +240,7 @@ public override Task<TransactionalBatchResponse> ExecuteAsync(
232240
this.operations = new List<ItemBatchOperation>();
233241
return executor.ExecuteAsync(trace, cancellationToken);
234242
},
235-
openTelemetry: (response) => new OpenTelemetryResponse(
236-
responseMessage: response,
237-
isHomogenousOperations: this.isHomogenousOperations,
238-
batchOperation: this.homogenousOperation));
243+
openTelemetry: new (OpenTelemetryConstants.Operations.ExecuteBatch, (response) => new OpenTelemetryResponse(responseMessage: response)));
239244
}
240245

241246
/// <summary>
@@ -250,7 +255,7 @@ public virtual TransactionalBatch PatchItemStream(
250255
Stream patchStream,
251256
TransactionalBatchPatchItemRequestOptions requestOptions = null)
252257
{
253-
this.AddOperation(new ItemBatchOperation(
258+
this.operations.Add(new ItemBatchOperation(
254259
operationType: OperationType.Patch,
255260
operationIndex: this.operations.Count,
256261
id: id,
@@ -286,7 +291,7 @@ public override TransactionalBatch PatchItem(
286291

287292
PatchSpec patchSpec = new PatchSpec(patchOperations, requestOptions);
288293

289-
this.AddOperation(new ItemBatchOperation<PatchSpec>(
294+
this.operations.Add(new ItemBatchOperation<PatchSpec>(
290295
operationType: OperationType.Patch,
291296
operationIndex: this.operations.Count,
292297
id: id,

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

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
namespace Microsoft.Azure.Cosmos
66
{
77
using System.Collections.Generic;
8-
using System.Linq;
9-
using Microsoft.Azure.Documents;
108

119
/// <summary>
1210
/// Represents an internal abstract class for handling transactional batches of operations.
@@ -15,47 +13,5 @@ namespace Microsoft.Azure.Cosmos
1513
/// </summary>
1614
internal abstract class TransactionalBatchInternal : TransactionalBatch
1715
{
18-
/// <summary>
19-
/// The list of operations in the batch.
20-
/// </summary>
21-
protected List<ItemBatchOperation> operations;
22-
23-
/// <summary>
24-
/// Initializes a new instance of the <see cref="TransactionalBatchInternal"/> class.
25-
/// </summary>
26-
public TransactionalBatchInternal()
27-
{
28-
this.operations = new List<ItemBatchOperation>();
29-
}
30-
31-
/// <summary>
32-
/// Indicates whether all operations in the batch are of the same type.
33-
/// </summary>
34-
internal bool isHomogenousOperations = true;
35-
36-
/// <summary>
37-
/// Stores the operation type if all operations in the batch are of the same type; otherwise, null.
38-
/// </summary>
39-
internal OperationType? homogenousOperation = null;
40-
41-
/// <summary>
42-
/// Adds an operation to the batch.
43-
/// </summary>
44-
/// <param name="itemBatchOperation">The operation to add to the batch.</param>
45-
/// <remarks>
46-
/// This method performs the following actions:
47-
/// 1. Checks if the batch is homogeneous (all operations of the same type) and if the new operation's type matches the type of the existing operations.
48-
/// 2. Updates the <see cref="isHomogenousOperations"/> flag and the <see cref="homogenousOperation"/> property based on the check.
49-
/// 3. Adds the operation to the list of operations.
50-
/// </remarks>
51-
protected void AddOperation(ItemBatchOperation itemBatchOperation)
52-
{
53-
if (this.isHomogenousOperations && this.operations.Count > 0)
54-
{
55-
this.isHomogenousOperations = this.operations.First().OperationType == itemBatchOperation.OperationType;
56-
this.homogenousOperation = this.isHomogenousOperations ? itemBatchOperation.OperationType : null;
57-
}
58-
this.operations.Add(itemBatchOperation);
59-
}
6016
}
6117
}

Microsoft.Azure.Cosmos/src/ChangeFeed/ChangeFeedIteratorCore.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ namespace Microsoft.Azure.Cosmos.ChangeFeed
1313
using Microsoft.Azure.Cosmos.Pagination;
1414
using Microsoft.Azure.Cosmos.Query.Core;
1515
using Microsoft.Azure.Cosmos.Query.Core.Monads;
16+
using Microsoft.Azure.Cosmos.Telemetry.OpenTelemetry;
1617
using Microsoft.Azure.Cosmos.Tracing;
1718
using Microsoft.Azure.Documents;
1819

@@ -50,6 +51,8 @@ public ChangeFeedIteratorCore(
5051
this.changeFeedRequestOptions = changeFeedRequestOptions ?? new ChangeFeedRequestOptions();
5152
this.changeFeedQuerySpec = changeFeedQuerySpec;
5253

54+
this.operationName = OpenTelemetryConstants.Operations.QueryChangeFeed;
55+
5356
this.lazyMonadicEnumerator = new AsyncLazy<TryCatch<CrossPartitionChangeFeedAsyncEnumerator>>(
5457
valueFactory: async (trace, cancellationToken) =>
5558
{
@@ -226,7 +229,7 @@ public override async Task<ResponseMessage> ReadNextAsync(CancellationToken canc
226229
operationType: OperationType.ReadFeed,
227230
requestOptions: this.changeFeedRequestOptions,
228231
task: (trace) => this.ReadNextInternalAsync(trace, cancellationToken),
229-
openTelemetry: (response) => new OpenTelemetryResponse(responseMessage: response),
232+
openTelemetry: new (OpenTelemetryConstants.Operations.QueryChangeFeed, (response) => new OpenTelemetryResponse(responseMessage: response)),
230233
traceComponent: TraceComponent.ChangeFeed,
231234
traceLevel: TraceLevel.Info);
232235
}

Microsoft.Azure.Cosmos/src/ChangeFeed/ChangeFeedPartitionKeyResultSetIteratorCore.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Microsoft.Azure.Cosmos.ChangeFeed
99
using System.Threading;
1010
using System.Threading.Tasks;
1111
using Microsoft.Azure.Cosmos.ChangeFeed.LeaseManagement;
12-
using Microsoft.Azure.Cosmos.CosmosElements;
12+
using Microsoft.Azure.Cosmos.Telemetry.OpenTelemetry;
1313
using Microsoft.Azure.Cosmos.Tracing;
1414
using Microsoft.Azure.Documents;
1515

@@ -81,6 +81,8 @@ private ChangeFeedPartitionKeyResultSetIteratorCore(
8181
this.changeFeedStartFrom = changeFeedStartFrom ?? throw new ArgumentNullException(nameof(changeFeedStartFrom));
8282
this.clientContext = this.container.ClientContext;
8383
this.changeFeedOptions = options;
84+
85+
this.operationName = OpenTelemetryConstants.Operations.QueryChangeFeed;
8486
}
8587

8688
public override bool HasMoreResults => this.hasMoreResultsInternal;
@@ -99,8 +101,6 @@ public override Task<ResponseMessage> ReadNextAsync(CancellationToken cancellati
99101
operationType: Documents.OperationType.ReadFeed,
100102
requestOptions: this.changeFeedOptions,
101103
task: (trace) => this.ReadNextAsync(trace, cancellationToken),
102-
openTelemetry: (response) => new OpenTelemetryResponse(
103-
responseMessage: response),
104104
traceComponent: TraceComponent.ChangeFeed,
105105
traceLevel: TraceLevel.Info);
106106
}

Microsoft.Azure.Cosmos/src/ChangeFeed/StandByFeedIteratorCore.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace Microsoft.Azure.Cosmos.ChangeFeed
1111
using System.Threading.Tasks;
1212
using Microsoft.Azure.Cosmos.CosmosElements;
1313
using Microsoft.Azure.Cosmos.Routing;
14+
using Microsoft.Azure.Cosmos.Telemetry.OpenTelemetry;
1415
using Microsoft.Azure.Cosmos.Tracing;
1516
using Microsoft.Azure.Documents;
1617

@@ -45,6 +46,8 @@ internal StandByFeedIteratorCore(
4546
this.changeFeedOptions = options;
4647
this.maxItemCount = maxItemCount;
4748
this.continuationToken = continuationToken;
49+
50+
this.operationName = OpenTelemetryConstants.Operations.QueryChangeFeed;
4851
}
4952

5053
/// <summary>

Microsoft.Azure.Cosmos/src/ChangeFeedProcessor/ChangeFeedEstimatorIterator.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ namespace Microsoft.Azure.Cosmos.ChangeFeed
1818
using Microsoft.Azure.Cosmos.Diagnostics;
1919
using Microsoft.Azure.Cosmos.Query.Core;
2020
using Microsoft.Azure.Cosmos.Query.Core.Monads;
21+
using Microsoft.Azure.Cosmos.Telemetry.OpenTelemetry;
2122
using Microsoft.Azure.Cosmos.Tracing;
2223
using Newtonsoft.Json.Linq;
2324

@@ -106,6 +107,8 @@ private ChangeFeedEstimatorIterator(
106107

107108
this.monitoredContainerFeedCreator = monitoredContainerFeedCreator;
108109
this.documentServiceLeaseContainer = documentServiceLeaseContainer;
110+
111+
this.operationName = OpenTelemetryConstants.Operations.QueryChangeFeedEstimator;
109112
}
110113

111114
public override bool HasMoreResults => this.hasMoreResults;
@@ -119,7 +122,7 @@ public override Task<FeedResponse<ChangeFeedProcessorState>> ReadNextAsync(Cance
119122
operationType: Documents.OperationType.ReadFeed,
120123
requestOptions: null,
121124
task: (trace) => this.ReadNextAsync(trace, cancellationToken),
122-
openTelemetry: (response) => new OpenTelemetryResponse<ChangeFeedProcessorState>(responseMessage: response),
125+
openTelemetry: new (OpenTelemetryConstants.Operations.QueryChangeFeedEstimator, (response) => new OpenTelemetryResponse<ChangeFeedProcessorState>(responseMessage: response)),
123126
traceComponent: TraceComponent.ChangeFeed,
124127
traceLevel: TraceLevel.Info);
125128
}

Microsoft.Azure.Cosmos/src/CosmosClient.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ namespace Microsoft.Azure.Cosmos
1818
using Microsoft.Azure.Cosmos.Handlers;
1919
using Microsoft.Azure.Cosmos.Query.Core.Monads;
2020
using Microsoft.Azure.Cosmos.Query.Core.QueryPlan;
21+
using Microsoft.Azure.Cosmos.Telemetry;
22+
using Microsoft.Azure.Cosmos.Telemetry.OpenTelemetry;
2123
using Microsoft.Azure.Cosmos.Tracing;
2224
using Microsoft.Azure.Cosmos.Tracing.TraceData;
2325
using Microsoft.Azure.Documents;
@@ -756,7 +758,7 @@ public virtual Task<DatabaseResponse> CreateDatabaseAsync(
756758
trace: trace,
757759
cancellationToken: cancellationToken);
758760
},
759-
openTelemetry: (response) => new OpenTelemetryResponse<DatabaseProperties>(responseMessage: response));
761+
openTelemetry: new (OpenTelemetryConstants.Operations.CreateDatabase, (response) => new OpenTelemetryResponse<DatabaseProperties>(responseMessage: response)));
760762
}
761763

762764
/// <summary>
@@ -804,7 +806,7 @@ public virtual Task<DatabaseResponse> CreateDatabaseAsync(
804806
trace: trace,
805807
cancellationToken: cancellationToken);
806808
},
807-
openTelemetry: (response) => new OpenTelemetryResponse<DatabaseProperties>(responseMessage: response));
809+
openTelemetry: new (OpenTelemetryConstants.Operations.CreateDatabase, (response) => new OpenTelemetryResponse<DatabaseProperties>(responseMessage: response)));
808810
}
809811

810812
/// <summary>
@@ -900,8 +902,7 @@ public virtual Task<DatabaseResponse> CreateDatabaseIfNotExistsAsync(
900902
return this.ClientContext.ResponseFactory.CreateDatabaseResponse(this.GetDatabase(databaseProperties.Id), readResponseAfterConflict);
901903
}
902904
},
903-
openTelemetry: (response) => new OpenTelemetryResponse<DatabaseProperties>(
904-
responseMessage: response));
905+
openTelemetry: new (OpenTelemetryConstants.Operations.CreateDatabaseIfNotExists, (response) => new OpenTelemetryResponse<DatabaseProperties>(responseMessage: response)));
905906
}
906907

907908
/// <summary>
@@ -1205,7 +1206,7 @@ public virtual Task<ResponseMessage> CreateDatabaseStreamAsync(
12051206
trace,
12061207
cancellationToken);
12071208
},
1208-
openTelemetry: (response) => new OpenTelemetryResponse(response));
1209+
openTelemetry: new (OpenTelemetryConstants.Operations.CreateDatabase, (response) => new OpenTelemetryResponse(response)));
12091210
}
12101211

12111212
/// <summary>
@@ -1288,7 +1289,7 @@ internal virtual Task<ResponseMessage> CreateDatabaseStreamAsync(
12881289
}
12891290

12901291
return this.ClientContext.OperationHelperAsync(
1291-
operationName: nameof(CreateDatabaseIfNotExistsAsync),
1292+
operationName: nameof(CreateDatabaseStreamAsync),
12921293
containerName: null,
12931294
databaseName: databaseProperties.Id,
12941295
operationType: OperationType.Create,
@@ -1303,7 +1304,7 @@ internal virtual Task<ResponseMessage> CreateDatabaseStreamAsync(
13031304
trace,
13041305
cancellationToken);
13051306
},
1306-
openTelemetry: (response) => new OpenTelemetryResponse(response));
1307+
openTelemetry: new (OpenTelemetryConstants.Operations.CreateDatabase, (response) => new OpenTelemetryResponse(response)));
13071308
}
13081309

13091310
private async Task<DatabaseResponse> CreateDatabaseInternalAsync(

Microsoft.Azure.Cosmos/src/Query/v3Query/QueryIterator.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ namespace Microsoft.Azure.Cosmos.Query
1919
using Microsoft.Azure.Cosmos.Query.Core.Pipeline.Pagination;
2020
using Microsoft.Azure.Cosmos.Query.Core.QueryClient;
2121
using Microsoft.Azure.Cosmos.Query.Core.QueryPlan;
22+
using Microsoft.Azure.Cosmos.Telemetry.OpenTelemetry;
2223
using Microsoft.Azure.Cosmos.Tracing;
2324

2425
internal sealed class QueryIterator : FeedIteratorInternal
@@ -53,6 +54,8 @@ private QueryIterator(
5354
this.correlatedActivityId = correlatedActivityId;
5455

5556
this.container = container;
57+
this.operationName = OpenTelemetryConstants.Operations.QueryItems;
58+
this.operationType = Documents.OperationType.Query;
5659
}
5760

5861
public static QueryIterator Create(

Microsoft.Azure.Cosmos/src/ReadFeed/ReadFeedIteratorCore.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ namespace Microsoft.Azure.Cosmos.ReadFeed
1717
using Microsoft.Azure.Cosmos.ReadFeed.Pagination;
1818
using Microsoft.Azure.Cosmos.Resource.CosmosExceptions;
1919
using Microsoft.Azure.Cosmos.Routing;
20+
using Microsoft.Azure.Cosmos.Telemetry.OpenTelemetry;
2021
using Microsoft.Azure.Cosmos.Tracing;
2122
using Microsoft.Azure.Documents;
2223

@@ -38,6 +39,7 @@ public ReadFeedIteratorCore(
3839
CancellationToken cancellationToken)
3940
{
4041
this.container = container;
42+
this.operationName = OpenTelemetryConstants.Operations.ReadFeedRanges;
4143

4244
this.queryRequestOptions = queryRequestOptions;
4345
readFeedPaginationOptions ??= ReadFeedExecutionOptions.Default;

0 commit comments

Comments
 (0)