@@ -16,7 +16,6 @@ import Foundation
1616import OpenTelemetryApi
1717import OpenTelemetrySdk
1818
19-
2019struct MetricsAdapter {
2120 static func toProtoResourceMetrics( metricDataList: [ Metric ] ) -> [ Opentelemetry_Proto_Metrics_V1_ResourceMetrics ] {
2221 let resourceAndLibraryMap = groupByResouceAndLibrary ( metricDataList: metricDataList)
@@ -26,7 +25,7 @@ struct MetricsAdapter {
2625 var instrumentationLibraryMetrics = [ Opentelemetry_Proto_Metrics_V1_InstrumentationLibraryMetrics] ( )
2726 resMap. value. forEach { instLibrary in
2827 var protoInst =
29- Opentelemetry_Proto_Metrics_V1_InstrumentationLibraryMetrics ( )
28+ Opentelemetry_Proto_Metrics_V1_InstrumentationLibraryMetrics ( )
3029 protoInst. instrumentationLibrary =
3130 CommonAdapter . toProtoInstrumentationLibrary ( instrumentationLibraryInfo: instLibrary. key)
3231 instLibrary. value. forEach {
@@ -38,112 +37,101 @@ struct MetricsAdapter {
3837 resourceMetric. resource = ResourceAdapter . toProtoResource ( resource: resMap. key)
3938 resourceMetric. instrumentationLibraryMetrics. append ( contentsOf: instrumentationLibraryMetrics)
4039 resourceMetrics. append ( resourceMetric)
41-
4240 }
4341
44-
45-
4642 return resourceMetrics
4743 }
4844
49- private static func groupByResouceAndLibrary( metricDataList: [ Metric ] ) -> [ Resource : [ InstrumentationLibraryInfo : [ Opentelemetry_Proto_Metrics_V1_Metric ] ] ] {
50- var results = [ Resource : [ InstrumentationLibraryInfo : [ Opentelemetry_Proto_Metrics_V1_Metric] ] ] ( )
45+ private static func groupByResouceAndLibrary( metricDataList: [ Metric ] ) -> [ Resource : [ InstrumentationLibraryInfo : [ Opentelemetry_Proto_Metrics_V1_Metric ] ] ] {
46+ var results = [ Resource: [ InstrumentationLibraryInfo: [ Opentelemetry_Proto_Metrics_V1_Metric] ] ] ( )
5147
5248 metricDataList. forEach {
53- results [ $0. resource, default: [ InstrumentationLibraryInfo : [ Opentelemetry_Proto_Metrics_V1_Metric] ] ( ) ] [ $0. instrumentationLibraryInfo, default: [ Opentelemetry_Proto_Metrics_V1_Metric] ( ) ]
49+ results [ $0. resource, default: [ InstrumentationLibraryInfo: [ Opentelemetry_Proto_Metrics_V1_Metric] ] ( ) ] [ $0. instrumentationLibraryInfo, default: [ Opentelemetry_Proto_Metrics_V1_Metric] ( ) ]
5450 . append ( toProtoMetric ( metric: $0) )
5551 }
5652
5753 return results
5854 }
5955
6056 static func toProtoMetric( metric: Metric ) -> Opentelemetry_Proto_Metrics_V1_Metric {
61-
6257 var protoMetric = Opentelemetry_Proto_Metrics_V1_Metric ( )
6358 protoMetric. name = metric. name
6459 protoMetric. description_p = metric. description
6560
66-
6761 metric. data. forEach {
68- switch metric. aggregationType {
69- case . doubleSum:
70- guard let sumData = $0 as? SumData < Double > else {
71- break
72- }
73- var protoDataPoint = Opentelemetry_Proto_Metrics_V1_DoubleDataPoint ( )
74- protoDataPoint. value = sumData. sum
75- sumData. labels. forEach {
76- var kvp = Opentelemetry_Proto_Common_V1_StringKeyValue ( )
77- kvp. key = $0. key
78- kvp. value = $0. value
79- protoDataPoint. labels. append ( kvp)
80- }
62+ switch metric. aggregationType {
63+ case . doubleSum:
64+ guard let sumData = $0 as? SumData < Double > else {
65+ break
66+ }
67+ var protoDataPoint = Opentelemetry_Proto_Metrics_V1_DoubleDataPoint ( )
68+ protoDataPoint. value = sumData. sum
69+ sumData. labels. forEach {
70+ var kvp = Opentelemetry_Proto_Common_V1_StringKeyValue ( )
71+ kvp. key = $0. key
72+ kvp. value = $0. value
73+ protoDataPoint. labels. append ( kvp)
74+ }
8175
82- protoMetric. doubleSum. dataPoints. append ( protoDataPoint)
83- break
84- case . doubleSummary:
76+ protoMetric. doubleSum. dataPoints. append ( protoDataPoint)
77+ case . doubleSummary:
8578
86- guard let summaryData = $0 as? SummaryData < Double > else {
87- break
88- }
89- var protoDataPoint = Opentelemetry_Proto_Metrics_V1_DoubleHistogramDataPoint ( )
90- protoDataPoint. sum = summaryData. sum
91- protoDataPoint. count = UInt64 ( summaryData. count)
92- protoDataPoint. explicitBounds = [ summaryData. min, summaryData. max]
79+ guard let summaryData = $0 as? SummaryData < Double > else {
80+ break
81+ }
82+ var protoDataPoint = Opentelemetry_Proto_Metrics_V1_DoubleHistogramDataPoint ( )
83+ protoDataPoint. sum = summaryData. sum
84+ protoDataPoint. count = UInt64 ( summaryData. count)
85+ protoDataPoint. explicitBounds = [ summaryData. min, summaryData. max]
9386
94- protoDataPoint. startTimeUnixNano = summaryData. startTimestamp. timeIntervalSince1970. toNanoseconds
95- protoDataPoint. timeUnixNano = summaryData. timestamp. timeIntervalSince1970. toNanoseconds
87+ protoDataPoint. startTimeUnixNano = summaryData. startTimestamp. timeIntervalSince1970. toNanoseconds
88+ protoDataPoint. timeUnixNano = summaryData. timestamp. timeIntervalSince1970. toNanoseconds
9689
97- summaryData. labels. forEach {
98- var kvp = Opentelemetry_Proto_Common_V1_StringKeyValue ( )
99- kvp. key = $0. key
100- kvp. value = $0. value
101- protoDataPoint. labels. append ( kvp)
102- }
90+ summaryData. labels. forEach {
91+ var kvp = Opentelemetry_Proto_Common_V1_StringKeyValue ( )
92+ kvp. key = $0. key
93+ kvp. value = $0. value
94+ protoDataPoint. labels. append ( kvp)
95+ }
10396
104- protoMetric. doubleHistogram. dataPoints. append ( protoDataPoint)
97+ protoMetric. doubleHistogram. dataPoints. append ( protoDataPoint)
10598
106- break
107- case . intSum:
108- guard let sumData = $0 as? SumData < Int > else {
109- break ;
110- }
111- var protoDataPoint = Opentelemetry_Proto_Metrics_V1_IntDataPoint ( )
112- protoDataPoint. value = Int64 ( sumData. sum)
113- sumData. labels. forEach {
114- var kvp = Opentelemetry_Proto_Common_V1_StringKeyValue ( )
115- kvp. key = $0. key
116- kvp. value = $0. value
117- protoDataPoint. labels. append ( kvp)
118- }
99+ case . intSum:
100+ guard let sumData = $0 as? SumData < Int > else {
101+ break
102+ }
103+ var protoDataPoint = Opentelemetry_Proto_Metrics_V1_IntDataPoint ( )
104+ protoDataPoint. value = Int64 ( sumData. sum)
105+ sumData. labels. forEach {
106+ var kvp = Opentelemetry_Proto_Common_V1_StringKeyValue ( )
107+ kvp. key = $0. key
108+ kvp. value = $0. value
109+ protoDataPoint. labels. append ( kvp)
110+ }
119111
120- protoMetric. intSum. dataPoints. append ( protoDataPoint)
112+ protoMetric. intSum. dataPoints. append ( protoDataPoint)
121113
122- break
123- case . intSummary:
124- guard let summaryData = $0 as? SummaryData < Int > else {
125- break
126- }
127- var protoDataPoint = Opentelemetry_Proto_Metrics_V1_IntHistogramDataPoint ( )
128- protoDataPoint. sum = Int64 ( summaryData. sum)
129- protoDataPoint. count = UInt64 ( summaryData. count)
130- protoDataPoint. bucketCounts = [ UInt64 ( summaryData. min) , UInt64 ( summaryData. max) ]
131- protoDataPoint. startTimeUnixNano = summaryData. startTimestamp. timeIntervalSince1970. toNanoseconds
132- protoDataPoint. timeUnixNano = summaryData. timestamp. timeIntervalSince1970. toNanoseconds
114+ case . intSummary:
115+ guard let summaryData = $0 as? SummaryData < Int > else {
116+ break
117+ }
118+ var protoDataPoint = Opentelemetry_Proto_Metrics_V1_IntHistogramDataPoint ( )
119+ protoDataPoint. sum = Int64 ( summaryData. sum)
120+ protoDataPoint. count = UInt64 ( summaryData. count)
121+ protoDataPoint. bucketCounts = [ UInt64 ( summaryData. min) , UInt64 ( summaryData. max) ]
122+ protoDataPoint. startTimeUnixNano = summaryData. startTimestamp. timeIntervalSince1970. toNanoseconds
123+ protoDataPoint. timeUnixNano = summaryData. timestamp. timeIntervalSince1970. toNanoseconds
133124
134- summaryData. labels. forEach {
135- var kvp = Opentelemetry_Proto_Common_V1_StringKeyValue ( )
136- kvp. key = $0. key
137- kvp. value = $0. value
138- protoDataPoint. labels. append ( kvp)
139- }
140-
141- protoMetric. intHistogram. dataPoints. append ( protoDataPoint)
125+ summaryData. labels. forEach {
126+ var kvp = Opentelemetry_Proto_Common_V1_StringKeyValue ( )
127+ kvp. key = $0. key
128+ kvp. value = $0. value
129+ protoDataPoint. labels. append ( kvp)
130+ }
142131
143- break
132+ protoMetric . intHistogram . dataPoints . append ( protoDataPoint )
144133 }
145134 }
146135 return protoMetric
147136 }
148-
149137}
0 commit comments