Skip to content

Commit

Permalink
MeterListener.Dispose always disables measurements (#102661)
Browse files Browse the repository at this point in the history
  • Loading branch information
spanglerco authored and pull[bot] committed Jun 3, 2024
1 parent 4dd1b5e commit 102b976
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -258,14 +258,17 @@ public void Dispose()
s_allStartedListeners.Remove(this);

DiagNode<Instrument>? current = _enabledMeasurementInstruments.First;
if (current is not null && measurementsCompleted is not null)
if (current is not null)
{
callbacksArguments = new Dictionary<Instrument, object?>();
if (measurementsCompleted is not null)
{
callbacksArguments = new Dictionary<Instrument, object?>();
}

do
{
object? state = current.Value.DisableMeasurements(this);
callbacksArguments.Add(current.Value, state);
callbacksArguments?.Add(current.Value, state);
current = current.Next;
} while (current is not null);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,63 @@ public void ListenerDisposalsTest()
}).Dispose();
}

[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
public void ListenerWithoutMeasurementsCompletedDisposalsTest()
{
RemoteExecutor.Invoke(() => {
Meter meter = new Meter("MinimalListenerDisposalsTest");
Counter<int> counter = meter.CreateCounter<int>("Counter");
UpDownCounter<short> upDownCounter = meter.CreateUpDownCounter<short>("upDownCounter");
Histogram<double> histogram = meter.CreateHistogram<double>("Histogram");
ObservableCounter<long> observableCounter = meter.CreateObservableCounter("ObservableCounter", () => new Measurement<long>(10, new KeyValuePair<string, object?>[] { new KeyValuePair<string, object?>("Key", "value")}));
ObservableGauge<decimal> observableGauge = meter.CreateObservableGauge("ObservableGauge", () => new Measurement<decimal>(5.7m, new KeyValuePair<string, object?>[] { new KeyValuePair<string, object?>("Key", "value")}));
ObservableUpDownCounter<float> observableUpDownCounter = meter.CreateObservableUpDownCounter("ObservableUpDownCounter", () => new Measurement<float>(-5.7f, new KeyValuePair<string, object?>[] { new KeyValuePair<string, object?>("Key", "value")}));
MeterListener listener = new MeterListener();
listener.InstrumentPublished = (theInstrument, theListener) => theListener.EnableMeasurementEvents(theInstrument, theInstrument);
int count = 0;
listener.SetMeasurementEventCallback<short>((inst, measurement, tags, state) => count++);
listener.SetMeasurementEventCallback<int>((inst, measurement, tags, state) => count++);
listener.SetMeasurementEventCallback<float>((inst, measurement, tags, state) => count++);
listener.SetMeasurementEventCallback<double>((inst, measurement, tags, state) => count++);
listener.SetMeasurementEventCallback<long>((inst, measurement, tags, state) => count++);
listener.SetMeasurementEventCallback<decimal>((inst, measurement, tags, state) => count++);
listener.Start();
Assert.Equal(0, count);
counter.Add(1);
Assert.Equal(1, count);
upDownCounter.Add(-1);
Assert.Equal(2, count);
histogram.Record(1);
Assert.Equal(3, count);
listener.RecordObservableInstruments();
Assert.Equal(6, count);
listener.Dispose();
counter.Add(1);
Assert.Equal(6, count);
upDownCounter.Add(-1);
Assert.Equal(6, count);
histogram.Record(1);
Assert.Equal(6, count);
listener.RecordObservableInstruments();
Assert.Equal(6, count);
}).Dispose();
}

[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
public void MultipleListenersTest()
{
Expand Down

0 comments on commit 102b976

Please sign in to comment.