Skip to content

Commit ad1bcd8

Browse files
sourabh1007kirankumarkollijcocchi
authored
Open Telemetry : Adds query text in attribute (#4664)
## Description 1. Added `db.query.text` attribute to record queries in Traces. 2. Introduced new `QueryTextMode`, which will have valid values as a) `None` :Do not show query. b) `ParameterizedOnly` : Print parameterized query only. b) `All` 3. It can be set as part of `CosmosClientTelemetryOptions` and `RequestOptions` (i.e. `QueryRequestOptions` and `ChangeFeedRequestOptions`) ## Type of change - [] New feature (non-breaking change which adds functionality) --------- Co-authored-by: Kiran Kumar Kolli <[email protected]> Co-authored-by: Justine Cocchi <[email protected]>
1 parent 50c401e commit ad1bcd8

24 files changed

+273
-29
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ public override async Task<ResponseMessage> ReadNextAsync(CancellationToken canc
229229
operationType: OperationType.ReadFeed,
230230
requestOptions: this.changeFeedRequestOptions,
231231
task: (trace) => this.ReadNextInternalAsync(trace, cancellationToken),
232-
openTelemetry: new (OpenTelemetryConstants.Operations.QueryChangeFeed, (response) => new OpenTelemetryResponse(responseMessage: response)),
232+
openTelemetry: new (OpenTelemetryConstants.Operations.QueryChangeFeed, (response) => new OpenTelemetryResponse(responseMessage: response, querySpecFunc: () => this.changeFeedQuerySpec?.ToSqlQuerySpec())),
233233
traceComponent: TraceComponent.ChangeFeed,
234234
traceLevel: TraceLevel.Info);
235235
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,13 @@ internal bool ShouldSerializeQueryText()
5454
{
5555
return this.QueryText.Length > 0;
5656
}
57+
58+
/// <summary>
59+
/// Converts to SQL Query Specs
60+
/// </summary>
61+
internal SqlQuerySpec ToSqlQuerySpec()
62+
{
63+
return new SqlQuerySpec(this.QueryText);
64+
}
5765
}
5866
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public override Task<FeedResponse<ChangeFeedProcessorState>> ReadNextAsync(Cance
122122
operationType: Documents.OperationType.ReadFeed,
123123
requestOptions: null,
124124
task: (trace) => this.ReadNextAsync(trace, cancellationToken),
125-
openTelemetry: new (OpenTelemetryConstants.Operations.QueryChangeFeedEstimator, (response) => new OpenTelemetryResponse<ChangeFeedProcessorState>(responseMessage: response)),
125+
openTelemetry: new (OpenTelemetryConstants.Operations.QueryChangeFeedEstimator, (response) => new OpenTelemetryResponse<ChangeFeedProcessorState>(responseMessage: response, querySpec: this.querySpec)),
126126
traceComponent: TraceComponent.ChangeFeed,
127127
traceLevel: TraceLevel.Info);
128128
}

Microsoft.Azure.Cosmos/src/CosmosClientTelemetryOptions.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,12 @@ public class CosmosClientTelemetryOptions
4646
/// These values decides whether to generate an <see cref="System.Diagnostics.Tracing.EventSource"/> with request diagnostics or not.
4747
/// </summary>
4848
public CosmosThresholdOptions CosmosThresholdOptions { get; set; } = new CosmosThresholdOptions();
49+
50+
/// <summary>
51+
/// Enables printing query in Traces db.query.text attribute. By default, query is not printed.
52+
/// Users have the option to enable printing parameterized or all queries,
53+
/// but has to beware that customer data may be shown when the later option is chosen. It's the user's responsibility to sanitize the queries if necessary.
54+
/// </summary>
55+
public QueryTextMode QueryTextMode { get; set; } = QueryTextMode.None;
4956
}
5057
}

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ private QueryIterator(
4343
RequestOptions requestOptions,
4444
CosmosClientContext clientContext,
4545
Guid correlatedActivityId,
46-
ContainerInternal container)
46+
ContainerInternal container,
47+
SqlQuerySpec sqlQuerySpec)
4748
{
4849
this.cosmosQueryContext = cosmosQueryContext ?? throw new ArgumentNullException(nameof(cosmosQueryContext));
4950
this.queryPipelineStage = cosmosQueryExecutionContext ?? throw new ArgumentNullException(nameof(cosmosQueryExecutionContext));
@@ -53,6 +54,7 @@ private QueryIterator(
5354
this.hasMoreResults = true;
5455
this.correlatedActivityId = correlatedActivityId;
5556

57+
this.querySpec = sqlQuerySpec;
5658
this.container = container;
5759
this.operationName = OpenTelemetryConstants.Operations.QueryItems;
5860
this.operationType = Documents.OperationType.Query;
@@ -119,7 +121,8 @@ public static QueryIterator Create(
119121
queryRequestOptions,
120122
clientContext,
121123
correlatedActivityId,
122-
containerCore);
124+
containerCore,
125+
sqlQuerySpec);
123126
}
124127

