-
Notifications
You must be signed in to change notification settings - Fork 10
/
OTelInMemoryLogRecordProcessor.swift
44 lines (36 loc) · 1.46 KB
/
OTelInMemoryLogRecordProcessor.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
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift OTel open source project
//
// Copyright (c) 2024 Moritz Lang and the Swift OTel project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import NIOConcurrencyHelpers
@_spi(Logging) import OTel
/// An in-memory log record processor, collecting emitted log records into ``onEmit(_:)``.
package final class OTelInMemoryLogRecordProcessor: OTelLogRecordProcessor {
package var records: [OTelLogRecord] { _records.withLockedValue { $0 } }
private let _records = NIOLockedValueBox([OTelLogRecord]())
private let _numberOfShutdowns = NIOLockedValueBox(0)
private let _numberOfForceFlushes = NIOLockedValueBox(0)
private let stream: AsyncStream<Void>
private let continuation: AsyncStream<Void>.Continuation
package init() {
(stream, continuation) = AsyncStream.makeStream()
}
package func run() async throws {
for await _ in stream.cancelOnGracefulShutdown() {}
_numberOfShutdowns.withLockedValue { $0 += 1 }
}
package func onEmit(_ record: OTelLogRecord) {
_records.withLockedValue { $0.append(record) }
}
package func forceFlush() async throws {
_numberOfForceFlushes.withLockedValue { $0 += 1 }
}
}