Skip to content

Commit

Permalink
Reduce the data emitted with metric value publishing
Browse files Browse the repository at this point in the history
  • Loading branch information
tarekgh committed Jul 21, 2024
1 parent 8501906 commit 95024ec
Show file tree
Hide file tree
Showing 7 changed files with 257 additions and 407 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ internal static string FormatTags(IEnumerable<KeyValuePair<string, object?>>? ta

internal static string FormatTags(KeyValuePair<string, string>[] labels)
{
if (labels is null || labels.Length == 0)
{
return string.Empty;
}

StringBuilder sb = new StringBuilder();
for (int i = 0; i < labels.Length; i++)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ internal sealed class AggregationManager
private readonly MeterListener _listener;
private int _currentTimeSeries;
private int _currentHistograms;
private readonly Action<Instrument, LabeledAggregationStatistics> _collectMeasurement;
private readonly Action<Instrument, LabeledAggregationStatistics, InstrumentState?> _collectMeasurement;
private readonly Action<DateTime, DateTime> _beginCollection;
private readonly Action<DateTime, DateTime> _endCollection;
private readonly Action<Instrument> _beginInstrumentMeasurements;
private readonly Action<Instrument> _endInstrumentMeasurements;
private readonly Action<Instrument> _instrumentPublished;
private readonly Action<Instrument, InstrumentState> _beginInstrumentMeasurements;
private readonly Action<Instrument, InstrumentState> _endInstrumentMeasurements;
private readonly Action<Instrument, InstrumentState?> _instrumentPublished;
private readonly Action _initialInstrumentEnumerationComplete;
private readonly Action<Exception> _collectionError;
private readonly Action _timeSeriesLimitReached;
Expand All @@ -47,12 +47,12 @@ internal sealed class AggregationManager
public AggregationManager(
int maxTimeSeries,
int maxHistograms,
Action<Instrument, LabeledAggregationStatistics> collectMeasurement,
Action<Instrument, LabeledAggregationStatistics, InstrumentState?> collectMeasurement,
Action<DateTime, DateTime> beginCollection,
Action<DateTime, DateTime> endCollection,
Action<Instrument> beginInstrumentMeasurements,
Action<Instrument> endInstrumentMeasurements,
Action<Instrument> instrumentPublished,
Action<Instrument, InstrumentState> beginInstrumentMeasurements,
Action<Instrument, InstrumentState> endInstrumentMeasurements,
Action<Instrument, InstrumentState?> instrumentPublished,
Action initialInstrumentEnumerationComplete,
Action<Exception> collectionError,
Action timeSeriesLimitReached,
Expand Down Expand Up @@ -118,17 +118,18 @@ public AggregationManager SetCollectionPeriod(TimeSpan collectionPeriod)
private void CompletedMeasurements(Instrument instrument, object? cookie)
{
_instruments.Remove(instrument);
_endInstrumentMeasurements(instrument);
Debug.Assert(cookie is not null);
_endInstrumentMeasurements(instrument, (InstrumentState)cookie);
RemoveInstrumentState(instrument);
}

private void PublishedInstrument(Instrument instrument, MeterListener _)
{
_instrumentPublished(instrument);
InstrumentState? state = GetInstrumentState(instrument);
_instrumentPublished(instrument, state);
if (state != null)
{
_beginInstrumentMeasurements(instrument);
_beginInstrumentMeasurements(instrument, state);
#pragma warning disable CA1864 // Prefer the 'IDictionary.TryAdd(TKey, TValue)' method. IDictionary.TryAdd() is not available in one of the builds
if (!_instruments.ContainsKey(instrument))
#pragma warning restore CA1864
Expand Down Expand Up @@ -418,7 +419,7 @@ internal void Collect()
{
kv.Value.Collect(kv.Key, (LabeledAggregationStatistics labeledAggStats) =>
{
_collectMeasurement(kv.Key, labeledAggStats);
_collectMeasurement(kv.Key, labeledAggStats, kv.Value);
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ public abstract class Instrument
{
internal static KeyValuePair<string, object?>[] EmptyTags => Array.Empty<KeyValuePair<string, object?>>();

private string? _formattedTags;

// The SyncObject is used to synchronize the following operations:
// - Instrument.Publish()
// - Meter constructor
Expand Down Expand Up @@ -145,8 +143,6 @@ protected void Publish()
/// </summary>
public virtual bool IsObservable => false;

internal string FormattedTags => _formattedTags ??= Helpers.FormatTags(Tags);

// NotifyForUnpublishedInstrument is called from Meter.Dispose()
internal void NotifyForUnpublishedInstrument()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Collections.Generic;
using System.Security;
using System.Threading;

namespace System.Diagnostics.Metrics
{
Expand All @@ -14,16 +15,19 @@ internal abstract class InstrumentState

// This can be called concurrently with Update()
public abstract void Collect(Instrument instrument, Action<LabeledAggregationStatistics> aggregationVisitFunc);
}

public abstract int ID { get; }
}

internal sealed class InstrumentState<TAggregator> : InstrumentState
where TAggregator : Aggregator
{
private AggregatorStore<TAggregator> _aggregatorStore;
private static int s_idCounter;

public InstrumentState(Func<TAggregator?> createAggregatorFunc)
{
ID = Interlocked.Increment(ref s_idCounter);
_aggregatorStore = new AggregatorStore<TAggregator>(createAggregatorFunc);
}

Expand All @@ -38,5 +42,7 @@ public override void Update(double measurement, ReadOnlySpan<KeyValuePair<string
TAggregator? aggregator = _aggregatorStore.GetAggregator(labels);
aggregator?.Update(measurement);
}

public override int ID { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,10 @@ public class Meter : IDisposable
private List<Instrument> _instruments = new List<Instrument>();
private Dictionary<string, List<Instrument>> _nonObservableInstrumentsCache = new();

private string? _formattedTags;
private string? _formattedScopeHash;

internal bool Disposed { get; private set; }

internal static bool IsSupported { get; } = InitializeIsSupported();

internal string FormattedTags => _formattedTags ??= Helpers.FormatTags(Tags);
internal string FormattedScopeHash => _formattedScopeHash ??= Helpers.FormatObjectHash(Scope);

private static bool InitializeIsSupported() =>
AppContext.TryGetSwitch("System.Diagnostics.Metrics.Meter.IsSupported", out bool isSupported) ? isSupported : true;

Expand Down
Loading

0 comments on commit 95024ec

Please sign in to comment.