Skip to content

Commit ab96e89

Browse files
author
Ignacio Bonafonte
authored
Merge pull request #174 from bryce-b/metric-proto-fix
corrected use of various metric protobuf types Histogram datapoints were incorrectly being used in place of summary data points.
2 parents 3b5c4c4 + 1423221 commit ab96e89

File tree

1 file changed

+72
-75
lines changed

1 file changed

+72
-75
lines changed

Sources/Exporters/OpenTelemetryProtocol/metric/MetricsAdapter.swift

Lines changed: 72 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ struct MetricsAdapter {
2020
static func toProtoResourceMetrics(metricDataList: [Metric]) -> [Opentelemetry_Proto_Metrics_V1_ResourceMetrics] {
2121
let resourceAndLibraryMap = groupByResouceAndLibrary(metricDataList: metricDataList)
2222
var resourceMetrics = [Opentelemetry_Proto_Metrics_V1_ResourceMetrics]()
23-
23+
2424
resourceAndLibraryMap.forEach { resMap in
2525
var instrumentationLibraryMetrics = [Opentelemetry_Proto_Metrics_V1_InstrumentationLibraryMetrics]()
2626
resMap.value.forEach { instLibrary in
@@ -38,98 +38,95 @@ struct MetricsAdapter {
3838
resourceMetric.instrumentationLibraryMetrics.append(contentsOf: instrumentationLibraryMetrics)
3939
resourceMetrics.append(resourceMetric)
4040
}
41-
41+
4242
return resourceMetrics
4343
}
44-
44+
4545
private static func groupByResouceAndLibrary(metricDataList: [Metric]) -> [Resource: [InstrumentationLibraryInfo: [Opentelemetry_Proto_Metrics_V1_Metric]]] {
4646
var results = [Resource: [InstrumentationLibraryInfo: [Opentelemetry_Proto_Metrics_V1_Metric]]]()
4747

4848
metricDataList.forEach {
4949
results[$0.resource, default: [InstrumentationLibraryInfo: [Opentelemetry_Proto_Metrics_V1_Metric]]()][$0.instrumentationLibraryInfo, default: [Opentelemetry_Proto_Metrics_V1_Metric]()]
5050
.append(toProtoMetric(metric: $0))
5151
}
52-
52+
5353
return results
5454
}
55-
55+
5656
static func toProtoMetric(metric: Metric) -> Opentelemetry_Proto_Metrics_V1_Metric {
5757
var protoMetric = Opentelemetry_Proto_Metrics_V1_Metric()
5858
protoMetric.name = metric.name
5959
protoMetric.description_p = metric.description
6060

6161
metric.data.forEach {
6262
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-
}
75-
76-
protoMetric.doubleSum.dataPoints.append(protoDataPoint)
77-
case .doubleSummary:
78-
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]
86-
87-
protoDataPoint.startTimeUnixNano = summaryData.startTimestamp.timeIntervalSince1970.toNanoseconds
88-
protoDataPoint.timeUnixNano = summaryData.timestamp.timeIntervalSince1970.toNanoseconds
89-
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-
}
96-
97-
protoMetric.doubleHistogram.dataPoints.append(protoDataPoint)
98-
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-
}
111-
112-
protoMetric.intSum.dataPoints.append(protoDataPoint)
113-
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
124-
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-
}
131-
132-
protoMetric.intHistogram.dataPoints.append(protoDataPoint)
63+
case .doubleSum:
64+
guard let sumData = $0 as? SumData<Double> else {
65+
break
66+
}
67+
68+
var protoDataPoint = Opentelemetry_Proto_Metrics_V1_DoubleDataPoint()
69+
protoDataPoint.value = sumData.sum
70+
sumData.labels.forEach {
71+
var kvp = Opentelemetry_Proto_Common_V1_StringKeyValue()
72+
kvp.key = $0.key
73+
kvp.value = $0.value
74+
protoDataPoint.labels.append(kvp)
75+
}
76+
77+
protoMetric.doubleSum.dataPoints.append(protoDataPoint)
78+
case .doubleSummary:
79+
80+
guard let summaryData = $0 as? SummaryData<Double> else {
81+
break
82+
}
83+
var protoDataPoint = Opentelemetry_Proto_Metrics_V1_DoubleSummaryDataPoint()
84+
protoDataPoint.sum = summaryData.sum
85+
protoDataPoint.count = UInt64(summaryData.count)
86+
87+
protoDataPoint.startTimeUnixNano = summaryData.startTimestamp.timeIntervalSince1970.toNanoseconds
88+
protoDataPoint.timeUnixNano = summaryData.timestamp.timeIntervalSince1970.toNanoseconds
89+
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+
}
96+
97+
protoMetric.doubleSummary.dataPoints.append(protoDataPoint)
98+
case .intSum:
99+
guard let sumData = $0 as? SumData<Int> else {
100+
break
101+
}
102+
var protoDataPoint = Opentelemetry_Proto_Metrics_V1_IntDataPoint()
103+
protoDataPoint.value = Int64(sumData.sum)
104+
sumData.labels.forEach {
105+
var kvp = Opentelemetry_Proto_Common_V1_StringKeyValue()
106+
kvp.key = $0.key
107+
kvp.value = $0.value
108+
protoDataPoint.labels.append(kvp)
109+
}
110+
111+
protoMetric.intSum.dataPoints.append(protoDataPoint)
112+
case .intSummary:
113+
guard let summaryData = $0 as? SummaryData<Int> else {
114+
break
115+
}
116+
var protoDataPoint = Opentelemetry_Proto_Metrics_V1_DoubleSummaryDataPoint()
117+
protoDataPoint.sum = Double(summaryData.sum)
118+
protoDataPoint.count = UInt64(summaryData.count)
119+
protoDataPoint.startTimeUnixNano = summaryData.startTimestamp.timeIntervalSince1970.toNanoseconds
120+
protoDataPoint.timeUnixNano = summaryData.timestamp.timeIntervalSince1970.toNanoseconds
121+
122+
summaryData.labels.forEach {
123+
var kvp = Opentelemetry_Proto_Common_V1_StringKeyValue()
124+
kvp.key = $0.key
125+
kvp.value = $0.value
126+
protoDataPoint.labels.append(kvp)
127+
}
128+
129+
protoMetric.doubleSummary.dataPoints.append(protoDataPoint)
133130
}
134131
}
135132
return protoMetric

0 commit comments

Comments
 (0)