diff --git a/Microsoft.Azure.Cosmos/src/Telemetry/ClientTelemetryHelper.cs b/Microsoft.Azure.Cosmos/src/Telemetry/ClientTelemetryHelper.cs index 2399bcb9fb..b13cdbf89d 100644 --- a/Microsoft.Azure.Cosmos/src/Telemetry/ClientTelemetryHelper.cs +++ b/Microsoft.Azure.Cosmos/src/Telemetry/ClientTelemetryHelper.cs @@ -51,7 +51,7 @@ private static List RecordSystemUsage( SystemUsageHistory systemUsageHistory, bool isDirectConnectionMode) { - if (systemUsageHistory.Values == null) + if (systemUsageHistory.Values == null || systemUsageHistory.Values.Count == 0) { return null; } diff --git a/Microsoft.Azure.Cosmos/src/Telemetry/ClientTelemetryPayloadWriter.cs b/Microsoft.Azure.Cosmos/src/Telemetry/ClientTelemetryPayloadWriter.cs index 370d576fcc..7dfb305321 100644 --- a/Microsoft.Azure.Cosmos/src/Telemetry/ClientTelemetryPayloadWriter.cs +++ b/Microsoft.Azure.Cosmos/src/Telemetry/ClientTelemetryPayloadWriter.cs @@ -38,17 +38,28 @@ public static async Task SerializedPayloadChunksAsync( foreach (KeyValuePair entry in operationInfoSnapshot) { long lengthNow = stringBuilder.Length; - - OperationInfo payloadForLatency = entry.Key; - payloadForLatency.MetricInfo = new MetricInfo(ClientTelemetryOptions.RequestLatencyName, ClientTelemetryOptions.RequestLatencyUnit); - payloadForLatency.SetAggregators(entry.Value.latency, ClientTelemetryOptions.TicksToMsFactor); - - OperationInfo payloadForRequestCharge = payloadForLatency.Copy(); - payloadForRequestCharge.MetricInfo = new MetricInfo(ClientTelemetryOptions.RequestChargeName, ClientTelemetryOptions.RequestChargeUnit); - payloadForRequestCharge.SetAggregators(entry.Value.requestcharge, ClientTelemetryOptions.HistogramPrecisionFactor); - - string latencyMetrics = JsonConvert.SerializeObject(payloadForLatency); - string requestChargeMetrics = JsonConvert.SerializeObject(payloadForRequestCharge); + + // Latency Metrics is not captured then don't add it in payload + string latencyMetrics = string.Empty; + if (entry.Value.latency != null && entry.Value.latency.TotalCount > 0) + { + OperationInfo payloadForLatency = entry.Key; + payloadForLatency.MetricInfo = new MetricInfo(ClientTelemetryOptions.RequestLatencyName, ClientTelemetryOptions.RequestLatencyUnit); + payloadForLatency.SetAggregators(entry.Value.latency, ClientTelemetryOptions.TicksToMsFactor); + + latencyMetrics = JsonConvert.SerializeObject(payloadForLatency); + } + + // RequestCharge Metrics is not captured then don't add it in payload + string requestChargeMetrics = string.Empty; + if (entry.Value.requestcharge != null && entry.Value.requestcharge.TotalCount > 0) + { + OperationInfo payloadForRequestCharge = entry.Key; + payloadForRequestCharge.MetricInfo = new MetricInfo(ClientTelemetryOptions.RequestChargeName, ClientTelemetryOptions.RequestChargeUnit); + payloadForRequestCharge.SetAggregators(entry.Value.requestcharge, ClientTelemetryOptions.HistogramPrecisionFactor); + + requestChargeMetrics = JsonConvert.SerializeObject(payloadForRequestCharge); + } int thisSectionLength = latencyMetrics.Length + requestChargeMetrics.Length; if (lengthNow + thisSectionLength > ClientTelemetryOptions.PayloadSizeThreshold) @@ -64,7 +75,6 @@ public static async Task SerializedPayloadChunksAsync( writer.WriteRawValue(latencyMetrics); writer.WriteRawValue(requestChargeMetrics); } - } writer.WriteEndArray(); @@ -76,14 +86,18 @@ public static async Task SerializedPayloadChunksAsync( foreach (KeyValuePair entry in cacheRefreshInfoSnapshot) { long lengthNow = stringBuilder.Length; - - CacheRefreshInfo payloadForLatency = entry.Key; - payloadForLatency.MetricInfo = new MetricInfo(ClientTelemetryOptions.RequestLatencyName, ClientTelemetryOptions.RequestLatencyUnit); - payloadForLatency.SetAggregators(entry.Value, ClientTelemetryOptions.TicksToMsFactor); - string latencyMetrics = JsonConvert.SerializeObject(payloadForLatency); + string cacheLatencyMetrics = string.Empty; + if (entry.Value != null && entry.Value.TotalCount > 0) + { + CacheRefreshInfo payloadForLatency = entry.Key; + payloadForLatency.MetricInfo = new MetricInfo(ClientTelemetryOptions.RequestLatencyName, ClientTelemetryOptions.RequestLatencyUnit); + payloadForLatency.SetAggregators(entry.Value, ClientTelemetryOptions.TicksToMsFactor); - if (lengthNow + latencyMetrics.Length > ClientTelemetryOptions.PayloadSizeThreshold) + cacheLatencyMetrics = JsonConvert.SerializeObject(payloadForLatency); + } + + if (lengthNow + cacheLatencyMetrics.Length > ClientTelemetryOptions.PayloadSizeThreshold) { writer.WriteEndArray(); writer.WriteEndObject(); @@ -93,7 +107,7 @@ public static async Task SerializedPayloadChunksAsync( writer = ClientTelemetryPayloadWriter.GetWriterWithSectionStartTag(stringBuilder, properties, "cacheRefreshInfo"); } - writer.WriteRawValue(latencyMetrics); + writer.WriteRawValue(cacheLatencyMetrics); } writer.WriteEndArray(); diff --git a/Microsoft.Azure.Cosmos/src/Telemetry/NetworkDataRecorder.cs b/Microsoft.Azure.Cosmos/src/Telemetry/NetworkDataRecorder.cs index 20e6f975da..52f7821917 100644 --- a/Microsoft.Azure.Cosmos/src/Telemetry/NetworkDataRecorder.cs +++ b/Microsoft.Azure.Cosmos/src/Telemetry/NetworkDataRecorder.cs @@ -24,23 +24,13 @@ public void Record(List storeResponseStatistics, string { if (NetworkDataRecorder.IsStatusCodeNotExcluded((int)storeStatistics.StoreResult.StatusCode, (int)storeStatistics.StoreResult.SubStatusCode)) { - if (NetworkDataRecorder.IsUserOrServerError((int)storeStatistics.StoreResult.StatusCode)) - { - RequestInfo requestInfo = this.CreateRequestInfo(storeStatistics, databaseId, containerId); - LongConcurrentHistogram latencyHist = this.RequestInfoErrorBucket.GetOrAdd(requestInfo, x => new LongConcurrentHistogram(ClientTelemetryOptions.RequestLatencyMin, - ClientTelemetryOptions.RequestLatencyMax, - ClientTelemetryOptions.RequestLatencyPrecision)); - latencyHist.RecordValue(storeStatistics.RequestLatency.Ticks); + RequestInfo requestInfo = this.CreateRequestInfo(storeStatistics, databaseId, containerId); + ConcurrentDictionary bucket = NetworkDataRecorder.IsUserOrServerError((int)storeStatistics.StoreResult.StatusCode) ? this.RequestInfoErrorBucket : this.RequestInfoHighLatencyBucket; - } - else - { - RequestInfo requestInfo = this.CreateRequestInfo(storeStatistics, databaseId, containerId); - LongConcurrentHistogram latencyHist = this.RequestInfoHighLatencyBucket.GetOrAdd(requestInfo, x => new LongConcurrentHistogram(ClientTelemetryOptions.RequestLatencyMin, + LongConcurrentHistogram latencyHist = bucket.GetOrAdd(requestInfo, x => new LongConcurrentHistogram(ClientTelemetryOptions.RequestLatencyMin, ClientTelemetryOptions.RequestLatencyMax, ClientTelemetryOptions.RequestLatencyPrecision)); - latencyHist.RecordValue(storeStatistics.RequestLatency.Ticks); - } + latencyHist.RecordValue(storeStatistics.RequestLatency.Ticks); } } } @@ -53,6 +43,12 @@ ConcurrentDictionary requestInfoErrorList List allRequests = new List(); foreach (KeyValuePair entry in requestInfoErrorList) { + // No metrics available to record + if (entry.Value == null || entry.Value.TotalCount == 0) + { + continue; + } + MetricInfo metricInfo = new MetricInfo(ClientTelemetryOptions.RequestLatencyName, ClientTelemetryOptions.RequestLatencyUnit); metricInfo.SetAggregators(entry.Value, ClientTelemetryOptions.TicksToMsFactor); @@ -73,6 +69,12 @@ ConcurrentDictionary requestInfoHighLatenc List allRequests = new List(); foreach (KeyValuePair entry in requestInfoHighLatencyList) { + // No metrics available to record + if (entry.Value == null || entry.Value.TotalCount == 0) + { + continue; + } + MetricInfo metricInfo = new MetricInfo(ClientTelemetryOptions.RequestLatencyName, ClientTelemetryOptions.RequestLatencyUnit); metricInfo.SetAggregators(entry.Value, ClientTelemetryOptions.TicksToMsFactor); diff --git a/Microsoft.Azure.Cosmos/src/Telemetry/TelemetrySystemUsage.cs b/Microsoft.Azure.Cosmos/src/Telemetry/TelemetrySystemUsage.cs index 9106bafd7d..8afa8a9fad 100644 --- a/Microsoft.Azure.Cosmos/src/Telemetry/TelemetrySystemUsage.cs +++ b/Microsoft.Azure.Cosmos/src/Telemetry/TelemetrySystemUsage.cs @@ -41,11 +41,8 @@ public static SystemInfo GetCpuInfo(IReadOnlyCollection systemU } } - if (histogram.TotalCount > 0) - { - systemInfo.SetAggregators(histogram, ClientTelemetryOptions.HistogramPrecisionFactor); - } - + systemInfo.SetAggregators(histogram, ClientTelemetryOptions.HistogramPrecisionFactor); + return systemInfo; } @@ -70,10 +67,7 @@ public static SystemInfo GetMemoryRemainingInfo(IReadOnlyCollection 0) - { - systemInfo.SetAggregators(histogram, ClientTelemetryOptions.KbToMbFactor); - } + systemInfo.SetAggregators(histogram, ClientTelemetryOptions.KbToMbFactor); return systemInfo; } @@ -99,10 +93,7 @@ public static SystemInfo GetAvailableThreadsInfo(IReadOnlyCollection 0) - { - systemInfo.SetAggregators(histogram); - } + systemInfo.SetAggregators(histogram); return systemInfo; } @@ -153,10 +144,7 @@ public static SystemInfo GetThreadWaitIntervalInMs(IReadOnlyCollection 0) - { - systemInfo.SetAggregators(histogram, ClientTelemetryOptions.TicksToMsFactor); - } + systemInfo.SetAggregators(histogram, ClientTelemetryOptions.TicksToMsFactor); return systemInfo; } @@ -189,11 +177,13 @@ public static SystemInfo GetTcpConnectionCount(IReadOnlyCollection 0) + if (histogram.TotalCount == 0) { - systemInfo.SetAggregators(histogram); + return null; } + systemInfo.SetAggregators(histogram); + return systemInfo; } }