forked from open-telemetry/opentelemetry-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMetricStreamIdentity.cs
More file actions
187 lines (152 loc) · 7.14 KB
/
Copy pathMetricStreamIdentity.cs
File metadata and controls
187 lines (152 loc) · 7.14 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
// <copyright file="MetricStreamIdentity.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
using System.Diagnostics.Metrics;
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;
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 = (metricStreamConfiguration as ExplicitBucketHistogramConfiguration)?.CopiedBoundaries;
this.ExponentialHistogramMaxSize = (metricStreamConfiguration as Base2ExponentialBucketHistogramConfiguration)?.MaxSize ?? 0;
this.ExponentialHistogramMaxScale = (metricStreamConfiguration as Base2ExponentialBucketHistogramConfiguration)?.MaxScale ?? 0;
this.HistogramRecordMinMax = (metricStreamConfiguration as HistogramConfiguration)?.RecordMinMax ?? true;
#if NET6_0_OR_GREATER
HashCode hashCode = default;
hashCode.Add(this.InstrumentType);
hashCode.Add(this.MeterName);
hashCode.Add(this.MeterVersion);
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();
// MeterTags is not part of identity, so not included here.
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 IEnumerable<KeyValuePair<string, object?>>? 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 int ExponentialHistogramMaxSize { get; }
public int ExponentialHistogramMaxScale { get; }
public bool HistogramRecordMinMax { get; }
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)
{
return obj is MetricStreamIdentity other && this.Equals(other);
}
public bool Equals(MetricStreamIdentity other)
{
return this.InstrumentType == other.InstrumentType
&& this.MeterName == other.MeterName
&& this.MeterVersion == other.MeterVersion
&& this.InstrumentName == other.InstrumentName
&& this.Unit == other.Unit
&& this.Description == other.Description
&& this.ViewId == other.ViewId
&& 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 bool HistogramBoundsEqual(double[]? bounds1, double[]? bounds2)
{
if (ReferenceEquals(bounds1, bounds2))
{
return true;
}
if (ReferenceEquals(bounds1, null) || ReferenceEquals(bounds2, null))
{
return false;
}
var len1 = bounds1.Length;
if (len1 != bounds2.Length)
{
return false;
}
for (int i = 0; i < len1; i++)
{
if (!bounds1[i].Equals(bounds2[i]))
{
return false;
}
}
return true;
}
}