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

Remove InstrumentRecorder class #87873

Merged
merged 1 commit into from
Jun 21, 2023
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 @@ -116,69 +116,35 @@ public void MeterDisposeTest()
IMeterFactory meterFactory = sp.GetRequiredService<IMeterFactory>();

Meter meter = meterFactory.Create("DisposableMeter");

Counter<int> counter = meter.CreateCounter<int>("MyCounter");
InstrumentRecorder<int> recorder = new InstrumentRecorder<int>(counter);

using MeterListener listener = new MeterListener();
listener.InstrumentPublished = (instrument, theListener) =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works and I'm fine if you merge it this way, but a simpler option is:

using MeterListener listener = new MeterListener();
int lastMeasurement = 0;
listener.SetMeasurementEventCallback<int>((inst, measurement, tags, state) => lastMeasurement = measurement);
listener.EnableMeasurementEvents(counter, null);

There is no requirement to use publishing events if you already know the counter you want to listen to.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll fix this in another PR. Thanks!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

opened the PR #87898

{
if (instrument == counter)
{
listener.EnableMeasurementEvents(counter, counter);
}
};
int lastMeasurement = 0;
listener.SetMeasurementEventCallback<int>((inst, measurement, tags, state) => lastMeasurement = measurement);
listener.Start();

counter.Add(10);
Assert.Equal(1, recorder.GetMeasurements().Count());
Assert.Equal(10, recorder.GetMeasurements().ElementAt(0).Value);
Assert.Equal(10, lastMeasurement);
meter.Dispose(); // should be no-op

counter.Add(20);
Assert.Equal(2, recorder.GetMeasurements().Count());
Assert.Equal(20, recorder.GetMeasurements().ElementAt(1).Value);
Assert.Equal(20, lastMeasurement);

meter.Dispose(); // dispose again, should be no-op too
counter.Add(30);
Assert.Equal(3, recorder.GetMeasurements().Count());
Assert.Equal(30, recorder.GetMeasurements().ElementAt(2).Value);
Assert.Equal(30, lastMeasurement);

// Now dispose the factory, the meter should be disposed too
meterFactory.Dispose();
counter.Add(40); // recorder shouldn't observe this value as the meter created this instrument is disposed
Assert.Equal(3, recorder.GetMeasurements().Count());
}

[Fact]
public void InstrumentRecorderTest()
{
ServiceCollection services = new ServiceCollection();
services.AddMetrics();
var sp = services.BuildServiceProvider();
using IMeterFactory meterFactory = sp.GetRequiredService<IMeterFactory>();

MeterOptions options = new MeterOptions("name")
{
Version = "version",
Tags = new TagList() { { "key1", "value1" }, { "key2", "value2" } }
};

Meter meter = meterFactory.Create("MyMeter", "1.0.0", new TagList() { { "key1", "value1" }, { "key2", "value2" } });
Assert.Same(meterFactory, meter.Scope);

Counter<int> counter = meter.CreateCounter<int>("MyCounter");

InstrumentRecorder<int> recorder1 = new InstrumentRecorder<int>(counter);
Assert.Same(counter, recorder1.Instrument);

InstrumentRecorder<int> recorder2 = new InstrumentRecorder<int>(meter, "MyCounter");
Assert.Same(counter, recorder2.Instrument);

InstrumentRecorder<int> recorder3 = new InstrumentRecorder<int>(scopeFilter: meterFactory, "MyMeter", "MyCounter");
Assert.Same(counter, recorder3.Instrument);

counter.Add(100, new KeyValuePair<string, object?>("k", "v"));
Assert.Equal(1, recorder1.GetMeasurements().Count());
Assert.Equal(1, recorder2.GetMeasurements().Count());
Assert.Equal(1, recorder3.GetMeasurements().Count());

Assert.Equal(100, recorder1.GetMeasurements().ElementAt(0).Value);
Assert.Equal(100, recorder2.GetMeasurements().ElementAt(0).Value);
Assert.Equal(100, recorder2.GetMeasurements().ElementAt(0).Value);

KeyValuePair<string, object?>[] tags = new KeyValuePair<string, object?>[] { new KeyValuePair<string, object?>("k", "v") };
Assert.Equal(tags, recorder1.GetMeasurements().ElementAt(0).Tags.ToArray());
Assert.Equal(tags, recorder2.GetMeasurements().ElementAt(0).Tags.ToArray());
Assert.Equal(tags, recorder3.GetMeasurements().ElementAt(0).Tags.ToArray());
Assert.Equal(30, lastMeasurement);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,15 +389,6 @@ public abstract class Instrument<T> : Instrument where T : struct
protected void RecordMeasurement(T measurement, in TagList tagList) { throw null; }
protected void RecordMeasurement(T measurement, ReadOnlySpan<System.Collections.Generic.KeyValuePair<string, object?>> tags) { throw null; }
}
public sealed class InstrumentRecorder<T> : IDisposable where T : struct
{
public InstrumentRecorder(Instrument instrument) { throw null; }
public InstrumentRecorder(object? scopeFilter, string meterName, string instrumentName) { throw null; }
public InstrumentRecorder(Meter meter, string instrumentName) { throw null; }
public Instrument? Instrument { get { throw null; } }
public System.Collections.Generic.IEnumerable<Measurement<T>> GetMeasurements(bool clear = false) { throw null; }
public void Dispose() { throw null; }
}
public readonly struct Measurement<T> where T : struct
{
public Measurement(T value) { throw null; }
Expand Down Expand Up @@ -545,7 +536,7 @@ public class MeterOptions
public System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string,object?>>? Tags { get { throw null;} set { throw null;} }
public object? Scope { get { throw null;} set { throw null;} }
public MeterOptions(string name) { throw null;}
}
}
public sealed class ObservableCounter<T> : ObservableInstrument<T> where T : struct
{
internal ObservableCounter(Meter meter, string name, string? unit, string? description) : base(meter, name, unit, description) { throw null; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ System.Diagnostics.DiagnosticSource</PackageDescription>
<Compile Include="System\Diagnostics\Metrics\Histogram.cs" />
<Compile Include="System\Diagnostics\Metrics\Instrument.cs" />
<Compile Include="System\Diagnostics\Metrics\Instrument.common.cs" />
<Compile Include="System\Diagnostics\Metrics\InstrumentRecorder.cs" />
<Compile Include="System\Diagnostics\Metrics\InstrumentState.cs" />
<Compile Include="System\Diagnostics\Metrics\LastValueAggregator.cs" />
<Compile Include="System\Diagnostics\Metrics\Measurement.cs" />
Expand Down

This file was deleted.

Loading