From ae3b6f5227f3e9d706ddb6ed90d5f4f12ea23326 Mon Sep 17 00:00:00 2001 From: "Timothy Mothra Lee (from Dev Box)" Date: Thu, 3 Oct 2024 17:02:29 -0700 Subject: [PATCH 1/4] removing buffer capacity --- .../src/LiveMetrics/DataCollection/DocumentBuffer.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/LiveMetrics/DataCollection/DocumentBuffer.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/LiveMetrics/DataCollection/DocumentBuffer.cs index 6b05d32251e6..e706a67b3cbc 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/LiveMetrics/DataCollection/DocumentBuffer.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/LiveMetrics/DataCollection/DocumentBuffer.cs @@ -7,12 +7,11 @@ namespace Azure.Monitor.OpenTelemetry.AspNetCore.LiveMetrics.DataCollection { /// - /// Manages a thread-safe collection of DocumentIngress objects with a fixed capacity. + /// Manages a thread-safe collection of DocumentIngress objects. /// internal class DocumentBuffer { private readonly ConcurrentQueue _documents = new(); - private readonly int _capacity = 20; // ConcurrentQueue.Count is not used because it is not an O(1) operation. Instead, we use a separate counter. // Atomic counter for the number of documents in the queue. private int _count = 0; @@ -20,7 +19,7 @@ internal class DocumentBuffer public void Add(DocumentIngress document) { // Ensure the queue does not exceed capacity. - if (Interlocked.CompareExchange(ref _count, 0, 0) < _capacity) + if (Interlocked.CompareExchange(ref _count, 0, 0) < int.MaxValue) { _documents.Enqueue(document); Interlocked.Increment(ref _count); From 7589b26299ed955395887623721957ac32d7d230 Mon Sep 17 00:00:00 2001 From: "Timothy Mothra Lee (from Dev Box)" Date: Thu, 3 Oct 2024 17:19:12 -0700 Subject: [PATCH 2/4] remove counter --- .../LiveMetrics/DataCollection/DocumentBuffer.cs | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/LiveMetrics/DataCollection/DocumentBuffer.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/LiveMetrics/DataCollection/DocumentBuffer.cs index e706a67b3cbc..5d689eca79fe 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/LiveMetrics/DataCollection/DocumentBuffer.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/LiveMetrics/DataCollection/DocumentBuffer.cs @@ -12,28 +12,14 @@ namespace Azure.Monitor.OpenTelemetry.AspNetCore.LiveMetrics.DataCollection internal class DocumentBuffer { private readonly ConcurrentQueue _documents = new(); - // ConcurrentQueue.Count is not used because it is not an O(1) operation. Instead, we use a separate counter. - // Atomic counter for the number of documents in the queue. - private int _count = 0; public void Add(DocumentIngress document) { - // Ensure the queue does not exceed capacity. - if (Interlocked.CompareExchange(ref _count, 0, 0) < int.MaxValue) - { - _documents.Enqueue(document); - Interlocked.Increment(ref _count); - } - else - { - AzureMonitorAspNetCoreEventSource.Log.DroppedDocument(documentType: document.DocumentType); - } + _documents.Enqueue(document); } public IEnumerable ReadAllAndClear() { - // There is no need to decrement the count since we are clearing the queue. After this operation, the instance will not be used anymore. - // The method 'Add' is not called while this method is running; therefore, the count will remain unchanged. while (_documents.TryDequeue(out DocumentIngress? item)) { yield return item; From f41aac13246747d5a373405269b949f3c28e87c5 Mon Sep 17 00:00:00 2001 From: "Timothy Mothra Lee (from Dev Box)" Date: Thu, 3 Oct 2024 18:06:39 -0700 Subject: [PATCH 3/4] remove DocumentBuffer class --- .../DataCollection/DocumentBuffer.cs | 29 ------------------- .../DataCollection/DoubleBuffer.cs | 9 +++--- .../src/LiveMetrics/Manager.Metrics.cs | 6 ++-- 3 files changed, 9 insertions(+), 35 deletions(-) delete mode 100644 sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/LiveMetrics/DataCollection/DocumentBuffer.cs diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/LiveMetrics/DataCollection/DocumentBuffer.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/LiveMetrics/DataCollection/DocumentBuffer.cs deleted file mode 100644 index 5d689eca79fe..000000000000 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/LiveMetrics/DataCollection/DocumentBuffer.cs +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -using System.Collections.Concurrent; -using Azure.Monitor.OpenTelemetry.AspNetCore.Models; - -namespace Azure.Monitor.OpenTelemetry.AspNetCore.LiveMetrics.DataCollection -{ - /// - /// Manages a thread-safe collection of DocumentIngress objects. - /// - internal class DocumentBuffer - { - private readonly ConcurrentQueue _documents = new(); - - public void Add(DocumentIngress document) - { - _documents.Enqueue(document); - } - - public IEnumerable ReadAllAndClear() - { - while (_documents.TryDequeue(out DocumentIngress? item)) - { - yield return item; - } - } - } -} diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/LiveMetrics/DataCollection/DoubleBuffer.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/LiveMetrics/DataCollection/DoubleBuffer.cs index 9b91dae14f40..e14202d6a57a 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/LiveMetrics/DataCollection/DoubleBuffer.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/LiveMetrics/DataCollection/DoubleBuffer.cs @@ -1,6 +1,7 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. +using System.Collections.Concurrent; using Azure.Monitor.OpenTelemetry.AspNetCore.Models; namespace Azure.Monitor.OpenTelemetry.AspNetCore.LiveMetrics.DataCollection @@ -14,17 +15,17 @@ namespace Azure.Monitor.OpenTelemetry.AspNetCore.LiveMetrics.DataCollection /// internal class DoubleBuffer { - private DocumentBuffer _currentBuffer = new(); + private ConcurrentQueue _currentBuffer = new(); public void WriteDocument(DocumentIngress document) { - _currentBuffer.Add(document); + _currentBuffer.Enqueue(document); } - public DocumentBuffer FlipDocumentBuffers() + public ConcurrentQueue FlipDocumentBuffers() { // Atomically exchange the current buffer with a new empty buffer and return the old buffer - return Interlocked.Exchange(ref _currentBuffer, new DocumentBuffer()); + return Interlocked.Exchange(ref _currentBuffer, new ConcurrentQueue()); } } } diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/LiveMetrics/Manager.Metrics.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/LiveMetrics/Manager.Metrics.cs index 628769a1cc32..c899948373f9 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/LiveMetrics/Manager.Metrics.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/LiveMetrics/Manager.Metrics.cs @@ -1,6 +1,7 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. +using System.Collections.Concurrent; using Azure.Monitor.OpenTelemetry.AspNetCore.LiveMetrics; using Azure.Monitor.OpenTelemetry.AspNetCore.LiveMetrics.DataCollection; using Azure.Monitor.OpenTelemetry.AspNetCore.LiveMetrics.Filtering; @@ -45,9 +46,10 @@ public MonitoringDataPoint GetDataPoint() string projectionError = string.Empty; Dictionary metricAccumulators = CreateMetricAccumulators(_collectionConfiguration); LiveMetricsBuffer liveMetricsBuffer = new(); - DocumentBuffer filledBuffer = _documentBuffer.FlipDocumentBuffers(); IEnumerable documentStreams = _collectionConfiguration.DocumentStreams; - foreach (var item in filledBuffer.ReadAllAndClear()) + + ConcurrentQueue filledBuffer = _documentBuffer.FlipDocumentBuffers(); + while (filledBuffer.TryDequeue(out DocumentIngress? item)) { bool matchesDocumentStreamFilter = false; From ba64cb55dfa19036f150000d4ad8beba76b5713e Mon Sep 17 00:00:00 2001 From: "Timothy Mothra Lee (from Dev Box)" Date: Fri, 4 Oct 2024 09:50:52 -0700 Subject: [PATCH 4/4] changelog --- .../Azure.Monitor.OpenTelemetry.AspNetCore/CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/CHANGELOG.md b/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/CHANGELOG.md index 00059aa11ba7..cecfe69c2b9b 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/CHANGELOG.md +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/CHANGELOG.md @@ -21,6 +21,9 @@ * Fixed a bug in LiveMetrics that counted all manually created Dependencies as failures. ([#45103](https://github.com/Azure/azure-sdk-for-net/pull/45103)) +* Fixed a bug in LiveMetrics that caused incorrect counts for telemetry. + ([#46429](https://github.com/Azure/azure-sdk-for-net/pull/46429)) + ### Other Changes * Updated the code of vendored resource detector library `OpenTelemetry.Resources.Azure` from the OpenTelemetry .NET contrib repository.