Skip to content

Commit

Permalink
Remove InstrumentRecorder class (#87873)
Browse files Browse the repository at this point in the history
  • Loading branch information
tarekgh authored Jun 21, 2023
1 parent 1900574 commit babe6d6
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 355 deletions.
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) =>
{
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

0 comments on commit babe6d6

Please sign in to comment.