Skip to content

Commit 9de43c7

Browse files
author
Nacho Bonafonte
committed
Simplify ExemplarData converting to a class avoid using AnyExemplarData
Rename AggregatorFactory as aggregation to avoid using two protocols
1 parent 249e7f4 commit 9de43c7

17 files changed

+127
-188
lines changed

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
//
22
// Copyright The OpenTelemetry Authors
33
// SPDX-License-Identifier: Apache-2.0
4-
//
4+
//
55

66
import Foundation
77

8-
public protocol Aggregation : AggregatorFactory {}
9-
10-
public class Aggregations {
8+
public enum Aggregations {
119
public static func drop() -> Aggregation {
1210
DropAggregation.instance
1311
}
@@ -29,7 +27,7 @@ public class Aggregations {
2927
}
3028

3129
public static func explicitBucketHistogram(buckets: [Double]) -> Aggregation {
32-
ExplicitBucketHistogramAggregation(bucketBoundaries: buckets)
30+
ExplicitBucketHistogramAggregation(bucketBoundaries: buckets)
3331
}
3432

3533
static func base2ExponentialBucketHistogram() {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import Foundation
77

8-
public protocol AggregatorFactory {
8+
public protocol Aggregation {
99
func createAggregator(descriptor: InstrumentDescriptor, exemplarFilter: ExemplarFilter) -> StableAggregator
1010
func isCompatible(with descriptor: InstrumentDescriptor) -> Bool
1111
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//import Foundation
77
//import OpenTelemetryApi
88
//
9-
//public class Base2ExponentialBucketHistogramAggregation : Aggregation, AggregatorFactory {
9+
//public class Base2ExponentialBucketHistogramAggregation : Aggregation {
1010
// private static let defaultMaxBuckets = 160
1111
// private static let defaultMaxScale = 20
1212
//
Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
//
22
// Copyright The OpenTelemetry Authors
33
// SPDX-License-Identifier: Apache-2.0
4-
//
4+
//
55

66
import Foundation
77

8-
9-
public class DefaultAggregation : Aggregation, AggregatorFactory {
8+
public class DefaultAggregation: Aggregation {
109
public private(set) static var instance = DefaultAggregation()
1110

1211
public func createAggregator(descriptor: InstrumentDescriptor, exemplarFilter: ExemplarFilter) -> any StableAggregator {
@@ -17,19 +16,14 @@ public class DefaultAggregation : Aggregation, AggregatorFactory {
1716
resolve(for: descriptor).isCompatible(with: descriptor)
1817
}
1918

20-
private func resolve(for instrument: InstrumentDescriptor) -> AggregatorFactory {
21-
switch(instrument.type) {
19+
private func resolve(for instrument: InstrumentDescriptor) -> Aggregation {
20+
switch instrument.type {
2221
case .counter, .upDownCounter, .observableCounter, .observableUpDownCounter:
23-
return SumAggregation.instance
22+
return SumAggregation.instance
2423
case .histogram:
2524
return ExplicitBucketHistogramAggregation.instance
2625
case .observableGauge:
2726
return LastValueAggregation.instance
2827
}
29-
30-
return DropAggregation.instance
3128
}
32-
33-
34-
3529
}
Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
11
//
22
// Copyright The OpenTelemetry Authors
33
// SPDX-License-Identifier: Apache-2.0
4-
//
4+
//
55

66
import Foundation
77
import OpenTelemetryApi
88

9-
10-
public class DropAggregation : Aggregation, AggregatorFactory {
11-
9+
public class DropAggregation: Aggregation {
1210
public private(set) static var instance = DropAggregation()
13-
14-
public func createAggregator(descriptor: InstrumentDescriptor, exemplarFilter: ExemplarFilter) -> any StableAggregator {
15-
11+
12+
public func createAggregator(descriptor: InstrumentDescriptor, exemplarFilter: ExemplarFilter) -> any StableAggregator {
1613
return DropAggregator()
1714
}
1815

1916
public func isCompatible(with descriptor: InstrumentDescriptor) -> Bool {
2017
true
2118
}
22-
23-
2419
}
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,33 @@
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 class ExplicitBucketHistogramAggregation : Aggregation, AggregatorFactory {
10-
public private(set) static var DEFAULT_BOUNDARIES : [Double] = [0,5,10,25,50,75,100,250,500,750,1_000,2_500,5_000,7_500]
9+
public class ExplicitBucketHistogramAggregation: Aggregation {
10+
public private(set) static var DEFAULT_BOUNDARIES: [Double] = [0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1_000, 2_500, 5_000, 7_500]
1111
public private(set) static var instance = ExplicitBucketHistogramAggregation(bucketBoundaries: DEFAULT_BOUNDARIES)
1212

13+
private let bucketBoundaries: [Double]
1314

14-
private let bucketBoundaries : [Double]
15-
16-
init(bucketBoundaries : [Double]) {
15+
init(bucketBoundaries: [Double]) {
1716
self.bucketBoundaries = bucketBoundaries
1817
}
1918

2019
public func createAggregator(descriptor: InstrumentDescriptor, exemplarFilter: ExemplarFilter) -> any StableAggregator {
2120
DoubleExplicitBucketHistogramAggregator(boundaries: bucketBoundaries) {
22-
FilteredExemplarReservoir(filter: exemplarFilter, reservoir: HistogramExemplarReservoir(clock: MillisClock(), boundaries: self.bucketBoundaries)) // todo: inject correct clock
21+
FilteredExemplarReservoir(filter: exemplarFilter, reservoir: HistogramExemplarReservoir(clock: MillisClock(), boundaries: self.bucketBoundaries)) // TODO: inject correct clock
2322
}
2423
}
2524

2625
public func isCompatible(with descriptor: InstrumentDescriptor) -> Bool {
27-
switch(descriptor.type) {
26+
switch descriptor.type {
2827
case .counter, .histogram:
2928
return true
3029
default:
31-
return false
30+
return false
3231
}
3332
}
3433
}
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,28 @@
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 class LastValueAggregation : Aggregation, AggregatorFactory {
9+
public class LastValueAggregation: Aggregation {
1010
public private(set) static var instance = LastValueAggregation()
1111

1212
public func createAggregator(descriptor: InstrumentDescriptor, exemplarFilter: ExemplarFilter) -> StableAggregator {
1313
switch descriptor.valueType {
1414
case .double:
1515
return DoubleLastValueAggregator(resevoirSupplier: {
16-
FilteredExemplarReservoir(filter:exemplarFilter, reservoir: RandomFixedSizedExemplarReservoir.createDouble(clock: MillisClock(), size: 2))
16+
FilteredExemplarReservoir(filter: exemplarFilter, reservoir: RandomFixedSizedExemplarReservoir.createDouble(clock: MillisClock(), size: 2))
1717
})
1818
case .long:
1919
return LongLastValueAggregator(resevoirSupplier: {
2020
FilteredExemplarReservoir(filter: exemplarFilter, reservoir: RandomFixedSizedExemplarReservoir.createLong(clock: MillisClock(), size: 2))
2121
})
2222
}
2323
}
24-
24+
2525
public func isCompatible(with descriptor: InstrumentDescriptor) -> Bool {
2626
return descriptor.type == .observableGauge
2727
}
28-
2928
}

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
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 class SumAggregation : Aggregation, AggregatorFactory {
10-
9+
public class SumAggregation: Aggregation {
1110
public private(set) static var instance = SumAggregation()
1211

1312
public func isCompatible(with descriptor: InstrumentDescriptor) -> Bool {
14-
switch (descriptor.type) {
15-
case .counter,.observableUpDownCounter,.observableCounter,.upDownCounter,.histogram:
13+
switch descriptor.type {
14+
case .counter, .observableUpDownCounter, .observableCounter, .upDownCounter, .histogram:
1615
return true
1716
default:
1817
return false
1918
}
2019
}
21-
22-
20+
2321
public func createAggregator(descriptor: InstrumentDescriptor, exemplarFilter: ExemplarFilter) -> any StableAggregator {
24-
switch(descriptor.valueType) {
22+
switch descriptor.valueType {
2523
case .long:
2624
return LongSumAggregator(descriptor: descriptor, reservoirSupplier: {
2725
FilteredExemplarReservoir(filter: exemplarFilter,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import OpenTelemetryApi
88

99
public class ExemplarReservoir {
1010
public func collectAndReset(attribute: [String: AttributeValue]) -> [ExemplarData] {
11-
return [AnyExemplarData]()
11+
return [ExemplarData]()
1212
}
1313

1414
public func offerDoubleMeasurement(value: Double, attributes: [String: OpenTelemetryApi.AttributeValue]) {}

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

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,53 @@
11
//
22
// Copyright The OpenTelemetry Authors
33
// SPDX-License-Identifier: Apache-2.0
4-
//
4+
//
55

66
import Foundation
77
import OpenTelemetryApi
88

9-
109
public class ReservoirCell {
11-
let clock : Clock
10+
let clock: Clock
1211
var attributes = [String: AttributeValue]()
13-
var spanContext : SpanContext? = nil
14-
var recordTime : UInt64 = 0
12+
var spanContext: SpanContext?
13+
var recordTime: UInt64 = 0
1514

16-
var doubleValue : Double = 0
17-
var longValue: Int = 0
15+
var doubleValue: Double = 0
16+
var longValue: Int = 0
1817

1918
init(clock: Clock) {
2019
self.clock = clock
2120
}
2221

23-
func recordLongValue(value: Int, attributes: [String:AttributeValue]) {
22+
func recordLongValue(value: Int, attributes: [String: AttributeValue]) {
2423
longValue = value
2524
offerMeasurement(attributes: attributes)
2625
}
2726

28-
func recordDoubleValue(value: Double, attributes: [String:AttributeValue]) {
27+
func recordDoubleValue(value: Double, attributes: [String: AttributeValue]) {
2928
doubleValue = value
3029
offerMeasurement(attributes: attributes)
3130
}
3231

33-
private func offerMeasurement(attributes: [String:AttributeValue]) {
32+
private func offerMeasurement(attributes: [String: AttributeValue]) {
3433
self.attributes = attributes
3534
recordTime = clock.nanoTime
3635
if let context = OpenTelemetry.instance.contextProvider.activeSpan?.context, context.isValid {
3736
self.spanContext = context
3837
}
3938
}
4039

41-
func getAndResetLong(pointAttributes: [String: AttributeValue]) -> ImmutableLongExemplarData {
42-
let result = ImmutableLongExemplarData(filteredAttributes: filtered(attributes, pointAttributes), recordTimeNanos: recordTime, spanContext: spanContext, value: longValue)
40+
func getAndResetLong(pointAttributes: [String: AttributeValue]) -> LongExemplarData {
41+
let result = LongExemplarData(filteredAttributes: filtered(attributes, pointAttributes), recordTimeNanos: recordTime, spanContext: spanContext, value: longValue)
4342
reset()
4443
return result
4544
}
4645

47-
func getAndResetDouble(pointAttributes: [String:AttributeValue]) -> DoubleExemplarData {
48-
let result = ImmutableDoubleExemplarData(filteredAttributes: filtered(attributes,pointAttributes), recordTimeNanos: recordTime, spanContext: spanContext, value: doubleValue)
49-
reset()
46+
func getAndResetDouble(pointAttributes: [String: AttributeValue]) -> DoubleExemplarData {
47+
let result = DoubleExemplarData(value: doubleValue, epochNanos: recordTime, filteredAttributes: filtered(attributes, pointAttributes), spanContext: spanContext)
5048
return result
5149
}
5250

53-
5451
func reset() {
5552
attributes = [String: AttributeValue]()
5653
longValue = 0
@@ -59,7 +56,7 @@ public class ReservoirCell {
5956
recordTime = 0
6057
}
6158

62-
func filtered(_ original : [String: AttributeValue], _ metricPoint : [String: AttributeValue]) -> [String: AttributeValue] {
59+
func filtered(_ original: [String: AttributeValue], _ metricPoint: [String: AttributeValue]) -> [String: AttributeValue] {
6360
if metricPoint.isEmpty {
6461
return original
6562
}

0 commit comments

Comments
 (0)