Skip to content
This repository was archived by the owner on Apr 23, 2021. It is now read-only.

Commit 38374be

Browse files
committed
review feedback: background -> topLevel rename, as it makes more sense
yet keeps the "huh?" effect of "this likely is wrong" if it appears in the middle of a codebase
1 parent f0f91b7 commit 38374be

File tree

7 files changed

+41
-40
lines changed

7 files changed

+41
-40
lines changed

Diff for: Sources/Baggage/Baggage.swift

+8-9
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
/// ## Usage
4848
/// Using a baggage container is fairly straight forward, as it boils down to using the prepared computed properties:
4949
///
50-
/// var context = Baggage.background
50+
/// var context = Baggage.topLevel
5151
/// // set a new value
5252
/// context.testID = "abc"
5353
/// // retrieve a stored value
@@ -70,17 +70,16 @@ public struct Baggage {
7070

7171
private var _storage = [AnyBaggageKey: Any]()
7272

73-
/// Internal on purpose, please use `Baggage.TODO` or `Baggage.background` to create an "empty" context,
73+
/// Internal on purpose, please use `Baggage.TODO` or `Baggage.topLevel` to create an "empty" context,
7474
/// which carries more meaning to other developers why an empty context was used.
7575
init() {}
7676
}
7777

7878
extension Baggage {
79-
/// Creates a new empty baggage, generally used for background processing tasks or an "initial" baggage to be immediately
80-
/// populated with some values by a framework or runtime.
81-
///
82-
/// Typically, this would only be called in a "top" or "background" setting, such as the main function, initialization,
83-
/// tests, beginning of some background task or some other top-level baggage to be immediately populated with incoming request/message information.
79+
/// Creates a new empty "top level" baggage, generally used as an "initial" baggage to immediately be populated with
80+
/// some values by a framework or runtime. Another use case is for tasks starting in the "background" (e.g. on a timer),
81+
/// which don't have a "request context" per se that they can pick up, and as such they have to create a "top level"
82+
/// baggage for their work.
8483
///
8584
/// ## Usage in frameworks and libraries
8685
/// This function is really only intended to be used frameworks and libraries, at the "top-level" where a request's,
@@ -99,7 +98,7 @@ extension Baggage {
9998
/// in order to inform other developers that the lack of context passing was not done on purpose, but rather because either
10099
/// not being sure where to obtain a context from, or other framework limitations -- e.g. the outer framework not being
101100
/// baggage context aware just yet.
102-
public static var background: Baggage {
101+
public static var topLevel: Baggage {
103102
return Baggage()
104103
}
105104
}
@@ -127,7 +126,7 @@ extension Baggage {
127126
/// - reason: Informational reason for developers, why a placeholder context was used instead of a proper one,
128127
/// - Returns: Empty "to-do" baggage context which should be eventually replaced with a carried through one, or `background`.
129128
public static func TODO(_ reason: StaticString? = "", function: String = #function, file: String = #file, line: UInt = #line) -> Baggage {
130-
var context = Baggage.background
129+
var context = Baggage.topLevel
131130
#if BAGGAGE_CRASH_TODOS
132131
fatalError("BAGGAGE_CRASH_TODOS: at \(file):\(line) (function \(function)), reason: \(reason)")
133132
#else

Diff for: Sources/BaggageContext/Context.swift

+9-7
Original file line numberDiff line numberDiff line change
@@ -100,20 +100,20 @@ public struct DefaultContext: Context {
100100
didSet {
101101
// Since someone could have completely replaced the logger (not just changed the log level),
102102
// we have to update the baggage again, since perhaps the new logger has empty metadata.
103-
self.logger.update(previous: .background, latest: self.baggage)
103+
self.logger.update(previous: .topLevel, latest: self.baggage)
104104
}
105105
}
106106

107107
public init(baggage: Baggage, logger: Logger) {
108108
self.baggage = baggage
109109
self.logger = logger
110-
self.logger.update(previous: .background, latest: baggage)
110+
self.logger.update(previous: .topLevel, latest: baggage)
111111
}
112112

113113
public init<C>(context: C) where C: Context {
114114
self.baggage = context.baggage
115115
self.logger = context.logger
116-
self.logger.update(previous: .background, latest: self.baggage)
116+
self.logger.update(previous: .topLevel, latest: self.baggage)
117117
}
118118
}
119119

@@ -172,9 +172,11 @@ extension DefaultContext {
172172
// MARK: Context Initializers
173173

174174
extension DefaultContext {
175-
/// An empty baggage context intended as the "root" or "initial" baggage context background processing tasks, or as the "root" baggage context.
175+
/// Creates a new empty "top level" default baggage context, generally used as an "initial" context to immediately be populated with
176+
/// some values by a framework or runtime. Another use case is for tasks starting in the "background" (e.g. on a timer),
177+
/// which don't have a "request context" per se that they can pick up, and as such they have to create a "top level"
178+
/// baggage for their work.
176179
///
177-
/// It is never canceled, has no values, and has no deadline.
178180
/// It is typically used by the main function, initialization, and tests, and as the top-level Context for incoming requests.
179181
///
180182
/// ### Usage in frameworks and libraries
@@ -194,8 +196,8 @@ extension DefaultContext {
194196
/// such that other developers are informed that the lack of context was not done on purpose, but rather because either
195197
/// not being sure where to obtain a context from, or other framework limitations -- e.g. the outer framework not being
196198
/// context aware just yet.
197-
public static func background(logger: Logger) -> DefaultContext {
198-
return .init(baggage: .background, logger: logger)
199+
public static func topLevel(logger: Logger) -> DefaultContext {
200+
return .init(baggage: .topLevel, logger: logger)
199201
}
200202
}
201203

Diff for: Sources/BaggageContextBenchmarks/BaggageLoggingBenchmarks.swift

+8-8
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public let BaggageLoggingBenchmarks: [BenchmarkInfo] = [
6262
name: pad("BaggageLoggingBenchmarks.0_log_noop_loggerWithBaggage_small"),
6363
runFunction: { iters in
6464
let logger = Logger(label: "0_log_noop_loggerWithBaggage_small", factory: { _ in SwiftLogNoOpLogHandler() })
65-
var baggage = Baggage.background
65+
var baggage = Baggage.topLevel
6666
baggage[TestK1.self] = "k1-value"
6767
baggage[TestK2.self] = "k2-value"
6868
baggage[TestK3.self] = "k3-value"
@@ -77,7 +77,7 @@ public let BaggageLoggingBenchmarks: [BenchmarkInfo] = [
7777
BenchmarkInfo(
7878
name: pad("BaggageLoggingBenchmarks.0_log_noop_context_with_baggage_small"),
7979
runFunction: { iters in
80-
var context = DefaultContext.background(logger: Logger(label: "0_log_noop_context_with_baggage_small", factory: { _ in SwiftLogNoOpLogHandler() }))
80+
var context = DefaultContext.topLevel(logger: Logger(label: "0_log_noop_context_with_baggage_small", factory: { _ in SwiftLogNoOpLogHandler() }))
8181
context.baggage[TestK1.self] = "k1-value"
8282
context.baggage[TestK2.self] = "k2-value"
8383
context.baggage[TestK3.self] = "k3-value"
@@ -125,7 +125,7 @@ public let BaggageLoggingBenchmarks: [BenchmarkInfo] = [
125125
name: pad("BaggageLoggingBenchmarks.1_log_real_loggerWithBaggage_small"),
126126
runFunction: { iters in
127127
let logger = Logger(label: "1_log_real_loggerWithBaggage_small", factory: StreamLogHandler.standardError)
128-
var baggage = Baggage.background
128+
var baggage = Baggage.topLevel
129129
baggage[TestK1.self] = "k1-value"
130130
baggage[TestK2.self] = "k2-value"
131131
baggage[TestK3.self] = "k3-value"
@@ -140,7 +140,7 @@ public let BaggageLoggingBenchmarks: [BenchmarkInfo] = [
140140
BenchmarkInfo(
141141
name: pad("BaggageLoggingBenchmarks.1_log_real_context_with_baggage_small"),
142142
runFunction: { iters in
143-
var context = DefaultContext.background(logger: Logger(label: "1_log_real_context_with_baggage_small", factory: StreamLogHandler.standardError))
143+
var context = DefaultContext.topLevel(logger: Logger(label: "1_log_real_context_with_baggage_small", factory: StreamLogHandler.standardError))
144144
context.baggage[TestK1.self] = "k1-value"
145145
context.baggage[TestK2.self] = "k2-value"
146146
context.baggage[TestK3.self] = "k3-value"
@@ -188,7 +188,7 @@ public let BaggageLoggingBenchmarks: [BenchmarkInfo] = [
188188
name: pad("BaggageLoggingBenchmarks.2_log_real-trace_loggerWithBaggage_small"),
189189
runFunction: { iters in
190190
let logger = Logger(label: "2_log_real-trace_loggerWithBaggage_small", factory: StreamLogHandler.standardError)
191-
var baggage = Baggage.background
191+
var baggage = Baggage.topLevel
192192
baggage[TestK1.self] = "k1-value"
193193
baggage[TestK2.self] = "k2-value"
194194
baggage[TestK3.self] = "k3-value"
@@ -203,7 +203,7 @@ public let BaggageLoggingBenchmarks: [BenchmarkInfo] = [
203203
BenchmarkInfo(
204204
name: pad("BaggageLoggingBenchmarks.2_log_real-trace_context_with_baggage_small"),
205205
runFunction: { iters in
206-
var context = DefaultContext.background(logger: Logger(label: "2_log_real-trace_context_with_baggage_small", factory: StreamLogHandler.standardError))
206+
var context = DefaultContext.topLevel(logger: Logger(label: "2_log_real-trace_context_with_baggage_small", factory: StreamLogHandler.standardError))
207207
context.baggage[TestK1.self] = "k1-value"
208208
context.baggage[TestK2.self] = "k2-value"
209209
context.baggage[TestK3.self] = "k3-value"
@@ -222,7 +222,7 @@ public let BaggageLoggingBenchmarks: [BenchmarkInfo] = [
222222
BenchmarkInfo(
223223
name: pad("BaggageLoggingBenchmarks.3_log_real_small_context_materializeOnce"),
224224
runFunction: { iters in
225-
var context = DefaultContext.background(logger: Logger(label: "3_log_real_context_materializeOnce", factory: StreamLogHandler.standardError))
225+
var context = DefaultContext.topLevel(logger: Logger(label: "3_log_real_context_materializeOnce", factory: StreamLogHandler.standardError))
226226
context.baggage[TestK1.self] = "k1-value"
227227
context.baggage[TestK2.self] = "k2-value"
228228
context.baggage[TestK3.self] = "k3-value"
@@ -237,7 +237,7 @@ public let BaggageLoggingBenchmarks: [BenchmarkInfo] = [
237237
BenchmarkInfo(
238238
name: pad("BaggageLoggingBenchmarks.3_log_real-trace_small_context_materializeOnce"),
239239
runFunction: { iters in
240-
var context = DefaultContext.background(logger: Logger(label: "3_log_real_context_materializeOnce", factory: StreamLogHandler.standardError))
240+
var context = DefaultContext.topLevel(logger: Logger(label: "3_log_real_context_materializeOnce", factory: StreamLogHandler.standardError))
241241
context.baggage[TestK1.self] = "k1-value"
242242
context.baggage[TestK2.self] = "k2-value"
243243
context.baggage[TestK3.self] = "k3-value"

Diff for: Sources/BaggageContextBenchmarks/BaggagePassingBenchmarks.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public let BaggagePassingBenchmarks: [BenchmarkInfo] = [
2222
BenchmarkInfo(
2323
name: "BaggagePassingBenchmarks.pass_async_empty_100_000 ",
2424
runFunction: { _ in
25-
let context = Baggage.background
25+
let context = Baggage.topLevel
2626
pass_async(context: context, times: 100_000)
2727
},
2828
tags: [],
@@ -32,7 +32,7 @@ public let BaggagePassingBenchmarks: [BenchmarkInfo] = [
3232
BenchmarkInfo(
3333
name: "BaggagePassingBenchmarks.pass_async_smol_100_000 ",
3434
runFunction: { _ in
35-
var context = Baggage.background
35+
var context = Baggage.topLevel
3636
context.k1 = "one"
3737
context.k2 = "two"
3838
context.k3 = "three"
@@ -46,7 +46,7 @@ public let BaggagePassingBenchmarks: [BenchmarkInfo] = [
4646
BenchmarkInfo(
4747
name: "BaggagePassingBenchmarks.pass_async_small_nonconst_100_000",
4848
runFunction: { _ in
49-
var context = Baggage.background
49+
var context = Baggage.topLevel
5050
context.k1 = "\(Int.random(in: 1 ... Int.max))"
5151
context.k2 = "\(Int.random(in: 1 ... Int.max))"
5252
context.k3 = "\(Int.random(in: 1 ... Int.max))"
@@ -66,7 +66,7 @@ public let BaggagePassingBenchmarks: [BenchmarkInfo] = [
6666
BenchmarkInfo(
6767
name: "BaggagePassingBenchmarks.pass_mut_async_small_100_000 ",
6868
runFunction: { _ in
69-
var context = Baggage.background
69+
var context = Baggage.topLevel
7070
context.k1 = "\(Int.random(in: 1 ... Int.max))"
7171
context.k2 = "\(Int.random(in: 1 ... Int.max))"
7272
context.k3 = "\(Int.random(in: 1 ... Int.max))"

Diff for: Tests/BaggageContextTests/BaggageContextTests.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import XCTest
1818

1919
final class BaggageContextTests: XCTestCase {
2020
func test_ExampleFrameworkContext_dumpBaggage() throws {
21-
var baggage = Baggage.background
21+
var baggage = Baggage.topLevel
2222
let logger = Logger(label: "TheLogger")
2323

2424
baggage.testID = 42
@@ -43,7 +43,7 @@ final class BaggageContextTests: XCTestCase {
4343
}
4444

4545
func test_ExampleMutableFrameworkContext_log_withBaggage() throws {
46-
let baggage = Baggage.background
46+
let baggage = Baggage.topLevel
4747
let logging = TestLogging()
4848
let logger = Logger(label: "TheLogger", factory: { label in logging.make(label: label) })
4949

@@ -66,7 +66,7 @@ final class BaggageContextTests: XCTestCase {
6666
}
6767

6868
func test_ExampleMutableFrameworkContext_log_prefersBaggageContextOverExistingLoggerMetadata() {
69-
let baggage = Baggage.background
69+
let baggage = Baggage.topLevel
7070
let logging = TestLogging()
7171
var logger = Logger(label: "TheLogger", factory: { label in logging.make(label: label) })
7272
logger[metadataKey: "secondIDExplicitlyNamed"] = "set on logger"
@@ -126,7 +126,7 @@ struct CoolFrameworkContext: BaggageContext.Context {
126126
return self._logger.with(self.baggage)
127127
}
128128

129-
var baggage: Baggage = .background
129+
var baggage: Baggage = .topLevel
130130

131131
// framework context defines other values as well
132132
let frameworkField: String = ""

Diff for: Tests/BaggageContextTests/FrameworkContextTests.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ final class FrameworkBaggageContextTests: XCTestCase {
4444
}
4545

4646
private struct TestFrameworkContext: Context {
47-
var baggage = Baggage.background
47+
var baggage = Baggage.topLevel
4848

4949
private var _logger = Logger(label: "test")
5050
var logger: Logger {

Diff for: Tests/BaggageTests/BaggageTests.swift

+7-7
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ final class BaggageTests: XCTestCase {
1818
func testSubscriptAccess() {
1919
let testID = 42
2020

21-
var baggage = Baggage.background
21+
var baggage = Baggage.topLevel
2222
XCTAssertNil(baggage[TestIDKey.self])
2323

2424
baggage[TestIDKey.self] = testID
@@ -31,7 +31,7 @@ final class BaggageTests: XCTestCase {
3131
func testRecommendedConvenienceExtension() {
3232
let testID = 42
3333

34-
var baggage = Baggage.background
34+
var baggage = Baggage.topLevel
3535
XCTAssertNil(baggage.testID)
3636

3737
baggage.testID = testID
@@ -42,18 +42,18 @@ final class BaggageTests: XCTestCase {
4242
}
4343

4444
func testEmptyBaggageDescription() {
45-
XCTAssertEqual(String(describing: Baggage.background), "Baggage(keys: [])")
45+
XCTAssertEqual(String(describing: Baggage.topLevel), "Baggage(keys: [])")
4646
}
4747

4848
func testSingleKeyBaggageDescription() {
49-
var baggage = Baggage.background
49+
var baggage = Baggage.topLevel
5050
baggage.testID = 42
5151

5252
XCTAssertEqual(String(describing: baggage), #"Baggage(keys: ["TestIDKey"])"#)
5353
}
5454

5555
func testMultiKeysBaggageDescription() {
56-
var baggage = Baggage.background
56+
var baggage = Baggage.topLevel
5757
baggage.testID = 42
5858
baggage[SecondTestIDKey.self] = "test"
5959

@@ -80,15 +80,15 @@ final class BaggageTests: XCTestCase {
8080
}
8181

8282
func test_todo_empty() {
83-
let context = Baggage.background
83+
let context = Baggage.topLevel
8484
_ = context // avoid "not used" warning
8585

8686
// TODO: Can't work with protocols; re-consider the entire carrier approach... Context being a Baggage + Logger, and a specific type.
8787
// static member 'empty' cannot be used on protocol metatype 'BaggageContextProtocol.Protocol'
8888
// func take(context: BaggageContextProtocol) {
8989
// _ = context // ignore
9090
// }
91-
// take(context: .background)
91+
// take(context: .topLevel)
9292
}
9393
}
9494

0 commit comments

Comments
 (0)