Skip to content

Commit 249e7f4

Browse files
author
Nacho Bonafonte
committed
Simplify ExemplarReservoir to be a class, avoid existence of AnyExemplarReservoir
1 parent ac04c5c commit 249e7f4

File tree

8 files changed

+49
-83
lines changed

8 files changed

+49
-83
lines changed

Sources/OpenTelemetrySdk/Metrics/Stable/Aggregation/AggregatorHandle.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ internal protocol AggregatorHandleProtocol {
1313
}
1414

1515
public class AggregatorHandle {
16-
let exemplarReservoir: AnyExemplarReservoir
16+
let exemplarReservoir: ExemplarReservoir
1717

18-
internal init(exemplarReservoir: AnyExemplarReservoir) {
18+
internal init(exemplarReservoir: ExemplarReservoir) {
1919
self.exemplarReservoir = exemplarReservoir
2020
}
2121

Sources/OpenTelemetrySdk/Metrics/Stable/Aggregation/DoubleExplicitBucketHistogramAggregator.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class DoubleExplicitBucketHistogramAggregator: StableAggregator {
1414
class Handle: AggregatorHandle {
1515
let lock = Lock()
1616

17-
internal init(boundaries: [Double], exemplarReservoir: AnyExemplarReservoir) {
17+
internal init(boundaries: [Double], exemplarReservoir: ExemplarReservoir) {
1818
self.boundaries = boundaries
1919

2020
self.sum = 0
@@ -79,9 +79,9 @@ public class DoubleExplicitBucketHistogramAggregator: StableAggregator {
7979
}
8080

8181
private let boundaries: [Double]
82-
private let reservoirSupplier: () -> AnyExemplarReservoir
82+
private let reservoirSupplier: () -> ExemplarReservoir
8383

84-
public init(boundaries: [Double], reservoirSupplier: @escaping () -> AnyExemplarReservoir) {
84+
public init(boundaries: [Double], reservoirSupplier: @escaping () -> ExemplarReservoir) {
8585
self.boundaries = boundaries
8686
self.reservoirSupplier = reservoirSupplier
8787
}

Sources/OpenTelemetrySdk/Metrics/Stable/Aggregation/DoubleLastValueAggregator.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import Foundation
77
import OpenTelemetryApi
88

99
public class DoubleLastValueAggregator: StableAggregator {
10-
private var resevoirSupplier: () -> AnyExemplarReservoir
10+
private var resevoirSupplier: () -> ExemplarReservoir
1111

12-
internal init(resevoirSupplier: @escaping () -> AnyExemplarReservoir) {
12+
internal init(resevoirSupplier: @escaping () -> ExemplarReservoir) {
1313
self.resevoirSupplier = resevoirSupplier
1414
}
1515

Sources/OpenTelemetrySdk/Metrics/Stable/Aggregation/DoubleSumAggregator.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Foundation
77
import OpenTelemetryApi
88

99
public class DoubleSumAggregator: SumAggregator, StableAggregator {
10-
private let reservoirSupplier: () -> AnyExemplarReservoir
10+
private let reservoirSupplier: () -> ExemplarReservoir
1111

1212
public func diff(previousCumulative: PointData, currentCumulative: PointData) throws -> PointData {
1313
currentCumulative - previousCumulative
@@ -25,7 +25,7 @@ public class DoubleSumAggregator: SumAggregator, StableAggregator {
2525
StableMetricData.createDoubleSum(resource: resource, instrumentationScopeInfo: scope, name: descriptor.instrument.name, description: descriptor.instrument.description, unit: descriptor.instrument.unit, data: StableSumData(aggregationTemporality: temporality, points: points as! [DoublePointData]))
2626
}
2727

28-
init(instrumentDescriptor: InstrumentDescriptor, reservoirSupplier: @escaping () -> AnyExemplarReservoir) {
28+
init(instrumentDescriptor: InstrumentDescriptor, reservoirSupplier: @escaping () -> ExemplarReservoir) {
2929
self.reservoirSupplier = reservoirSupplier
3030
super.init(instrumentDescriptor: instrumentDescriptor)
3131
}

Sources/OpenTelemetrySdk/Metrics/Stable/Aggregation/LongLastValueAggregator.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import Foundation
77
import OpenTelemetryApi
88

99
public class LongLastValueAggregator: StableAggregator {
10-
private var resevoirSupplier: () -> AnyExemplarReservoir
10+
private var resevoirSupplier: () -> ExemplarReservoir
1111

12-
internal init(resevoirSupplier: @escaping () -> AnyExemplarReservoir) {
12+
internal init(resevoirSupplier: @escaping () -> ExemplarReservoir) {
1313
self.resevoirSupplier = resevoirSupplier
1414
}
1515

Sources/OpenTelemetrySdk/Metrics/Stable/Aggregation/LongSumAggregator.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import Foundation
77
import OpenTelemetryApi
88

99
public class LongSumAggregator: SumAggregator, StableAggregator {
10-
private let reservoirSupplier: () -> AnyExemplarReservoir
10+
private let reservoirSupplier: () -> ExemplarReservoir
1111

12-
init(descriptor: InstrumentDescriptor, reservoirSupplier: @escaping () -> AnyExemplarReservoir) {
12+
init(descriptor: InstrumentDescriptor, reservoirSupplier: @escaping () -> ExemplarReservoir) {
1313
self.reservoirSupplier = reservoirSupplier
1414
super.init(instrumentDescriptor: descriptor)
1515
}
Lines changed: 33 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,62 @@
11
//
22
// Copyright The OpenTelemetry Authors
33
// SPDX-License-Identifier: Apache-2.0
4-
//
4+
//
55

66
import Foundation
77
import 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-

Sources/OpenTelemetrySdk/Metrics/Stable/Exemplar/FilteredExemplarReservoir.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
import Foundation
77
import OpenTelemetryApi
88

9-
public class FilteredExemplarReservoir: AnyExemplarReservoir {
9+
public class FilteredExemplarReservoir: ExemplarReservoir {
1010
let exemplarFilter : ExemplarFilter
11-
let reservoir : AnyExemplarReservoir
11+
let reservoir : ExemplarReservoir
1212

13-
init(filter: ExemplarFilter, reservoir: AnyExemplarReservoir) {
13+
init(filter: ExemplarFilter, reservoir: ExemplarReservoir) {
1414
self.exemplarFilter = filter
1515
self.reservoir = reservoir
1616
}

0 commit comments

Comments
 (0)