Skip to content

Commit

Permalink
Merge pull request #113 from nachoBonafonte/Update-to-Opentelemetry-s…
Browse files Browse the repository at this point in the history
…pec-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
  • Loading branch information
Ignacio Bonafonte authored Mar 9, 2021
2 parents 3161c2b + 5ff604e commit 2356f1f
Show file tree
Hide file tree
Showing 52 changed files with 1,905 additions and 1,805 deletions.
37 changes: 23 additions & 14 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"repositoryURL": "https://github.com/grpc/grpc-swift.git",
"state": {
"branch": null,
"revision": "b83ee1ee2caa0660eb02444977b9b6e353c2adbf",
"version": "1.0.0-alpha.12"
"revision": "9e464a75079928366aa7041769a271fac89271bf",
"version": "1.0.0"
}
},
{
Expand All @@ -33,53 +33,62 @@
"repositoryURL": "https://github.com/apple/swift-log.git",
"state": {
"branch": null,
"revision": "74d7b91ceebc85daf387ebb206003f78813f71aa",
"version": "1.2.0"
"revision": "12d3a8651d32295794a850307f77407f95b8c881",
"version": "1.4.1"
}
},
{
"package": "swift-nio",
"repositoryURL": "https://github.com/apple/swift-nio.git",
"state": {
"branch": null,
"revision": "120acb15c39aa3217e9888e515de160378fbcc1e",
"version": "2.18.0"
"revision": "6d3ca7e54e06a69d0f2612c2ce8bb8b7319085a4",
"version": "2.26.0"
}
},
{
"package": "swift-nio-extras",
"repositoryURL": "https://github.com/apple/swift-nio-extras.git",
"state": {
"branch": null,
"revision": "de1c80ad1fdff1ba772bcef6b392c3ef735f39a6",
"version": "1.8.0"
}
},
{
"package": "swift-nio-http2",
"repositoryURL": "https://github.com/apple/swift-nio-http2.git",
"state": {
"branch": null,
"revision": "c5d10f4165128c3d0cc0e3c0f0a8ef55947a73a6",
"version": "1.12.2"
"revision": "f4736a3b78a2bbe3feb7fc0f33f6683a8c27974c",
"version": "1.16.3"
}
},
{
"package": "swift-nio-ssl",
"repositoryURL": "https://github.com/apple/swift-nio-ssl.git",
"state": {
"branch": null,
"revision": "f0b118d9af6c4e78bc4f3f4fbb464020172b0bf4",
"version": "2.7.5"
"revision": "bbb38fbcbbe9dc4665b2c638dfa5681b01079bfb",
"version": "2.10.4"
}
},
{
"package": "swift-nio-transport-services",
"repositoryURL": "https://github.com/apple/swift-nio-transport-services.git",
"state": {
"branch": null,
"revision": "2ac8fde712c1b1a147ecb7065824a40d2c09d0cb",
"version": "1.6.0"
"revision": "1d28d48e071727f4558a8a4bb1894472abc47a58",
"version": "1.9.2"
}
},
{
"package": "SwiftProtobuf",
"repositoryURL": "https://github.com/apple/swift-protobuf.git",
"state": {
"branch": null,
"revision": "7f36441e3372665b1b414f8ac93b5905cc42a405",
"version": "1.9.0"
"revision": "e1904bf5a5f79cb7e0ff68a427a53a93b652fcd1",
"version": "1.15.0"
}
},
{
Expand Down
3 changes: 1 addition & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ let package = Package(
.package(name: "Opentracing", url: "https://github.com/undefinedlabs/opentracing-objc", from: "0.5.2"),
.package(name: "Thrift", url: "https://github.com/undefinedlabs/Thrift-Swift", from: "1.1.1"),
.package(name: "swift-nio", url: "https://github.com/apple/swift-nio.git", from: "2.0.0"),
.package(name: "grpc-swift", url: "https://github.com/grpc/grpc-swift.git", .exact("1.0.0-alpha.12")),
.package(name: "SwiftProtobuf", url: "https://github.com/apple/swift-protobuf.git", from: "1.6.0"),
.package(name: "grpc-swift", url: "https://github.com/grpc/grpc-swift.git", from: "1.0.0"),
.package(name: "swift-atomics", url: "https://github.com/apple/swift-atomics.git", from: "0.0.1")
],
targets: [
Expand Down
23 changes: 12 additions & 11 deletions Sources/Exporters/DatadogExporter/Spans/SpanEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import Foundation
import OpenTelemetryApi
import OpenTelemetrySdk

internal struct Constants {
internal enum Constants {
static let ddsource = "ios"
}

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

if spanData.status.isError {
self.error = true
self.errorType = spanData.attributes["error.type"]?.description ?? spanData.status.description
self.errorMessage = spanData.attributes["error.message"]?.description
self.errorStack = spanData.attributes["error.stack"]?.description
} else {
self.error = false
self.errorMessage = nil
self.errorType = nil
self.errorStack = nil
switch spanData.status {
case .error(let errorDescription):
self.error = true
self.errorType = spanData.attributes["error.type"]?.description ?? errorDescription
self.errorMessage = spanData.attributes["error.message"]?.description
self.errorStack = spanData.attributes["error.stack"]?.description
default:
self.error = false
self.errorMessage = nil
self.errorType = nil
self.errorStack = nil
}

let spanType = spanData.attributes["type"] ?? spanData.attributes["db.type"]
Expand Down
11 changes: 7 additions & 4 deletions Sources/Exporters/Jaeger/Adapter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ final class Adapter {
logs.append(contentsOf: toJaegerLogs(events: span.events))
references.append(contentsOf: toSpanRefs(links: span.links))

if let parentId = span.parentSpanId,
parentId.isValid {
if let parentId = span.parentSpanId, parentId.isValid {
let parentTraceIdHigh = traceIdHigh
let parentTraceIdLow = traceIdLow

Expand All @@ -75,8 +74,12 @@ final class Adapter {
}

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

if span.status != .ok {
tags.append(Tag(key: keyError, vType: .bool, vStr: nil, vDouble: nil, vBool: true, vLong: nil, vBinary: nil))
Expand Down
142 changes: 65 additions & 77 deletions Sources/Exporters/OpenTelemetryProtocol/metric/MetricsAdapter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import Foundation
import OpenTelemetryApi
import OpenTelemetrySdk


struct MetricsAdapter {
static func toProtoResourceMetrics(metricDataList: [Metric]) -> [Opentelemetry_Proto_Metrics_V1_ResourceMetrics] {
let resourceAndLibraryMap = groupByResouceAndLibrary(metricDataList: metricDataList)
Expand All @@ -26,7 +25,7 @@ struct MetricsAdapter {
var instrumentationLibraryMetrics = [Opentelemetry_Proto_Metrics_V1_InstrumentationLibraryMetrics]()
resMap.value.forEach { instLibrary in
var protoInst =
Opentelemetry_Proto_Metrics_V1_InstrumentationLibraryMetrics()
Opentelemetry_Proto_Metrics_V1_InstrumentationLibraryMetrics()
protoInst.instrumentationLibrary =
CommonAdapter.toProtoInstrumentationLibrary(instrumentationLibraryInfo: instLibrary.key)
instLibrary.value.forEach {
Expand All @@ -38,112 +37,101 @@ struct MetricsAdapter {
resourceMetric.resource = ResourceAdapter.toProtoResource(resource: resMap.key)
resourceMetric.instrumentationLibraryMetrics.append(contentsOf: instrumentationLibraryMetrics)
resourceMetrics.append(resourceMetric)

}



return resourceMetrics
}

private static func groupByResouceAndLibrary(metricDataList: [Metric]) -> [Resource :[InstrumentationLibraryInfo : [Opentelemetry_Proto_Metrics_V1_Metric]]] {
var results = [Resource : [InstrumentationLibraryInfo : [Opentelemetry_Proto_Metrics_V1_Metric]]]()
private static func groupByResouceAndLibrary(metricDataList: [Metric]) -> [Resource: [InstrumentationLibraryInfo: [Opentelemetry_Proto_Metrics_V1_Metric]]] {
var results = [Resource: [InstrumentationLibraryInfo: [Opentelemetry_Proto_Metrics_V1_Metric]]]()

metricDataList.forEach {
results[$0.resource, default:[InstrumentationLibraryInfo : [Opentelemetry_Proto_Metrics_V1_Metric]]()][$0.instrumentationLibraryInfo,default:[Opentelemetry_Proto_Metrics_V1_Metric]()]
results[$0.resource, default: [InstrumentationLibraryInfo: [Opentelemetry_Proto_Metrics_V1_Metric]]()][$0.instrumentationLibraryInfo, default: [Opentelemetry_Proto_Metrics_V1_Metric]()]
.append(toProtoMetric(metric: $0))
}

return results
}

static func toProtoMetric(metric: Metric) -> Opentelemetry_Proto_Metrics_V1_Metric {

var protoMetric = Opentelemetry_Proto_Metrics_V1_Metric()
protoMetric.name = metric.name
protoMetric.description_p = metric.description


metric.data.forEach {
switch metric.aggregationType {
case .doubleSum:
guard let sumData = $0 as? SumData<Double> else {
break
}
var protoDataPoint = Opentelemetry_Proto_Metrics_V1_DoubleDataPoint()
protoDataPoint.value = sumData.sum
sumData.labels.forEach {
var kvp = Opentelemetry_Proto_Common_V1_StringKeyValue()
kvp.key = $0.key
kvp.value = $0.value
protoDataPoint.labels.append(kvp)
}
switch metric.aggregationType {
case .doubleSum:
guard let sumData = $0 as? SumData<Double> else {
break
}
var protoDataPoint = Opentelemetry_Proto_Metrics_V1_DoubleDataPoint()
protoDataPoint.value = sumData.sum
sumData.labels.forEach {
var kvp = Opentelemetry_Proto_Common_V1_StringKeyValue()
kvp.key = $0.key
kvp.value = $0.value
protoDataPoint.labels.append(kvp)
}

protoMetric.doubleSum.dataPoints.append(protoDataPoint)
break
case .doubleSummary:
protoMetric.doubleSum.dataPoints.append(protoDataPoint)
case .doubleSummary:

guard let summaryData = $0 as? SummaryData<Double> else {
break
}
var protoDataPoint = Opentelemetry_Proto_Metrics_V1_DoubleHistogramDataPoint()
protoDataPoint.sum = summaryData.sum
protoDataPoint.count = UInt64(summaryData.count)
protoDataPoint.explicitBounds = [summaryData.min, summaryData.max]
guard let summaryData = $0 as? SummaryData<Double> else {
break
}
var protoDataPoint = Opentelemetry_Proto_Metrics_V1_DoubleHistogramDataPoint()
protoDataPoint.sum = summaryData.sum
protoDataPoint.count = UInt64(summaryData.count)
protoDataPoint.explicitBounds = [summaryData.min, summaryData.max]

protoDataPoint.startTimeUnixNano = summaryData.startTimestamp.timeIntervalSince1970.toNanoseconds
protoDataPoint.timeUnixNano = summaryData.timestamp.timeIntervalSince1970.toNanoseconds
protoDataPoint.startTimeUnixNano = summaryData.startTimestamp.timeIntervalSince1970.toNanoseconds
protoDataPoint.timeUnixNano = summaryData.timestamp.timeIntervalSince1970.toNanoseconds

summaryData.labels.forEach {
var kvp = Opentelemetry_Proto_Common_V1_StringKeyValue()
kvp.key = $0.key
kvp.value = $0.value
protoDataPoint.labels.append(kvp)
}
summaryData.labels.forEach {
var kvp = Opentelemetry_Proto_Common_V1_StringKeyValue()
kvp.key = $0.key
kvp.value = $0.value
protoDataPoint.labels.append(kvp)
}

protoMetric.doubleHistogram.dataPoints.append(protoDataPoint)
protoMetric.doubleHistogram.dataPoints.append(protoDataPoint)

break
case .intSum:
guard let sumData = $0 as? SumData<Int> else {
break;
}
var protoDataPoint = Opentelemetry_Proto_Metrics_V1_IntDataPoint()
protoDataPoint.value = Int64(sumData.sum)
sumData.labels.forEach {
var kvp = Opentelemetry_Proto_Common_V1_StringKeyValue()
kvp.key = $0.key
kvp.value = $0.value
protoDataPoint.labels.append(kvp)
}
case .intSum:
guard let sumData = $0 as? SumData<Int> else {
break
}
var protoDataPoint = Opentelemetry_Proto_Metrics_V1_IntDataPoint()
protoDataPoint.value = Int64(sumData.sum)
sumData.labels.forEach {
var kvp = Opentelemetry_Proto_Common_V1_StringKeyValue()
kvp.key = $0.key
kvp.value = $0.value
protoDataPoint.labels.append(kvp)
}

protoMetric.intSum.dataPoints.append(protoDataPoint)
protoMetric.intSum.dataPoints.append(protoDataPoint)

break
case .intSummary:
guard let summaryData = $0 as? SummaryData<Int> else {
break
}
var protoDataPoint = Opentelemetry_Proto_Metrics_V1_IntHistogramDataPoint()
protoDataPoint.sum = Int64(summaryData.sum)
protoDataPoint.count = UInt64(summaryData.count)
protoDataPoint.bucketCounts = [UInt64(summaryData.min), UInt64(summaryData.max)]
protoDataPoint.startTimeUnixNano = summaryData.startTimestamp.timeIntervalSince1970.toNanoseconds
protoDataPoint.timeUnixNano = summaryData.timestamp.timeIntervalSince1970.toNanoseconds
case .intSummary:
guard let summaryData = $0 as? SummaryData<Int> else {
break
}
var protoDataPoint = Opentelemetry_Proto_Metrics_V1_IntHistogramDataPoint()
protoDataPoint.sum = Int64(summaryData.sum)
protoDataPoint.count = UInt64(summaryData.count)
protoDataPoint.bucketCounts = [UInt64(summaryData.min), UInt64(summaryData.max)]
protoDataPoint.startTimeUnixNano = summaryData.startTimestamp.timeIntervalSince1970.toNanoseconds
protoDataPoint.timeUnixNano = summaryData.timestamp.timeIntervalSince1970.toNanoseconds

summaryData.labels.forEach {
var kvp = Opentelemetry_Proto_Common_V1_StringKeyValue()
kvp.key = $0.key
kvp.value = $0.value
protoDataPoint.labels.append(kvp)
}

protoMetric.intHistogram.dataPoints.append(protoDataPoint)
summaryData.labels.forEach {
var kvp = Opentelemetry_Proto_Common_V1_StringKeyValue()
kvp.key = $0.key
kvp.value = $0.value
protoDataPoint.labels.append(kvp)
}

break
protoMetric.intHistogram.dataPoints.append(protoDataPoint)
}
}
return protoMetric
}

}
Loading

0 comments on commit 2356f1f

Please sign in to comment.