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

Emit More EventSource Data For Metrics Measurements #104993

Merged
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 @@ -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);
tarekgh marked this conversation as resolved.
Show resolved Hide resolved
}
}
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
Loading