Skip to content

Commit ac04c5c

Browse files
author
Nacho Bonafonte
committed
PointData changed to be a class instead of a protocol, it avoids usage of any keyword and the existence of the AnyPointData class
Removed other protocols dependant on AnyPointData Fix formatting
1 parent d625747 commit ac04c5c

21 files changed

+268
-394
lines changed

Sources/Exporters/OpenTelemetryProtocol/metric/MetricsAdapter.swift

Lines changed: 49 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ import OpenTelemetryApi
77
import OpenTelemetrySdk
88

99
struct MetricsAdapter {
10-
11-
static func toProtoResourceMetrics(stableMetricData : [StableMetricData]) -> [Opentelemetry_Proto_Metrics_V1_ResourceMetrics] {
10+
static func toProtoResourceMetrics(stableMetricData: [StableMetricData]) -> [Opentelemetry_Proto_Metrics_V1_ResourceMetrics] {
1211
let resourceAndScopeMap = groupByResouceAndScope(stableMetricData: stableMetricData)
13-
12+
1413
var resourceMetrics = [Opentelemetry_Proto_Metrics_V1_ResourceMetrics]()
1514
resourceAndScopeMap.forEach { resMap in
1615
var instrumentationScopeMetrics = [Opentelemetry_Proto_Metrics_V1_ScopeMetrics]()
@@ -30,7 +29,7 @@ struct MetricsAdapter {
3029
}
3130
return resourceMetrics
3231
}
33-
32+
3433
static func toProtoResourceMetrics(metricDataList: [Metric]) -> [Opentelemetry_Proto_Metrics_V1_ResourceMetrics] {
3534
let resourceAndScopeMap = groupByResouceAndScope(metricDataList: metricDataList)
3635
var resourceMetrics = [Opentelemetry_Proto_Metrics_V1_ResourceMetrics]()
@@ -39,7 +38,7 @@ struct MetricsAdapter {
3938
var instrumentationScopeMetrics = [Opentelemetry_Proto_Metrics_V1_ScopeMetrics]()
4039
resMap.value.forEach { instScope in
4140
var protoInst =
42-
Opentelemetry_Proto_Metrics_V1_ScopeMetrics()
41+
Opentelemetry_Proto_Metrics_V1_ScopeMetrics()
4342
protoInst.scope =
4443
CommonAdapter.toProtoInstrumentationScope(instrumentationScopeInfo: instScope.key)
4544
instScope.value.forEach {
@@ -56,18 +55,17 @@ struct MetricsAdapter {
5655
return resourceMetrics
5756
}
5857

59-
6058
private static func groupByResouceAndScope(stableMetricData: [StableMetricData]) -> [Resource: [InstrumentationScopeInfo: [Opentelemetry_Proto_Metrics_V1_Metric]]] {
61-
var results = [Resource : [InstrumentationScopeInfo : [Opentelemetry_Proto_Metrics_V1_Metric]]]()
62-
59+
var results = [Resource: [InstrumentationScopeInfo: [Opentelemetry_Proto_Metrics_V1_Metric]]]()
60+
6361
stableMetricData.forEach {
6462
if let metric = toProtoMetric(stableMetric: $0) {
6563
results[$0.resource, default: [InstrumentationScopeInfo: [Opentelemetry_Proto_Metrics_V1_Metric]]()][$0.instrumentationScopeInfo, default: [Opentelemetry_Proto_Metrics_V1_Metric]()].append(metric)
6664
}
6765
}
6866
return results
6967
}
70-
68+
7169
private static func groupByResouceAndScope(metricDataList: [Metric]) -> [Resource: [InstrumentationScopeInfo: [Opentelemetry_Proto_Metrics_V1_Metric]]] {
7270
var results = [Resource: [InstrumentationScopeInfo: [Opentelemetry_Proto_Metrics_V1_Metric]]]()
7371

@@ -86,8 +84,8 @@ struct MetricsAdapter {
8684
protoMetric.name = stableMetric.name
8785
protoMetric.unit = stableMetric.unit
8886
protoMetric.description_p = stableMetric.description
89-
if stableMetric.data.points.isEmpty {return nil}
90-
87+
if stableMetric.data.points.isEmpty { return nil }
88+
9189
stableMetric.data.points.forEach {
9290
switch stableMetric.type {
9391
case .LongGauge:
@@ -115,7 +113,7 @@ struct MetricsAdapter {
115113
injectPointData(protoNumberPoint: &protoDataPoint, pointData: gaugeData)
116114
protoDataPoint.value = .asDouble(gaugeData.value)
117115
protoMetric.gauge.dataPoints.append(protoDataPoint)
118-
case.DoubleSum:
116+
case .DoubleSum:
119117
guard let gaugeData = $0 as? DoublePointData else {
120118
break
121119
}
@@ -147,55 +145,53 @@ struct MetricsAdapter {
147145
injectPointData(protoHistogramPoint: &protoDataPoint, pointData: histogramData)
148146
protoDataPoint.sum = Double(histogramData.sum)
149147
protoDataPoint.count = UInt64(histogramData.count)
150-
protoDataPoint.explicitBounds = histogramData.boundaries.map {Double($0)}
151-
protoDataPoint.bucketCounts = histogramData.counts.map{UInt64($0)}
148+
protoDataPoint.explicitBounds = histogramData.boundaries.map { Double($0) }
149+
protoDataPoint.bucketCounts = histogramData.counts.map { UInt64($0) }
152150
protoMetric.histogram.aggregationTemporality = .cumulative
153151
protoMetric.histogram.dataPoints.append(protoDataPoint)
154152
case .ExponentialHistogram:
155-
// todo: implement
153+
// TODO: implement
156154
break
157-
158155
}
159156
}
160157
return protoMetric
161158
}
162-
163-
static func injectPointData(protoHistogramPoint protoPoint: inout Opentelemetry_Proto_Metrics_V1_HistogramDataPoint, pointData: AnyPointData) {
164-
165-
protoPoint.timeUnixNano = pointData.endEpochNanos
166-
protoPoint.startTimeUnixNano = pointData.startEpochNanos
167-
168-
pointData.attributes.forEach {
169-
protoPoint.attributes.append(CommonAdapter.toProtoAttribute(key: $0.key, attributeValue: $0.value))
159+
160+
static func injectPointData(protoHistogramPoint protoPoint: inout Opentelemetry_Proto_Metrics_V1_HistogramDataPoint, pointData: PointData) {
161+
protoPoint.timeUnixNano = pointData.endEpochNanos
162+
protoPoint.startTimeUnixNano = pointData.startEpochNanos
163+
164+
pointData.attributes.forEach {
165+
protoPoint.attributes.append(CommonAdapter.toProtoAttribute(key: $0.key, attributeValue: $0.value))
166+
}
167+
168+
pointData.exemplars.forEach {
169+
var protoExemplar = Opentelemetry_Proto_Metrics_V1_Exemplar()
170+
protoExemplar.timeUnixNano = $0.epochNanos
171+
172+
$0.filteredAttributes.forEach {
173+
protoExemplar.filteredAttributes.append(CommonAdapter.toProtoAttribute(key: $0.key, attributeValue: $0.value))
170174
}
171-
172-
pointData.exemplars.forEach {
173-
var protoExemplar = Opentelemetry_Proto_Metrics_V1_Exemplar()
174-
protoExemplar.timeUnixNano = $0.epochNanos
175-
176-
$0.filteredAttributes.forEach{
177-
protoExemplar.filteredAttributes.append(CommonAdapter.toProtoAttribute(key: $0.key, attributeValue: $0.value))
178-
}
179-
if let spanContext = $0.spanContext {
180-
protoExemplar.spanID = TraceProtoUtils.toProtoSpanId(spanId: spanContext.spanId)
181-
protoExemplar.traceID = TraceProtoUtils.toProtoTraceId(traceId: spanContext.traceId)
182-
}
175+
if let spanContext = $0.spanContext {
176+
protoExemplar.spanID = TraceProtoUtils.toProtoSpanId(spanId: spanContext.spanId)
177+
protoExemplar.traceID = TraceProtoUtils.toProtoTraceId(traceId: spanContext.traceId)
183178
}
179+
}
184180
}
185-
186-
static func injectPointData(protoSummaryPoint protoPoint: inout Opentelemetry_Proto_Metrics_V1_SummaryDataPoint, pointData: AnyPointData) {
181+
182+
static func injectPointData(protoSummaryPoint protoPoint: inout Opentelemetry_Proto_Metrics_V1_SummaryDataPoint, pointData: PointData) {
187183
protoPoint.timeUnixNano = pointData.endEpochNanos
188184
protoPoint.startTimeUnixNano = pointData.startEpochNanos
189-
185+
190186
pointData.attributes.forEach {
191187
protoPoint.attributes.append(CommonAdapter.toProtoAttribute(key: $0.key, attributeValue: $0.value))
192188
}
193-
189+
194190
pointData.exemplars.forEach {
195191
var protoExemplar = Opentelemetry_Proto_Metrics_V1_Exemplar()
196192
protoExemplar.timeUnixNano = $0.epochNanos
197-
198-
$0.filteredAttributes.forEach{
193+
194+
$0.filteredAttributes.forEach {
199195
protoExemplar.filteredAttributes.append(CommonAdapter.toProtoAttribute(key: $0.key, attributeValue: $0.value))
200196
}
201197
if let spanContext = $0.spanContext {
@@ -204,32 +200,30 @@ struct MetricsAdapter {
204200
}
205201
}
206202
}
207-
208-
static func injectPointData( protoNumberPoint protoPoint: inout Opentelemetry_Proto_Metrics_V1_NumberDataPoint, pointData: AnyPointData) {
203+
204+
static func injectPointData(protoNumberPoint protoPoint: inout Opentelemetry_Proto_Metrics_V1_NumberDataPoint, pointData: PointData) {
209205
protoPoint.timeUnixNano = pointData.endEpochNanos
210206
protoPoint.startTimeUnixNano = pointData.startEpochNanos
211-
207+
212208
pointData.attributes.forEach {
213209
protoPoint.attributes.append(CommonAdapter.toProtoAttribute(key: $0.key, attributeValue: $0.value))
214210
}
215-
211+
216212
pointData.exemplars.forEach {
217213
var protoExemplar = Opentelemetry_Proto_Metrics_V1_Exemplar()
218214
protoExemplar.timeUnixNano = $0.epochNanos
219-
220-
$0.filteredAttributes.forEach{
215+
216+
$0.filteredAttributes.forEach {
221217
protoExemplar.filteredAttributes.append(CommonAdapter.toProtoAttribute(key: $0.key, attributeValue: $0.value))
222218
}
223219
if let spanContext = $0.spanContext {
224220
protoExemplar.spanID = TraceProtoUtils.toProtoSpanId(spanId: spanContext.spanId)
225221
protoExemplar.traceID = TraceProtoUtils.toProtoTraceId(traceId: spanContext.traceId)
226222
}
227223
protoPoint.exemplars.append(protoExemplar)
228-
229224
}
230-
231225
}
232-
226+
233227
static func toProtoMetric(metric: Metric) -> Opentelemetry_Proto_Metrics_V1_Metric? {
234228
var protoMetric = Opentelemetry_Proto_Metrics_V1_Metric()
235229
protoMetric.name = metric.name
@@ -364,14 +358,14 @@ struct MetricsAdapter {
364358
protoDataPoint.timeUnixNano = histogramData.timestamp.timeIntervalSince1970.toNanoseconds
365359
protoDataPoint.explicitBounds = histogramData.buckets.boundaries.map { Double($0) }
366360
protoDataPoint.bucketCounts = histogramData.buckets.counts.map { UInt64($0) }
367-
361+
368362
histogramData.labels.forEach {
369363
var kvp = Opentelemetry_Proto_Common_V1_KeyValue()
370364
kvp.key = $0.key
371365
kvp.value.stringValue = $0.value
372366
protoDataPoint.attributes.append(kvp)
373367
}
374-
368+
375369
protoMetric.histogram.aggregationTemporality = .cumulative
376370
protoMetric.histogram.dataPoints.append(protoDataPoint)
377371
case .doubleHistogram:
@@ -385,14 +379,14 @@ struct MetricsAdapter {
385379
protoDataPoint.timeUnixNano = histogramData.timestamp.timeIntervalSince1970.toNanoseconds
386380
protoDataPoint.explicitBounds = histogramData.buckets.boundaries.map { Double($0) }
387381
protoDataPoint.bucketCounts = histogramData.buckets.counts.map { UInt64($0) }
388-
382+
389383
histogramData.labels.forEach {
390384
var kvp = Opentelemetry_Proto_Common_V1_KeyValue()
391385
kvp.key = $0.key
392386
kvp.value.stringValue = $0.value
393387
protoDataPoint.attributes.append(kvp)
394388
}
395-
389+
396390
protoMetric.histogram.aggregationTemporality = .cumulative
397391
protoMetric.histogram.dataPoints.append(protoDataPoint)
398392
}
Lines changed: 2 additions & 3 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-
98
public protocol AggregatorFactory {
10-
func createAggregator(descriptor : InstrumentDescriptor, exemplarFilter : ExemplarFilter) -> any StableAggregator
9+
func createAggregator(descriptor: InstrumentDescriptor, exemplarFilter: ExemplarFilter) -> StableAggregator
1110
func isCompatible(with descriptor: InstrumentDescriptor) -> Bool
1211
}
Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +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

99
internal protocol AggregatorHandleProtocol {
10-
func doAggregateThenMaybeReset(startEpochNano: Int, endEpochNano: Int, attributes: [String: AttributeValue], reset: Bool) -> AnyPointData
10+
func doAggregateThenMaybeReset(startEpochNano: Int, endEpochNano: Int, attributes: [String: AttributeValue], reset: Bool) -> PointData
1111
func doRecordLong(value: Int)
1212
func doRecordDouble(value: Double)
1313
}
1414

15-
1615
public class AggregatorHandle {
17-
18-
19-
let exemplarReservoir : AnyExemplarReservoir
16+
let exemplarReservoir: AnyExemplarReservoir
2017

2118
internal init(exemplarReservoir: AnyExemplarReservoir) {
2219
self.exemplarReservoir = exemplarReservoir
2320
}
2421

25-
public func aggregateThenMaybeReset(startEpochNano: UInt64, endEpochNano: UInt64, attributes : [String: AttributeValue], reset: Bool) -> AnyPointData {
22+
public func aggregateThenMaybeReset(startEpochNano: UInt64, endEpochNano: UInt64, attributes: [String: AttributeValue], reset: Bool) -> PointData {
2623
doAggregateThenMaybeReset(startEpochNano: startEpochNano, endEpochNano: endEpochNano, attributes: attributes, exemplars: exemplarReservoir.collectAndReset(attribute: attributes), reset: reset)
2724
}
2825

@@ -35,7 +32,6 @@ public class AggregatorHandle {
3532
doRecordLong(value: value)
3633
}
3734

38-
3935
public func recordDouble(value: Double, attributes: [String: AttributeValue]) {
4036
exemplarReservoir.offerDoubleMeasurement(value: value, attributes: attributes)
4137
recordDouble(value: value)
@@ -45,10 +41,9 @@ public class AggregatorHandle {
4541
doRecordDouble(value: value)
4642
}
4743

48-
internal func doRecordDouble(value: Double) { fatalError() } // todo: better way to force subclass override
44+
internal func doRecordDouble(value: Double) { fatalError() } // TODO: better way to force subclass override
4945

50-
internal func doRecordLong(value: Int) { fatalError()}
51-
52-
internal func doAggregateThenMaybeReset(startEpochNano: UInt64, endEpochNano: UInt64, attributes: [String: AttributeValue],exemplars: [ExemplarData], reset: Bool) -> AnyPointData {fatalError()}
46+
internal func doRecordLong(value: Int) { fatalError() }
5347

48+
internal func doAggregateThenMaybeReset(startEpochNano: UInt64, endEpochNano: UInt64, attributes: [String: AttributeValue], exemplars: [ExemplarData], reset: Bool) -> PointData { fatalError() }
5449
}

0 commit comments

Comments
 (0)