Skip to content

Commit

Permalink
Use int for meter and instrument hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
tarekgh committed Jul 20, 2024
1 parent e1b2689 commit 8501906
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public abstract class Instrument
internal static KeyValuePair<string, object?>[] EmptyTags => Array.Empty<KeyValuePair<string, object?>>();

private string? _formattedTags;
private string? _formattedHash;

// The SyncObject is used to synchronize the following operations:
// - Instrument.Publish()
Expand Down Expand Up @@ -147,7 +146,6 @@ protected void Publish()
public virtual bool IsObservable => false;

internal string FormattedTags => _formattedTags ??= Helpers.FormatTags(Tags);
internal string FormattedHash => _formattedHash ??= Helpers.FormatObjectHash(this);

// NotifyForUnpublishedInstrument is called from Meter.Dispose()
internal void NotifyForUnpublishedInstrument()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@ public class Meter : IDisposable

private string? _formattedTags;
private string? _formattedScopeHash;
private string? _formattedHash;

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);
internal string FormattedHash => _formattedHash ??= Helpers.FormatObjectHash(this);

private static bool InitializeIsSupported() =>
AppContext.TryGetSwitch("System.Diagnostics.Metrics.Meter.IsSupported", out bool isSupported) ? isSupported : true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public void CollectionStop(string sessionId, DateTime intervalStartTime, DateTim
Justification = "This calls WriteEvent with all primitive arguments which is safe. Primitives are always serialized properly.")]
#endif
public void CounterRateValuePublished(string sessionId, string meterName, string? meterVersion, string instrumentName, string? unit, string tags, string rate, string value,
string instrumentTags, string instrumentHash, string meterTags, string meterHash, string scopeHash)
string instrumentTags, int instrumentHash, string meterTags, int meterHash, string scopeHash)
{
WriteEvent(4, sessionId, meterName, meterVersion ?? "", instrumentName, unit ?? "", tags, rate, value, instrumentTags, instrumentHash, meterTags, meterHash, scopeHash);
}
Expand All @@ -132,7 +132,7 @@ public void CounterRateValuePublished(string sessionId, string meterName, string
Justification = "This calls WriteEvent with all primitive arguments which is safe. Primitives are always serialized properly.")]
#endif
public void GaugeValuePublished(string sessionId, string meterName, string? meterVersion, string instrumentName, string? unit, string tags, string lastValue,
string instrumentTags, string instrumentHash, string meterTags, string meterHash, string scopeHash)
string instrumentTags, int instrumentHash, string meterTags, int meterHash, string scopeHash)
{
WriteEvent(5, sessionId, meterName, meterVersion ?? "", instrumentName, unit ?? "", tags, lastValue, instrumentTags, instrumentHash, meterTags, meterHash, scopeHash);
}
Expand All @@ -143,7 +143,7 @@ public void GaugeValuePublished(string sessionId, string meterName, string? mete
Justification = "This calls WriteEvent with all primitive arguments which is safe. Primitives are always serialized properly.")]
#endif
public void HistogramValuePublished(string sessionId, string meterName, string? meterVersion, string instrumentName, string? unit, string tags, string quantiles, int count, double sum,
string instrumentTags, string instrumentHash, string meterTags, string meterHash, string scopeHash)
string instrumentTags, int instrumentHash, string meterTags, int meterHash, string scopeHash)
{
WriteEvent(6, sessionId, meterName, meterVersion ?? "", instrumentName, unit ?? "", tags, quantiles, count, sum, instrumentTags, instrumentHash, meterTags, meterHash, scopeHash);
}
Expand All @@ -167,8 +167,8 @@ public void BeginInstrumentReporting(
string instrumentTags,
string meterTags,
string meterScopeHash,
string instrumentHash,
string meterHash)
int instrumentHash,
int meterHash)
{
WriteEvent(7, sessionId, meterName, meterVersion ?? "", instrumentName, instrumentType, unit ?? "", description ?? "",
instrumentTags, meterTags, meterScopeHash, instrumentHash, meterHash);
Expand All @@ -192,8 +192,8 @@ public void EndInstrumentReporting(
string instrumentTags,
string meterTags,
string meterScopeHash,
string instrumentHash,
string meterHash)
int instrumentHash,
int meterHash)
{
WriteEvent(8, sessionId, meterName, meterVersion ?? "", instrumentName, instrumentType, unit ?? "", description ?? "",
instrumentTags, meterTags, meterScopeHash, instrumentHash, meterHash);
Expand Down Expand Up @@ -227,8 +227,8 @@ public void InstrumentPublished(
string instrumentTags,
string meterTags,
string meterScopeHash,
string instrumentHash,
string meterHash)
int instrumentHash,
int meterHash)
{
WriteEvent(11, sessionId, meterName, meterVersion ?? "", instrumentName, instrumentType, unit ?? "", description ?? "",
instrumentTags, meterTags, meterScopeHash, instrumentHash, meterHash);
Expand Down Expand Up @@ -264,7 +264,7 @@ public void MultipleSessionsNotSupportedError(string runningSessionId)
Justification = "This calls WriteEvent with all primitive arguments which is safe. Primitives are always serialized properly.")]
#endif
public void UpDownCounterRateValuePublished(string sessionId, string meterName, string? meterVersion, string instrumentName, string? unit, string tags, string rate, string value,
string instrumentTags, string instrumentHash, string meterTags, string meterHash, string scopeHash)
string instrumentTags, int instrumentHash, string meterTags, int meterHash, string scopeHash)
{
WriteEvent(16, sessionId, meterName, meterVersion ?? "", instrumentName, unit ?? "", tags, rate, value, instrumentTags, instrumentHash, meterTags, meterHash, scopeHash);
}
Expand Down Expand Up @@ -448,11 +448,11 @@ public void OnEventCommand(EventCommandEventArgs command)
(startIntervalTime, endIntervalTime) => Parent.CollectionStart(sessionId, startIntervalTime, endIntervalTime),
(startIntervalTime, endIntervalTime) => Parent.CollectionStop(sessionId, startIntervalTime, endIntervalTime),
i => Parent.BeginInstrumentReporting(sessionId, i.Meter.Name, i.Meter.Version, i.Name, i.GetType().Name, i.Unit, i.Description,
i.FormattedTags, i.Meter.FormattedTags, i.Meter.FormattedScopeHash, i.FormattedHash, i.Meter.FormattedHash),
i.FormattedTags, i.Meter.FormattedTags, i.Meter.FormattedScopeHash, RuntimeHelpers.GetHashCode(i), RuntimeHelpers.GetHashCode(i.Meter)),
i => Parent.EndInstrumentReporting(sessionId, i.Meter.Name, i.Meter.Version, i.Name, i.GetType().Name, i.Unit, i.Description,
i.FormattedTags, i.Meter.FormattedTags, i.Meter.FormattedScopeHash, i.FormattedHash, i.Meter.FormattedHash),
i.FormattedTags, i.Meter.FormattedTags, i.Meter.FormattedScopeHash, RuntimeHelpers.GetHashCode(i), RuntimeHelpers.GetHashCode(i.Meter)),
i => Parent.InstrumentPublished(sessionId, i.Meter.Name, i.Meter.Version, i.Name, i.GetType().Name, i.Unit, i.Description,
i.FormattedTags, i.Meter.FormattedTags, i.Meter.FormattedScopeHash, i.FormattedHash, i.Meter.FormattedHash),
i.FormattedTags, i.Meter.FormattedTags, i.Meter.FormattedScopeHash, RuntimeHelpers.GetHashCode(i), RuntimeHelpers.GetHashCode(i.Meter)),
() => Parent.InitialInstrumentEnumerationComplete(sessionId),
e => Parent.Error(sessionId, e.ToString()),
() => Parent.TimeSeriesLimitReached(sessionId),
Expand Down Expand Up @@ -675,25 +675,25 @@ private static void TransmitMetricValue(Instrument instrument, LabeledAggregatio
{
Log.CounterRateValuePublished(sessionId, instrument.Meter.Name, instrument.Meter.Version, instrument.Name, instrument.Unit, Helpers.FormatTags(stats.Labels),
rateStats.Delta.HasValue ? rateStats.Delta.Value.ToString(CultureInfo.InvariantCulture) : "", rateStats.Value.ToString(CultureInfo.InvariantCulture),
instrument.FormattedTags, instrument.FormattedHash, instrument.Meter.FormattedTags, instrument.Meter.FormattedHash, instrument.Meter.FormattedScopeHash);
instrument.FormattedTags, RuntimeHelpers.GetHashCode(instrument), instrument.Meter.FormattedTags, RuntimeHelpers.GetHashCode(instrument.Meter), instrument.Meter.FormattedScopeHash);
}
else
{
Log.UpDownCounterRateValuePublished(sessionId, instrument.Meter.Name, instrument.Meter.Version, instrument.Name, instrument.Unit, Helpers.FormatTags(stats.Labels),
rateStats.Delta.HasValue ? rateStats.Delta.Value.ToString(CultureInfo.InvariantCulture) : "", rateStats.Value.ToString(CultureInfo.InvariantCulture),
instrument.FormattedTags, instrument.FormattedHash, instrument.Meter.FormattedTags, instrument.Meter.FormattedHash, instrument.Meter.FormattedScopeHash);
instrument.FormattedTags, RuntimeHelpers.GetHashCode(instrument), instrument.Meter.FormattedTags, RuntimeHelpers.GetHashCode(instrument.Meter), instrument.Meter.FormattedScopeHash);
}
}
else if (stats.AggregationStatistics is LastValueStatistics lastValueStats)
{
Log.GaugeValuePublished(sessionId, instrument.Meter.Name, instrument.Meter.Version, instrument.Name, instrument.Unit, Helpers.FormatTags(stats.Labels),
lastValueStats.LastValue.HasValue ? lastValueStats.LastValue.Value.ToString(CultureInfo.InvariantCulture) : "",
instrument.FormattedTags, instrument.FormattedHash, instrument.Meter.FormattedTags, instrument.Meter.FormattedHash, instrument.Meter.FormattedScopeHash);
instrument.FormattedTags, RuntimeHelpers.GetHashCode(instrument), instrument.Meter.FormattedTags, RuntimeHelpers.GetHashCode(instrument.Meter), instrument.Meter.FormattedScopeHash);
}
else if (stats.AggregationStatistics is HistogramStatistics histogramStats)
{
Log.HistogramValuePublished(sessionId, instrument.Meter.Name, instrument.Meter.Version, instrument.Name, instrument.Unit, Helpers.FormatTags(stats.Labels), FormatQuantiles(histogramStats.Quantiles),
histogramStats.Count, histogramStats.Sum, instrument.FormattedTags, instrument.FormattedHash, instrument.Meter.FormattedTags, instrument.Meter.FormattedHash, instrument.Meter.FormattedScopeHash);
histogramStats.Count, histogramStats.Sum, instrument.FormattedTags, RuntimeHelpers.GetHashCode(instrument), instrument.Meter.FormattedTags, RuntimeHelpers.GetHashCode(instrument.Meter), instrument.Meter.FormattedScopeHash);
}
}

Expand Down
Loading

0 comments on commit 8501906

Please sign in to comment.