Skip to content

Commit 8f99545

Browse files
committed
Update to BaggageContext 0.2.0
1 parent 3c7b0fd commit 8f99545

File tree

6 files changed

+585
-570
lines changed

6 files changed

+585
-570
lines changed

Diff for: Package.swift

+4-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ let package = Package(
2626
.package(url: "https://github.com/apple/swift-nio-extras.git", from: "1.3.0"),
2727
.package(url: "https://github.com/apple/swift-nio-transport-services.git", from: "1.5.1"),
2828
.package(url: "https://github.com/apple/swift-log.git", from: "1.4.0"),
29-
.package(url: "https://github.com/slashmo/gsoc-swift-tracing.git", .branch("main"))
29+
.package(url: "https://github.com/slashmo/gsoc-swift-tracing.git", .branch("main")),
30+
.package(url: "https://github.com/slashmo/gsoc-swift-baggage-context.git", from: "0.2.0"),
3031
],
3132
targets: [
3233
.target(
@@ -35,7 +36,8 @@ let package = Package(
3536
"NIOFoundationCompat", "NIOTransportServices", "Logging",
3637
.product(name: "TracingInstrumentation", package: "gsoc-swift-tracing"),
3738
.product(name: "OpenTelemetryInstrumentationSupport", package: "gsoc-swift-tracing"),
38-
.product(name: "NIOInstrumentation", package: "gsoc-swift-tracing")]
39+
.product(name: "NIOInstrumentation", package: "gsoc-swift-tracing"),
40+
.product(name: "BaggageLogging", package: "swift-baggage-context")]
3941
),
4042
.testTarget(
4143
name: "AsyncHTTPClientTests",

Diff for: Sources/AsyncHTTPClient/HTTPClient.swift

+20-19
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
//===----------------------------------------------------------------------===//
1414

1515
import Baggage
16+
import BaggageLogging
1617
import Foundation
1718
import Instrumentation
1819
import TracingInstrumentation
@@ -232,7 +233,7 @@ public class HTTPClient {
232233
/// - url: Remote URL.
233234
/// - context: Baggage context associated with this request
234235
/// - deadline: Point in time by which the request must complete.
235-
public func get(url: String, context: BaggageContext, deadline: NIODeadline? = nil) -> EventLoopFuture<Response> {
236+
public func get(url: String, context: LoggingBaggageContextCarrier, deadline: NIODeadline? = nil) -> EventLoopFuture<Response> {
236237
return self.execute(.GET, url: url, context: context, deadline: deadline)
237238
}
238239

@@ -243,7 +244,7 @@ public class HTTPClient {
243244
/// - context: Baggage context associated with this request
244245
/// - body: Request body.
245246
/// - deadline: Point in time by which the request must complete.
246-
public func post(url: String, context: BaggageContext, body: Body? = nil, deadline: NIODeadline? = nil) -> EventLoopFuture<Response> {
247+
public func post(url: String, context: LoggingBaggageContextCarrier, body: Body? = nil, deadline: NIODeadline? = nil) -> EventLoopFuture<Response> {
247248
return self.execute(.POST, url: url, context: context, body: body, deadline: deadline)
248249
}
249250

@@ -254,7 +255,7 @@ public class HTTPClient {
254255
/// - context: Baggage context associated with this request
255256
/// - body: Request body.
256257
/// - deadline: Point in time by which the request must complete.
257-
public func patch(url: String, context: BaggageContext, body: Body? = nil, deadline: NIODeadline? = nil) -> EventLoopFuture<Response> {
258+
public func patch(url: String, context: LoggingBaggageContextCarrier, body: Body? = nil, deadline: NIODeadline? = nil) -> EventLoopFuture<Response> {
258259
return self.execute(.PATCH, url: url, context: context, body: body, deadline: deadline)
259260
}
260261

@@ -265,7 +266,7 @@ public class HTTPClient {
265266
/// - context: Baggage context associated with this request
266267
/// - body: Request body.
267268
/// - deadline: Point in time by which the request must complete.
268-
public func put(url: String, context: BaggageContext, body: Body? = nil, deadline: NIODeadline? = nil) -> EventLoopFuture<Response> {
269+
public func put(url: String, context: LoggingBaggageContextCarrier, body: Body? = nil, deadline: NIODeadline? = nil) -> EventLoopFuture<Response> {
269270
return self.execute(.PUT, url: url, context: context, body: body, deadline: deadline)
270271
}
271272

@@ -275,7 +276,7 @@ public class HTTPClient {
275276
/// - url: Remote URL.
276277
/// - context: Baggage context associated with this request
277278
/// - deadline: The time when the request must have been completed by.
278-
public func delete(url: String, context: BaggageContext, deadline: NIODeadline? = nil) -> EventLoopFuture<Response> {
279+
public func delete(url: String, context: LoggingBaggageContextCarrier, deadline: NIODeadline? = nil) -> EventLoopFuture<Response> {
279280
return self.execute(.DELETE, url: url, context: context, deadline: deadline)
280281
}
281282

@@ -287,7 +288,7 @@ public class HTTPClient {
287288
/// - context: Baggage context associated with this request
288289
/// - body: Request body.
289290
/// - deadline: Point in time by which the request must complete.
290-
public func execute(_ method: HTTPMethod = .GET, url: String, context: BaggageContext, body: Body? = nil, deadline: NIODeadline? = nil) -> EventLoopFuture<Response> {
291+
public func execute(_ method: HTTPMethod = .GET, url: String, context: LoggingBaggageContextCarrier, body: Body? = nil, deadline: NIODeadline? = nil) -> EventLoopFuture<Response> {
291292
do {
292293
let request = try Request(url: url, method: method, body: body)
293294
return self.execute(request: request, context: context, deadline: deadline)
@@ -305,7 +306,7 @@ public class HTTPClient {
305306
/// - context: Baggage context associated with this request
306307
/// - body: Request body.
307308
/// - deadline: Point in time by which the request must complete.
308-
public func execute(_ method: HTTPMethod = .GET, socketPath: String, urlPath: String, context: BaggageContext, body: Body? = nil, deadline: NIODeadline? = nil) -> EventLoopFuture<Response> {
309+
public func execute(_ method: HTTPMethod = .GET, socketPath: String, urlPath: String, context: LoggingBaggageContextCarrier, body: Body? = nil, deadline: NIODeadline? = nil) -> EventLoopFuture<Response> {
309310
do {
310311
guard let url = URL(httpURLWithSocketPath: socketPath, uri: urlPath) else {
311312
throw HTTPClientError.invalidURL
@@ -327,7 +328,7 @@ public class HTTPClient {
327328
/// - body: Request body.
328329
/// - deadline: Point in time by which the request must complete.
329330
/// - logger: The logger to use for this request.
330-
public func execute(_ method: HTTPMethod = .GET, secureSocketPath: String, urlPath: String, context: BaggageContext, body: Body? = nil, deadline: NIODeadline? = nil) -> EventLoopFuture<Response> {
331+
public func execute(_ method: HTTPMethod = .GET, secureSocketPath: String, urlPath: String, context: LoggingBaggageContextCarrier, body: Body? = nil, deadline: NIODeadline? = nil) -> EventLoopFuture<Response> {
331332
do {
332333
guard let url = URL(httpsURLWithSocketPath: secureSocketPath, uri: urlPath) else {
333334
throw HTTPClientError.invalidURL
@@ -345,7 +346,7 @@ public class HTTPClient {
345346
/// - request: HTTP request to execute.
346347
/// - context: Baggage context associated with this request
347348
/// - deadline: Point in time by which the request must complete.
348-
public func execute(request: Request, context: BaggageContext, deadline: NIODeadline? = nil) -> EventLoopFuture<Response> {
349+
public func execute(request: Request, context: LoggingBaggageContextCarrier, deadline: NIODeadline? = nil) -> EventLoopFuture<Response> {
349350
let accumulator = ResponseAccumulator(request: request)
350351
return self.execute(request: request, delegate: accumulator, context: context, deadline: deadline).futureResult
351352
}
@@ -357,7 +358,7 @@ public class HTTPClient {
357358
/// - eventLoop: NIO Event Loop preference.
358359
/// - context: Baggage context associated with this request
359360
/// - deadline: Point in time by which the request must complete.
360-
public func execute(request: Request, eventLoop: EventLoopPreference, context: BaggageContext, deadline: NIODeadline? = nil) -> EventLoopFuture<Response> {
361+
public func execute(request: Request, eventLoop: EventLoopPreference, context: LoggingBaggageContextCarrier, deadline: NIODeadline? = nil) -> EventLoopFuture<Response> {
361362
let accumulator = ResponseAccumulator(request: request)
362363
return self.execute(request: request, delegate: accumulator, eventLoop: eventLoop, context: context, deadline: deadline).futureResult
363364
}
@@ -371,7 +372,7 @@ public class HTTPClient {
371372
/// - deadline: Point in time by which the request must complete.
372373
public func execute<Delegate: HTTPClientResponseDelegate>(request: Request,
373374
delegate: Delegate,
374-
context: BaggageContext,
375+
context: LoggingBaggageContextCarrier,
375376
deadline: NIODeadline? = nil) -> Task<Delegate.Response> {
376377
return self.execute(request: request, delegate: delegate, eventLoop: .indifferent, context: context, deadline: deadline)
377378
}
@@ -387,7 +388,7 @@ public class HTTPClient {
387388
public func execute<Delegate: HTTPClientResponseDelegate>(request: Request,
388389
delegate: Delegate,
389390
eventLoop eventLoopPreference: EventLoopPreference,
390-
context: BaggageContext,
391+
context: LoggingBaggageContextCarrier,
391392
deadline: NIODeadline? = nil) -> Task<Delegate.Response> {
392393
var span = InstrumentationSystem.tracingInstrument.startSpan(named: request.method.rawValue, context: context, ofKind: .client, at: nil)
393394
span.attributes.http.method = request.method.rawValue
@@ -401,10 +402,10 @@ public class HTTPClient {
401402
// TODO: net.peer.ip / Not required, but recommended
402403

403404
var request = request
404-
InstrumentationSystem.instrument.inject(context, into: &request.headers, using: HTTPHeadersInjector())
405+
InstrumentationSystem.instrument.inject(context.baggage, into: &request.headers, using: HTTPHeadersInjector())
406+
407+
let logger = context.logger.attachingRequestInformation(request, requestID: globalRequestID.add(1))
405408

406-
// let logger = (originalLogger ?? HTTPClient.loggingDisabled).attachingRequestInformation(request, requestID: globalRequestID.add(1))
407-
let logger = HTTPClient.loggingDisabled
408409
let taskEL: EventLoop
409410
switch eventLoopPreference.preference {
410411
case .indifferent:
@@ -418,16 +419,16 @@ public class HTTPClient {
418419
case .testOnly_exact(_, delegateOn: let delegateEL):
419420
taskEL = delegateEL
420421
}
421-
// logger.trace("selected EventLoop for task given the preference",
422-
// metadata: ["ahc-eventloop": "\(taskEL)",
423-
// "ahc-el-preference": "\(eventLoopPreference)"])
422+
logger.trace("selected EventLoop for task given the preference",
423+
metadata: ["ahc-eventloop": "\(taskEL)",
424+
"ahc-el-preference": "\(eventLoopPreference)"])
424425

425426
let failedTask: Task<Delegate.Response>? = self.stateLock.withLock {
426427
switch state {
427428
case .upAndRunning:
428429
return nil
429430
case .shuttingDown, .shutDown:
430-
// logger.debug("client is shutting down, failing request")
431+
logger.debug("client is shutting down, failing request")
431432
return Task<Delegate.Response>.failedTask(eventLoop: taskEL,
432433
error: HTTPClientError.alreadyShutdown,
433434
logger: logger)

0 commit comments

Comments
 (0)