Skip to content
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
b596c48
first draft
sourabh1007 Sep 4, 2024
27d3f8c
fix query iterator
sourabh1007 Sep 5, 2024
f4705b1
fixing test
sourabh1007 Sep 10, 2024
e49f87a
enable show query mode
sourabh1007 Sep 10, 2024
3f830c3
fix imports
sourabh1007 Sep 10, 2024
7c64cfb
fix tests
sourabh1007 Sep 11, 2024
38a50a8
changefeed fix
sourabh1007 Sep 11, 2024
a58fe18
fix changefeed
sourabh1007 Sep 13, 2024
48135cf
fix imports
sourabh1007 Sep 18, 2024
78b8144
added querymode
sourabh1007 Sep 23, 2024
69aedb2
feedback changes
sourabh1007 Sep 25, 2024
b760982
change text casing in querytext node
sourabh1007 Sep 26, 2024
fdd6715
Update Microsoft.Azure.Cosmos/src/Resource/FeedIterators/FeedIterator…
sourabh1007 Sep 26, 2024
423e391
Update Microsoft.Azure.Cosmos/src/Resource/FeedIterators/FeedIterator.cs
sourabh1007 Sep 26, 2024
70047ab
Update Microsoft.Azure.Cosmos/src/Telemetry/OpenTelemetry/OpenTelemet…
sourabh1007 Sep 26, 2024
a991c90
refactor code
sourabh1007 Sep 26, 2024
b064d37
Update Microsoft.Azure.Cosmos/src/Telemetry/OpenTelemetry/QueryTextMo…
sourabh1007 Sep 27, 2024
a5b7153
Update Microsoft.Azure.Cosmos/src/Telemetry/OpenTelemetry/QueryTextMo…
sourabh1007 Sep 27, 2024
8c048a3
Update Microsoft.Azure.Cosmos/src/Telemetry/OpenTelemetry/QueryTextMo…
sourabh1007 Sep 27, 2024
f587ab3
Update Microsoft.Azure.Cosmos/src/Telemetry/OpenTelemetry/QueryTextMo…
sourabh1007 Sep 27, 2024
6feb8b8
Merge branch 'master' into users/sourabhjain/addqueryTextinOtel
sourabh1007 Sep 28, 2024
54520fa
contract fix
sourabh1007 Sep 29, 2024
70d93e9
Merge branch 'master' into users/sourabhjain/addqueryTextinOtel
sourabh1007 Sep 30, 2024
31d45d6
Merge branch 'master' into users/sourabhjain/addqueryTextinOtel
sourabh1007 Oct 1, 2024
7cba059
Merge branch 'master' into users/sourabhjain/addqueryTextinOtel
sourabh1007 Oct 2, 2024
78bd8eb
Merge branch 'master' into users/sourabhjain/addqueryTextinOtel
sourabh1007 Oct 3, 2024
74a59ac
initialize QueryTextNode
sourabh1007 Oct 3, 2024
45d865e
Merge branch 'master' into users/sourabhjain/addqueryTextinOtel
sourabh1007 Oct 3, 2024
40574e6
fix test
sourabh1007 Oct 3, 2024
f2be22b
Merge branch 'master' into users/sourabhjain/addqueryTextinOtel
kirankumarkolli Oct 6, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public override async Task<ResponseMessage> ReadNextAsync(CancellationToken canc
operationType: OperationType.ReadFeed,
requestOptions: this.changeFeedRequestOptions,
task: (trace) => this.ReadNextInternalAsync(trace, cancellationToken),
openTelemetry: new (OpenTelemetryConstants.Operations.QueryChangeFeed, (response) => new OpenTelemetryResponse(responseMessage: response)),
openTelemetry: new (OpenTelemetryConstants.Operations.QueryChangeFeed, (response) => new OpenTelemetryResponse(responseMessage: response, querySpecFunc: () => this.changeFeedQuerySpec?.ToSqlQuerySpec())),
traceComponent: TraceComponent.ChangeFeed,
traceLevel: TraceLevel.Info);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,13 @@ internal bool ShouldSerializeQueryText()
{
return this.QueryText.Length > 0;
}

