Skip to content

Commit

Permalink
Delete MeterListener.
Browse files Browse the repository at this point in the history
  • Loading branch information
bgrainger committed Oct 28, 2023
1 parent d3ec1b9 commit 03aa268
Showing 1 changed file with 0 additions and 59 deletions.
59 changes: 0 additions & 59 deletions tests/MySqlConnector.Tests/Metrics/MetricsTestsBase.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#nullable enable

using System.Diagnostics.Metrics;

namespace MySqlConnector.Tests.Metrics;

public abstract class MetricsTestsBase : IDisposable
Expand All @@ -13,18 +11,6 @@ public MetricsTestsBase()

Server = new FakeMySqlServer();
Server.Start();

m_meterListener = new MeterListener
{
InstrumentPublished = (instrument, listener) =>
{
if (instrument.Meter.Name == "MySqlConnector")
listener.EnableMeasurementEvents(instrument);
}
};
m_meterListener.SetMeasurementEventCallback<int>(OnMeasurementRecorded);
m_meterListener.SetMeasurementEventCallback<float>(OnMeasurementRecorded);
m_meterListener.Start();
}

public void Dispose()
Expand All @@ -37,7 +23,6 @@ protected virtual void Dispose(bool disposing)
{
if (disposing)
{
m_meterListener.Dispose();
Server.Stop();
}
}
Expand Down Expand Up @@ -68,50 +53,6 @@ protected List<float> GetAndClearMeasurements(string name)
return list;
}

private void OnMeasurementRecorded(Instrument instrument, int measurement, ReadOnlySpan<KeyValuePair<string, object?>> tags, object? state)
{
var (poolName, stateTag) = GetTags(tags);
if (poolName != PoolName)
return;

lock (m_measurements)
{
m_measurements[instrument.Name] = m_measurements.GetValueOrDefault(instrument.Name) + measurement;
if (stateTag.Length != 0)
m_measurements[$"{instrument.Name}|{stateTag}"] = m_measurements.GetValueOrDefault($"{instrument.Name}|{stateTag}") + measurement;
}
}

private void OnMeasurementRecorded(Instrument instrument, float measurement, ReadOnlySpan<KeyValuePair<string, object?>> tags, object? state)
{
var (poolName, stateTag) = GetTags(tags);
if (poolName != PoolName)
return;

lock (m_timeMeasurements)
{
if (!m_timeMeasurements.TryGetValue(instrument.Name, out var list))
list = m_timeMeasurements[instrument.Name] = new List<float>();
list.Add(measurement);
}
}

private (string PoolName, string State) GetTags(ReadOnlySpan<KeyValuePair<string, object?>> tags)
{
var poolName = "";
var state = "";
foreach (var tag in tags)
{
if (tag.Key == "pool.name" && tag.Value is string s1)
poolName = s1;
else if (tag.Key == "state" && tag.Value is string s2)
state = s2;
}
return (poolName, state);
}


private readonly Dictionary<string, int> m_measurements;
private readonly Dictionary<string, List<float>> m_timeMeasurements;
private readonly MeterListener m_meterListener;
}

0 comments on commit 03aa268

Please sign in to comment.