From 12bcdd5a32f7f8505ba9004e74cd9e985dc8372f Mon Sep 17 00:00:00 2001 From: pokryfka Date: Mon, 27 Sep 2021 23:36:52 +0800 Subject: [PATCH] chore: update NIO and InstrumentationBaggage (breaking), formatting --- Package.swift | 6 +- Sources/AWSXRayRecorder/AWS.swift | 4 +- Sources/AWSXRayRecorder/Baggage.swift | 12 +- Sources/AWSXRayRecorder/Config.swift | 4 +- Sources/AWSXRayRecorder/Exception.swift | 6 +- Sources/AWSXRayRecorder/HTTP.swift | 4 +- Sources/AWSXRayRecorder/NoOpRecorder.swift | 7 +- Sources/AWSXRayRecorder/NoOpSegment.swift | 8 +- .../AWSXRayRecorder/Recorder+Helpers.swift | 130 +++++++++--------- Sources/AWSXRayRecorder/Recorder+NIO.swift | 28 ++-- Sources/AWSXRayRecorder/Recorder.swift | 8 +- Sources/AWSXRayRecorder/Segment+NIOHTTP.swift | 10 +- Sources/AWSXRayRecorder/Segment.swift | 14 +- Sources/AWSXRayRecorder/Timestamp.swift | 4 +- Sources/AWSXRayRecorder/Trace.swift | 12 +- Sources/AWSXRaySDK/DefaultEncoding.swift | 4 +- Sources/AWSXRaySDK/DefaultRecorder.swift | 6 +- Sources/AWSXRayUDPEmitter/Config.swift | 4 +- Sources/AWSXRayUDPEmitter/Locks.swift | 12 +- Tests/AWSXRayRecorderTests/ContextTests.swift | 8 +- Tests/AWSXRayRecorderTests/HTTPTests.swift | 10 +- .../NoOpRecorderTests.swift | 18 +-- .../NoOpSegmentTests.swift | 4 +- .../SegmentExceptionTests.swift | 8 +- .../SegmentLoggingTests.swift | 4 +- Tests/AWSXRayRecorderTests/SegmentTests.swift | 2 +- 26 files changed, 169 insertions(+), 168 deletions(-) diff --git a/Package.swift b/Package.swift index ead8aa4..fd73856 100644 --- a/Package.swift +++ b/Package.swift @@ -15,9 +15,9 @@ let package = Package( .library(name: "AWSXRayTesting", targets: ["AWSXRayTesting"]), ], dependencies: [ - .package(url: "https://github.com/apple/swift-nio.git", .upToNextMajor(from: "2.17.0")), + .package(url: "https://github.com/apple/swift-nio.git", .upToNextMajor(from: "2.33.0")), .package(url: "https://github.com/apple/swift-log.git", .upToNextMajor(from: "1.0.0")), - .package(name: "swift-baggage-context", url: "https://github.com/slashmo/gsoc-swift-baggage-context.git", .upToNextMinor(from: "0.2.0")), + .package(url: "https://github.com/apple/swift-distributed-tracing-baggage", .upToNextMinor(from: "0.2.1")), .package(url: "https://github.com/fabianfett/pure-swift-json.git", .upToNextMinor(from: "0.4.0")), ], targets: [ @@ -39,7 +39,7 @@ let package = Package( .product(name: "NIO", package: "swift-nio"), .product(name: "NIOHTTP1", package: "swift-nio"), .product(name: "Logging", package: "swift-log"), - .product(name: "Baggage", package: "swift-baggage-context"), + .product(name: "InstrumentationBaggage", package: "swift-distributed-tracing-baggage"), ] ), .testTarget( diff --git a/Sources/AWSXRayRecorder/AWS.swift b/Sources/AWSXRayRecorder/AWS.swift index 62edec2..184fb3c 100644 --- a/Sources/AWSXRayRecorder/AWS.swift +++ b/Sources/AWSXRayRecorder/AWS.swift @@ -18,7 +18,7 @@ extension XRayRecorder.Segment { /// For example, a Multicontainer Docker Elastic Beanstalk environment runs your application on an Amazon ECS container, /// which in turn runs on an Amazon EC2 instance. /// In this case you would set the origin to `AWS::ElasticBeanstalk::Environment` as the environment is the parent of the other two resources. - internal enum Origin: String, Encodable { + enum Origin: String, Encodable { /// An Amazon EC2 instance. case ec2Instance = "AWS::EC2::Instance" /// An Amazon ECS container. @@ -34,7 +34,7 @@ extension XRayRecorder.Segment { /// /// # References /// - [AWS X-Ray segment documents - AWS resource data](https://docs.aws.amazon.com/xray/latest/devguide/xray-api-segmentdocuments.html#api-segmentdocuments-aws) - internal struct AWS: Encodable { + struct AWS: Encodable { #if false // not used at the moment, see https://github.com/pokryfka/aws-xray-sdk-swift/issues/26 /// If your application sends segments to a different AWS account, record the ID of the account running your application. var accountId: String? diff --git a/Sources/AWSXRayRecorder/Baggage.swift b/Sources/AWSXRayRecorder/Baggage.swift index f848789..97c4a1b 100644 --- a/Sources/AWSXRayRecorder/Baggage.swift +++ b/Sources/AWSXRayRecorder/Baggage.swift @@ -11,16 +11,16 @@ // //===----------------------------------------------------------------------===// -import Baggage +import InstrumentationBaggage -private enum XRayContextKey: BaggageContextKey { +private enum XRayContextKey: BaggageKey { typealias Value = XRayRecorder.TraceContext var name: String { "XRayTraceContext" } } -extension BaggageContext { +public extension Baggage { /// Contains `XRayContext`. - public var xRayContext: XRayContext? { + var xRayContext: XRayContext? { get { self[XRayContextKey.self] } @@ -30,8 +30,8 @@ extension BaggageContext { } } -internal extension BaggageContext { - func withParent(_ parentId: XRayRecorder.Segment.ID) throws -> BaggageContext { +internal extension Baggage { + func withParent(_ parentId: XRayRecorder.Segment.ID) throws -> Baggage { guard var context = xRayContext else { throw XRayRecorder.TraceError.missingContext } context.parentId = parentId var updated = self diff --git a/Sources/AWSXRayRecorder/Config.swift b/Sources/AWSXRayRecorder/Config.swift index dd50e02..349f8ff 100644 --- a/Sources/AWSXRayRecorder/Config.swift +++ b/Sources/AWSXRayRecorder/Config.swift @@ -13,9 +13,9 @@ import Logging -extension XRayRecorder { +public extension XRayRecorder { /// `XRayRecorder` configuration. - public struct Config { + struct Config { /// Context missing strategy. public enum ContextMissingStrategy: String { /// Indicate that a precondition was violated. diff --git a/Sources/AWSXRayRecorder/Exception.swift b/Sources/AWSXRayRecorder/Exception.swift index aa4227e..de1ad4c 100644 --- a/Sources/AWSXRayRecorder/Exception.swift +++ b/Sources/AWSXRayRecorder/Exception.swift @@ -12,7 +12,7 @@ //===----------------------------------------------------------------------===// extension XRayRecorder.Segment { - internal struct Cause { + struct Cause { #if false // not used /// The full path of the working directory when the exception occurred. var workingDirectory: String? @@ -20,10 +20,10 @@ extension XRayRecorder.Segment { var paths: [String]? #endif /// The **array** of **exception** objects. - var exceptions: [Exception] = [Exception]() + var exceptions = [Exception]() } - internal struct Exception { + struct Exception { /// A 64-bit identifier in **16 hexadecimal digits**. struct ID: RawRepresentable, Hashable, Encodable, CustomStringConvertible { let rawValue: String diff --git a/Sources/AWSXRayRecorder/HTTP.swift b/Sources/AWSXRayRecorder/HTTP.swift index 12c7796..b64ef0c 100644 --- a/Sources/AWSXRayRecorder/HTTP.swift +++ b/Sources/AWSXRayRecorder/HTTP.swift @@ -12,7 +12,7 @@ //===----------------------------------------------------------------------===// extension XRayRecorder.Segment { - internal enum Namespace: String, Encodable { + enum Namespace: String, Encodable { /// AWS SDK calls case aws /// other downstream calls @@ -28,7 +28,7 @@ extension XRayRecorder.Segment { /// /// # References /// - [AWS X-Ray segment documents - HTTP request data](https://docs.aws.amazon.com/xray/latest/devguide/xray-api-segmentdocuments.html#api-segmentdocuments-http) - internal struct HTTP: Encodable { + struct HTTP: Encodable { /// Information about a request. struct Request: Encodable { /// The request method. For example, `GET`. diff --git a/Sources/AWSXRayRecorder/NoOpRecorder.swift b/Sources/AWSXRayRecorder/NoOpRecorder.swift index e9fb4c5..549241c 100644 --- a/Sources/AWSXRayRecorder/NoOpRecorder.swift +++ b/Sources/AWSXRayRecorder/NoOpRecorder.swift @@ -11,7 +11,7 @@ // //===----------------------------------------------------------------------===// -import Baggage +import InstrumentationBaggage import Logging // TODO: just like with NoOpSegment we could reduce allocations by having a base class @@ -28,12 +28,13 @@ public class XRayNoOpRecorder: XRayRecorder { override public func beginSegment(name: String, context: TraceContext, startTime: XRayRecorder.Timestamp = .now(), metadata: XRayRecorder.Segment.Metadata? = nil) -> XRayRecorder.Segment { - var baggage = BaggageContext() + // TODO: TODO or topLevel? + var baggage = Baggage.TODO("NoOp") baggage.xRayContext = context return NoOpSegment(id: .init(), name: name, baggage: baggage) } - override public func beginSegment(name: String, baggage: BaggageContext, startTime: XRayRecorder.Timestamp = .now(), + override public func beginSegment(name: String, baggage: Baggage, startTime: XRayRecorder.Timestamp = .now(), metadata: XRayRecorder.Segment.Metadata? = nil) -> XRayRecorder.Segment { NoOpSegment(id: .init(), name: name, baggage: baggage) diff --git a/Sources/AWSXRayRecorder/NoOpSegment.swift b/Sources/AWSXRayRecorder/NoOpSegment.swift index 27ff2c7..b3c7cd3 100644 --- a/Sources/AWSXRayRecorder/NoOpSegment.swift +++ b/Sources/AWSXRayRecorder/NoOpSegment.swift @@ -11,20 +11,20 @@ // //===----------------------------------------------------------------------===// -import Baggage +import InstrumentationBaggage import Logging // TODO: reduce allocation by by making Segment an abstract class extended by NoOpSegment and DoOpSegment ? extension XRayRecorder { - internal class NoOpSegment: Segment { + class NoOpSegment: Segment { override public var isSampled: Bool { false } override private init( id: ID, name: String, context: TraceContext, - baggage: BaggageContext, + baggage: Baggage, startTime: XRayRecorder.Timestamp = Timestamp(), subsegment: Bool = false, service: Service? = nil, user: String? = nil, @@ -36,7 +36,7 @@ extension XRayRecorder { fatalError() } - init(id: ID, name: String, baggage: BaggageContext, startTime: Timestamp = .now(), logger: Logger? = nil) { + init(id: ID, name: String, baggage: Baggage, startTime: Timestamp = .now(), logger: Logger? = nil) { // the context is not of much importance as the segment will not be emitted // however pass the baggage which may contain more than just the X-Ray trace let context = baggage.xRayContext ?? XRayRecorder.TraceContext() diff --git a/Sources/AWSXRayRecorder/Recorder+Helpers.swift b/Sources/AWSXRayRecorder/Recorder+Helpers.swift index 3ad8ee2..4a2c98c 100644 --- a/Sources/AWSXRayRecorder/Recorder+Helpers.swift +++ b/Sources/AWSXRayRecorder/Recorder+Helpers.swift @@ -11,9 +11,9 @@ // //===----------------------------------------------------------------------===// -import Baggage +import InstrumentationBaggage -extension XRayRecorder { +public extension XRayRecorder { /// Creates new segment. /// /// Records thrown `Error`. @@ -25,22 +25,22 @@ extension XRayRecorder { /// - metadata: segment metadata /// - body: segment body @inlinable - public func segment(name: String, context: TraceContext, startTime: XRayRecorder.Timestamp = .now(), - metadata: XRayRecorder.Segment.Metadata? = nil, - body: (XRayRecorder.Segment) throws -> T) + func segment(name: String, context: TraceContext, startTime: XRayRecorder.Timestamp = .now(), + metadata: XRayRecorder.Segment.Metadata? = nil, + body: (XRayRecorder.Segment) throws -> T) rethrows -> T { - let segment = beginSegment(name: name, context: context, startTime: startTime, metadata: metadata) - defer { - segment.end() - } - do { - return try body(segment) - } catch { - segment.addError(error) - throw error - } + let segment = beginSegment(name: name, context: context, startTime: startTime, metadata: metadata) + defer { + segment.end() + } + do { + return try body(segment) + } catch { + segment.addError(error) + throw error } + } /// Creates new segment. /// @@ -58,22 +58,22 @@ extension XRayRecorder { /// - metadata: segment metadata /// - body: segment body @inlinable - public func segment(name: String, baggage: BaggageContext, startTime: XRayRecorder.Timestamp = .now(), - metadata: XRayRecorder.Segment.Metadata? = nil, - body: (XRayRecorder.Segment) throws -> T) + func segment(name: String, baggage: Baggage, startTime: XRayRecorder.Timestamp = .now(), + metadata: XRayRecorder.Segment.Metadata? = nil, + body: (XRayRecorder.Segment) throws -> T) rethrows -> T { - let segment = beginSegment(name: name, baggage: baggage, startTime: startTime, metadata: metadata) - defer { - segment.end() - } - do { - return try body(segment) - } catch { - segment.addError(error) - throw error - } + let segment = beginSegment(name: name, baggage: baggage, startTime: startTime, metadata: metadata) + defer { + segment.end() + } + do { + return try body(segment) + } catch { + segment.addError(error) + throw error } + } /// Creates new segment. /// @@ -86,26 +86,26 @@ extension XRayRecorder { /// - metadata: segment metadata /// - body: segment body @inlinable - public func segment(name: String, context: TraceContext, startTime: XRayRecorder.Timestamp = .now(), - metadata: XRayRecorder.Segment.Metadata? = nil, - body: (XRayRecorder.Segment) throws -> Result) + func segment(name: String, context: TraceContext, startTime: XRayRecorder.Timestamp = .now(), + metadata: XRayRecorder.Segment.Metadata? = nil, + body: (XRayRecorder.Segment) throws -> Result) rethrows -> Result { - let segment = beginSegment(name: name, context: context, startTime: startTime, metadata: metadata) - defer { - segment.end() - } - do { - let result = try body(segment) - if case Result.failure(let error) = result { - segment.addError(error) - } - return result - } catch { + let segment = beginSegment(name: name, context: context, startTime: startTime, metadata: metadata) + defer { + segment.end() + } + do { + let result = try body(segment) + if case Result.failure(let error) = result { segment.addError(error) - throw error } + return result + } catch { + segment.addError(error) + throw error } + } /// Creates new segment. /// @@ -123,29 +123,29 @@ extension XRayRecorder { /// - metadata: segment metadata /// - body: segment body @inlinable - public func segment(name: String, baggage: BaggageContext, startTime: XRayRecorder.Timestamp = .now(), - metadata: XRayRecorder.Segment.Metadata? = nil, - body: (XRayRecorder.Segment) throws -> Result) + func segment(name: String, baggage: Baggage, startTime: XRayRecorder.Timestamp = .now(), + metadata: XRayRecorder.Segment.Metadata? = nil, + body: (XRayRecorder.Segment) throws -> Result) rethrows -> Result { - let segment = beginSegment(name: name, baggage: baggage, startTime: startTime, metadata: metadata) - defer { - segment.end() - } - do { - let result = try body(segment) - if case Result.failure(let error) = result { - segment.addError(error) - } - return result - } catch { + let segment = beginSegment(name: name, baggage: baggage, startTime: startTime, metadata: metadata) + defer { + segment.end() + } + do { + let result = try body(segment) + if case Result.failure(let error) = result { segment.addError(error) - throw error } + return result + } catch { + segment.addError(error) + throw error } + } } -extension XRayRecorder.Segment { +public extension XRayRecorder.Segment { /// Creates new subsegment. /// /// Records thrown `Error`. @@ -156,9 +156,9 @@ extension XRayRecorder.Segment { /// - metadata: segment metadata /// - body: subsegment body @inlinable - public func subsegment(name: String, startTime: XRayRecorder.Timestamp = .now(), - metadata: XRayRecorder.Segment.Metadata? = nil, - body: (XRayRecorder.Segment) throws -> T) rethrows -> T + func subsegment(name: String, startTime: XRayRecorder.Timestamp = .now(), + metadata: XRayRecorder.Segment.Metadata? = nil, + body: (XRayRecorder.Segment) throws -> T) rethrows -> T { let segment = beginSubsegment(name: name, startTime: startTime, metadata: metadata) defer { @@ -182,9 +182,9 @@ extension XRayRecorder.Segment { /// - metadata: segment metadata /// - body: subsegment body @inlinable - public func subsegment(name: String, startTime: XRayRecorder.Timestamp = .now(), - metadata: XRayRecorder.Segment.Metadata? = nil, - body: (XRayRecorder.Segment) throws -> Result) rethrows -> Result + func subsegment(name: String, startTime: XRayRecorder.Timestamp = .now(), + metadata: XRayRecorder.Segment.Metadata? = nil, + body: (XRayRecorder.Segment) throws -> Result) rethrows -> Result { let segment = beginSubsegment(name: name, startTime: startTime, metadata: metadata) defer { diff --git a/Sources/AWSXRayRecorder/Recorder+NIO.swift b/Sources/AWSXRayRecorder/Recorder+NIO.swift index ab0d54a..73c960b 100644 --- a/Sources/AWSXRayRecorder/Recorder+NIO.swift +++ b/Sources/AWSXRayRecorder/Recorder+NIO.swift @@ -11,14 +11,14 @@ // //===----------------------------------------------------------------------===// -import Baggage +import InstrumentationBaggage import NIO -extension XRayRecorder { +public extension XRayRecorder { /// Flushes the emitter in `SwiftNIO` future. /// /// - Parameter eventLoop: `EventLoop` used to "do the flushing". - public func flush(on eventLoop: EventLoop) -> EventLoopFuture { + func flush(on eventLoop: EventLoop) -> EventLoopFuture { waitEmitting() // wait for the emitter to send them if let nioEmitter = emitter as? XRayNIOEmitter { @@ -37,7 +37,7 @@ extension XRayRecorder { } } -extension XRayRecorder { +public extension XRayRecorder { /// Creates new segment. /// /// Records `Error`. @@ -49,9 +49,9 @@ extension XRayRecorder { /// - metadata: segment metadata /// - body: segment body @inlinable - public func segment(name: String, context: TraceContext, startTime: XRayRecorder.Timestamp = .now(), - metadata: XRayRecorder.Segment.Metadata? = nil, - body: () -> EventLoopFuture) -> EventLoopFuture + func segment(name: String, context: TraceContext, startTime: XRayRecorder.Timestamp = .now(), + metadata: XRayRecorder.Segment.Metadata? = nil, + body: () -> EventLoopFuture) -> EventLoopFuture { let segment = beginSegment(name: name, context: context, startTime: startTime, metadata: metadata) return body().always { result in @@ -73,9 +73,9 @@ extension XRayRecorder { /// - metadata: segment metadata /// - body: segment body @inlinable - public func segment(name: String, baggage: BaggageContext, startTime: XRayRecorder.Timestamp = .now(), - metadata: XRayRecorder.Segment.Metadata? = nil, - body: () -> EventLoopFuture) -> EventLoopFuture + func segment(name: String, baggage: Baggage, startTime: XRayRecorder.Timestamp = .now(), + metadata: XRayRecorder.Segment.Metadata? = nil, + body: () -> EventLoopFuture) -> EventLoopFuture { let segment = beginSegment(name: name, baggage: baggage, startTime: startTime, metadata: metadata) return body().always { result in @@ -87,13 +87,13 @@ extension XRayRecorder { } } -extension EventLoopFuture { +public extension EventLoopFuture { /// Ends segment when the `EventLoopFuture` is fulfilled, records `.failure`. /// /// - parameters: /// - segment: the segment to end when the `EventLoopFuture` is fulfilled. /// - returns: the current `EventLoopFuture` - public func endSegment(_ segment: XRayRecorder.Segment) -> EventLoopFuture { + func endSegment(_ segment: XRayRecorder.Segment) -> EventLoopFuture { whenComplete { result in if case Result.failure(let error) = result { segment.addError(error) @@ -104,14 +104,14 @@ extension EventLoopFuture { } } -extension EventLoopFuture where Value == Void { +public extension EventLoopFuture where Value == Void { /// Flushes the recorder. /// /// - Parameters: /// - recorder: the recorder to flush /// - recover: if false and the future is in error state the error is propagated after flushing. /// - Returns: the current `EventLoopFuture` - public func flush(_ recorder: XRayRecorder, recover: Bool = true) -> EventLoopFuture { + func flush(_ recorder: XRayRecorder, recover: Bool = true) -> EventLoopFuture { map { Result.success(()) } .recover { Result.failure($0) } .flatMap { result in diff --git a/Sources/AWSXRayRecorder/Recorder.swift b/Sources/AWSXRayRecorder/Recorder.swift index c5c3b87..bde13fe 100644 --- a/Sources/AWSXRayRecorder/Recorder.swift +++ b/Sources/AWSXRayRecorder/Recorder.swift @@ -11,8 +11,8 @@ // //===----------------------------------------------------------------------===// -import Baggage import Dispatch +import InstrumentationBaggage import Logging import NIOConcurrencyHelpers @@ -59,7 +59,7 @@ public class XRayRecorder { } } - internal func beginSegment(name: String, context: TraceContext, baggage: BaggageContext, + internal func beginSegment(name: String, context: TraceContext, baggage: Baggage, startTime: Timestamp = .now(), aws: Segment.AWS? = nil, metadata: Segment.Metadata? = nil) -> Segment { @@ -109,7 +109,7 @@ public class XRayRecorder { public func beginSegment(name: String, context: TraceContext, startTime: XRayRecorder.Timestamp = .now(), metadata: XRayRecorder.Segment.Metadata? = nil) -> XRayRecorder.Segment { - var baggage = BaggageContext() + var baggage = Baggage.topLevel // TODO: check baggage.xRayContext = context return beginSegment(name: name, context: context, baggage: baggage, startTime: startTime, metadata: metadata) } @@ -127,7 +127,7 @@ public class XRayRecorder { /// - startTime: start time, defaults to now /// - metadata: segment metadata /// - Returns: new segment - public func beginSegment(name: String, baggage: BaggageContext, startTime: XRayRecorder.Timestamp = .now(), + public func beginSegment(name: String, baggage: Baggage, startTime: XRayRecorder.Timestamp = .now(), metadata: XRayRecorder.Segment.Metadata? = nil) -> XRayRecorder.Segment { if let context = baggage.xRayContext { diff --git a/Sources/AWSXRayRecorder/Segment+NIOHTTP.swift b/Sources/AWSXRayRecorder/Segment+NIOHTTP.swift index 42c020d..44aeaec 100644 --- a/Sources/AWSXRayRecorder/Segment+NIOHTTP.swift +++ b/Sources/AWSXRayRecorder/Segment+NIOHTTP.swift @@ -13,7 +13,7 @@ @_exported import NIOHTTP1 -extension XRayRecorder.Segment { +public extension XRayRecorder.Segment { /// Records details about an HTTP request that your application served (in a segment) or /// that your application made to a downstream HTTP API (in a subsegment). /// @@ -25,7 +25,7 @@ extension XRayRecorder.Segment { /// - url: The full URL of the request, compiled from the protocol, hostname, and path of the request. /// - userAgent: The user agent string from the requester's client. /// - clientIP: The IP address of the requester. - public func setHTTPRequest(method: HTTPMethod, url: String, userAgent: String? = nil, clientIP: String? = nil) { + func setHTTPRequest(method: HTTPMethod, url: String, userAgent: String? = nil, clientIP: String? = nil) { setHTTPRequest(method: method.rawValue, url: url, userAgent: userAgent, clientIP: clientIP) } @@ -36,7 +36,7 @@ extension XRayRecorder.Segment { /// /// - Parameters: /// - request: HTTP request. - public func setHTTPRequest(_ request: HTTPRequestHead) { + func setHTTPRequest(_ request: HTTPRequestHead) { let userAgent = request.headers["User-Agent"].first let clientIP = request.headers["X-Forwarded-For"].first setHTTPRequest(method: request.method.rawValue, url: request.uri, userAgent: userAgent, clientIP: clientIP) @@ -52,7 +52,7 @@ extension XRayRecorder.Segment { /// /// - Parameters: /// - response: HTTP response. - public func setHTTPResponse(_ response: HTTPResponseHead) { + func setHTTPResponse(_ response: HTTPResponseHead) { setHTTPResponse(status: response.status.code) } @@ -66,7 +66,7 @@ extension XRayRecorder.Segment { /// /// - Parameters: /// - status: HTTP status. - public func setHTTPResponse(status: HTTPResponseStatus) { + func setHTTPResponse(status: HTTPResponseStatus) { setHTTPResponse(status: status.code) } } diff --git a/Sources/AWSXRayRecorder/Segment.swift b/Sources/AWSXRayRecorder/Segment.swift index ea8c51c..ec339fd 100644 --- a/Sources/AWSXRayRecorder/Segment.swift +++ b/Sources/AWSXRayRecorder/Segment.swift @@ -11,7 +11,7 @@ // //===----------------------------------------------------------------------===// -import Baggage +import InstrumentationBaggage import Logging import NIOHTTP1 @@ -104,10 +104,10 @@ extension XRayRecorder { private var state: State { lock.withReaderLock { _state } } - private let _baggage: BaggageContext + private let _baggage: Baggage /// Context baggage containing `XRayContext`. - public var baggage: BaggageContext { lock.withReaderLock { _baggage } } + public var baggage: Baggage { lock.withReaderLock { _baggage } } /// Indicates if the segment is recording information. public var isSampled: Bool { true } @@ -200,7 +200,7 @@ extension XRayRecorder { private var _fault: Bool? /// the exception(s) that caused the error. - private var _cause: Cause = Cause() + private var _cause = Cause() /// annotations object with key-value pairs that you want X-Ray to index for search. private var _annotations: Annotations @@ -209,7 +209,7 @@ extension XRayRecorder { private var _metadata: Metadata /// **array** of subsegment objects. - private var _subsegments: [Segment] = [Segment]() + private var _subsegments = [Segment]() // MARK: Optional Subsegment Fields @@ -248,7 +248,7 @@ extension XRayRecorder { id: ID, name: String, context: TraceContext, - baggage: BaggageContext, + baggage: Baggage, startTime: Timestamp = Timestamp(), subsegment: Bool = false, service: Service? = nil, user: String? = nil, @@ -286,7 +286,7 @@ extension XRayRecorder { logger: Logger? = nil, callback: StateChangeCallback? = nil ) { - var baggage = BaggageContext() + var baggage = Baggage.topLevel // TODO: check baggage.xRayContext = .init(traceId: context.traceId, parentId: id, sampled: context.sampled) self.init(id: id, name: name, context: context, baggage: baggage, startTime: startTime, subsegment: subsegment, service: service, user: user, diff --git a/Sources/AWSXRayRecorder/Timestamp.swift b/Sources/AWSXRayRecorder/Timestamp.swift index cb0d2cd..95500da 100644 --- a/Sources/AWSXRayRecorder/Timestamp.swift +++ b/Sources/AWSXRayRecorder/Timestamp.swift @@ -19,8 +19,8 @@ import Darwin // timespec import Glibc // timespec #endif -extension XRayRecorder { - public struct Timestamp { +public extension XRayRecorder { + struct Timestamp { /// It's already the past. public static func now() -> Timestamp { Timestamp() diff --git a/Sources/AWSXRayRecorder/Trace.swift b/Sources/AWSXRayRecorder/Trace.swift index 17d37e6..20644e8 100644 --- a/Sources/AWSXRayRecorder/Trace.swift +++ b/Sources/AWSXRayRecorder/Trace.swift @@ -73,7 +73,7 @@ extension XRayRecorder.TraceID { extension XRayRecorder { /// Sampling decision. - internal enum SampleDecision: String { + enum SampleDecision: String { // "?" value not document, spotted in https://github.com/aws/aws-xray-sdk-java/blob/829f4c92f099349dbb14d6efd5c19e8452c3f6bc/aws-xray-recorder-sdk-core/src/main/java/com/amazonaws/xray/entities/TraceHeader.java#L41 case sampled = "Sampled=1" case notSampled = "Sampled=0" @@ -86,7 +86,7 @@ extension XRayRecorder { } } -extension XRayRecorder { +public extension XRayRecorder { /// X-Ray Trace Context propagated in a tracing header. /// /// # Tracing header @@ -117,7 +117,7 @@ extension XRayRecorder { /// /// # References /// - [AWS X-Ray concepts - Tracing header](https://docs.aws.amazon.com/xray/latest/devguide/xray-concepts.html#xray-concepts-tracingheader) - public struct TraceContext { + struct TraceContext { /// Root trace ID. public let traceId: TraceID /// Parent segment ID. @@ -154,12 +154,12 @@ extension XRayRecorder { } } -extension XRayRecorder.TraceContext { +public extension XRayRecorder.TraceContext { /// Parses and validates string with Tracing Header. /// /// - Parameter string: string with `TraceContext`. /// - Throws: may throw `XRayRecorder.TraceError` if the value is invalid. - public init(tracingHeader: String) throws { + init(tracingHeader: String) throws { let values = tracingHeader.split(separator: ";") guard values.count >= 1, values.count <= 3, @@ -205,7 +205,7 @@ extension XRayRecorder.TraceContext { } /// Tracing header value. - public var tracingHeader: String { + var tracingHeader: String { let segments: [String?] = [ "Root=\(traceId.rawValue)", { diff --git a/Sources/AWSXRaySDK/DefaultEncoding.swift b/Sources/AWSXRaySDK/DefaultEncoding.swift index de32e87..d35813c 100644 --- a/Sources/AWSXRaySDK/DefaultEncoding.swift +++ b/Sources/AWSXRaySDK/DefaultEncoding.swift @@ -15,9 +15,9 @@ import AWSXRayUDPEmitter import NIO import PureSwiftJSON -extension XRayUDPEmitter.SegmentEncoding { +public extension XRayUDPEmitter.SegmentEncoding { /// Default encoding of `XRayRecorder.Segment` to JSON string. - public static let `default`: XRayUDPEmitter.SegmentEncoding = { + static let `default`: XRayUDPEmitter.SegmentEncoding = { let jsonEncoder = PSJSONEncoder() return XRayUDPEmitter.SegmentEncoding { segment in ByteBuffer(bytes: try jsonEncoder.encode(segment)) diff --git a/Sources/AWSXRaySDK/DefaultRecorder.swift b/Sources/AWSXRaySDK/DefaultRecorder.swift index 8721ae9..901d94c 100644 --- a/Sources/AWSXRaySDK/DefaultRecorder.swift +++ b/Sources/AWSXRaySDK/DefaultRecorder.swift @@ -14,14 +14,14 @@ @_exported import AWSXRayRecorder import AWSXRayUDPEmitter -extension XRayRecorder { +public extension XRayRecorder { /// Creates an instance of `XRayRecorder` with `XRayUDPEmitter`. /// /// - Parameters: /// - eventLoopGroupProvider: specifies how the `EventLoopGroup` used by `XRayUDPEmitter` will be created and establishes lifecycle ownership. /// - config: configuration, **overrides** enviromental variables. - public convenience init(eventLoopGroupProvider: XRayUDPEmitter.EventLoopGroupProvider = .createNew, - config: Config = Config()) + convenience init(eventLoopGroupProvider: XRayUDPEmitter.EventLoopGroupProvider = .createNew, + config: Config = Config()) { do { let emitter = try XRayUDPEmitter(encoding: XRayUDPEmitter.SegmentEncoding.default, diff --git a/Sources/AWSXRayUDPEmitter/Config.swift b/Sources/AWSXRayUDPEmitter/Config.swift index 264c095..3334b08 100644 --- a/Sources/AWSXRayUDPEmitter/Config.swift +++ b/Sources/AWSXRayUDPEmitter/Config.swift @@ -13,9 +13,9 @@ import Logging -extension XRayUDPEmitter { +public extension XRayUDPEmitter { /// `XRayUDPEmitter` configuration. - public struct Config { + struct Config { internal let daemonEndpoint: String internal let logLevel: Logger.Level diff --git a/Sources/AWSXRayUDPEmitter/Locks.swift b/Sources/AWSXRayUDPEmitter/Locks.swift index b5e2cd0..26f8906 100644 --- a/Sources/AWSXRayUDPEmitter/Locks.swift +++ b/Sources/AWSXRayUDPEmitter/Locks.swift @@ -93,7 +93,7 @@ extension Lock { /// - Parameter body: The block to execute while holding the lock. /// - Returns: The value returned by the block. @inlinable - internal func withLock(_ body: () throws -> T) rethrows -> T { + func withLock(_ body: () throws -> T) rethrows -> T { lock() defer { self.unlock() @@ -103,7 +103,7 @@ extension Lock { // specialise Void return (for performance) @inlinable - internal func withLockVoid(_ body: () throws -> Void) rethrows { + func withLockVoid(_ body: () throws -> Void) rethrows { try withLock(body) } } @@ -166,7 +166,7 @@ extension ReadWriteLock { /// - Parameter body: The block to execute while holding the lock. /// - Returns: The value returned by the block. @inlinable - internal func withReaderLock(_ body: () throws -> T) rethrows -> T { + func withReaderLock(_ body: () throws -> T) rethrows -> T { lockRead() defer { self.unlock() @@ -183,7 +183,7 @@ extension ReadWriteLock { /// - Parameter body: The block to execute while holding the lock. /// - Returns: The value returned by the block. @inlinable - internal func withWriterLock(_ body: () throws -> T) rethrows -> T { + func withWriterLock(_ body: () throws -> T) rethrows -> T { lockWrite() defer { self.unlock() @@ -193,13 +193,13 @@ extension ReadWriteLock { // specialise Void return (for performance) @inlinable - internal func withReaderLockVoid(_ body: () throws -> Void) rethrows { + func withReaderLockVoid(_ body: () throws -> Void) rethrows { try withReaderLock(body) } // specialise Void return (for performance) @inlinable - internal func withWriterLockVoid(_ body: () throws -> Void) rethrows { + func withWriterLockVoid(_ body: () throws -> Void) rethrows { try withWriterLock(body) } } diff --git a/Tests/AWSXRayRecorderTests/ContextTests.swift b/Tests/AWSXRayRecorderTests/ContextTests.swift index e93d001..0833663 100644 --- a/Tests/AWSXRayRecorderTests/ContextTests.swift +++ b/Tests/AWSXRayRecorderTests/ContextTests.swift @@ -11,7 +11,7 @@ // //===----------------------------------------------------------------------===// -import Baggage +import InstrumentationBaggage import Logging import XCTest @@ -25,18 +25,18 @@ final class ContextTests: XCTestCase { let logger = Logger(label: "test", factory: { _ in logHandler }) let recorder = XRayRecorder(emitter: XRayNoOpEmitter(), logger: logger) - let baggage = BaggageContext() + let baggage = Baggage.topLevel _ = recorder.beginSegment(name: UUID().uuidString, baggage: baggage) XCTAssertEqual(1, logHandler.errorMessages.count) } func testContextPropagation() { - enum TestKey: BaggageContextKey { + enum TestKey: BaggageKey { typealias Value = String } let recorder = XRayRecorder(emitter: XRayNoOpEmitter()) - var baggage = BaggageContext() + var baggage = Baggage.topLevel let context = XRayContext() baggage.xRayContext = context let testValue = UUID().uuidString diff --git a/Tests/AWSXRayRecorderTests/HTTPTests.swift b/Tests/AWSXRayRecorderTests/HTTPTests.swift index b0edb17..354f37b 100644 --- a/Tests/AWSXRayRecorderTests/HTTPTests.swift +++ b/Tests/AWSXRayRecorderTests/HTTPTests.swift @@ -60,7 +60,7 @@ final class HTTPTests: XCTestCase { let segment = recorder.beginSegment(name: UUID().uuidString, context: context) let status: UInt = 200 - let contentLength: UInt = UInt.random(in: 0 ... UInt.max) + let contentLength = UInt.random(in: 0 ... UInt.max) segment.setHTTPResponse(status: status, contentLength: contentLength) let response = HTTP.Response(status: status, contentLength: contentLength) @@ -73,7 +73,7 @@ final class HTTPTests: XCTestCase { let segment = recorder.beginSegment(name: UUID().uuidString, context: context) let status: UInt = 404 - let contentLength: UInt = UInt.random(in: 0 ... UInt.max) + let contentLength = UInt.random(in: 0 ... UInt.max) segment.setHTTPResponse(status: status, contentLength: contentLength) let response = HTTP.Response(status: status, contentLength: contentLength) @@ -94,7 +94,7 @@ final class HTTPTests: XCTestCase { let segment = recorder.beginSegment(name: UUID().uuidString, context: context) let status: UInt = 429 - let contentLength: UInt = UInt.random(in: 0 ... UInt.max) + let contentLength = UInt.random(in: 0 ... UInt.max) segment.setHTTPResponse(status: status, contentLength: contentLength) let response = HTTP.Response(status: status, contentLength: contentLength) @@ -115,7 +115,7 @@ final class HTTPTests: XCTestCase { let segment = recorder.beginSegment(name: UUID().uuidString, context: context) let status: UInt = 500 - let contentLength: UInt = UInt.random(in: 0 ... UInt.max) + let contentLength = UInt.random(in: 0 ... UInt.max) segment.setHTTPResponse(status: status, contentLength: contentLength) let response = HTTP.Response(status: status, contentLength: contentLength) @@ -134,7 +134,7 @@ final class HTTPTests: XCTestCase { let logHandler = TestLogHandler() let logger = Logger(label: "test", factory: { _ in logHandler }) - let segment = Segment(id: .init(), name: UUID().uuidString, context: .init(), baggage: .init(), logger: logger) + let segment = Segment(id: .init(), name: UUID().uuidString, context: .init(), baggage: .topLevel, logger: logger) XCTAssertTrue(segment.isSampled) let invalidHTTPMethod = "abc" diff --git a/Tests/AWSXRayRecorderTests/NoOpRecorderTests.swift b/Tests/AWSXRayRecorderTests/NoOpRecorderTests.swift index a6fa1ef..975fea7 100644 --- a/Tests/AWSXRayRecorderTests/NoOpRecorderTests.swift +++ b/Tests/AWSXRayRecorderTests/NoOpRecorderTests.swift @@ -11,7 +11,7 @@ // //===----------------------------------------------------------------------===// -import Baggage +import InstrumentationBaggage import XCTest @testable import AWSXRayRecorder @@ -21,8 +21,8 @@ final class NoOpRecorderTests: XCTestCase { let recorder = XRayNoOpRecorder() XCTAssertTrue(recorder.beginSegment(name: UUID().uuidString, context: .init()) is XRayRecorder.NoOpSegment) XCTAssertFalse(recorder.beginSegment(name: UUID().uuidString, context: .init()).isSampled) - XCTAssertTrue(recorder.beginSegment(name: UUID().uuidString, baggage: .init()) is XRayRecorder.NoOpSegment) - XCTAssertFalse(recorder.beginSegment(name: UUID().uuidString, baggage: .init()).isSampled) + XCTAssertTrue(recorder.beginSegment(name: UUID().uuidString, baggage: .topLevel) is XRayRecorder.NoOpSegment) + XCTAssertFalse(recorder.beginSegment(name: UUID().uuidString, baggage: .topLevel).isSampled) } func testFlushing() { @@ -46,16 +46,16 @@ final class NoOpRecorderTests: XCTestCase { } func testContextPropagation() { - enum TestKey: BaggageContextKey { + enum TestKey: BaggageKey { typealias Value = String } let recorder = XRayNoOpRecorder() - var baggage = BaggageContext() + var baggage = Baggage.topLevel let context = XRayContext() baggage.xRayContext = context let testValue = UUID().uuidString - baggage[TestKey] = testValue + baggage[TestKey.self] = testValue let segment = recorder.beginSegment(name: "Segment 1", baggage: baggage) XCTAssertNotEqual(context.parentId, segment.id) @@ -65,7 +65,7 @@ final class NoOpRecorderTests: XCTestCase { XCTAssertEqual(context.traceId, segment.baggage.xRayContext?.traceId) XCTAssertEqual(segment.id, segment.baggage.xRayContext?.parentId) XCTAssertEqual(context.isSampled, segment.baggage.xRayContext?.isSampled) - XCTAssertEqual(testValue, segment.baggage[TestKey]) + XCTAssertEqual(testValue, segment.baggage[TestKey.self]) let subsegment = recorder.beginSegment(name: "Subsegment 1.1", baggage: segment.baggage) XCTAssertNotEqual(segment.id, subsegment.id) @@ -75,7 +75,7 @@ final class NoOpRecorderTests: XCTestCase { XCTAssertEqual(context.traceId, subsegment.baggage.xRayContext?.traceId) XCTAssertEqual(subsegment.id, subsegment.baggage.xRayContext?.parentId) XCTAssertEqual(context.isSampled, subsegment.baggage.xRayContext?.isSampled) - XCTAssertEqual(testValue, subsegment.baggage[TestKey]) + XCTAssertEqual(testValue, subsegment.baggage[TestKey.self]) let subsegment2 = segment.beginSubsegment(name: "Subsegment 1.2") XCTAssertNotEqual(segment.id, subsegment2.id) @@ -85,6 +85,6 @@ final class NoOpRecorderTests: XCTestCase { XCTAssertEqual(context.traceId, subsegment.baggage.xRayContext?.traceId) XCTAssertEqual(subsegment2.id, subsegment2.baggage.xRayContext?.parentId) XCTAssertEqual(context.isSampled, subsegment2.baggage.xRayContext?.isSampled) - XCTAssertEqual(testValue, subsegment2.baggage[TestKey]) + XCTAssertEqual(testValue, subsegment2.baggage[TestKey.self]) } } diff --git a/Tests/AWSXRayRecorderTests/NoOpSegmentTests.swift b/Tests/AWSXRayRecorderTests/NoOpSegmentTests.swift index 8288c56..12c3a42 100644 --- a/Tests/AWSXRayRecorderTests/NoOpSegmentTests.swift +++ b/Tests/AWSXRayRecorderTests/NoOpSegmentTests.swift @@ -62,7 +62,7 @@ final class NoOpSegmentTests: XCTestCase { func testRecordingWithMissingContext() { let recorder = XRayRecorder(emitter: XRayNoOpEmitter(), config: .init(enabled: true)) - let segment = recorder.beginSegment(name: UUID().uuidString, baggage: .init()) + let segment = recorder.beginSegment(name: UUID().uuidString, baggage: .topLevel) XCTAssertFalse(segment.isSampled) XCTAssertTrue(segment is NoOpSegment) @@ -147,7 +147,7 @@ final class NoOpSegmentTests: XCTestCase { let logHandler = TestLogHandler() let logger = Logger(label: "test", factory: { _ in logHandler }) - var segment: Segment? = NoOpSegment(id: .init(), name: UUID().uuidString, baggage: .init(), logger: logger) + var segment: Segment? = NoOpSegment(id: .init(), name: UUID().uuidString, baggage: .topLevel, logger: logger) XCTAssertFalse(segment!.isSampled) XCTAssertTrue(segment is NoOpSegment) diff --git a/Tests/AWSXRayRecorderTests/SegmentExceptionTests.swift b/Tests/AWSXRayRecorderTests/SegmentExceptionTests.swift index 901bae2..069dafa 100644 --- a/Tests/AWSXRayRecorderTests/SegmentExceptionTests.swift +++ b/Tests/AWSXRayRecorderTests/SegmentExceptionTests.swift @@ -11,7 +11,7 @@ // //===----------------------------------------------------------------------===// -import Baggage +import InstrumentationBaggage import XCTest @testable import AWSXRayRecorder @@ -80,7 +80,7 @@ final class SegmentExceptionTests: XCTestCase { emitter.reset() XCTAssertNil(emitter.segments.first) do { - var baggage = BaggageContext() + var baggage = Baggage.topLevel baggage.xRayContext = .init() try recorder.segment(name: UUID().uuidString, baggage: baggage) { _ in throw TestError.test @@ -115,7 +115,7 @@ final class SegmentExceptionTests: XCTestCase { emitter.reset() XCTAssertNil(emitter.segments.first) - var baggage = BaggageContext() + var baggage = Baggage.topLevel baggage.xRayContext = .init() _ = recorder.segment(name: UUID().uuidString, baggage: baggage) { _ in Result.failure(TestError.test) @@ -220,7 +220,7 @@ final class SegmentExceptionNIOTests: XCTestCase { let eventLoop = eventLoopGroup.next() - var baggage = BaggageContext() + var baggage = Baggage.topLevel baggage.xRayContext = .init() try! recorder.segment(name: UUID().uuidString, baggage: baggage) { doWork(on: eventLoop) diff --git a/Tests/AWSXRayRecorderTests/SegmentLoggingTests.swift b/Tests/AWSXRayRecorderTests/SegmentLoggingTests.swift index a720bc5..32e7c05 100644 --- a/Tests/AWSXRayRecorderTests/SegmentLoggingTests.swift +++ b/Tests/AWSXRayRecorderTests/SegmentLoggingTests.swift @@ -31,7 +31,7 @@ final class SegmentLoggingTests: XCTestCase { var numErrors: Int = 0 - let segment = Segment(id: .init(), name: UUID().uuidString, context: .init(), baggage: .init(), + let segment = Segment(id: .init(), name: UUID().uuidString, context: .init(), baggage: .topLevel, startTime: startTime, logger: logger) @@ -94,7 +94,7 @@ final class SegmentLoggingTests: XCTestCase { let logHandler = TestLogHandler() let logger = Logger(label: "test", factory: { _ in logHandler }) - var segment: Segment? = Segment(id: .init(), name: UUID().uuidString, context: .init(), baggage: .init(), + var segment: Segment? = Segment(id: .init(), name: UUID().uuidString, context: .init(), baggage: .topLevel, logger: logger) XCTAssertTrue(segment!.isSampled) segment = nil diff --git a/Tests/AWSXRayRecorderTests/SegmentTests.swift b/Tests/AWSXRayRecorderTests/SegmentTests.swift index 5a726ec..4d60f5e 100644 --- a/Tests/AWSXRayRecorderTests/SegmentTests.swift +++ b/Tests/AWSXRayRecorderTests/SegmentTests.swift @@ -84,7 +84,7 @@ final class SegmentTests: XCTestCase { } func testSubsegmentsInProgress() { - let segment = Segment(id: .init(), name: UUID().uuidString, context: .init(), baggage: .init()) + let segment = Segment(id: .init(), name: UUID().uuidString, context: .init(), baggage: .topLevel) let subsegmentA = segment.beginSubsegment(name: UUID().uuidString) let subsegmentB = segment.beginSubsegment(name: UUID().uuidString) let subsegmentA1 = subsegmentA.beginSubsegment(name: UUID().uuidString)