Skip to content

Commit

Permalink
code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
sourabh1007 committed Mar 7, 2023
1 parent 3463df6 commit 6ad1d99
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ internal static async Task<AccountProperties> SetAccountNameAsync(GlobalEndpoint
}

DefaultTrace.TraceVerbose("System Usage recorded by telemetry is : {0}", systemUsageHistory);
List<SystemInfo> systemInfoCollection = new List<SystemInfo>

List<SystemInfo> systemInfoCollection = new List<SystemInfo>(6)
{
TelemetrySystemUsage.GetCpuInfo(systemUsageHistory.Values),
TelemetrySystemUsage.GetMemoryRemainingInfo(systemUsageHistory.Values),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace Microsoft.Azure.Cosmos.Telemetry
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HdrHistogram;
Expand All @@ -16,9 +17,6 @@ namespace Microsoft.Azure.Cosmos.Telemetry

internal static class ClientTelemetryPayloadWriter
{
private static readonly StringBuilder stringBuilder = new StringBuilder(ClientTelemetryOptions.PayloadSizeThreshold);
private static readonly StringWriter stringWriter = new StringWriter(stringBuilder);

public static async Task SerializedPayloadChunksAsync(
ClientTelemetryProperties properties,
ConcurrentDictionary<OperationInfo, (LongConcurrentHistogram latency, LongConcurrentHistogram requestcharge)> operationInfoSnapshot,
Expand All @@ -30,17 +28,17 @@ internal static class ClientTelemetryPayloadWriter
{
throw new ArgumentNullException(nameof(properties));
}

JsonWriter writer = ClientTelemetryPayloadWriter.GetWriter(properties, "operationInfo");

if (operationInfoSnapshot != null && operationInfoSnapshot.Count > 0)
StringBuilder stringBuilder = new StringBuilder(ClientTelemetryOptions.PayloadSizeThreshold);

JsonWriter writer = ClientTelemetryPayloadWriter.GetWriterWithSectionStartTag(stringBuilder, properties, "operationInfo");

if (operationInfoSnapshot.Any())
{
var opsEnumerator = operationInfoSnapshot.GetEnumerator();
while (opsEnumerator.MoveNext())
foreach (KeyValuePair<OperationInfo, (LongConcurrentHistogram latency, LongConcurrentHistogram requestcharge)> entry in operationInfoSnapshot)
{
long lengthNow = stringBuilder.Length;
KeyValuePair<OperationInfo, (LongConcurrentHistogram latency, LongConcurrentHistogram requestcharge)> entry = opsEnumerator.Current;


OperationInfo payloadForLatency = entry.Key;
payloadForLatency.MetricInfo = new MetricInfo(ClientTelemetryOptions.RequestLatencyName, ClientTelemetryOptions.RequestLatencyUnit);
payloadForLatency.SetAggregators(entry.Value.latency, ClientTelemetryOptions.TicksToMsFactor);
Expand All @@ -59,7 +57,7 @@ internal static class ClientTelemetryPayloadWriter

await callback.Invoke(stringBuilder.ToString());

writer = ClientTelemetryPayloadWriter.GetWriter(properties, "operationInfo");
writer = ClientTelemetryPayloadWriter.GetWriterWithSectionStartTag(stringBuilder, properties, "operationInfo");
}

writer.WriteRawValue(latencyMetrics);
Expand All @@ -69,17 +67,15 @@ internal static class ClientTelemetryPayloadWriter
}
writer.WriteEndArray();

if (cacheRefreshInfoSnapshot != null && cacheRefreshInfoSnapshot.Count > 0)
if (cacheRefreshInfoSnapshot.Any())
{
writer.WritePropertyName("cacheRefreshInfo");
writer.WriteStartArray();
var crEnumerator = cacheRefreshInfoSnapshot.GetEnumerator();
while (crEnumerator.MoveNext())

foreach (KeyValuePair<CacheRefreshInfo, LongConcurrentHistogram> entry in cacheRefreshInfoSnapshot)
{
long lengthNow = stringBuilder.Length;

KeyValuePair<CacheRefreshInfo, LongConcurrentHistogram> entry = crEnumerator.Current;


CacheRefreshInfo payloadForLatency = entry.Key;
payloadForLatency.MetricInfo = new MetricInfo(ClientTelemetryOptions.RequestLatencyName, ClientTelemetryOptions.RequestLatencyUnit);
payloadForLatency.SetAggregators(entry.Value, ClientTelemetryOptions.TicksToMsFactor);
Expand All @@ -93,7 +89,7 @@ internal static class ClientTelemetryPayloadWriter

await callback.Invoke(stringBuilder.ToString());

writer = ClientTelemetryPayloadWriter.GetWriter(properties, "cacheRefreshInfo");
writer = ClientTelemetryPayloadWriter.GetWriterWithSectionStartTag(stringBuilder, properties, "cacheRefreshInfo");
}

writer.WriteRawValue(latencyMetrics);
Expand All @@ -102,16 +98,15 @@ internal static class ClientTelemetryPayloadWriter

}

if (requestInfoSnapshot != null && requestInfoSnapshot.Count > 0)
if (requestInfoSnapshot.Any())
{
writer.WritePropertyName("requestInfo");
writer.WriteStartArray();
var riEnumerator = requestInfoSnapshot.GetEnumerator();
while (riEnumerator.MoveNext())

foreach (KeyValuePair<RequestInfo, LongConcurrentHistogram> entry in requestInfoSnapshot)
{
long lengthNow = stringBuilder.Length;
KeyValuePair<RequestInfo, LongConcurrentHistogram> entry = riEnumerator.Current;


MetricInfo metricInfo = new MetricInfo(ClientTelemetryOptions.RequestLatencyName, ClientTelemetryOptions.RequestLatencyUnit);
metricInfo.SetAggregators(entry.Value, ClientTelemetryOptions.TicksToMsFactor);

Expand All @@ -126,7 +121,7 @@ internal static class ClientTelemetryPayloadWriter

await callback.Invoke(stringBuilder.ToString());

writer = ClientTelemetryPayloadWriter.GetWriter(properties, "requestInfo");
writer = ClientTelemetryPayloadWriter.GetWriterWithSectionStartTag(stringBuilder, properties, "requestInfo");
}

writer.WriteRawValue(latencyMetrics);
Expand All @@ -139,10 +134,15 @@ internal static class ClientTelemetryPayloadWriter
await callback.Invoke(stringBuilder.ToString());
}

private static JsonWriter GetWriter(ClientTelemetryProperties properties, string sectionName)
private static JsonWriter GetWriterWithSectionStartTag(
StringBuilder stringBuilder,
ClientTelemetryProperties properties,
string sectionName)
{
stringBuilder.Clear();

StringWriter stringWriter = new StringWriter(stringBuilder);

JsonWriter writer = new JsonTextWriter(stringWriter)
{
AutoCompleteOnClose = false
Expand Down
28 changes: 4 additions & 24 deletions Microsoft.Azure.Cosmos/src/Telemetry/ClientTelemetryProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,29 +44,6 @@ internal ClientTelemetryProcessor(CosmosHttpClient httpClient, AuthorizationToke
ConcurrentDictionary<CacheRefreshInfo, LongConcurrentHistogram> cacheRefreshInfoSnapshot,
ConcurrentDictionary<RequestInfo, LongConcurrentHistogram> requestInfoSnapshot,
CancellationToken cancellationToken)
{
await this.GenerateOptimalSizeOfPayloadAndSendAsync(
clientTelemetryInfo,
operationInfoSnapshot,
cacheRefreshInfoSnapshot,
requestInfoSnapshot,
cancellationToken);
}

/// <summary>
/// If payload size is greater than 2 MB, it breaks the list of objects into multiple chunks and send it one by one.
/// else send as it is.
/// </summary>
/// <param name="clientTelemetryInfo"></param>
/// <param name="operationInfoSnapshot"></param>
/// <param name="cacheRefreshInfoSnapshot"></param>
/// <param name="requestInfoSnapshot"></param>
/// <param name="cancellationToken"></param>
internal async Task GenerateOptimalSizeOfPayloadAndSendAsync(ClientTelemetryProperties clientTelemetryInfo,
ConcurrentDictionary<OperationInfo, (LongConcurrentHistogram latency, LongConcurrentHistogram requestcharge)> operationInfoSnapshot,
ConcurrentDictionary<CacheRefreshInfo, LongConcurrentHistogram> cacheRefreshInfoSnapshot,
ConcurrentDictionary<RequestInfo, LongConcurrentHistogram> requestInfoSnapshot,
CancellationToken cancellationToken)
{
try
{
Expand All @@ -91,7 +68,10 @@ internal ClientTelemetryProcessor(CosmosHttpClient httpClient, AuthorizationToke
/// In any case it resets the telemetry information to collect the latest one.
/// </summary>
/// <returns>Async Task</returns>
private async Task SendAsync(string globalDatabaseAccountName, string jsonPayload, CancellationToken cancellationToken)
private async Task SendAsync(
string globalDatabaseAccountName,
string jsonPayload,
CancellationToken cancellationToken)
{
if (endpointUrl == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ public async Task CheckIfPayloadIsDividedCorrectlyAsync(int expectedOperationInf
requestInfoInfoSnapshot.TryAdd(reqInfo, latency);
}

await processor.GenerateOptimalSizeOfPayloadAndSendAsync(
await processor.ProcessAndSendAsync(
clientTelemetryProperties,
operationInfoSnapshot,
cacheRefreshInfoSnapshot,
Expand Down

0 comments on commit 6ad1d99

Please sign in to comment.