Skip to content

Commit 4855809

Browse files
authored
Add API for Pre-aggregated data (#321)
These PR adds an API that allows for pre-aggregated histogram data to be recorded through the metric API. I'll be adding an additional API for counted metrics
1 parent 6ee76bc commit 4855809

21 files changed

+983
-1
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>FILEHEADER</key>
6+
<string>
7+
// Copyright The OpenTelemetry Authors
8+
// SPDX-License-Identifier: Apache-2.0
9+
// </string>
10+
</dict>
11+
</plist>

Sources/Exporters/OpenTelemetryProtocol/metric/MetricsAdapter.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ struct MetricsAdapter {
4848
static func toProtoMetric(metric: Metric) -> Opentelemetry_Proto_Metrics_V1_Metric? {
4949
var protoMetric = Opentelemetry_Proto_Metrics_V1_Metric()
5050
protoMetric.name = metric.name
51+
protoMetric.unit = "unit"
5152
protoMetric.description_p = metric.description
5253
if metric.data.isEmpty { return nil }
5354

Sources/OpenTelemetryApi/Metrics/BoundHistogramMetric.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@ open class BoundHistogramMetric<T> {
1414
/// - value: the histogram to be recorded.
1515
open func record(value: T) {
1616
}
17+
18+
1719
}

Sources/OpenTelemetryApi/Metrics/Meter.swift

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,21 @@ public protocol Meter {
5151
/// - Returns:The histogram instance.
5252
func createDoubleHistogram(name: String, explicitBoundaries: Array<Double>?, absolute: Bool) -> AnyHistogramMetric<Double>
5353

54+
// Creates a double histogram given the name, boundries, counts, and start and end dates.
55+
/// - Parameters:
56+
/// - name: The name of the measure.
57+
func createRawDoubleHistogram(name: String) -> AnyRawHistogramMetric<Double>
58+
59+
// Creates a Int histogram given the name, boundries, counts, and start and end dates.
60+
/// - Parameters:
61+
/// - name: The name of the measure.
62+
func createRawIntHistogram(name: String) -> AnyRawHistogramMetric<Int>
63+
64+
65+
func createRawDoubleCounter(name: String) -> AnyRawCounterMetric<Double>
66+
67+
func createRawIntCounter(name: String) -> AnyRawCounterMetric<Int>
68+
5469
/// Creates Int Observer with given name.
5570
/// - Parameters:
5671
/// - name: The name of the observer.
@@ -78,7 +93,7 @@ public protocol Meter {
7893
/// - Parameters:
7994
/// - name: The name of the gauge.
8095
/// - callback: The callback to be called to observe metric value.
81-
/// - Returns:The gauge instance.
96+
/// - Returns:The gauge instance.
8297
func createDoubleObservableGauge(name: String, callback: @escaping (DoubleObserverMetric) -> Void) -> DoubleObserverMetric
8398

8499
/// Constructs or retrieves the LabelSet from the given dictionary.

Sources/OpenTelemetryApi/Metrics/ProxyMeter.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,24 @@ import Foundation
77

88
/// Proxy Meter which act as a No-Op Meter, until real meter is provided.
99
public struct ProxyMeter: Meter {
10+
11+
public func createRawDoubleCounter(name: String) -> AnyRawCounterMetric<Double> {
12+
return realMeter?.createRawDoubleCounter(name: name) ?? AnyRawCounterMetric<Double>(NoopRawCounterMetric<Double>())
13+
}
14+
15+
16+
public func createRawIntCounter(name: String) -> AnyRawCounterMetric<Int> {
17+
return realMeter?.createRawIntCounter(name: name) ?? AnyRawCounterMetric<Int>(NoopRawCounterMetric<Int>())
18+
}
19+
20+
public func createRawDoubleHistogram(name: String) -> AnyRawHistogramMetric<Double> {
21+
return realMeter?.createRawDoubleHistogram(name: name) ?? AnyRawHistogramMetric<Double>(NoopRawHistogramMetric<Double>())
22+
}
23+
24+
public func createRawIntHistogram(name: String) -> AnyRawHistogramMetric<Int> {
25+
return realMeter?.createRawIntHistogram(name: name) ?? AnyRawHistogramMetric<Int>(NoopRawHistogramMetric<Int>())
26+
}
27+
1028
private var realMeter: Meter?
1129

1230
public func getLabelSet(labels: [String: String]) -> LabelSet {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
import Foundation
7+
8+
open class BoundRawCounterMetric<T> {
9+
public init() {}
10+
11+
open func record(sum: T, startDate: Date, endDate: Date) {}
12+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
import Foundation
7+
8+
open class BoundRawHistogramMetric<T> {
9+
public init() {}
10+
11+
/// record a raw histogarm metric
12+
/// - Parameters:
13+
/// - explicitBoundaries: Array of boundies
14+
/// - counts: Array of counts in each bucket
15+
/// - startDate: the start of the time range of the histogram
16+
/// - endDate : the end of the time range of the histogram
17+
open func record(explicitBoundaries: Array<T>, counts: Array<Int>, startDate: Date, endDate: Date, count: Int, sum: T) {}
18+
19+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
import Foundation
7+
8+
public protocol RawCounterMetric {
9+
associatedtype T
10+
11+
func record(sum: T, startDate : Date, endDate: Date, labels: [String:String])
12+
func record(sum: T, startDate : Date, endDate: Date, labelset: LabelSet)
13+
14+
/// Gets the bound counter metric with given labelset.
15+
/// - Parameters:
16+
/// - labelset: The labelset associated with this value.
17+
/// - Returns: The bound counter metric.
18+
func bind(labelset: LabelSet) -> BoundRawCounterMetric<T>
19+
20+
/// Gets the bound counter metric with given labels.
21+
/// - Parameters:
22+
/// - labels: The labels or dimensions associated with this value.
23+
/// - Returns: The bound counter metric.
24+
func bind(labels: [String: String]) -> BoundRawCounterMetric<T>
25+
}
26+
27+
28+
public struct AnyRawCounterMetric<T> :RawCounterMetric {
29+
30+
let internalCounter : Any
31+
private let _recordLabels: (T, Date, Date, [String:String]) -> Void
32+
private let _recordLabelset: (T, Date, Date, LabelSet) -> Void
33+
private let _bindLabels: ([String:String]) -> BoundRawCounterMetric<T>
34+
private let _bindLabelset: (LabelSet) -> BoundRawCounterMetric<T>
35+
36+
public init<U: RawCounterMetric>(_ countable: U) where U.T == T {
37+
internalCounter = countable
38+
_recordLabels = countable.record(sum:startDate:endDate:labels:)
39+
_recordLabelset = countable.record(sum:startDate:endDate:labelset:)
40+
_bindLabels = countable.bind(labels:)
41+
_bindLabelset = countable.bind(labelset:)
42+
}
43+
44+
public func bind(labelset: LabelSet) -> BoundRawCounterMetric<T> {
45+
_bindLabelset(labelset)
46+
}
47+
48+
public func bind(labels: [String : String]) -> BoundRawCounterMetric<T> {
49+
_bindLabels(labels)
50+
}
51+
52+
public func record(sum: T, startDate: Date, endDate: Date, labelset: LabelSet) {
53+
_recordLabelset(sum, startDate, endDate, labelset)
54+
}
55+
56+
public func record(sum: T, startDate: Date, endDate: Date, labels:[String:String]) {
57+
_recordLabels(sum, startDate, endDate, labels)
58+
}
59+
60+
61+
}
62+
63+
public struct NoopRawCounterMetric<T> : RawCounterMetric {
64+
public func record(sum: T, startDate: Date, endDate: Date, labels: [String : String]) {}
65+
66+
public func record(sum: T, startDate: Date, endDate: Date, labelset: LabelSet) {}
67+
68+
public func bind(labelset: LabelSet) -> BoundRawCounterMetric<T> {
69+
BoundRawCounterMetric<T>()
70+
}
71+
72+
public func bind(labels: [String : String]) -> BoundRawCounterMetric<T> {
73+
BoundRawCounterMetric<T>()
74+
}
75+
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
7+
import Foundation
8+
9+
// Raw Histogram Metric
10+
// use to record pre-aggregated data
11+
public protocol RawHistogramMetric {
12+
associatedtype T
13+
14+
/// Gets the bound raw histogram metric with given labelset.
15+
/// - Parameters:
16+
/// - labelset: The labelset from which bound instrument should be constructed.
17+
/// - Returns: The bound raw histogram metric.
18+
19+
func bind(labelset: LabelSet) -> BoundRawHistogramMetric<T>
20+
21+
/// Gets the bound histogram metric with given labelset.
22+
/// - Parameters:
23+
/// - labels: The labels or dimensions associated with this value.
24+
/// - Returns: The bound histogram metric.
25+
func bind(labels: [String: String]) -> BoundRawHistogramMetric<T>
26+
27+
/// record a raw histogarm metric
28+
/// - Parameters:
29+
/// - explicitBoundaries: Array of boundies
30+
/// - counts: Array of counts in each bucket
31+
/// - startDate: the start of the time range of the histogram
32+
/// - endDate : the end of the time range of the histogram
33+
/// - labelset: The labelset from which bound instrument should be constructed.
34+
func record(explicitBoundaries: Array<T>,
35+
counts: Array<Int>,
36+
startDate: Date,
37+
endDate: Date,
38+
count: Int,
39+
sum: T,
40+
labelset: LabelSet) -> Void
41+
// {
42+
// bind(labelset: labelset).record(explicitBoundaries: explicitBoundaries, counts: counts, startDate: startDate, endDate: endDate, count: count, sum: sum)
43+
// }
44+
45+
/// record a raw histogarm metric
46+
/// - Parameters:
47+
/// - explicitBoundaries: Array of boundies
48+
/// - counts: Array of counts in each bucket
49+
/// - startDate: the start of the time range of the histogram
50+
/// - endDate : the end of the time range of the histogram
51+
/// - labels: the labels or dimensions associated with the histogram
52+
func record(explicitBoundaries: Array<T>,
53+
counts: Array<Int>,
54+
startDate: Date,
55+
endDate: Date,
56+
count: Int,
57+
sum: T,
58+
labels: [String:String]) -> Void
59+
// {
60+
//
61+
// bind(labels:labels).record(explicitBoundaries: explicitBoundaries, counts: counts, startDate: startDate, endDate: endDate, count: count, sum: sum)
62+
// }
63+
}
64+
65+
66+
public struct AnyRawHistogramMetric<T> : RawHistogramMetric {
67+
let internalHistogram : Any
68+
69+
private let _bindLabelSet: (LabelSet) -> BoundRawHistogramMetric<T>
70+
private let _bindLabels: ([String:String]) -> BoundRawHistogramMetric<T>
71+
private let _bindRecordLabelSet: (Array<T>, Array<Int>, Date, Date, Int, T, LabelSet) -> Void
72+
private let _bindRecordLabels: (Array<T>, Array<Int>, Date, Date, Int, T, [String:String]) -> Void
73+
74+
75+
public init <U: RawHistogramMetric>(_ histogram: U) where U.T == T {
76+
internalHistogram = histogram
77+
_bindLabelSet = histogram.bind(labelset:)
78+
_bindLabels = histogram.bind(labels:)
79+
_bindRecordLabels = histogram.record(explicitBoundaries:counts:startDate:endDate:count:sum:labels:)
80+
_bindRecordLabelSet = histogram.record(explicitBoundaries:counts:startDate:endDate:count:sum:labelset:)
81+
}
82+
83+
public func bind(labelset: LabelSet) -> BoundRawHistogramMetric<T> {
84+
_bindLabelSet(labelset)
85+
}
86+
87+
public func bind(labels: [String : String]) -> BoundRawHistogramMetric<T> {
88+
_bindLabels(labels)
89+
}
90+
91+
public func record(explicitBoundaries: Array<T>,
92+
counts: Array<Int>,
93+
startDate: Date,
94+
endDate: Date,
95+
count: Int,
96+
sum: T,
97+
labels: [String:String]) {
98+
_bindRecordLabels(explicitBoundaries, counts, startDate, endDate, count, sum, labels)
99+
}
100+
public func record(explicitBoundaries: Array<T>,
101+
counts: Array<Int>,
102+
startDate: Date,
103+
endDate: Date,
104+
count: Int,
105+
sum: T,
106+
labelset: LabelSet) {
107+
_bindRecordLabelSet(explicitBoundaries, counts, startDate, endDate, count, sum, labelset)
108+
}
109+
}
110+
111+
public struct NoopRawHistogramMetric<T> : RawHistogramMetric {
112+
public func record(explicitBoundaries: Array<T>, counts: Array<Int>, startDate: Date, endDate: Date, count: Int, sum: T, labelset: LabelSet) {
113+
114+
}
115+
116+
public func record(explicitBoundaries: Array<T>, counts: Array<Int>, startDate: Date, endDate: Date, count: Int, sum: T, labels: [String : String]) {
117+
118+
}
119+
120+
121+
public init() {}
122+
123+
public func bind(labelset: LabelSet) -> BoundRawHistogramMetric<T> {
124+
BoundRawHistogramMetric<T>()
125+
}
126+
127+
public func bind(labels: [String : String]) -> BoundRawHistogramMetric<T> {
128+
BoundRawHistogramMetric<T>()
129+
}
130+
131+
}
132+

Sources/OpenTelemetrySdk/Metrics/Export/MetricData.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,30 @@ public struct NoopMetricData: MetricData {
1818
}
1919

2020
public struct SumData<T>: MetricData {
21+
public init(startTimestamp: Date, timestamp: Date, labels: [String : String] = [String: String](), sum: T) {
22+
self.startTimestamp = startTimestamp
23+
self.timestamp = timestamp
24+
self.labels = labels
25+
self.sum = sum
26+
}
27+
2128
public var startTimestamp: Date
2229
public var timestamp: Date
2330
public var labels: [String: String] = [String: String]()
2431
public var sum: T
2532
}
2633

2734
public struct SummaryData<T>: MetricData {
35+
public init(startTimestamp: Date, timestamp: Date, labels: [String : String] = [String: String](), count: Int, sum: T, min: T, max: T) {
36+
self.startTimestamp = startTimestamp
37+
self.timestamp = timestamp
38+
self.labels = labels
39+
self.count = count
40+
self.sum = sum
41+
self.min = min
42+
self.max = max
43+
}
44+
2845
public var startTimestamp: Date
2946
public var timestamp: Date
3047
public var labels: [String: String] = [String: String]()
@@ -35,6 +52,15 @@ public struct SummaryData<T>: MetricData {
3552
}
3653

3754
public struct HistogramData<T>: MetricData {
55+
public init(startTimestamp: Date, timestamp: Date, labels: [String : String] = [String: String](), buckets: (boundaries: Array<T>, counts: Array<Int>), count: Int, sum: T) {
56+
self.startTimestamp = startTimestamp
57+
self.timestamp = timestamp
58+
self.labels = labels
59+
self.buckets = buckets
60+
self.count = count
61+
self.sum = sum
62+
}
63+
3864
public var startTimestamp: Date
3965
public var timestamp: Date
4066
public var labels: [String: String] = [String: String]()

0 commit comments

Comments
 (0)