11//
22// Copyright The OpenTelemetry Authors
33// SPDX-License-Identifier: Apache-2.0
4- //
4+ //
55
66import Foundation
77import OpenTelemetryApi
88
9- public protocol ExemplarReservoir {
10- func offerDoubleMeasurement( value: Double , attributes: [ String : AttributeValue ] )
11- func offerLongMeasurement( value: Int , attributes: [ String : AttributeValue ] )
12- func collectAndReset( attribute: [ String : AttributeValue ] ) -> [ ExemplarData ]
13- }
14-
15- public class AnyExemplarReservoir : ExemplarReservoir {
16-
17- public func collectAndReset( attribute: [ String : AttributeValue ] ) -> [ ExemplarData ] {
9+ public class ExemplarReservoir {
10+ public func collectAndReset( attribute: [ String : AttributeValue ] ) -> [ ExemplarData ] {
1811 return [ AnyExemplarData] ( )
1912 }
2013
14+ public func offerDoubleMeasurement( value: Double , attributes: [ String : OpenTelemetryApi . AttributeValue ] ) { }
2115
22- public func offerDoubleMeasurement( value: Double , attributes: [ String : OpenTelemetryApi . AttributeValue ] ) {
23-
24- }
25-
26- public func offerLongMeasurement( value: Int , attributes: [ String : OpenTelemetryApi . AttributeValue ] ) {
27-
28- }
29-
30-
16+ public func offerLongMeasurement( value: Int , attributes: [ String : OpenTelemetryApi . AttributeValue ] ) { }
3117}
3218
33-
34- public class NoopExemplarReservoir : AnyExemplarReservoir {
35- public override func offerDoubleMeasurement( value: Double , attributes: [ String : OpenTelemetryApi . AttributeValue ] ) {
19+ public class NoopExemplarReservoir : ExemplarReservoir {
20+ override public func offerDoubleMeasurement( value: Double , attributes: [ String : OpenTelemetryApi . AttributeValue ] ) {
3621 // noop
3722 }
3823
39- public override func offerLongMeasurement( value: Int , attributes: [ String : OpenTelemetryApi . AttributeValue ] ) {
24+ override public func offerLongMeasurement( value: Int , attributes: [ String : OpenTelemetryApi . AttributeValue ] ) {
4025 // noop
4126 }
4227
43- public override func collectAndReset( attribute: [ String : AttributeValue ] ) -> [ ExemplarData ] {
28+ override public func collectAndReset( attribute: [ String : AttributeValue ] ) -> [ ExemplarData ] {
4429 return [ ExemplarData] ( )
4530 }
46-
47-
4831}
4932
50- public class ExemplarReservoirCollection {
51-
52- static func doubleNoSamples( ) -> AnyExemplarReservoir {
33+ public enum ExemplarReservoirCollection {
34+ static func doubleNoSamples( ) -> ExemplarReservoir {
5335 return NoopExemplarReservoir ( )
5436 }
5537
56- static func longNoSamples( ) -> AnyExemplarReservoir {
38+ static func longNoSamples( ) -> ExemplarReservoir {
5739 return NoopExemplarReservoir ( )
5840 }
59-
60-
61-
6241}
6342
64-
65- public class FixedSizedExemplarReservoir : AnyExemplarReservoir {
66- var storage : [ ReservoirCell ]
67- let reservoirCellSelector : ReservoirCellSelector
68- let mapAndResetCell : ( ReservoirCell , [ String : AttributeValue ] ) -> ExemplarData ?
43+ public class FixedSizedExemplarReservoir : ExemplarReservoir {
44+ var storage : [ ReservoirCell ]
45+ let reservoirCellSelector : ReservoirCellSelector
46+ let mapAndResetCell : ( ReservoirCell , [ String : AttributeValue ] ) -> ExemplarData ?
6947 var hasMeasurements = false
7048
71- init ( clock: Clock , size: Int , reservoirCellSelector: ReservoirCellSelector , mapAndResetCell: @escaping ( ReservoirCell , [ String : AttributeValue ] ) -> ExemplarData ? ) {
49+ init ( clock: Clock , size: Int , reservoirCellSelector: ReservoirCellSelector , mapAndResetCell: @escaping ( ReservoirCell , [ String : AttributeValue ] ) -> ExemplarData ? ) {
7250 storage = [ ReservoirCell] ( )
7351 self . reservoirCellSelector = reservoirCellSelector
7452 self . mapAndResetCell = mapAndResetCell
7553
7654 for _ in 0 ... size {
77- storage. append ( ReservoirCell ( clock: clock) )
55+ storage. append ( ReservoirCell ( clock: clock) )
7856 }
7957 }
8058
81- override public func offerLongMeasurement( value: Int , attributes: [ String : AttributeValue ] ) {
59+ override public func offerLongMeasurement( value: Int , attributes: [ String : AttributeValue ] ) {
8260 let bucketIndex = reservoirCellSelector. reservoirCellIndex ( for: storage, value: value, attributes: attributes)
8361
8462 if bucketIndex != - 1 {
@@ -87,7 +65,7 @@ public class FixedSizedExemplarReservoir : AnyExemplarReservoir {
8765 }
8866 }
8967
90- override public func offerDoubleMeasurement( value: Double , attributes: [ String : AttributeValue ] ) {
68+ override public func offerDoubleMeasurement( value: Double , attributes: [ String : AttributeValue ] ) {
9169 let bucketIndex = reservoirCellSelector. reservoirCellIndex ( for: storage, value: value, attributes: attributes)
9270
9371 if bucketIndex != - 1 {
@@ -96,8 +74,8 @@ public class FixedSizedExemplarReservoir : AnyExemplarReservoir {
9674 }
9775 }
9876
99- override public func collectAndReset( attribute: [ String : AttributeValue ] ) -> [ ExemplarData ] {
100- var results = [ ExemplarData] ( )
77+ override public func collectAndReset( attribute: [ String : AttributeValue ] ) -> [ ExemplarData ] {
78+ var results = [ ExemplarData] ( )
10179 if !hasMeasurements {
10280 return results
10381 }
@@ -110,46 +88,36 @@ public class FixedSizedExemplarReservoir : AnyExemplarReservoir {
11088 hasMeasurements = false
11189 return results
11290 }
113-
11491}
11592
116- public class RandomFixedSizedExemplarReservoir : FixedSizedExemplarReservoir {
117-
118- private init ( clock: Clock , size: Int , mapAndResetCell: @escaping ( ReservoirCell , [ String : AttributeValue ] ) -> ExemplarData ? ) {
119- super. init ( clock: clock, size: size, reservoirCellSelector: RandomCellSelector ( ) , mapAndResetCell : mapAndResetCell)
93+ public class RandomFixedSizedExemplarReservoir : FixedSizedExemplarReservoir {
94+ private init ( clock: Clock , size: Int , mapAndResetCell: @escaping ( ReservoirCell , [ String : AttributeValue ] ) -> ExemplarData ? ) {
95+ super. init ( clock: clock, size: size, reservoirCellSelector: RandomCellSelector ( ) , mapAndResetCell: mapAndResetCell)
12096 }
12197
122- static func createLong( clock: Clock , size : Int ) -> RandomFixedSizedExemplarReservoir {
123-
98+ static func createLong( clock: Clock , size: Int ) -> RandomFixedSizedExemplarReservoir {
12499 return RandomFixedSizedExemplarReservoir ( clock: clock, size: size, mapAndResetCell: { cell, attributes in
125100 cell. getAndResetLong ( pointAttributes: attributes)
126101 } )
127-
128102 }
129103
130- static func createDouble( clock: Clock , size : Int ) -> RandomFixedSizedExemplarReservoir {
104+ static func createDouble( clock: Clock , size: Int ) -> RandomFixedSizedExemplarReservoir {
131105 return RandomFixedSizedExemplarReservoir ( clock: clock, size: size, mapAndResetCell: { cell, attributes in
132- return cell. getAndResetDouble ( pointAttributes: attributes)
106+ cell. getAndResetDouble ( pointAttributes: attributes)
133107 } )
134-
135-
136108 }
137109
138- class RandomCellSelector : ReservoirCellSelector {
139- var numMeasurments : Int = 0
110+ class RandomCellSelector : ReservoirCellSelector {
111+ var numMeasurments : Int = 0
140112
141-
142- func reservoirCellIndex( for cells: [ ReservoirCell ] , value: Int , attributes: [ String : OpenTelemetryApi . AttributeValue ] ) -> Int {
113+ func reservoirCellIndex( for cells: [ ReservoirCell ] , value: Int , attributes: [ String : OpenTelemetryApi . AttributeValue ] ) -> Int {
143114 return getIndex ( cells: cells)
144-
145115 }
146116
147- func reservoirCellIndex( for cells: [ ReservoirCell ] , value: Double , attributes: [ String : OpenTelemetryApi . AttributeValue ] ) -> Int {
117+ func reservoirCellIndex( for cells: [ ReservoirCell ] , value: Double , attributes: [ String : OpenTelemetryApi . AttributeValue ] ) -> Int {
148118 return getIndex ( cells: cells)
149-
150119 }
151120
152-
153121 func reset( ) {
154122 numMeasurments = 0
155123 }
@@ -158,12 +126,10 @@ public class RandomFixedSizedExemplarReservoir : FixedSizedExemplarReservoir {
158126 let count = numMeasurments + 1
159127 let index = Int . random ( in: Int . min... Int . max) > 0 ? count : 1
160128 numMeasurments += 1
161- if ( index < cells. count) {
129+ if index < cells. count {
162130 return index
163131 }
164132 return - 1
165133 }
166-
167134 }
168135}
169-
0 commit comments