Skip to content

Commit 2356f1f

Browse files
author
Ignacio Bonafonte
authored
Merge pull request #113 from nachoBonafonte/Update-to-Opentelemetry-spec-1-0-1
Update to opentelemetry spec 1.0.1, including: * Swiftify Status struct, converting it to an enum and associated description only for error status * Renamed TraceConfig to SpanLimits, and moved Sampler out to TracerSharedState * Renamed Probability sampler to TraceIdRatioBased * Allow usage of ParentBased sampler (it was not possible to create it before from a user perspective) * Update OTLP protocol to 0.7.0 * Update grpc-swift to 1.0.0 and generate swift-proto files with this version ( Fixes #96) * Rename IdsGenerator protocol to IdGenerator
2 parents 3161c2b + 5ff604e commit 2356f1f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+1905
-1805
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/DatadogExporter/Spans/SpanEncoder.swift

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import Foundation
1717
import OpenTelemetryApi
1818
import OpenTelemetrySdk
1919

20-
internal struct Constants {
20+
internal enum Constants {
2121
static let ddsource = "ios"
2222
}
2323

@@ -91,16 +91,17 @@ internal struct DDSpan: Encodable {
9191
self.startTime = spanData.startTime.timeIntervalSince1970.toNanoseconds
9292
self.duration = spanData.endTime.timeIntervalSince(spanData.startTime).toNanoseconds
9393

94-
if spanData.status.isError {
95-
self.error = true
96-
self.errorType = spanData.attributes["error.type"]?.description ?? spanData.status.description
97-
self.errorMessage = spanData.attributes["error.message"]?.description
98-
self.errorStack = spanData.attributes["error.stack"]?.description
99-
} else {
100-
self.error = false
101-
self.errorMessage = nil
102-
self.errorType = nil
103-
self.errorStack = nil
94+
switch spanData.status {
95+
case .error(let errorDescription):
96+
self.error = true
97+
self.errorType = spanData.attributes["error.type"]?.description ?? errorDescription
98+
self.errorMessage = spanData.attributes["error.message"]?.description
99+
self.errorStack = spanData.attributes["error.stack"]?.description
100+
default:
101+
self.error = false
102+
self.errorMessage = nil
103+
self.errorType = nil
104+
self.errorStack = nil
104105
}
105106

106107
let spanType = spanData.attributes["type"] ?? spanData.attributes["db.type"]

Sources/Exporters/Jaeger/Adapter.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ final class Adapter {
6060
logs.append(contentsOf: toJaegerLogs(events: span.events))
6161
references.append(contentsOf: toSpanRefs(links: span.links))
6262

63-
if let parentId = span.parentSpanId,
64-
parentId.isValid {
63+
if let parentId = span.parentSpanId, parentId.isValid {
6564
let parentTraceIdHigh = traceIdHigh
6665
let parentTraceIdLow = traceIdLow
6766

@@ -75,8 +74,12 @@ final class Adapter {
7574
}
7675

7776
tags.append(Tag(key: Adapter.keySpanKind, vType: .string, vStr: span.kind.rawValue.uppercased(), vDouble: nil, vBool: nil, vLong: nil, vBinary: nil))
78-
tags.append(Tag(key: Adapter.keySpanStatusMessage, vType: .string, vStr: span.status.statusDescription ?? "", vDouble: nil, vBool: nil, vLong: nil, vBinary: nil))
79-
tags.append(Tag(key: Adapter.keySpanStatusCode, vType: .long, vStr: nil, vDouble: nil, vBool: nil, vLong: Int64(span.status.statusCode.rawValue), vBinary: nil))
77+
if case let Status.error(description) = span.status {
78+
tags.append(Tag(key: Adapter.keySpanStatusMessage, vType: .string, vStr: description, vDouble: nil, vBool: nil, vLong: nil, vBinary: nil))
79+
} else {
80+
tags.append(Tag(key: Adapter.keySpanStatusMessage, vType: .string, vStr: "", vDouble: nil, vBool: nil, vLong: nil, vBinary: nil))
81+
}
82+
tags.append(Tag(key: Adapter.keySpanStatusCode, vType: .long, vStr: nil, vDouble: nil, vBool: nil, vLong: Int64(span.status.code), vBinary: nil))
8083

8184
if span.status != .ok {
8285
tags.append(Tag(key: keyError, vType: .bool, vStr: nil, vDouble: nil, vBool: true, vLong: nil, vBinary: nil))

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
}

0 commit comments

Comments
 (0)