@@ -7,10 +7,9 @@ import OpenTelemetryApi
77import OpenTelemetrySdk
88
99struct 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 }
0 commit comments