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.
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 6b05d32251e6..000000000000
--- a/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/LiveMetrics/DataCollection/DocumentBuffer.cs
+++ /dev/null
@@ -1,44 +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 with a fixed capacity.
- ///
- 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;
-
- public void Add(DocumentIngress document)
- {
- // Ensure the queue does not exceed capacity.
- if (Interlocked.CompareExchange(ref _count, 0, 0) < _capacity)
- {
- _documents.Enqueue(document);
- Interlocked.Increment(ref _count);
- }
- else
- {
- AzureMonitorAspNetCoreEventSource.Log.DroppedDocument(documentType: document.DocumentType);
- }
- }
-
- 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;
- }
- }
- }
-}
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;