Skip to content

Commit

Permalink
firdst draft
Browse files Browse the repository at this point in the history
  • Loading branch information
sourabh1007 committed Aug 28, 2023
1 parent 83dd4b5 commit bd5c3f7
Show file tree
Hide file tree
Showing 22 changed files with 354 additions and 199 deletions.
9 changes: 9 additions & 0 deletions Microsoft.Azure.Cosmos/src/ConnectionPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,15 @@ internal int? MaxTcpPartitionCount
set;
}

/// <summary>
/// ClientTelemetryOptions
/// </summary>
public CosmosClientTelemetryOptions CosmosClientTelemetryOptions
{
get;
set;
}

/// <summary>
/// GlobalEndpointManager will subscribe to this event if user updates the preferredLocations list in the Azure Cosmos DB service.
/// </summary>
Expand Down
32 changes: 4 additions & 28 deletions Microsoft.Azure.Cosmos/src/CosmosClientOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -730,9 +730,9 @@ internal Protocol ConnectionProtocol
internal bool? EnableCpuMonitor { get; set; }

/// <summary>
/// Flag to enable telemetry
/// Client Telemetry Options
/// </summary>
internal bool? EnableClientTelemetry { get; set; }
public CosmosClientTelemetryOptions CosmosClientTelemetryOptions { get; set; }

internal void SetSerializerIfNotConfigured(CosmosSerializer serializer)
{
Expand Down Expand Up @@ -774,9 +774,9 @@ internal virtual ConnectionPolicy GetConnectionPolicy(int clientId)
ServerCertificateCustomValidationCallback = this.ServerCertificateCustomValidationCallback
};

if (this.EnableClientTelemetry.HasValue)
if (this.CosmosClientTelemetryOptions != null)
{
connectionPolicy.EnableClientTelemetry = this.EnableClientTelemetry.Value;
connectionPolicy.CosmosClientTelemetryOptions = this.CosmosClientTelemetryOptions;
}

if (this.ApplicationRegion != null)
Expand Down Expand Up @@ -1013,29 +1013,5 @@ public override bool CanConvert(Type objectType)
return objectType == typeof(DateTime);
}
}

/// <summary>
/// Distributed Tracing Options. <see cref="Microsoft.Azure.Cosmos.DistributedTracingOptions"/>
/// </summary>
/// <remarks> Applicable only when Operation level distributed tracing is enabled through <see cref="Microsoft.Azure.Cosmos.CosmosClientOptions.IsDistributedTracingEnabled"/></remarks>
internal DistributedTracingOptions DistributedTracingOptions { get; set; }

/// <summary>
/// Gets or sets the flag to generate operation level <see cref="System.Diagnostics.Activity"/> for methods calls using the Source Name "Azure.Cosmos.Operation".
/// </summary>
/// <value>
/// The default value is true (for preview package).
/// </value>
/// <remarks>This flag is there to disable it from source. Please Refer https://opentelemetry.io/docs/instrumentation/net/exporters/ to know more about open telemetry exporters</remarks>
#if PREVIEW
public
#else
internal
#endif
bool IsDistributedTracingEnabled { get; set; }
#if PREVIEW
= true;
#endif

}
}
46 changes: 46 additions & 0 deletions Microsoft.Azure.Cosmos/src/CosmosClientTelemetryOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------

namespace Microsoft.Azure.Cosmos
{
/// <summary>
/// Telemetry Options for Cosmos Client
/// </summary>
public class CosmosClientTelemetryOptions
{
/// <summary>
/// Enable sending telemetry to service, <see cref="CosmosThresholdOptions"/> is not applicable to this as of now
/// </summary>
public bool EnableSendingMetricsToService { get; set; } = false;

/// <summary>
/// Gets or sets the flag to generate operation level <see cref="System.Diagnostics.Activity"/> for methods calls using the Source Name "Azure.Cosmos.Operation".
/// </summary>
/// <value>
/// The default value is true (for preview package).
/// </value>
/// <remarks>This flag is there to disable it from source. Please Refer https://opentelemetry.io/docs/instrumentation/net/exporters/ to know more about open telemetry exporters</remarks>
#if PREVIEW
public
#else
internal
#endif
bool DisableDistributedTracing { get; set; } =
#if PREVIEW
false;
#else
true;
#endif

/// <summary>
/// Threshold values for telemetry
/// </summary>
#if PREVIEW
public
#else
internal
#endif
CosmosThresholdOptions CosmosThresholdOptions { get; set; }
}
}
29 changes: 29 additions & 0 deletions Microsoft.Azure.Cosmos/src/CosmosThresholdOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------