125128
requestContinuationToken = tryParse.Result;
@@ -152,7 +155,8 @@ public static QueryIterator Create(
152155
queryRequestOptions,
153156
clientContext,
154157
correlatedActivityId,
155-
containerCore);
158+
containerCore,
159+
sqlQuerySpec);
156160
}
157161

158162
public override bool HasMoreResults => this.hasMoreResults;

Microsoft.Azure.Cosmos/src/RequestOptions/ChangeFeedRequestOptions.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ internal override void PopulateRequestOptions(RequestMessage request)
7979
#endif
8080
JsonSerializationFormatOptions JsonSerializationFormatOptions { get; set; }
8181

82+
/// <summary>
83+
/// Enables printing query in Traces db.query.text attribute. By default, query is not printed.
84+
/// Users have the option to enable printing parameterized or all queries,
85+
/// but has to beware that customer data may be shown when the later option is chosen. It's the user's responsibility to sanitize the queries if necessary.
86+
/// </summary>
87+
internal QueryTextMode QueryTextMode { get; set; } = Cosmos.QueryTextMode.None;
88+
8289
internal ChangeFeedRequestOptions Clone()
8390
{
8491
return new ChangeFeedRequestOptions()

Microsoft.Azure.Cosmos/src/RequestOptions/QueryRequestOptions.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,13 @@ public ConsistencyLevel? ConsistencyLevel
170170
/// These options have no effect otherwise.
171171
/// </summary>
172172
public DedicatedGatewayRequestOptions DedicatedGatewayRequestOptions { get; set; }
173+
174+
/// <summary>
175+
/// Enables printing query in Traces db.query.text attribute. By default, query is not printed.
176+
/// Users have the option to enable printing parameterized or all queries,
177+
/// but has to beware that customer data may be shown when the later option is chosen. It's the user's responsibility to sanitize the queries if necessary.
178+
/// </summary>
179+
public QueryTextMode QueryTextMode { get; set; } = QueryTextMode.None;
173180

174181
internal CosmosElement CosmosElementContinuationToken { get; set; }
175182

Microsoft.Azure.Cosmos/src/Resource/FeedIterators/FeedIterator.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace Microsoft.Azure.Cosmos
77
using System;
88
using System.Threading;
99
using System.Threading.Tasks;
10+
using Microsoft.Azure.Cosmos.Query.Core;
1011

1112
/// <summary>
1213
/// Cosmos Result set iterator that keeps track of the continuation token when retrieving results form a query.
@@ -145,5 +146,10 @@ public void Dispose()
145146
/// Operation Type used for open telemetry traces, it will set optionally, otherwise default operation type will be used in traces
146147
/// </summary>
147148
internal Documents.OperationType? operationType;
149+
150+
/// <summary>
151+
/// collect SQL query Specs for tracing
152+
/// </summary>
153+
internal SqlQuerySpec querySpec;
148154
}
149155
}

Microsoft.Azure.Cosmos/src/Resource/FeedIterators/FeedIteratorCore.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ internal sealed class FeedIteratorCore : FeedIteratorInternal
2727
private readonly CosmosClientContext clientContext;
2828
private readonly string resourceLink;
2929
private readonly ResourceType resourceType;
30-
private readonly SqlQuerySpec querySpec;
3130
private bool hasMoreResultsInternal;
3231

3332
public FeedIteratorCore(

Microsoft.Azure.Cosmos/src/Resource/FeedIterators/FeedIteratorInlineCore.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ internal FeedIteratorInlineCore(
2828
this.feedIteratorInternal = feedIteratorInternal;
2929
this.clientContext = clientContext;
3030

31+
this.querySpec = feedIteratorInternal.querySpec;
3132
this.container = feedIteratorInternal.container;
3233
this.databaseName = feedIteratorInternal.databaseName;
3334

@@ -62,7 +63,7 @@ public override Task<ResponseMessage> ReadNextAsync(CancellationToken cancellati
6263
task: (trace) => this.feedIteratorInternal.ReadNextAsync(trace, cancellationToken),
6364
openTelemetry: new (this.operationName, (response) =>
6465
{
65-
OpenTelemetryResponse openTelemetryResponse = new OpenTelemetryResponse(responseMessage: response);
66+
OpenTelemetryResponse openTelemetryResponse = new OpenTelemetryResponse(responseMessage: response, querySpecFunc: () => this.querySpec);
6667

6768
if (this.operationType.HasValue)
6869
{

0 commit comments

Comments
 (0)