-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathMultiplexInstrument.swift
49 lines (43 loc) · 1.86 KB
/
MultiplexInstrument.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Distributed Tracing open source project
//
// Copyright (c) 2020-2023 Apple Inc. and the Swift Distributed Tracing project
// authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import InstrumentationBaggage
/// A pseudo-``InstrumentProtocol`` that may be used to instrument using multiple other ``InstrumentProtocol``s across a
/// common `Baggage`.
public struct MultiplexInstrument {
private var instruments: [InstrumentProtocol]
/// Create a ``MultiplexInstrument``.
///
/// - Parameter instruments: An array of ``InstrumentProtocol``s, each of which will be used to ``InstrumentProtocol/inject(_:into:using:)`` or ``InstrumentProtocol/extract(_:into:using:)``
/// through the same `Baggage`.
public init(_ instruments: [InstrumentProtocol]) {
self.instruments = instruments
}
}
extension MultiplexInstrument {
func firstInstrument(where predicate: (InstrumentProtocol) -> Bool) -> InstrumentProtocol? {
self.instruments.first(where: predicate)
}
}
extension MultiplexInstrument: InstrumentProtocol {
public func inject<Carrier, Inject>(_ baggage: Baggage, into carrier: inout Carrier, using injector: Inject)
where Inject: Injector, Carrier == Inject.Carrier
{
self.instruments.forEach { $0.inject(baggage, into: &carrier, using: injector) }
}
public func extract<Carrier, Extract>(_ carrier: Carrier, into baggage: inout Baggage, using extractor: Extract)
where Extract: Extractor, Carrier == Extract.Carrier
{
self.instruments.forEach { $0.extract(carrier, into: &baggage, using: extractor) }
}
}