Skip to content

Commit 8a6fce9

Browse files
committed
Remove package trait, and cleanup sources; Handle failed HTTP responses
1 parent 3bbfd56 commit 8a6fce9

12 files changed

+204
-363
lines changed

Package.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ let package = Package(
7171
.product(name: "NIOHTTPCompression", package: "swift-nio-extras"),
7272
.product(name: "NIOSOCKS", package: "swift-nio-extras"),
7373
.product(name: "NIOTransportServices", package: "swift-nio-transport-services"),
74-
.product(name: "Logging", package: "swift-log"),
7574
.product(name: "Atomics", package: "swift-atomics"),
7675
.product(name: "Algorithms", package: "swift-algorithms"),
7776
// Observability support
77+
.product(name: "Logging", package: "swift-log"),
7878
.product(name: "Tracing", package: "swift-distributed-tracing"),
7979
.product(name: "InMemoryTracing", package: "swift-distributed-tracing"),
8080
],
@@ -93,10 +93,10 @@ let package = Package(
9393
.product(name: "NIOSSL", package: "swift-nio-ssl"),
9494
.product(name: "NIOHTTP2", package: "swift-nio-http2"),
9595
.product(name: "NIOSOCKS", package: "swift-nio-extras"),
96-
.product(name: "Logging", package: "swift-log"),
9796
.product(name: "Atomics", package: "swift-atomics"),
9897
.product(name: "Algorithms", package: "swift-algorithms"),
9998
// Observability support
99+
.product(name: "Logging", package: "swift-log"),
100100
.product(name: "Tracing", package: "swift-distributed-tracing"),
101101
.product(name: "InMemoryTracing", package: "swift-distributed-tracing"),
102102
],

[email protected]

Lines changed: 0 additions & 148 deletions
This file was deleted.

Sources/AsyncHTTPClient/AsyncAwait/HTTPClient+execute.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,10 @@
1515
import Logging
1616
import NIOCore
1717
import NIOHTTP1
18+
import Tracing
1819

1920
import struct Foundation.URL
2021