namespace Microsoft.Azure.Cosmos
{
using System;

/// <summary>
/// Threshold options for telemetry
/// </summary>
#if PREVIEW
public
#else
internal
#endif
class CosmosThresholdOptions
{
/// <summary>
/// Latency Threshold for non point operations
/// </summary>
public TimeSpan NonPointOperationLatencyThreshold { get; set; } = TimeSpan.FromMilliseconds(500);

/// <summary>
/// Latency Threshold for point operations
/// </summary>
public TimeSpan PointOperationLatencyThreshold { get; set; } = TimeSpan.FromMilliseconds(100);
}
}
14 changes: 7 additions & 7 deletions Microsoft.Azure.Cosmos/src/DocumentClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ internal partial class DocumentClient : IDisposable, IAuthorizationTokenProvider
internal RemoteCertificateValidationCallback remoteCertificateValidationCallback;

//Distributed Tracing Flag
internal bool isDistributedTracingEnabled;
internal CosmosClientTelemetryOptions cosmosClientTelemetryOptions;

//SessionContainer.
internal ISessionContainer sessionContainer;
Expand Down Expand Up @@ -432,7 +432,7 @@ internal DocumentClient(Uri serviceEndpoint,
/// <param name="isLocalQuorumConsistency">Flag to allow Quorum Read with Eventual Consistency Account</param>
/// <param name="cosmosClientId"></param>
/// <param name="remoteCertificateValidationCallback">This delegate responsible for validating the third party certificate. </param>
/// <param name="isDistributedTracingEnabled">This is distributed tracing flag</param>
/// <param name="cosmosClientTelemetryOptions">This is distributed tracing flag</param>
/// <remarks>
/// The service endpoint can be obtained from the Azure Management Portal.
/// If you are connecting using one of the Master Keys, these can be obtained along with the endpoint from the Azure Management Portal
Expand Down Expand Up @@ -460,7 +460,7 @@ internal DocumentClient(Uri serviceEndpoint,
bool isLocalQuorumConsistency = false,
string cosmosClientId = null,
RemoteCertificateValidationCallback remoteCertificateValidationCallback = null,
bool isDistributedTracingEnabled = false)
CosmosClientTelemetryOptions cosmosClientTelemetryOptions = null)
{
if (sendingRequestEventArgs != null)
{
Expand Down Expand Up @@ -494,7 +494,7 @@ internal DocumentClient(Uri serviceEndpoint,
storeClientFactory: storeClientFactory,
cosmosClientId: cosmosClientId,
remoteCertificateValidationCallback: remoteCertificateValidationCallback,
isDistributedTracingEnabled: isDistributedTracingEnabled);
cosmosClientTelemetryOptions: cosmosClientTelemetryOptions);
}

