Skip to content

Commit 13d54d9

Browse files
author
Nacho Bonafonte
committed
Add some more tests
1 parent 2e32611 commit 13d54d9

File tree

8 files changed

+235
-9
lines changed

8 files changed

+235
-9
lines changed

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 Aggregation {
8+
public protocol Aggregation: AnyObject {
99
func createAggregator(descriptor: InstrumentDescriptor, exemplarFilter: ExemplarFilter) -> StableAggregator
1010
func isCompatible(with descriptor: InstrumentDescriptor) -> Bool
1111
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ public class DoubleExplicitBucketHistogramAggregator: StableAggregator {
2525
super.init(exemplarReservoir: exemplarReservoir)
2626
}
2727

28-
private var boundaries: [Double]
29-
private var sum: Double
30-
private var min: Double
31-
private var max: Double
32-
private var count: UInt64
33-
private var counts: [Int]
28+
internal var boundaries: [Double]
29+
internal var sum: Double
30+
internal var min: Double
31+
internal var max: Double
32+
internal var count: UInt64
33+
internal var counts: [Int]
3434

3535
override func doAggregateThenMaybeReset(startEpochNano: UInt64, endEpochNano: UInt64, attributes: [String: AttributeValue], exemplars: [ExemplarData], reset: Bool) -> PointData {
3636
lock.lock()
@@ -78,7 +78,7 @@ public class DoubleExplicitBucketHistogramAggregator: StableAggregator {
7878
}
7979
}
8080

81-
private let boundaries: [Double]
81+
internal let boundaries: [Double]
8282
private let reservoirSupplier: () -> ExemplarReservoir
8383

8484
public init(boundaries: [Double], reservoirSupplier: @escaping () -> ExemplarReservoir) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class ExplicitBucketHistogramAggregation: Aggregation {
1010
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]
13+
internal let bucketBoundaries: [Double]
1414

1515
init(bucketBoundaries: [Double]) {
1616
self.bucketBoundaries = bucketBoundaries
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//
2+
// Copyright The OpenTelemetry Authors
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
6+
import Foundation
7+
import OpenTelemetryApi
8+
9+
import OpenTelemetryApi
10+
@testable import OpenTelemetrySdk
11+
import XCTest
12+
13+
public class AggregationSelectorTests: XCTestCase {
14+
func testGetDefaultAggregation() {
15+
let selector = AggregationSelector()
16+
let instrumentType = InstrumentType.counter
17+
XCTAssert(selector.getDefaultAggregation(for: instrumentType) === Aggregations.defaultAggregation())
18+
}
19+
20+
func testDefaultAggregationResolver() {
21+
let resolver = AggregationSelector.defaultSelector()
22+
let instrumentType = InstrumentType.histogram
23+
XCTAssert(resolver(instrumentType) === Aggregations.defaultAggregation())
24+
}
25+
26+
func testAggregationResolverForInstrumentType() {
27+
let selector = AggregationSelector()
28+
let instrumentType = InstrumentType.observableUpDownCounter
29+
let aggregation = Aggregations.sum()
30+
let resolver = selector.with(instrumentType: instrumentType, aggregation: aggregation)
31+
XCTAssert(resolver(instrumentType) === aggregation)
32+
}
33+
34+
func testAggregationResolverForDifferentInstrumentType() {
35+
let selector = AggregationSelector()
36+
let instrumentType = InstrumentType.observableCounter
37+
let aggregation = Aggregations.sum()
38+
let resolver = selector.with(instrumentType: instrumentType, aggregation: aggregation)
39+
let instrumentType2 = InstrumentType.histogram
40+
XCTAssert(resolver(instrumentType2) === Aggregations.defaultAggregation())
41+
}
42+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//
2+
// Copyright The OpenTelemetry Authors
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
6+
import OpenTelemetryApi
7+
@testable import OpenTelemetrySdk
8+
import XCTest
9+
10+
public class AggregationsTests: XCTestCase {
11+
func testDropAggregation() {
12+
XCTAssert(Aggregations.drop() === DropAggregation.instance)
13+
}
14+
15+
func testDefaultAggregation() {
16+
XCTAssert(Aggregations.defaultAggregation() === DefaultAggregation.instance)
17+
}
18+
19+
func testSumAggregation() {
20+
XCTAssert(Aggregations.sum() === SumAggregation.instance)
21+
}
22+
23+
func testLastValueAggregation() {
24+
XCTAssert(Aggregations.lastValue() === LastValueAggregation.instance)
25+
}
26+
27+
func testExplicitBucketHistogramAggregation() {
28+
XCTAssert(Aggregations.explicitBucketHistogram() === ExplicitBucketHistogramAggregation.instance)
29+
}
30+
31+
func testExplicitBucketHistogramAggregationWithBuckets() {
32+
let buckets = [0.0, 10.0, 20.0, 30.0]
33+
let aggregation = Aggregations.explicitBucketHistogram(buckets: buckets) as? ExplicitBucketHistogramAggregation
34+
XCTAssertEqual(aggregation?.bucketBoundaries, buckets)
35+
}
36+
37+
func testBase2ExponentialBucketHistogram() {
38+
// TODO: write test case
39+
}
40+
41+
func testBase2ExponentialBucketHistogramWithMaxBucketsAndMaxScale() {
42+
// TODO: write test case
43+
}
44+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
//
2+
// Copyright The OpenTelemetry Authors
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
6+
import OpenTelemetryApi
7+
@testable import OpenTelemetrySdk
8+
import XCTest
9+
10+
class DoubleExplicitBucketHistogramAggregatorTests: XCTestCase {
11+
// Test the creation of a new handle
12+
func testCreateHandle() {
13+
let aggregator = DoubleExplicitBucketHistogramAggregator(boundaries: [1.0, 2.0], reservoirSupplier: { ExemplarReservoir() })
14+
let handle = aggregator.createHandle() as! DoubleExplicitBucketHistogramAggregator.Handle
15+
XCTAssertEqual(handle.count, 0)
16+
XCTAssertEqual(handle.sum, 0)
17+
XCTAssertEqual(handle.min, Double.greatestFiniteMagnitude)
18+
XCTAssertEqual(handle.max, -1)
19+
XCTAssertEqual(handle.counts, [0, 0, 0])
20+
XCTAssertEqual(handle.boundaries, [1.0, 2.0])
21+
}
22+
23+
// Test the aggregation of double values
24+
func testDoRecordDouble() {
25+
let aggregator = DoubleExplicitBucketHistogramAggregator(boundaries: [1.0, 2.0], reservoirSupplier: { ExemplarReservoir() })
26+
let handle = aggregator.createHandle() as! DoubleExplicitBucketHistogramAggregator.Handle
27+
handle.recordDouble(value: 1.5)
28+
handle.recordDouble(value: 2.5)
29+
30+
let pointData = handle.aggregateThenMaybeReset(startEpochNano: 0, endEpochNano: 1, attributes: [:], reset: false) as! HistogramPointData
31+
32+
XCTAssertEqual(pointData.sum, 4.0)
33+
XCTAssertEqual(pointData.count, 2)
34+
XCTAssertEqual(pointData.min, 1.5)
35+
XCTAssertEqual(pointData.max, 2.5)
36+
XCTAssertEqual(pointData.counts, [0, 1, 1])
37+
XCTAssertEqual(pointData.boundaries, [1.0, 2.0])
38+
XCTAssertEqual(pointData.hasMin, true)
39+
XCTAssertEqual(pointData.hasMax, true)
40+
}
41+
42+
// Test the aggregation of long values
43+
func testDoRecordLong() {
44+
let aggregator = DoubleExplicitBucketHistogramAggregator(boundaries: [1.0, 2.0], reservoirSupplier: { ExemplarReservoir() })
45+
let handle = aggregator.createHandle()
46+
handle.recordLong(value: 1)
47+
handle.recordLong(value: 3)
48+
49+
let pointData = handle.aggregateThenMaybeReset(startEpochNano: 0, endEpochNano: 1, attributes: [:], reset: false) as! HistogramPointData
50+
51+
XCTAssertEqual(pointData.sum, 4.0)
52+
XCTAssertEqual(pointData.count, 2)
53+
XCTAssertEqual(pointData.min, 1.0)
54+
XCTAssertEqual(pointData.max, 3.0)
55+
XCTAssertEqual(pointData.counts, [1, 0, 1])
56+
XCTAssertEqual(pointData.boundaries, [1.0, 2.0])
57+
XCTAssertEqual(pointData.hasMin, true)
58+
XCTAssertEqual(pointData.hasMax, true)
59+
}
60+
61+
// Test the aggregation of double values and reset of the handle
62+
func testAggregateThenMaybeReset() {
63+
let aggregator = DoubleExplicitBucketHistogramAggregator(boundaries: [0.5, 1.0, 2.0], reservoirSupplier: { NoopExemplarReservoir() })
64+
let handle = aggregator.createHandle()
65+
handle.recordDouble(value: 1.5)
66+
handle.recordDouble(value: 0.25)
67+
handle.recordDouble(value: 0.75)
68+
69+
// Aggregate without resetting
70+
let pointData = handle.aggregateThenMaybeReset(startEpochNano: 0, endEpochNano: 1000, attributes: [:], reset: false) as! HistogramPointData
71+
XCTAssertEqual(pointData.sum, 2.5)
72+
XCTAssertEqual(pointData.count, 3)
73+
XCTAssertEqual(pointData.min, 0.25)
74+
XCTAssertEqual(pointData.max, 1.5)
75+
XCTAssertEqual(pointData.counts, [1, 1, 1, 0])
76+
77+
// Aggregate and reset
78+
let pointData2 = handle.aggregateThenMaybeReset(startEpochNano: 0, endEpochNano: 1000, attributes: [:], reset: true) as! HistogramPointData
79+
XCTAssertEqual(pointData2.sum, 2.5)
80+
XCTAssertEqual(pointData2.count, 3)
81+
XCTAssertEqual(pointData2.min, 0.25)
82+
XCTAssertEqual(pointData2.max, 1.5)
83+
XCTAssertEqual(pointData2.counts, [1, 1, 1, 0])
84+
85+
// Aggregate after reset
86+
let pointData3 = handle.aggregateThenMaybeReset(startEpochNano: 0, endEpochNano: 1000, attributes: [:], reset: false) as! HistogramPointData
87+
XCTAssertEqual(pointData3.sum, 0)
88+
XCTAssertEqual(pointData3.count, 0)
89+
XCTAssertEqual(pointData3.min, Double.greatestFiniteMagnitude)
90+
XCTAssertEqual(pointData3.max, -1)
91+
XCTAssertEqual(pointData3.counts, [0, 0, 0, 0])
92+
}
93+
94+
func testDiff() {
95+
// Initialize aggregator
96+
let aggregator = DoubleExplicitBucketHistogramAggregator(boundaries: [1.0, 2.0, 3.0], reservoirSupplier: { NoopExemplarReservoir() })
97+
98+
// Create some dummy PointData objects
99+
let previousCumulative = HistogramPointData(startEpochNanos: 0, endEpochNanos: 100, attributes: [:], exemplars: [], sum: 10.0, count: 1, min: 5.0, max: 5.0, boundaries: [], counts: [], hasMin: true, hasMax: true)
100+
let currentCumulative = HistogramPointData(startEpochNanos: 0, endEpochNanos: 200, attributes: [:], exemplars: [], sum: 20.0, count: 2, min: 3.0, max: 7.0, boundaries: [], counts: [], hasMin: true, hasMax: true)
101+
102+
// Verify that the diff() method throws the expected error
103+
XCTAssertThrowsError(try aggregator.diff(previousCumulative: previousCumulative, currentCumulative: currentCumulative))
104+
}
105+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//
2+
// Copyright The OpenTelemetry Authors
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
6+
import Foundation
7+
import OpenTelemetryApi
8+
@testable import OpenTelemetrySdk
9+
import XCTest
10+
11+
class ExplicitBucketHistogramAggregationTests: XCTestCase {
12+
func testCreateAggregator() {
13+
let boundaries: [Double] = [0, 10, 20, 30, 40]
14+
let aggregation = ExplicitBucketHistogramAggregation(bucketBoundaries: boundaries)
15+
let descriptor = InstrumentDescriptor(name: "test", description: "test", unit: "unit", type: .histogram, valueType: .double)
16+
let exemplarFilter = AlwaysOnFilter()
17+
18+
let aggregator = aggregation.createAggregator(descriptor: descriptor, exemplarFilter: exemplarFilter) as! DoubleExplicitBucketHistogramAggregator
19+
20+
XCTAssertNotNil(aggregator)
21+
XCTAssertEqual(aggregator.boundaries, boundaries)
22+
}
23+
24+
func testIsCompatible() {
25+
let aggregation = ExplicitBucketHistogramAggregation(bucketBoundaries: [0, 10, 20, 30, 40])
26+
27+
let compatibleDescriptor1 = InstrumentDescriptor(name: "test", description: "test", unit: "unit", type: .counter, valueType: .double)
28+
let compatibleDescriptor2 = InstrumentDescriptor(name: "test", description: "test", unit: "unit", type: .histogram, valueType: .double)
29+
let incompatibleDescriptor = InstrumentDescriptor(name: "test", description: "test", unit: "unit", type: .counter, valueType: .double)
30+
31+
XCTAssertTrue(aggregation.isCompatible(with: compatibleDescriptor1))
32+
XCTAssertTrue(aggregation.isCompatible(with: compatibleDescriptor2))
33+
XCTAssertFalse(aggregation.isCompatible(with: incompatibleDescriptor))
34+
}
35+
}

0 commit comments

Comments
 (0)