21-
#if TracingSupport
22-
import Tracing
23-
#endif
24-
2522
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
2623
extension HTTPClient {
2724
/// Execute arbitrary HTTP requests.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the AsyncHTTPClient open source project
4+
//
5+
// Copyright (c) 2021 Apple Inc. and the AsyncHTTPClient project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of AsyncHTTPClient project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import Logging
16+
import NIOCore
17+
import NIOHTTP1
18+
import Tracing
19+
20+
import struct Foundation.URL
21+
22+
23+
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
24+
extension HTTPClient {
25+
@inlinable
26+
func withRequestSpan(
27+
_ request: HTTPClientRequest,
28+
_ body: () async throws -> HTTPClientResponse
29+
) async rethrows -> HTTPClientResponse {
30+
guard let tracer = self.tracer else {
31+
return try await body()
32+
}
33+
34+
return try await tracer.withSpan(request.method.rawValue, ofKind: .client) { span in
35+
let keys = self.configuration.tracing.attributeKeys
36+
span.attributes[keys.requestMethod] = request.method.rawValue
37+
// TODO: set more attributes on the span
38+
let response = try await body()
39+
40+
// set response span attributes
41+
TracingSupport.handleResponseStatusCode(span, response.status, keys: tracing.attributeKeys)
42+
43+
return response
44+
}
45+
}
46+
}

Sources/AsyncHTTPClient/AsyncAwait/Transaction.swift

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ import NIOConcurrencyHelpers
1717
import NIOCore
1818
import NIOHTTP1
1919
import NIOSSL
20-
21-
#if TracingSupport
2220
import Tracing
23-
#endif // TracingSupport
2421

2522
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
2623
@usableFromInline
@@ -38,7 +35,6 @@ final class Transaction:
3835

3936
private let state: NIOLockedValueBox<StateMachine>
4037

41-
#if TracingSupport
4238
init(
4339
request: HTTPClientRequest.Prepared,
4440
requestOptions: RequestOptions,
@@ -55,7 +51,6 @@ final class Transaction:
5551
self.preferredEventLoop = preferredEventLoop
5652
self.state = NIOLockedValueBox(StateMachine(responseContinuation))
5753
}
58-
#endif // TracingSupport
5954

6055
init(
6156
request: HTTPClientRequest.Prepared,

Sources/AsyncHTTPClient/HTTPClient.swift

Lines changed: 14 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@ import NIOPosix
2323
import NIOSSL
2424
import NIOTLS
2525
import NIOTransportServices
26-
27-
#if TracingSupport
2826
import Tracing
29-
#endif
3027

3128
extension Logger {
3229
private func requestInfo(_ request: HTTPClient.Request) -> Logger.Metadata.Value {
@@ -77,12 +74,17 @@ public final class HTTPClient: Sendable {
7774
private let state: NIOLockedValueBox<State>
7875
private let canBeShutDown: Bool
7976

80-
#if TracingSupport
77+
/// Tracer configured for this HTTPClient at configuration time.
8178
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
8279
public var tracer: (any Tracer)? {
8380
configuration.tracing.tracer
8481
}
85-
#endif // TracingSupport
82+
83+
/// Access to tracing configuration in order to get configured attribute keys etc.
84+
@usableFromInline
85+
package var tracing: TracingConfiguration {
86+
self.configuration.tracing
87+
}
8688

8789
static let loggingDisabled = Logger(label: "AHC-do-not-log", factory: { _ in SwiftLogNoOpLogHandler() })
8890

@@ -748,28 +750,17 @@ public final class HTTPClient: Sendable {
748750
]
749751
)
750752

751-
let failedTask: Task<Delegate.Response>? = self.state.withLockedValue { state in
753+
let failedTask: Task<Delegate.Response>? = self.state.withLockedValue { state -> (Task<Delegate.Response>?) in
752754
switch state {
753755
case .upAndRunning:
754756
return nil
755757
case .shuttingDown, .shutDown:
756758
logger.debug("client is shutting down, failing request")
757-
#if TracingSupport
758-
if #available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) {
759-
return Task<Delegate.Response>.failedTask(
760-
eventLoop: taskEL,
761-
error: HTTPClientError.alreadyShutdown,
762-
logger: logger,
763-
tracer: tracer,
764-
makeOrGetFileIOThreadPool: self.makeOrGetFileIOThreadPool
765-
)
766-
}
767-
#endif // TracingSupport
768-
769759
return Task<Delegate.Response>.failedTask(
770760
eventLoop: taskEL,
771761
error: HTTPClientError.alreadyShutdown,
772762
logger: logger,
763+
tracing: tracing,
773764
makeOrGetFileIOThreadPool: self.makeOrGetFileIOThreadPool
774765
)
775766
}
@@ -794,29 +785,14 @@ public final class HTTPClient: Sendable {
794785
}
795786
}()
796787

797-
let task: HTTPClient.Task<Delegate.Response>
798-
#if TracingSupport
799-
if #available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) {
800-
task = Task<Delegate.Response>(
801-
eventLoop: taskEL,
802-
logger: logger,
803-
tracer: tracer,
804-
makeOrGetFileIOThreadPool: self.makeOrGetFileIOThreadPool
805-
)
806-
} else {
807-
task = Task<Delegate.Response>(
788+
789+
let task: HTTPClient.Task<Delegate.Response> =
790+
Task<Delegate.Response>(
808791
eventLoop: taskEL,
809792
logger: logger,
793+
tracing: self.tracing,
810794
makeOrGetFileIOThreadPool: self.makeOrGetFileIOThreadPool
811795
)
812-
}
813-
#else
814-
task = Task<Delegate.Response>(
815-
eventLoop: taskEL,
816-
logger: logger,
817-
makeOrGetFileIOThreadPool: self.makeOrGetFileIOThreadPool
818-
)
819-
#endif // TracingSupport
820796

821797
do {
822798
let requestBag = try RequestBag(
@@ -929,9 +905,8 @@ public final class HTTPClient: Sendable {
929905
/// A method with access to the HTTP/2 stream channel that is called when creating the stream.
930906
public var http2StreamChannelDebugInitializer: (@Sendable (Channel) -> EventLoopFuture<Void>)?
931907

932-
#if TracingSupport
908+
/// Configuration how distributed traces are created and handled.
933909
public var tracing: TracingConfiguration = .init()
934-
#endif
935910

936911
public init(
937912
tlsConfiguration: TLSConfiguration? = nil,
@@ -1062,7 +1037,6 @@ public final class HTTPClient: Sendable {
10621037
self.http2StreamChannelDebugInitializer = http2StreamChannelDebugInitializer
10631038
}
10641039

1065-
#if TracingSupport
10661040
public init(
10671041
tlsConfiguration: TLSConfiguration? = nil,
10681042
redirectConfiguration: RedirectConfiguration? = nil,
@@ -1090,10 +1064,8 @@ public final class HTTPClient: Sendable {
10901064
self.http2StreamChannelDebugInitializer = http2StreamChannelDebugInitializer
10911065
self.tracing = tracing
10921066
}
1093-
#endif
10941067
}
10951068

1096-
#if TracingSupport
10971069
public struct TracingConfiguration: Sendable {
10981070

10991071
@usableFromInline
@@ -1149,7 +1121,6 @@ public final class HTTPClient: Sendable {
11491121
public init() {}
11501122
}
11511123
}
1152-
#endif
11531124

11541125
/// Specifies how `EventLoopGroup` will be created and establishes lifecycle ownership.
11551126
public enum EventLoopGroupProvider {

0 commit comments

Comments
 (0)