/// <summary>
/// Converts to SQL Query Specs
/// </summary>
internal SqlQuerySpec ToSqlQuerySpec()
{
return new SqlQuerySpec(this.QueryText);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public override Task<FeedResponse<ChangeFeedProcessorState>> ReadNextAsync(Cance
operationType: Documents.OperationType.ReadFeed,
requestOptions: null,
task: (trace) => this.ReadNextAsync(trace, cancellationToken),
openTelemetry: new (OpenTelemetryConstants.Operations.QueryChangeFeedEstimator, (response) => new OpenTelemetryResponse<ChangeFeedProcessorState>(responseMessage: response)),
openTelemetry: new (OpenTelemetryConstants.Operations.QueryChangeFeedEstimator, (response) => new OpenTelemetryResponse<ChangeFeedProcessorState>(responseMessage: response, querySpec: this.querySpec)),
traceComponent: TraceComponent.ChangeFeed,
traceLevel: TraceLevel.Info);
}
Expand Down
7 changes: 7 additions & 0 deletions Microsoft.Azure.Cosmos/src/CosmosClientTelemetryOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,12 @@ public class CosmosClientTelemetryOptions
/// These values decides whether to generate an <see cref="System.Diagnostics.Tracing.EventSource"/> with request diagnostics or not.
/// </summary>
public CosmosThresholdOptions CosmosThresholdOptions { get; set; } = new CosmosThresholdOptions();

/// <summary>
/// Enables printing query in Traces db.query.text attribute. By default, query is not printed.
/// Users have the option to enable printing parameterized or all queries,
/// 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.
/// </summary>
public QueryTextMode QueryTextMode { get; set; } = QueryTextMode.None;
}
}
10 changes: 7 additions & 3 deletions Microsoft.Azure.Cosmos/src/Query/v3Query/QueryIterator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ private QueryIterator(
RequestOptions requestOptions,
CosmosClientContext clientContext,
Guid correlatedActivityId,
ContainerInternal container)
ContainerInternal container,
SqlQuerySpec sqlQuerySpec)
{
this.cosmosQueryContext = cosmosQueryContext ?? throw new ArgumentNullException(nameof(cosmosQueryContext));
this.queryPipelineStage = cosmosQueryExecutionContext ?? throw new ArgumentNullException(nameof(cosmosQueryExecutionContext));
Expand All @@ -53,6 +54,7 @@ private QueryIterator(
this.hasMoreResults = true;
this.correlatedActivityId = correlatedActivityId;

this.querySpec = sqlQuerySpec;
this.container = container;
this.operationName = OpenTelemetryConstants.Operations.QueryItems;
this.operationType = Documents.OperationType.Query;
Expand Down Expand Up @@ -119,7 +121,8 @@ public static QueryIterator Create(
queryRequestOptions,
clientContext,
correlatedActivityId,
containerCore);
containerCore,
sqlQuerySpec);
}

requestContinuationToken = tryParse.Result;
Expand Down Expand Up @@ -152,7 +155,8 @@ public static QueryIterator Create(
queryRequestOptions,
clientContext,
correlatedActivityId,
containerCore);
containerCore,
sqlQuerySpec);
}

public override bool HasMoreResults => this.hasMoreResults;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ internal override void PopulateRequestOptions(RequestMessage request)
#endif
JsonSerializationFormatOptions JsonSerializationFormatOptions { get; set; }

/// <summary>
/// Enables printing query in Traces db.query.text attribute. By default, query is not printed.
/// Users have the option to enable printing parameterized or all queries,
/// 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.
/// </summary>
internal QueryTextMode? QueryTextMode { get; set; }

