Skip to content

Commit

Permalink
Emit More EventSource Data For Metrics Measurements (dotnet#104993)
Browse files Browse the repository at this point in the history
* Emit More EventSource Data For Metrics Measurements

* rename event param names for consistency

* Update src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Helpers.cs

Co-authored-by: Stephen Toub <stoub@microsoft.com>

* Use int for meter and instrument hashes

* Reduce the data emitted with metric value publishing

---------

Co-authored-by: Stephen Toub <stoub@microsoft.com>
  • Loading branch information
tarekgh and stephentoub authored Jul 21, 2024
1 parent 7069cb3 commit 9e23387
Show file tree
Hide file tree
Showing 8 changed files with 218 additions and 146 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ System.Diagnostics.DiagnosticSource</PackageDescription>
<Compile Include="System\Diagnostics\DsesSamplerBuilder.cs" />
<Compile Include="System\Diagnostics\DistributedContextPropagator.cs" />
<Compile Include="System\Diagnostics\DsesFilterAndTransform.cs" />
<Compile Include="System\Diagnostics\Helpers.cs" />
<Compile Include="System\Diagnostics\LegacyPropagator.cs" />
<Compile Include="System\Diagnostics\NoOutputPropagator.cs" />
<Compile Include="System\Diagnostics\PassThroughPropagator.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Text;

namespace System.Diagnostics
{
internal static class Helpers
{
internal static string FormatTags(IEnumerable<KeyValuePair<string, object?>>? tags)
{
if (tags is null)
{
return string.Empty;
}

StringBuilder sb = new StringBuilder();
bool first = true;
foreach (KeyValuePair<string, object?> tag in tags)
{
if (first)
{
first = false;
}
else
{
sb.Append(',');
}

sb.Append(tag.Key).Append('=').Append(tag.Value);
}
return sb.ToString();
}

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++)
{
sb.Append(labels[i].Key).Append('=').Append(labels[i].Value);
if (i != labels.Length - 1)
{
sb.Append(',');
}
}
return sb.ToString();
}

internal static string FormatObjectHash(object? obj) =>
obj is null ? string.Empty : RuntimeHelpers.GetHashCode(obj).ToString(CultureInfo.InvariantCulture);
}
}
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 @@ -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 @@ -17,6 +17,7 @@ public class Meter : IDisposable
private static readonly List<Meter> s_allMeters = new List<Meter>();
private List<Instrument> _instruments = new List<Instrument>();
private Dictionary<string, List<Instrument>> _nonObservableInstrumentsCache = new();

internal bool Disposed { get; private set; }

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

0 comments on commit 9e23387

Please sign in to comment.