Skip to content

Commit 1e401d8

Browse files
committed
Added some tests for MetricsAdapter
Signed-off-by: Kevin Earls <[email protected]>
1 parent 1ec499c commit 1e401d8

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
//
2+
// Copyright The OpenTelemetry Authors
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
6+
import XCTest
7+
import OpenTelemetryApi
8+
@testable import OpenTelemetryProtocolExporter
9+
@testable import OpenTelemetrySdk
10+
11+
final class MetricsAdapterTest: XCTestCase {
12+
let resource = Resource(attributes: ["foo": AttributeValue("bar")])
13+
let instrumentationScopeInfo = InstrumentationScopeInfo(name: "test")
14+
let unit = "unit"
15+
16+
func testToProtoResourceMetricsWithLongGuage() throws {
17+
let pointValue = Int.random(in: 1...999)
18+
let point:PointData = LongPointData(startEpochNanos: 0, endEpochNanos: 1, attributes: [:], exemplars: [], value: pointValue)
19+
let guageData = StableGaugeData(aggregationTemporality: .cumulative, points: [point])
20+
let metricData = StableMetricData.createLongGauge(resource: resource, instrumentationScopeInfo: instrumentationScopeInfo, name: name, description: description, unit: unit, data: guageData)
21+
22+
let result = MetricsAdapter.toProtoMetric(stableMetric: metricData)
23+
guard let value = result?.gauge.dataPoints as? [Opentelemetry_Proto_Metrics_V1_NumberDataPoint] else {
24+
let actualType = type(of: result?.gauge.dataPoints)
25+
let errorMessage = "Got wrong type: \(actualType)"
26+
XCTFail(errorMessage)
27+
throw errorMessage
28+
}
29+
30+
XCTAssertEqual(value.first?.asInt, Int64(pointValue))
31+
}
32+
33+
func testToProtoResourceMetricsWithLongSum() throws {
34+
let pointValue = Int.random(in: 1...999)
35+
let point:PointData = LongPointData(startEpochNanos: 0, endEpochNanos: 1, attributes: [:], exemplars: [], value: pointValue)
36+
let sumData = StableSumData(aggregationTemporality: .cumulative, points: [point])
37+
let metricData = StableMetricData.createLongSum(resource: resource, instrumentationScopeInfo: instrumentationScopeInfo, name: name, description: description, unit: unit, data: sumData)
38+
39+
let result = MetricsAdapter.toProtoMetric(stableMetric: metricData)
40+
guard let value = result?.sum.dataPoints as? [Opentelemetry_Proto_Metrics_V1_NumberDataPoint] else {
41+
let actualType = type(of: result?.gauge.dataPoints)
42+
let errorMessage = "Got wrong type: \(actualType)"
43+
XCTFail(errorMessage)
44+
throw errorMessage
45+
}
46+
47+
XCTAssertEqual(value.first?.asInt, Int64(pointValue))
48+
}
49+
50+
func testToProtoResourceMetricsWithDoubleGuage() throws {
51+
let pointValue: Double = Double.random(in: 1...999)
52+
let point:PointData = DoublePointData(startEpochNanos: 0, endEpochNanos: 1, attributes: [:], exemplars: [], value: pointValue)
53+
let guageData = StableGaugeData(aggregationTemporality: .cumulative, points: [point])
54+
let metricData = StableMetricData.createDoubleGauge(resource: resource, instrumentationScopeInfo: instrumentationScopeInfo, name: name, description: description, unit: unit, data: guageData)
55+
56+
let result = MetricsAdapter.toProtoMetric(stableMetric: metricData)
57+
guard let value = result?.gauge.dataPoints as? [Opentelemetry_Proto_Metrics_V1_NumberDataPoint] else {
58+
let actualType = type(of: result?.gauge.dataPoints)
59+
let errorMessage = "Got wrong type: \(actualType)"
60+
XCTFail(errorMessage)
61+
throw errorMessage
62+
}
63+
64+
XCTAssertEqual(value.first?.asDouble, pointValue)
65+
}
66+
67+
func testToProtoResourceMetricsWithDoubleSum() throws {
68+
let pointValue: Double = Double.random(in: 1...999)
69+
let point:PointData = DoublePointData(startEpochNanos: 0, endEpochNanos: 1, attributes: [:], exemplars: [], value: pointValue)
70+
let sumData = StableSumData(aggregationTemporality: .cumulative, points: [point])
71+
let metricData = StableMetricData.createDoubleSum(resource: resource, instrumentationScopeInfo: instrumentationScopeInfo, name: name, description: description, unit: unit, data: sumData)
72+
73+
let result = MetricsAdapter.toProtoMetric(stableMetric: metricData)
74+
guard let value = result?.sum.dataPoints as? [Opentelemetry_Proto_Metrics_V1_NumberDataPoint] else {
75+
let actualType = type(of: result?.gauge.dataPoints)
76+
let errorMessage = "Got wrong type: \(actualType)"
77+
XCTFail(errorMessage)
78+
throw errorMessage
79+
}
80+
81+
XCTAssertEqual(value.first?.asDouble, pointValue)
82+
}
83+
84+
func testToProtoResourceMetricsWithHistogram() throws {
85+
let boundaries = [Double]()
86+
let sum:Double = Double.random(in: 1...999)
87+
let min = Double.greatestFiniteMagnitude
88+
let max:Double = -1
89+
let count = Int.random(in: 1...100)
90+
let counts = Array(repeating: 0, count: boundaries.count + 1)
91+
let histogramPointData = HistogramPointData(startEpochNanos: 0, endEpochNanos: 1, attributes: [:], exemplars: [ExemplarData](), sum: sum, count: UInt64(count), min: min, max: max, boundaries: boundaries, counts: counts, hasMin: count > 0, hasMax: count > 0)
92+
let points = [histogramPointData]
93+
let histogramData = StableHistogramData(aggregationTemporality: .cumulative, points: points)
94+
let metricData = StableMetricData.createHistogram(resource: resource, instrumentationScopeInfo: instrumentationScopeInfo, name: name, description: description, unit: unit, data: histogramData)
95+
96+
let result = MetricsAdapter.toProtoMetric(stableMetric: metricData)
97+
guard let value = result?.histogram.dataPoints as? [Opentelemetry_Proto_Metrics_V1_HistogramDataPoint]? else {
98+
let actualType = type(of: result?.gauge.dataPoints)
99+
let errorMessage = "Got wrong type: \(actualType)"
100+
XCTFail(errorMessage)
101+
throw errorMessage
102+
}
103+
104+
XCTAssertEqual(value?.first?.sum, sum)
105+
XCTAssertEqual(value?.first?.count, UInt64(count))
106+
}
107+
}

0 commit comments

Comments
 (0)