Skip to content

Commit d0c0e75

Browse files
committed
added tests. MaxValueAggregator reset max value on read.
1 parent e1e057f commit d0c0e75

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

Sources/OpenTelemetrySdk/Metrics/Aggregators/MaxValueAggregator.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class MaxValueAggregator<T: SignedNumeric & Comparable>: Aggregator<T> {
3333
lock.withLockVoid {
3434
super.checkpoint()
3535
self.pointCheck = self.value
36+
self.value = 0
3637
}
3738
}
3839

Tests/ExportersTests/OpenTelemetryProtocol/OtlpMetricExporterTests.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,31 @@ class OtlpMetricExproterTests: XCTestCase {
3737
exporter.shutdown()
3838
}
3939

40+
func testGaugeExport() {
41+
let metric = generateGaugeMetric()
42+
let exporter = OtelpMetricExporter(channel: channel)
43+
44+
let result = exporter.export(metrics: [metric]) { () -> Bool in
45+
false
46+
}
47+
XCTAssertEqual(result, MetricExporterResultCode.success)
48+
49+
XCTAssertEqual(fakeCollector.receivedMetrics.count, 1)
50+
let otlpMetric = fakeCollector.receivedMetrics[0].instrumentationLibraryMetrics[0].metrics[0]
51+
XCTAssertEqual(metric.name, otlpMetric.name)
52+
XCTAssertEqual(otlpMetric.intGauge.dataPoints.count, 1)
53+
let dataPoint = otlpMetric.intGauge.dataPoints[0]
54+
let sum = metric.data[0] as! SumData<Int>
55+
XCTAssertEqual(sum.timestamp.timeIntervalSince1970.toNanoseconds, dataPoint.timeUnixNano)
56+
XCTAssertEqual(sum.startTimestamp.timeIntervalSince1970.toNanoseconds, dataPoint.startTimeUnixNano)
57+
XCTAssertEqual(sum.sum, Int(dataPoint.value))
58+
59+
}
60+
4061
func testExportMultipleMetrics() {
4162
var metrics = [Metric]()
4263
for _ in 0 ..< 10 {
64+
4365
metrics.append(generateSumMetric())
4466
}
4567
let exporter = OtelpMetricExporter(channel: channel)
@@ -98,6 +120,14 @@ class OtlpMetricExproterTests: XCTestCase {
98120
metric.data.append(data)
99121
return metric
100122
}
123+
124+
func generateGaugeMetric() -> Metric {
125+
let library = InstrumentationLibraryInfo(name: "lib", version: "semver:0.0.0")
126+
var metric = Metric(namespace: "namespace", name: "MyGauge", desc: "description", type: .intGauge, resource: Resource(), instrumentationLibraryInfo: library)
127+
let data = SumData(startTimestamp: Date(), timestamp: Date(), labels: ["hello": "world"], sum: 100)
128+
metric.data.append(data)
129+
return metric
130+
}
101131
}
102132

103133
class FakeMetricCollector: Opentelemetry_Proto_Collector_Metrics_V1_MetricsServiceProvider {
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright 2021, OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
16+
@testable import OpenTelemetrySdk
17+
import XCTest
18+
19+
final class MaxValueAggregatorTests : XCTestCase {
20+
public func testAsyncSafety() {
21+
let agg = MaxValueAggregator<Int>()
22+
var sum = agg.toMetricData() as! SumData<Int>
23+
24+
XCTAssertEqual(sum.sum, 0)
25+
26+
DispatchQueue.concurrentPerform(iterations: 10) { _ in
27+
for i in 0 ..< 10000 {
28+
agg.update(value: i)
29+
}
30+
}
31+
32+
agg.update(value: 10001)
33+
34+
agg.checkpoint()
35+
sum = agg.toMetricData() as! SumData<Int>
36+
37+
XCTAssertEqual(sum.sum, 10001)
38+
}
39+
40+
public func testMaxAggPeriod() {
41+
let agg = MaxValueAggregator<Int>()
42+
var sum = agg.toMetricData() as! SumData<Int>
43+
44+
XCTAssertEqual(sum.sum, 0)
45+
46+
agg.update(value: 100)
47+
agg.checkpoint()
48+
49+
sum = agg.toMetricData() as! SumData<Int>
50+
51+
XCTAssertEqual(sum.sum, 100)
52+
53+
agg.update(value: 88)
54+
agg.checkpoint()
55+
56+
sum = agg.toMetricData() as! SumData<Int>
57+
58+
XCTAssertEqual(sum.sum, 88)
59+
60+
}
61+
}

0 commit comments

Comments
 (0)