@@ -24,7 +24,7 @@ public sealed class MetricFamily<TMetric, TImplementation, TLabels, TConfig> : I
24
24
private readonly IReadOnlyList < string > _metricNames ;
25
25
private readonly Func < TConfig , IReadOnlyList < string > , TImplementation > _instanceFactory ;
26
26
private readonly Lazy < TImplementation > _unlabelled ;
27
- private readonly ConcurrentDictionary < int , TImplementation > _labelledMetrics ;
27
+ private readonly ConcurrentDictionary < MetricKey , TImplementation > _labelledMetrics ;
28
28
29
29
public MetricFamily ( TConfig configuration , MetricType metricType , Func < TConfig , IReadOnlyList < string > , TImplementation > instanceFactory )
30
30
{
@@ -35,7 +35,7 @@ public MetricFamily(TConfig configuration, MetricType metricType, Func<TConfig,
35
35
_unlabelled = new Lazy < TImplementation > ( ( ) => _instanceFactory ( _configuration , default ) ) ;
36
36
LabelNames = LabelsHelper . FromArray < TLabels > ( configuration . LabelNames ) ;
37
37
if ( configuration . LabelNames . Count > 0 )
38
- _labelledMetrics = new ConcurrentDictionary < int , TImplementation > ( ) ;
38
+ _labelledMetrics = new ConcurrentDictionary < MetricKey , TImplementation > ( ) ;
39
39
}
40
40
41
41
public string Name => _configuration . Name ;
@@ -64,7 +64,7 @@ TMetric IMetricFamily<TMetric>.WithLabels(params string[] labels)
64
64
if ( labels . Length != _configuration . LabelNames . Count )
65
65
throw new ArgumentException ( "Wrong number of labels" ) ;
66
66
67
- var key = LabelsHelper . GetHashCode ( labels ) ;
67
+ var key = new MetricKey ( labels ) ;
68
68
69
69
if ( _labelledMetrics . TryGetValue ( key , out var metric ) )
70
70
{
@@ -83,7 +83,7 @@ TMetric IMetricFamily<TMetric>.RemoveLabelled(params string[] labels)
83
83
if ( labels . Length != _configuration . LabelNames . Count )
84
84
throw new ArgumentException ( "Wrong number of labels" ) ;
85
85
86
- var key = LabelsHelper . GetHashCode ( labels ) ;
86
+ var key = new MetricKey ( labels ) ;
87
87
_labelledMetrics . TryRemove ( key , out var removed ) ;
88
88
89
89
return removed ;
@@ -94,7 +94,7 @@ public TMetric WithLabels(TLabels labels)
94
94
if ( _labelledMetrics == null )
95
95
throw new InvalidOperationException ( "Metric family does not have any labels" ) ;
96
96
97
- var key = LabelsHelper . GetHashCode ( labels ) ;
97
+ var key = new MetricKey ( labels ) ;
98
98
99
99
if ( _labelledMetrics . TryGetValue ( key , out var metric ) )
100
100
{
@@ -110,7 +110,7 @@ public TMetric RemoveLabelled(TLabels labels)
110
110
if ( _labelledMetrics == null )
111
111
throw new InvalidOperationException ( "Metric family does not have any labels" ) ;
112
112
113
- var key = LabelsHelper . GetHashCode ( labels ) ;
113
+ var key = new MetricKey ( labels ) ;
114
114
_labelledMetrics . TryRemove ( key , out var removed ) ;
115
115
116
116
return removed ;
@@ -148,4 +148,32 @@ private IEnumerable<KeyValuePair<IReadOnlyList<string>, TMetric>> EnumerateLabel
148
148
foreach ( var labelled in _labelledMetrics )
149
149
yield return new KeyValuePair < IReadOnlyList < string > , TMetric > ( labelled . Value . LabelValues , labelled . Value ) ;
150
150
}
151
+
152
+ private readonly struct MetricKey : IEquatable < MetricKey >
153
+ {
154
+ private readonly int _hash1 ;
155
+ private readonly int _hash2 ;
156
+
157
+ public MetricKey ( TLabels labels )
158
+ {
159
+ _hash1 = LabelsHelper . GetHashCode ( labels , 0 ) ;
160
+ _hash2 = LabelsHelper . GetHashCode ( labels , 42 ) ;
161
+ }
162
+
163
+ public MetricKey ( params string [ ] labels )
164
+ {
165
+ _hash1 = LabelsHelper . GetHashCode ( labels , 0 ) ;
166
+ _hash2 = LabelsHelper . GetHashCode ( labels , 42 ) ;
167
+ }
168
+
169
+ public override int GetHashCode ( )
170
+ {
171
+ return _hash1 ;
172
+ }
173
+
174
+ public bool Equals ( MetricKey other )
175
+ {
176
+ return _hash1 == other . _hash1 && _hash2 == other . _hash2 ;
177
+ }
178
+ }
151
179
}
0 commit comments