/// <summary>
Expand Down Expand Up @@ -678,7 +678,7 @@ internal virtual void Initialize(Uri serviceEndpoint,
TokenCredential tokenCredential = null,
string cosmosClientId = null,
RemoteCertificateValidationCallback remoteCertificateValidationCallback = null,
bool isDistributedTracingEnabled = false)
CosmosClientTelemetryOptions cosmosClientTelemetryOptions = null)
{
if (serviceEndpoint == null)
{
Expand All @@ -687,7 +687,7 @@ internal virtual void Initialize(Uri serviceEndpoint,

this.clientId = cosmosClientId;
this.remoteCertificateValidationCallback = remoteCertificateValidationCallback;
this.isDistributedTracingEnabled = isDistributedTracingEnabled;
this.cosmosClientTelemetryOptions = cosmosClientTelemetryOptions;

this.queryPartitionProvider = new AsyncLazy<QueryPartitionProvider>(async () =>
{
Expand Down Expand Up @@ -6623,7 +6623,7 @@ private void InitializeDirectConnectivity(IStoreClientFactory storeClientFactory
{
Documents.Telemetry.DistributedTracingOptions distributedTracingOptions = new ()
{
IsDistributedTracingEnabled = this.isDistributedTracingEnabled
IsDistributedTracingEnabled = !this.cosmosClientTelemetryOptions.DisableDistributedTracing
};

StoreClientFactory newClientFactory = new StoreClientFactory(
Expand Down
62 changes: 11 additions & 51 deletions Microsoft.Azure.Cosmos/src/Fluent/CosmosClientBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -435,36 +435,6 @@ public CosmosClientBuilder WithConsistencyLevel(Cosmos.ConsistencyLevel consiste

}

/// <summary>
/// Sets whether Distributed Tracing for "Azure.Cosmos.Operation" source is enabled.
/// </summary>
/// <param name="isEnabled">Whether <see cref="CosmosClientOptions.IsDistributedTracingEnabled"/> is enabled.</param>
/// <returns>The current <see cref="CosmosClientBuilder"/>.</returns>
#if PREVIEW
public
#else
internal
#endif
CosmosClientBuilder WithDistributedTracing(bool isEnabled = true)
{
this.clientOptions.IsDistributedTracingEnabled = isEnabled;
return this;
}

/// <summary>
/// Enables Distributed Tracing with a Configuration ref. <see cref="DistributedTracingOptions"/>
/// </summary>
/// <param name="options"><see cref="DistributedTracingOptions"/>.</param>
/// <returns>The current <see cref="CosmosClientBuilder"/>.</returns>]
/// <remarks>Refer https://opentelemetry.io/docs/instrumentation/net/exporters/ to know more about open telemetry exporters</remarks>
internal CosmosClientBuilder WithDistributedTracingOptions(DistributedTracingOptions options)
{
this.clientOptions.IsDistributedTracingEnabled = true;
this.clientOptions.DistributedTracingOptions = options;

return this;
}

/// <summary>
/// Sets the connection mode to Gateway. This is used by the client when connecting to the Azure Cosmos DB service.
/// </summary>
Expand Down Expand Up @@ -679,27 +649,6 @@ internal CosmosClientBuilder WithCpuMonitorDisabled()
return this;
}

/// <summary>
/// Disable Telemetry if enabled using environment properties
/// </summary>
/// <returns>The <see cref="CosmosClientBuilder"/> object</returns>
internal CosmosClientBuilder WithTelemetryDisabled()
{
this.clientOptions.EnableClientTelemetry = false;
return this;
}

/// <summary>
/// To enable Telemetry, set COSMOS.CLIENT_TELEMETRY_ENABLED environment property.
/// This function is used by Test only.
/// </summary>
/// <returns>The <see cref="CosmosClientBuilder"/> object</returns>
internal CosmosClientBuilder WithTelemetryEnabled()
{
this.clientOptions.EnableClientTelemetry = true;
return this;
}

/// <summary>
/// Enabled partition level failover in the SDK
/// </summary>
Expand Down Expand Up @@ -731,5 +680,16 @@ internal CosmosClientBuilder WithRetryWithOptions(
this.clientOptions.TotalWaitTimeForRetryWithMilliseconds = totalWaitTimeForRetryWithMilliseconds;
return this;
}

/// <summary>
/// WithClientTelemetryOptions
/// </summary>
/// <param name="options"></param>
/// <returns>The <see cref="CosmosClientBuilder"/> object</returns>
public CosmosClientBuilder WithClientTelemetryOptions(CosmosClientTelemetryOptions options)
{
this.clientOptions.CosmosClientTelemetryOptions = options;
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public class RequestOptions
/// <summary>
/// Set Request Level Distributed Tracing Options.
/// </summary>
internal DistributedTracingOptions DistributedTracingOptions { get; set; }
internal CosmosThresholdOptions CosmosThresholdOptions { get; set; }

/// <summary>
/// Gets or sets the boolean to use effective partition key routing in the cosmos db request.
Expand Down
2 changes: 1 addition & 1 deletion Microsoft.Azure.Cosmos/src/Resource/ClientContextCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ internal static CosmosClientContext Create(
sessionContainer: clientOptions.SessionContainer,
cosmosClientId: cosmosClient.Id,
remoteCertificateValidationCallback: ClientContextCore.SslCustomValidationCallBack(clientOptions.ServerCertificateCustomValidationCallback),
isDistributedTracingEnabled: clientOptions.IsDistributedTracingEnabled);
cosmosClientTelemetryOptions: clientOptions.CosmosClientTelemetryOptions);

return ClientContextCore.Create(
cosmosClient,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static bool IsEnabled(EventLevel level)

[NonEvent]
public static void RecordDiagnosticsForRequests(
DistributedTracingOptions config,
CosmosThresholdOptions config,
Documents.OperationType operationType,
OpenTelemetryAttributes response)
{
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,11 @@ internal static class DiagnosticsFilterHelper
/// </summary>
/// <returns>true or false</returns>
public static bool IsLatencyThresholdCrossed(
DistributedTracingOptions config,
CosmosThresholdOptions config,
OperationType operationType,
OpenTelemetryAttributes response)
{
TimeSpan latencyThreshold;

if (config?.LatencyThresholdForDiagnosticEvent != null)
{
latencyThreshold = config.LatencyThresholdForDiagnosticEvent.Value;
}
else
{
latencyThreshold = operationType == OperationType.Query ? DistributedTracingOptions.DefaultQueryTimeoutThreshold : DistributedTracingOptions.DefaultCrudLatencyThreshold;
}

TimeSpan latencyThreshold = operationType == OperationType.Query ? config.NonPointOperationLatencyThreshold : config.PointOperationLatencyThreshold;
return response.Diagnostics.GetClientElapsedTime() > latencyThreshold;
}

Expand Down
Loading

0 comments on commit bd5c3f7

Please sign in to comment.