Skip to content

Commit 8e9421c

Browse files
author
Ignacio Bonafonte
committed
Update grpc-swift to version 1.0.0 and update generated protobuf files with this version
1 parent 0dcf007 commit 8e9421c

19 files changed

+1174
-1284
lines changed

Package.resolved

Lines changed: 23 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ let package = Package(
3737
.package(name: "Opentracing", url: "https://github.com/undefinedlabs/opentracing-objc", from: "0.5.2"),
3838
.package(name: "Thrift", url: "https://github.com/undefinedlabs/Thrift-Swift", from: "1.1.1"),
3939
.package(name: "swift-nio", url: "https://github.com/apple/swift-nio.git", from: "2.0.0"),
40-
.package(name: "grpc-swift", url: "https://github.com/grpc/grpc-swift.git", .exact("1.0.0-alpha.12")),
41-
.package(name: "SwiftProtobuf", url: "https://github.com/apple/swift-protobuf.git", from: "1.6.0"),
40+
.package(name: "grpc-swift", url: "https://github.com/grpc/grpc-swift.git", from: "1.0.0"),
4241
.package(name: "swift-atomics", url: "https://github.com/apple/swift-atomics.git", from: "0.0.1")
4342
],
4443
targets: [

Sources/Exporters/OpenTelemetryProtocol/metric/MetricsAdapter.swift

Lines changed: 65 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import Foundation
1616
import OpenTelemetryApi
1717
import OpenTelemetrySdk
1818

19-
2019
struct 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
}

Sources/Exporters/OpenTelemetryProtocol/metric/OtelpMetricExporter.swift

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,33 @@
1515

1616
import Foundation
1717
import GRPC
18+
import NIO
1819
import OpenTelemetryApi
1920
import OpenTelemetrySdk
2021

21-
public class OtelpMetricExporter : MetricExporter {
22-
23-
let channel : GRPCChannel
22+
public class OtelpMetricExporter: MetricExporter {
23+
let channel: GRPCChannel
2424
let metricClient: Opentelemetry_Proto_Collector_Metrics_V1_MetricsServiceClient
25-
let deadlineMS : Int
26-
27-
public init(channel: GRPCChannel, timeoutMS: Int = 0) {
25+
let timeoutNanos: Int64
26+
27+
public init(channel: GRPCChannel, timeoutNanos: Int64 = 0) {
2828
self.channel = channel
29-
self.deadlineMS = timeoutMS
29+
self.timeoutNanos = timeoutNanos
3030
self.metricClient = Opentelemetry_Proto_Collector_Metrics_V1_MetricsServiceClient(channel: self.channel)
3131
}
3232

33-
3433
public func export(metrics: [Metric], shouldCancel: (() -> Bool)?) -> MetricExporterResultCode {
3534
let exportRequest = Opentelemetry_Proto_Collector_Metrics_V1_ExportMetricsServiceRequest
3635
.with {
3736
$0.resourceMetrics = MetricsAdapter.toProtoResourceMetrics(metricDataList: metrics)
3837
}
3938

40-
if deadlineMS > 0 {
41-
metricClient.defaultCallOptions.timeout = try! GRPCTimeout.milliseconds(deadlineMS)
39+
if timeoutNanos > 0 {
40+
metricClient.defaultCallOptions.timeLimit = TimeLimit.timeout(TimeAmount.nanoseconds(timeoutNanos))
4241
}
4342

4443
let export = metricClient.export(exportRequest)
4544

46-
4745
do {
4846
_ = try export.response.wait()
4947
return .success
@@ -57,8 +55,6 @@ public class OtelpMetricExporter : MetricExporter {
5755
}
5856

5957
public func shutdown() {
60-
_ = channel.close()
58+
_ = channel.close()
6159
}
62-
63-
6460
}

0 commit comments

Comments
 (0)