internal ChangeFeedRequestOptions Clone()
{
return new ChangeFeedRequestOptions()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,13 @@ public ConsistencyLevel? ConsistencyLevel
/// These options have no effect otherwise.
/// </summary>
public DedicatedGatewayRequestOptions DedicatedGatewayRequestOptions { get; set; }

/// <summary>
/// Enables printing query in Traces db.query.text attribute. By default, query is not printed.
/// Users have the option to enable printing parameterized or all queries,
/// 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.
/// </summary>
public QueryTextMode? QueryTextMode { get; set; } = null;

internal CosmosElement CosmosElementContinuationToken { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace Microsoft.Azure.Cosmos
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos.Query.Core;

/// <summary>
/// Cosmos Result set iterator that keeps track of the continuation token when retrieving results form a query.
Expand Down Expand Up @@ -145,5 +146,10 @@ public void Dispose()
/// Operation Type used for open telemetry traces, it will set optionally, otherwise default operation type will be used in traces
/// </summary>
internal Documents.OperationType? operationType;

/// <summary>
/// collect SQL query Specs for tracing
/// </summary>
internal SqlQuerySpec querySpec;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ internal sealed class FeedIteratorCore : FeedIteratorInternal
private readonly CosmosClientContext clientContext;
private readonly string resourceLink;
private readonly ResourceType resourceType;
private readonly SqlQuerySpec querySpec;
private bool hasMoreResultsInternal;

public FeedIteratorCore(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ internal FeedIteratorInlineCore(
this.feedIteratorInternal = feedIteratorInternal;
this.clientContext = clientContext;

this.querySpec = feedIteratorInternal.querySpec;
this.container = feedIteratorInternal.container;
this.databaseName = feedIteratorInternal.databaseName;

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

if (this.operationType.HasValue)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ internal FeedIteratorInlineCore(
this.feedIteratorInternal = feedIteratorInternal;
this.clientContext = clientContext;

this.querySpec = feedIterator.querySpec;
this.container = feedIteratorInternal.container;
this.databaseName = feedIteratorInternal.databaseName;

Expand Down Expand Up @@ -62,7 +63,7 @@ public override Task<FeedResponse<T>> ReadNextAsync(CancellationToken cancellati
task: trace => this.feedIteratorInternal.ReadNextAsync(trace, cancellationToken),
openTelemetry: new (this.feedIteratorInternal.operationName, (response) =>
{
OpenTelemetryResponse<T> openTelemetryResponse = new OpenTelemetryResponse<T>(responseMessage: response);
OpenTelemetryResponse<T> openTelemetryResponse = new OpenTelemetryResponse<T>(responseMessage: response, querySpec: this.querySpec);

if (this.operationType.HasValue)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace Microsoft.Azure.Cosmos
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos.Query.Core;

/// <summary>
/// Cosmos Result set iterator that keeps track of the continuation token when retrieving results form a query.
Expand Down Expand Up @@ -133,5 +134,10 @@ public void Dispose()
/// Operation Type used for open telemetry traces
/// </summary>
internal Documents.OperationType? operationType;

/// <summary>
/// collect SQL query Specs for tracing
/// </summary>
internal SqlQuerySpec querySpec;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ internal sealed class OpenTelemetryAttributeKeys
/// </summary>
public const string CorrelatedActivityId = "db.cosmosdb.correlated_activity_id";

/// <summary>
/// Represents the Azure Cosmos DB SQL Query.
/// </summary>
public const string QueryText = "db.query.text";

/// <summary>
/// Represents the size of the batch operation.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Microsoft.Azure.Cosmos.Telemetry
{
using System;
using System.Net;
using System.Security.AccessControl;
using Microsoft.Azure.Cosmos.Query.Core;

internal class OpenTelemetryAttributes
{
Expand Down Expand Up @@ -90,5 +90,10 @@ internal OpenTelemetryAttributes(RequestMessage requestMessage)
/// Batch Size
/// </summary>
internal int? BatchSize { get; set; }

/// <summary>
/// Query Spec with Query Text and Parameters
/// </summary>
internal SqlQuerySpec QuerySpec { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ internal struct OpenTelemetryCoreRecorder : IDisposable
private readonly OperationType operationType = OperationType.Invalid;
private readonly string connectionModeCache = null;

private readonly QueryTextMode? queryTextMode = null;
private OpenTelemetryAttributes response = null;

/// <summary>
Expand Down Expand Up @@ -59,13 +60,15 @@ private OpenTelemetryCoreRecorder(
string databaseName,
OperationType operationType,
CosmosClientContext clientContext,
CosmosThresholdOptions config)
CosmosThresholdOptions config,
QueryTextMode queryTextMode)
{
this.scope = scope;
this.config = config;

this.operationType = operationType;
this.connectionModeCache = Enum.GetName(typeof(ConnectionMode), clientContext.ClientOptions.ConnectionMode);
this.queryTextMode = queryTextMode;

if (scope.IsEnabled)
{
Expand Down Expand Up @@ -107,7 +110,8 @@ public static OpenTelemetryCoreRecorder CreateOperationLevelParentActivity(
string databaseName,
Documents.OperationType operationType,
CosmosClientContext clientContext,
CosmosThresholdOptions config)
CosmosThresholdOptions config,
QueryTextMode queryTextMode)
{
return new OpenTelemetryCoreRecorder(
operationScope,
Expand All @@ -116,7 +120,8 @@ public static OpenTelemetryCoreRecorder CreateOperationLevelParentActivity(
databaseName,
operationType,
clientContext,
config);
config,
queryTextMode);
}

public bool IsEnabled => this.scope.IsEnabled;
Expand Down Expand Up @@ -245,6 +250,15 @@ OperationType operationType
this.scope.AddAttribute(OpenTelemetryAttributeKeys.ActivityId, this.response.ActivityId);
this.scope.AddAttribute(OpenTelemetryAttributeKeys.CorrelatedActivityId, this.response.CorrelatedActivityId);

if (this.response.QuerySpec is not null)
{
if (this.queryTextMode == QueryTextMode.All ||
(this.queryTextMode == QueryTextMode.ParameterizedOnly && this.response.QuerySpec.ShouldSerializeParameters()))
{
this.scope.AddAttribute(OpenTelemetryAttributeKeys.QueryText, this.response.QuerySpec?.QueryText);
}
}

if (this.response.Diagnostics != null)
{
this.scope.AddAttribute(OpenTelemetryAttributeKeys.Region, ClientTelemetryHelper.GetContactedRegions(this.response.Diagnostics.GetContactedRegions()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,17 @@ public static OpenTelemetryCoreRecorder CreateRecorder(Func<string> getOperation
{
scope.SetDisplayName($"{operationName} {containerName}");

QueryTextMode queryTextMode = GetQueryTextMode(requestOptions, clientContext);

openTelemetryRecorder = OpenTelemetryCoreRecorder.CreateOperationLevelParentActivity(
operationScope: scope,
operationName: operationName,
containerName: containerName,
databaseName: databaseName,
operationType: operationType,
clientContext: clientContext,
config: requestOptions?.CosmosThresholdOptions ?? clientContext.ClientOptions?.CosmosClientTelemetryOptions.CosmosThresholdOptions);
config: requestOptions?.CosmosThresholdOptions ?? clientContext.ClientOptions?.CosmosClientTelemetryOptions.CosmosThresholdOptions,
queryTextMode: queryTextMode);
}
#if !INTERNAL
// If there are no listeners at operation level and no parent activity created.
Expand All @@ -83,5 +86,22 @@ public static OpenTelemetryCoreRecorder CreateRecorder(Func<string> getOperation
}
return openTelemetryRecorder;
}

private static QueryTextMode GetQueryTextMode(RequestOptions requestOptions, CosmosClientContext clientContext)
{
QueryTextMode? queryTextMode = null;
if (requestOptions is QueryRequestOptions queryRequestOptions && queryRequestOptions.QueryTextMode.HasValue)
{
queryTextMode = queryRequestOptions.QueryTextMode.Value;
}
else if (requestOptions is ChangeFeedRequestOptions changeFeedRequestOptions && changeFeedRequestOptions.QueryTextMode.HasValue)
{
queryTextMode = changeFeedRequestOptions.QueryTextMode.Value;

}

queryTextMode ??= clientContext.ClientOptions?.CosmosClientTelemetryOptions?.QueryTextMode ?? QueryTextMode.None;
return queryTextMode.Value;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace Microsoft.Azure.Cosmos
using System.IO;
using System.Net;
using Microsoft.Azure.Cosmos.Core.Trace;
using Microsoft.Azure.Cosmos.Query.Core;
using Microsoft.Azure.Documents;
using Telemetry;

internal sealed class OpenTelemetryResponse : OpenTelemetryAttributes
Expand All @@ -27,7 +29,7 @@ internal OpenTelemetryResponse(TransactionalBatchResponse responseMessage)
{
}

internal OpenTelemetryResponse(ResponseMessage responseMessage)
internal OpenTelemetryResponse(ResponseMessage responseMessage, Func<SqlQuerySpec> querySpecFunc = null)
: this(
statusCode: responseMessage.StatusCode,
requestCharge: OpenTelemetryResponse.GetHeader(responseMessage)?.RequestCharge,
Expand All @@ -37,7 +39,8 @@ internal OpenTelemetryResponse(ResponseMessage responseMessage)
requestMessage: responseMessage.RequestMessage,
subStatusCode: OpenTelemetryResponse.GetHeader(responseMessage)?.SubStatusCode,
activityId: OpenTelemetryResponse.GetHeader(responseMessage)?.ActivityId,
correlationId: OpenTelemetryResponse.GetHeader(responseMessage)?.CorrelatedActivityId)
correlationId: OpenTelemetryResponse.GetHeader(responseMessage)?.CorrelatedActivityId,
querySpecFunc: querySpecFunc)
{
}

Expand All @@ -51,7 +54,8 @@ private OpenTelemetryResponse(
Documents.SubStatusCodes? subStatusCode,
string activityId,
string correlationId,
int? batchSize = null)
int? batchSize = null,
Func<SqlQuerySpec> querySpecFunc = null)
: base(requestMessage)
{
this.StatusCode = statusCode;
Expand All @@ -63,6 +67,10 @@ private OpenTelemetryResponse(
this.ActivityId = activityId;
this.CorrelatedActivityId = correlationId;
this.BatchSize = batchSize;
if (querySpecFunc != null)
{
this.QuerySpec = querySpecFunc();
}
}

private static string GetPayloadSize(ResponseMessage response)
Expand Down
Loading