BaggageKey

public protocol BaggageKey

BaggageKeys are used as keys in a Baggage. Their associated type Value guarantees type-safety. To give your BaggageKey an explicit name you may override the name property.

In general, BaggageKeys should be internal or private to the part of a system using it.

All access to baggage items should be performed through an accessor computed property defined as shown below:

/// The Key type should be internal (or private).
enum TestIDKey: Baggage.Key {
    typealias Value = String
    static var nameOverride: String? { "test-id" }
}

extension Baggage {
    /// This is some useful property documentation.
    public internal(set) var testID: String? {
        get {
            self[TestIDKey.self]
        }
        set {
            self[TestIDKey.self] = newValue
        }
    }
}

This pattern allows library authors fine-grained control over which values may be set, and whic only get by end-users.

  • The type of Value uniquely identified by this key.

    Declaration

    Swift

    associatedtype Value
  • nameOverride Default implementation

    The human-readable name of this key. This name will be used instead of the type name when a value is printed.

    It MAY also be picked up by an instrument (from Swift Tracing) which serializes baggage items and e.g. used as header name for carried metadata. Though generally speaking header names are NOT required to use the nameOverride, and MAY use their well known names for header names etc, as it depends on the specific transport and instrument used.

    For example, a baggage key representing the W3C “trace-state” header may want to return “trace-state” here, in order to achieve a consistent look and feel of this baggage item throughout logging and tracing systems.

    Defaults to nil.

    Default Implementation

    Declaration

    Swift

    static var nameOverride: String? { get }