Skip to content

Commit d0fac6c

Browse files
committed
Pass BaggageContext through public API
1 parent c65b2ae commit d0fac6c

File tree

6 files changed

+555
-611
lines changed

6 files changed

+555
-611
lines changed

Package.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@ 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"))
2930
],
3031
targets: [
3132
.target(
3233
name: "AsyncHTTPClient",
3334
dependencies: ["NIO", "NIOHTTP1", "NIOSSL", "NIOConcurrencyHelpers", "NIOHTTPCompression",
34-
"NIOFoundationCompat", "NIOTransportServices", "Logging"]
35+
"NIOFoundationCompat", "NIOTransportServices", "Logging",
36+
.product(name: "TracingInstrumentation", package: "gsoc-swift-tracing")]
3537
),
3638
.testTarget(
3739
name: "AsyncHTTPClientTests",

Sources/AsyncHTTPClient/HTTPClient.swift

Lines changed: 40 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15+
import Baggage
1516
import Foundation
1617
import Logging
1718
import NIO
@@ -225,115 +226,67 @@ public class HTTPClient {
225226
///
226227
/// - parameters:
227228
/// - url: Remote URL.
229+
/// - context: Metadata propagated for instrumentation.
228230
/// - deadline: Point in time by which the request must complete.
229-
public func get(url: String, deadline: NIODeadline? = nil) -> EventLoopFuture<Response> {
230-
return self.get(url: url, deadline: deadline, logger: HTTPClient.loggingDisabled)
231-
}
232-
233-
/// Execute `GET` request using specified URL.
234-
///
235-
/// - parameters:
236-
/// - url: Remote URL.
237-
/// - deadline: Point in time by which the request must complete.
238-
/// - logger: The logger to use for this request.
239-
public func get(url: String, deadline: NIODeadline? = nil, logger: Logger) -> EventLoopFuture<Response> {
240-
return self.execute(.GET, url: url, deadline: deadline, logger: logger)
241-
}
242-
243-
/// Execute `POST` request using specified URL.
244-
///
245-
/// - parameters:
246-
/// - url: Remote URL.
247-
/// - body: Request body.
248-
/// - deadline: Point in time by which the request must complete.
249-
public func post(url: String, body: Body? = nil, deadline: NIODeadline? = nil) -> EventLoopFuture<Response> {
250-
return self.post(url: url, body: body, deadline: deadline, logger: HTTPClient.loggingDisabled)
231+
public func get(url: String, context: BaggageContext, deadline: NIODeadline? = nil) -> EventLoopFuture<Response> {
232+
return self.execute(.GET, url: url, context: context, deadline: deadline)
251233
}
252234

253235
/// Execute `POST` request using specified URL.
254236
///
255237
/// - parameters:
256238
/// - url: Remote URL.
239+
/// - context: Metadata propagated for instrumentation.
257240
/// - body: Request body.
258241
/// - deadline: Point in time by which the request must complete.
259-
/// - logger: The logger to use for this request.
260-
public func post(url: String, body: Body? = nil, deadline: NIODeadline? = nil, logger: Logger) -> EventLoopFuture<Response> {
261-
return self.execute(.POST, url: url, body: body, deadline: deadline, logger: logger)
242+
public func post(url: String, context: BaggageContext, body: Body? = nil, deadline: NIODeadline? = nil) -> EventLoopFuture<Response> {
243+
return self.execute(.POST, url: url, context: context, body: body, deadline: deadline)
262244
}
263245

264246
/// Execute `PATCH` request using specified URL.
265247
///
266248
/// - parameters:
267249
/// - url: Remote URL.
250+
/// - context: Metadata propagated for instrumentation.
268251
/// - body: Request body.
269252
/// - deadline: Point in time by which the request must complete.
270-
public func patch(url: String, body: Body? = nil, deadline: NIODeadline? = nil) -> EventLoopFuture<Response> {
271-
return self.patch(url: url, body: body, deadline: deadline, logger: HTTPClient.loggingDisabled)
272-
}
273-
274-
/// Execute `PATCH` request using specified URL.
275-
///
276-
/// - parameters:
277-
/// - url: Remote URL.
278-
/// - body: Request body.
279-
/// - deadline: Point in time by which the request must complete.
280-
/// - logger: The logger to use for this request.
281-
public func patch(url: String, body: Body? = nil, deadline: NIODeadline? = nil, logger: Logger) -> EventLoopFuture<Response> {
282-
return self.execute(.PATCH, url: url, body: body, deadline: deadline, logger: logger)
253+
public func patch(url: String, context: BaggageContext, body: Body? = nil, deadline: NIODeadline? = nil) -> EventLoopFuture<Response> {
254+
return self.execute(.PATCH, url: url, context: context, body: body, deadline: deadline)
283255
}
284256

285257
/// Execute `PUT` request using specified URL.
286258
///
287259
/// - parameters:
288260
/// - url: Remote URL.
261+
/// - context: Metadata propagated for instrumentation.
289262
/// - body: Request body.
290263
/// - deadline: Point in time by which the request must complete.
291-
public func put(url: String, body: Body? = nil, deadline: NIODeadline? = nil) -> EventLoopFuture<Response> {
292-
return self.put(url: url, body: body, deadline: deadline, logger: HTTPClient.loggingDisabled)
293-
}
294-
295-
/// Execute `PUT` request using specified URL.
296-
///
297-
/// - parameters:
298-
/// - url: Remote URL.
299-
/// - body: Request body.
300-
/// - deadline: Point in time by which the request must complete.
301-
/// - logger: The logger to use for this request.
302-
public func put(url: String, body: Body? = nil, deadline: NIODeadline? = nil, logger: Logger) -> EventLoopFuture<Response> {
303-
return self.execute(.PUT, url: url, body: body, deadline: deadline, logger: logger)
264+
public func put(url: String, context: BaggageContext, body: Body? = nil, deadline: NIODeadline? = nil) -> EventLoopFuture<Response> {
265+
return self.execute(.PUT, url: url, context: context, body: body, deadline: deadline)
304266
}
305267

306268
/// Execute `DELETE` request using specified URL.
307269
///
308270
/// - parameters:
309271
/// - url: Remote URL.
272+
/// - context: Metadata propagated for instrumentation.
310273
/// - deadline: The time when the request must have been completed by.
311-
public func delete(url: String, deadline: NIODeadline? = nil) -> EventLoopFuture<Response> {
312-
return self.delete(url: url, deadline: deadline, logger: HTTPClient.loggingDisabled)
313-
}
314-
315-
/// Execute `DELETE` request using specified URL.
316-
///
317-
/// - parameters:
318-
/// - url: Remote URL.
319-
/// - deadline: The time when the request must have been completed by.
320-
/// - logger: The logger to use for this request.
321-
public func delete(url: String, deadline: NIODeadline? = nil, logger: Logger) -> EventLoopFuture<Response> {
322-
return self.execute(.DELETE, url: url, deadline: deadline, logger: logger)
274+
public func delete(url: String, context: BaggageContext, deadline: NIODeadline? = nil) -> EventLoopFuture<Response> {
275+
return self.execute(.DELETE, url: url, context: context, deadline: deadline)
323276
}
324277

325278
/// Execute arbitrary HTTP request using specified URL.
326279
///
327280
/// - parameters:
328281
/// - method: Request method.
329282
/// - url: Request url.
283+
/// - context: Metadata propagated for instrumentation.
330284
/// - body: Request body.
331285
/// - deadline: Point in time by which the request must complete.
332-
/// - logger: The logger to use for this request.
333-
public func execute(_ method: HTTPMethod = .GET, url: String, body: Body? = nil, deadline: NIODeadline? = nil, logger: Logger? = nil) -> EventLoopFuture<Response> {
286+
public func execute(_ method: HTTPMethod = .GET, url: String, context: BaggageContext, body: Body? = nil, deadline: NIODeadline? = nil) -> EventLoopFuture<Response> {
334287
do {
335288
let request = try Request(url: url, method: method, body: body)
336-
return self.execute(request: request, deadline: deadline, logger: logger ?? HTTPClient.loggingDisabled)
289+
return self.execute(request: request, context: context, deadline: deadline)
337290
} catch {
338291
return self.eventLoopGroup.next().makeFailedFuture(error)
339292
}
@@ -345,16 +298,16 @@ public class HTTPClient {
345298
/// - method: Request method.
346299
/// - socketPath: The path to the unix domain socket to connect to.
347300
/// - urlPath: The URL path and query that will be sent to the server.
301+
/// - context: Metadata propagated for instrumentation.
348302
/// - body: Request body.
349303
/// - deadline: Point in time by which the request must complete.
350-
/// - logger: The logger to use for this request.
351-
public func execute(_ method: HTTPMethod = .GET, socketPath: String, urlPath: String, body: Body? = nil, deadline: NIODeadline? = nil, logger: Logger? = nil) -> EventLoopFuture<Response> {
304+
public func execute(_ method: HTTPMethod = .GET, socketPath: String, urlPath: String, context: BaggageContext, body: Body? = nil, deadline: NIODeadline? = nil) -> EventLoopFuture<Response> {
352305
do {
353306
guard let url = URL(httpURLWithSocketPath: socketPath, uri: urlPath) else {
354307
throw HTTPClientError.invalidURL
355308
}
356309
let request = try Request(url: url, method: method, body: body)
357-
return self.execute(request: request, deadline: deadline, logger: logger ?? HTTPClient.loggingDisabled)
310+
return self.execute(request: request, context: context, deadline: deadline)
358311
} catch {
359312
return self.eventLoopGroup.next().makeFailedFuture(error)
360313
}
@@ -366,16 +319,17 @@ public class HTTPClient {
366319
/// - method: Request method.
367320
/// - secureSocketPath: The path to the unix domain socket to connect to.
368321
/// - urlPath: The URL path and query that will be sent to the server.
322+
/// - context: Metadata propagated for instrumentation.
369323
/// - body: Request body.
370324
/// - deadline: Point in time by which the request must complete.
371325
/// - logger: The logger to use for this request.
372-
public func execute(_ method: HTTPMethod = .GET, secureSocketPath: String, urlPath: String, body: Body? = nil, deadline: NIODeadline? = nil, logger: Logger? = nil) -> EventLoopFuture<Response> {
326+
public func execute(_ method: HTTPMethod = .GET, secureSocketPath: String, urlPath: String, context: BaggageContext, body: Body? = nil, deadline: NIODeadline? = nil) -> EventLoopFuture<Response> {
373327
do {
374328
guard let url = URL(httpsURLWithSocketPath: secureSocketPath, uri: urlPath) else {
375329
throw HTTPClientError.invalidURL
376330
}
377331
let request = try Request(url: url, method: method, body: body)
378-
return self.execute(request: request, deadline: deadline, logger: logger ?? HTTPClient.loggingDisabled)
332+
return self.execute(request: request, context: context, deadline: deadline)
379333
} catch {
380334
return self.eventLoopGroup.next().makeFailedFuture(error)
381335
}
@@ -385,74 +339,37 @@ public class HTTPClient {
385339
///
386340
/// - parameters:
387341
/// - request: HTTP request to execute.
342+
/// - context: Metadata propagated for instrumentation.
388343
/// - deadline: Point in time by which the request must complete.
389-
public func execute(request: Request, deadline: NIODeadline? = nil) -> EventLoopFuture<Response> {
390-
return self.execute(request: request, deadline: deadline, logger: HTTPClient.loggingDisabled)
391-
}
392-
393-
/// Execute arbitrary HTTP request using specified URL.
394-
///
395-
/// - parameters:
396-
/// - request: HTTP request to execute.
397-
/// - deadline: Point in time by which the request must complete.
398-
/// - logger: The logger to use for this request.
399-
public func execute(request: Request, deadline: NIODeadline? = nil, logger: Logger) -> EventLoopFuture<Response> {
344+
public func execute(request: Request, context: BaggageContext, deadline: NIODeadline? = nil) -> EventLoopFuture<Response> {
400345
let accumulator = ResponseAccumulator(request: request)
401-
return self.execute(request: request, delegate: accumulator, deadline: deadline, logger: logger).futureResult
346+
return self.execute(request: request, delegate: accumulator, context: context, deadline: deadline).futureResult
402347
}
403348

404349
/// Execute arbitrary HTTP request using specified URL.
405350
///
406351
/// - parameters:
407352
/// - request: HTTP request to execute.
408353
/// - eventLoop: NIO Event Loop preference.
354+
/// - context: Metadata propagated for instrumentation.
409355
/// - deadline: Point in time by which the request must complete.
410-
public func execute(request: Request, eventLoop: EventLoopPreference, deadline: NIODeadline? = nil) -> EventLoopFuture<Response> {
411-
return self.execute(request: request,
412-
eventLoop: eventLoop,
413-
deadline: deadline,
414-
logger: HTTPClient.loggingDisabled)
415-
}
416-
417-
/// Execute arbitrary HTTP request and handle response processing using provided delegate.
418-
///
419-
/// - parameters:
420-
/// - request: HTTP request to execute.
421-
/// - eventLoop: NIO Event Loop preference.
422-
/// - deadline: Point in time by which the request must complete.
423-
/// - logger: The logger to use for this request.
424-
public func execute(request: Request,
425-
eventLoop eventLoopPreference: EventLoopPreference,
426-
deadline: NIODeadline? = nil,
427-
logger: Logger?) -> EventLoopFuture<Response> {
356+
public func execute(request: Request, eventLoop: EventLoopPreference, context: BaggageContext, deadline: NIODeadline? = nil) -> EventLoopFuture<Response> {
428357
let accumulator = ResponseAccumulator(request: request)
429-
return self.execute(request: request, delegate: accumulator, eventLoop: eventLoopPreference, deadline: deadline, logger: logger).futureResult
358+
return self.execute(request: request, delegate: accumulator, eventLoop: eventLoop, context: context, deadline: deadline).futureResult
430359
}
431360

432361
/// Execute arbitrary HTTP request and handle response processing using provided delegate.
433362
///
434363
/// - parameters:
435364
/// - request: HTTP request to execute.
436365
/// - delegate: Delegate to process response parts.
366+
/// - context: Metadata propagated for instrumentation.
437367
/// - deadline: Point in time by which the request must complete.
438368
public func execute<Delegate: HTTPClientResponseDelegate>(request: Request,
439369
delegate: Delegate,
370+
context: BaggageContext,
440371
deadline: NIODeadline? = nil) -> Task<Delegate.Response> {
441-
return self.execute(request: request, delegate: delegate, deadline: deadline, logger: HTTPClient.loggingDisabled)
442-
}
443-
444-
/// Execute arbitrary HTTP request and handle response processing using provided delegate.
445-
///
446-
/// - parameters:
447-
/// - request: HTTP request to execute.
448-
/// - delegate: Delegate to process response parts.
449-
/// - deadline: Point in time by which the request must complete.
450-
/// - logger: The logger to use for this request.
451-
public func execute<Delegate: HTTPClientResponseDelegate>(request: Request,
452-
delegate: Delegate,
453-
deadline: NIODeadline? = nil,
454-
logger: Logger) -> Task<Delegate.Response> {
455-
return self.execute(request: request, delegate: delegate, eventLoop: .indifferent, deadline: deadline, logger: logger)
372+
return self.execute(request: request, delegate: delegate, eventLoop: .indifferent, context: context, deadline: deadline)
456373
}
457374

458375
/// Execute arbitrary HTTP request and handle response processing using provided delegate.
@@ -461,15 +378,18 @@ public class HTTPClient {
461378
/// - request: HTTP request to execute.
462379
/// - delegate: Delegate to process response parts.
463380
/// - eventLoop: NIO Event Loop preference.
381+
/// - context: Metadata propagated for instrumentation.
464382
/// - deadline: Point in time by which the request must complete.
465383
/// - logger: The logger to use for this request.
466384
public func execute<Delegate: HTTPClientResponseDelegate>(request: Request,
467385
delegate: Delegate,
468386
eventLoop eventLoopPreference: EventLoopPreference,
387+
context: BaggageContext,
469388
deadline: NIODeadline? = nil) -> Task<Delegate.Response> {
470389
return self.execute(request: request,
471390
delegate: delegate,
472391
eventLoop: eventLoopPreference,
392+
context: context,
473393
deadline: deadline,
474394
logger: HTTPClient.loggingDisabled)
475395
}
@@ -480,10 +400,12 @@ public class HTTPClient {
480400
/// - request: HTTP request to execute.
481401
/// - delegate: Delegate to process response parts.
482402
/// - eventLoop: NIO Event Loop preference.
403+
/// - context: Metadata propagated for instrumentation.
483404
/// - deadline: Point in time by which the request must complete.
484405
public func execute<Delegate: HTTPClientResponseDelegate>(request: Request,
485406
delegate: Delegate,
486407
eventLoop eventLoopPreference: EventLoopPreference,
408+
context: BaggageContext,
487409
deadline: NIODeadline? = nil,
488410
logger originalLogger: Logger?) -> Task<Delegate.Response> {
489411
let logger = (originalLogger ?? HTTPClient.loggingDisabled).attachingRequestInformation(request, requestID: globalRequestID.add(1))
@@ -531,6 +453,7 @@ public class HTTPClient {
531453
self.execute(request: newRequest,
532454
delegate: delegate,
533455
eventLoop: eventLoopPreference,
456+
context: context,
534457
deadline: deadline)
535458
}
536459
case .disallow:

0 commit comments

Comments
 (0)