-
Notifications
You must be signed in to change notification settings - Fork 892
Expand file tree
/
Copy pathMetricStreamIdentity.cs
More file actions
288 lines (240 loc) · 11.9 KB
/
MetricStreamIdentity.cs
File metadata and controls
288 lines (240 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
using System.Diagnostics.Metrics;
using System.Globalization;
namespace OpenTelemetry.Metrics;
internal readonly struct MetricStreamIdentity : IEquatable<MetricStreamIdentity>
{
private static readonly StringArrayEqualityComparer StringArrayComparer = new();
private readonly int hashCode;
public MetricStreamIdentity(Instrument instrument, MetricStreamConfiguration? metricStreamConfiguration)
{
this.MeterName = instrument.Meter.Name;
this.MeterVersion = instrument.Meter.Version ?? string.Empty;
this.MeterTags = instrument.Meter.Tags != null ? new Tags(instrument.Meter.Tags.ToArray()) : null;
this.MeterSchemaUrl = instrument.Meter.TelemetrySchemaUrl ?? string.Empty;
this.InstrumentName = metricStreamConfiguration?.Name ?? instrument.Name;
this.Unit = instrument.Unit ?? string.Empty;
this.Description = metricStreamConfiguration?.Description ?? instrument.Description ?? string.Empty;
this.InstrumentType = instrument.GetType();
this.ViewId = metricStreamConfiguration?.ViewId;
this.MetricStreamName = $"{this.MeterName}.{this.MeterVersion}.{this.InstrumentName}";
this.TagKeys = metricStreamConfiguration?.CopiedTagKeys;
this.HistogramBucketBounds = GetExplicitBucketHistogramBounds(instrument, metricStreamConfiguration);
this.HistogramBucketDisplayBounds = GetExplicitBucketHistogramDisplayBounds(instrument, this.HistogramBucketBounds);
this.ExponentialHistogramMaxSize = (metricStreamConfiguration as Base2ExponentialBucketHistogramConfiguration)?.MaxSize ?? 0;
this.ExponentialHistogramMaxScale = (metricStreamConfiguration as Base2ExponentialBucketHistogramConfiguration)?.MaxScale ?? 0;
this.HistogramRecordMinMax = (metricStreamConfiguration as HistogramConfiguration)?.RecordMinMax ?? true;
#if NET || NETSTANDARD2_1_OR_GREATER
HashCode hashCode = default;
hashCode.Add(this.InstrumentType);
hashCode.Add(this.MeterName);
hashCode.Add(this.MeterVersion);
hashCode.Add(this.MeterSchemaUrl);
hashCode.Add(this.MeterTags);
hashCode.Add(this.InstrumentName);
hashCode.Add(this.HistogramRecordMinMax);
hashCode.Add(this.Unit);
hashCode.Add(this.Description);
hashCode.Add(this.ViewId);
// Note: The this.TagKeys! here is strange but it is fine for the value
// to be null. HashCode.Add is coded to handle the value being null. We
// are essentially suppressing a false positive due to an issue/quirk
// with the annotations. See:
// https://github.com/dotnet/runtime/pull/91905.
hashCode.Add(this.TagKeys!, StringArrayComparer);
hashCode.Add(this.ExponentialHistogramMaxSize);
hashCode.Add(this.ExponentialHistogramMaxScale);
if (this.HistogramBucketBounds != null)
{
for (var i = 0; i < this.HistogramBucketBounds.Length; ++i)
{
hashCode.Add(this.HistogramBucketBounds[i]);
}
}
var hash = hashCode.ToHashCode();
#else
var hash = 17;
unchecked
{
hash = (hash * 31) + this.InstrumentType.GetHashCode();
hash = (hash * 31) + this.MeterName.GetHashCode();
hash = (hash * 31) + this.MeterVersion.GetHashCode();
hash = (hash * 31) + this.MeterSchemaUrl.GetHashCode();
hash = (hash * 31) + (this.MeterTags?.GetHashCode() ?? 0);
hash = (hash * 31) + this.InstrumentName.GetHashCode();
hash = (hash * 31) + this.HistogramRecordMinMax.GetHashCode();
hash = (hash * 31) + this.ExponentialHistogramMaxSize.GetHashCode();
hash = (hash * 31) + this.ExponentialHistogramMaxScale.GetHashCode();
hash = (hash * 31) + this.Unit.GetHashCode();
hash = (hash * 31) + this.Description.GetHashCode();
hash = (hash * 31) + (this.ViewId ?? 0);
hash = (hash * 31) + (this.TagKeys != null ? StringArrayComparer.GetHashCode(this.TagKeys) : 0);
if (this.HistogramBucketBounds != null)
{
var len = this.HistogramBucketBounds.Length;
for (var i = 0; i < len; ++i)
{
hash = (hash * 31) + this.HistogramBucketBounds[i].GetHashCode();
}
}
}
#endif
this.hashCode = hash;
}
public string MeterName { get; }
public string MeterVersion { get; }
public string MeterSchemaUrl { get; }
public Tags? MeterTags { get; }
public string InstrumentName { get; }
public string Unit { get; }
public string Description { get; }
public Type InstrumentType { get; }
public int? ViewId { get; }
public string MetricStreamName { get; }
public string[]? TagKeys { get; }
public double[]? HistogramBucketBounds { get; }
public double[]? HistogramBucketDisplayBounds { get; }
public int ExponentialHistogramMaxSize { get; }
public int ExponentialHistogramMaxScale { get; }
public bool HistogramRecordMinMax { get; }
public bool IsHistogram =>
this.InstrumentType == typeof(Histogram<long>)
|| this.InstrumentType == typeof(Histogram<int>)
|| this.InstrumentType == typeof(Histogram<short>)
|| this.InstrumentType == typeof(Histogram<byte>)
|| this.InstrumentType == typeof(Histogram<float>)
|| this.InstrumentType == typeof(Histogram<double>);
public bool IsAsynchronous =>
this.InstrumentType == typeof(ObservableCounter<long>)
|| this.InstrumentType == typeof(ObservableCounter<int>)
|| this.InstrumentType == typeof(ObservableCounter<short>)
|| this.InstrumentType == typeof(ObservableCounter<byte>)
|| this.InstrumentType == typeof(ObservableCounter<float>)
|| this.InstrumentType == typeof(ObservableCounter<double>)
|| this.InstrumentType == typeof(ObservableUpDownCounter<long>)
|| this.InstrumentType == typeof(ObservableUpDownCounter<int>)
|| this.InstrumentType == typeof(ObservableUpDownCounter<short>)
|| this.InstrumentType == typeof(ObservableUpDownCounter<byte>)
|| this.InstrumentType == typeof(ObservableUpDownCounter<float>)
|| this.InstrumentType == typeof(ObservableUpDownCounter<double>)
|| this.InstrumentType == typeof(ObservableGauge<long>)
|| this.InstrumentType == typeof(ObservableGauge<int>)
|| this.InstrumentType == typeof(ObservableGauge<short>)
|| this.InstrumentType == typeof(ObservableGauge<byte>)
|| this.InstrumentType == typeof(ObservableGauge<float>)
|| this.InstrumentType == typeof(ObservableGauge<double>);
public static bool operator ==(MetricStreamIdentity metricIdentity1, MetricStreamIdentity metricIdentity2) => metricIdentity1.Equals(metricIdentity2);
public static bool operator !=(MetricStreamIdentity metricIdentity1, MetricStreamIdentity metricIdentity2) => !metricIdentity1.Equals(metricIdentity2);
public override readonly bool Equals(object? obj)
=> obj is MetricStreamIdentity other && this.Equals(other);
public bool Equals(MetricStreamIdentity other)
=> this.InstrumentType == other.InstrumentType
&& this.MeterName == other.MeterName
&& this.MeterVersion == other.MeterVersion
&& this.MeterSchemaUrl == other.MeterSchemaUrl
&& this.InstrumentName == other.InstrumentName
&& this.Unit == other.Unit
&& this.Description == other.Description
&& this.ViewId == other.ViewId
&& this.MeterTags == other.MeterTags
&& this.HistogramRecordMinMax == other.HistogramRecordMinMax
&& this.ExponentialHistogramMaxSize == other.ExponentialHistogramMaxSize
&& this.ExponentialHistogramMaxScale == other.ExponentialHistogramMaxScale
&& StringArrayComparer.Equals(this.TagKeys, other.TagKeys)
&& HistogramBoundsEqual(this.HistogramBucketBounds, other.HistogramBucketBounds);
public override readonly int GetHashCode() => this.hashCode;
private static double[]? GetExplicitBucketHistogramBounds(Instrument instrument, MetricStreamConfiguration? metricStreamConfiguration) => metricStreamConfiguration switch
{
ExplicitBucketHistogramConfiguration explicitConfiguration when explicitConfiguration.CopiedBoundaries != null => explicitConfiguration.CopiedBoundaries,
_ => instrument switch
{
Histogram<long> longHistogram => GetExplicitBucketHistogramBoundsFromAdvice(longHistogram),
Histogram<int> intHistogram => GetExplicitBucketHistogramBoundsFromAdvice(intHistogram),
Histogram<short> shortHistogram => GetExplicitBucketHistogramBoundsFromAdvice(shortHistogram),
Histogram<byte> byteHistogram => GetExplicitBucketHistogramBoundsFromAdvice(byteHistogram),
Histogram<float> floatHistogram => GetExplicitBucketHistogramBoundsFromAdvice(floatHistogram),
Histogram<double> doubleHistogram => GetExplicitBucketHistogramBoundsFromAdvice(doubleHistogram),
_ => null,
},
};
private static double[]? GetExplicitBucketHistogramDisplayBounds(Instrument instrument, double[]? rawBounds)
{
if (rawBounds == null)
{
return null;
}
// Only float histograms need display bounds cleanup.
// For float types, the raw float->double cast produces imprecise values
// (e.g., 0.025f -> 0.02500000037252903). Display bounds convert via string
// to get clean values (0.025) for export/serialization, while raw bounds
// are kept for correct bucketing.
if (instrument is not Histogram<float>)
{
return null;
}
double[] displayBounds = new double[rawBounds.Length];
for (int i = 0; i < rawBounds.Length; i++)
{
// Cast back to float to recover the original float precision,
// then convert to string to get the clean representation.
// e.g., (float)0.0010000000474974513 -> 0.001f -> "0.001" -> 0.001
displayBounds[i] = double.Parse(
((float)rawBounds[i]).ToString("G", CultureInfo.InvariantCulture),
CultureInfo.InvariantCulture);
}
return displayBounds;
}
private static double[]? GetExplicitBucketHistogramBoundsFromAdvice<T>(Histogram<T> histogram)
where T : struct
{
var adviceExplicitBucketBoundaries = histogram.Advice?.HistogramBucketBoundaries;
if (adviceExplicitBucketBoundaries == null)
{
return null;
}
ExplicitBucketHistogramConfiguration.ThrowIfBoundaryCountExceedsLimit(
adviceExplicitBucketBoundaries.Count,
nameof(histogram));
if (typeof(T) == typeof(double))
{
var boundaries = (IReadOnlyList<double>)adviceExplicitBucketBoundaries;
return [.. boundaries];
}
else
{
var explicitBucketBoundaries = new double[adviceExplicitBucketBoundaries.Count];
for (var i = 0; i < adviceExplicitBucketBoundaries.Count; i++)
{
explicitBucketBoundaries[i] = Convert.ToDouble(
adviceExplicitBucketBoundaries[i],
CultureInfo.InvariantCulture);
}
return explicitBucketBoundaries;
}
}
private static bool HistogramBoundsEqual(double[]? bounds1, double[]? bounds2)
{
if (ReferenceEquals(bounds1, bounds2))
{
return true;
}
if (bounds1 is null || bounds2 is null)
{
return false;
}
var len1 = bounds1.Length;
if (len1 != bounds2.Length)
{
return false;
}
for (var i = 0; i < len1; i++)
{
if (!bounds1[i].Equals(bounds2[i]))
{
return false;
}
}
return true;
}
}