Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure MeterListener.RecordObservableInstruments invoking all instruments callbacks #56183

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -185,16 +185,30 @@ public void Start()
/// </summary>
public void RecordObservableInstruments()
{
List<Exception>? exceptionsList = null;
DiagNode<Instrument>? current = _enabledMeasurementInstruments.First;
while (current is not null)
{
if (current.Value.IsObservable)
{
current.Value.Observe(this);
try
{
current.Value.Observe(this);
}
catch (Exception e)
{
exceptionsList ??= new List<Exception>();
exceptionsList.Add(e);
}
}

current = current.Next;
}

if (exceptionsList is not null)
{
throw new AggregateException(exceptionsList);
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,50 @@ public void ListeningToInstrumentsPublishingTest()
}).Dispose();
}

[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
public void ThrowingExceptionsFromObservableInstrumentCallbacks()
{
RemoteExecutor.Invoke(() => {
using Meter meter = new Meter("ThrowingExceptionsFromObservableInstrumentCallbacks");

using (MeterListener listener = new MeterListener())
{
ObservableCounter<int> counter1 = meter.CreateObservableCounter<int>("observableCounter1", (Func<int>) (() => throw new ArgumentOutOfRangeException()));
ObservableGauge<int> gauge1 = meter.CreateObservableGauge<int>("observableGauge1", (Func<int>)(() => throw new ArgumentException()));
ObservableCounter<int> counter2 = meter.CreateObservableCounter<int>("observableCounter2", (Func<int>)(() => throw new PlatformNotSupportedException()));
ObservableGauge<int> gauge2 = meter.CreateObservableGauge<int>("observableGauge2", (Func<int>)(() => throw new NullReferenceException()));
ObservableCounter<int> counter3 = meter.CreateObservableCounter<int>("observableCounter3", () => 5);
ObservableGauge<int> gauge3 = meter.CreateObservableGauge<int>("observableGauge3", () => 7);

listener.EnableMeasurementEvents(counter1, null);
listener.EnableMeasurementEvents(gauge1, null);
listener.EnableMeasurementEvents(counter2, null);
listener.EnableMeasurementEvents(gauge2, null);
listener.EnableMeasurementEvents(counter3, null);
listener.EnableMeasurementEvents(gauge3, null);

int accumulated = 0;

listener.SetMeasurementEventCallback<int>((inst, measurement, tags, state) => accumulated += measurement);

Exception exception = Record.Exception(() => listener.RecordObservableInstruments());
Assert.NotNull(exception);
Assert.IsType<AggregateException>(exception);
AggregateException ae = exception as AggregateException;
Assert.Equal(4, ae.InnerExceptions.Count);

Assert.IsType<ArgumentOutOfRangeException>(ae.InnerExceptions[0]);
Assert.IsType<ArgumentException>(ae.InnerExceptions[1]);
Assert.IsType<PlatformNotSupportedException>(ae.InnerExceptions[2]);
Assert.IsType<NullReferenceException>(ae.InnerExceptions[3]);

// Ensure the instruments which didn't throw reported correct measurements.
Assert.Equal(12, accumulated);
}
tarekgh marked this conversation as resolved.
Show resolved Hide resolved

}).Dispose();
}

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