Skip to content

Structured Logs: Buffering & flushing of logs #5628

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 25 commits into from
Jul 21, 2025
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
a3b5366
implement logs batcher
denrase Jul 14, 2025
dd30616
add prnr to changelog
denrase Jul 14, 2025
1398df9
Merge branch 'main' into feat/structured-logs-batcher
denrase Jul 16, 2025
61175bb
make test class final
denrase Jul 16, 2025
e481e7c
Merge branch 'main' into feat/structured-logs-batcher
denrase Jul 17, 2025
aca9fc4
reverse if
denrase Jul 17, 2025
fb3feae
refactor batch logic, add missing item count o envelope
denrase Jul 17, 2025
fa94e7a
use a cancelable work item
denrase Jul 17, 2025
5316d91
update tests for new logic
denrase Jul 17, 2025
be27e95
fix linter issues, refactor
denrase Jul 17, 2025
40c8ddb
fix test expectations
denrase Jul 17, 2025
58ccb02
Merge branch 'main' into feat/structured-logs-batcher
denrase Jul 18, 2025
99d1416
remove comment, remove copy of logs with direct access
denrase Jul 18, 2025
ead2dbe
add additional docs
denrase Jul 18, 2025
1554b47
simplify fush/dispatch condition
denrase Jul 18, 2025
33ae73c
add dispatch with workitem to wrapper, update tests
denrase Jul 18, 2025
7c0a081
more explicit warning for injected queue
denrase Jul 18, 2025
f7539bb
add debug messages
denrase Jul 18, 2025
fad5c54
Use internal wrapper queue
denrase Jul 18, 2025
e1d2409
Merge branch 'main' into feat/structured-logs-batcher
denrase Jul 21, 2025
6e0f43a
add comment and make internal
denrase Jul 21, 2025
a253552
increase diffmax
denrase Jul 21, 2025
3350c06
Merge branch 'main' into feat/structured-logs-batcher
denrase Jul 21, 2025
e461493
increase timeout in test
denrase Jul 21, 2025
8290c16
fix cl
denrase Jul 21, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions SentryTestUtils/TestClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ public class TestClient: SentryClient {
flushInvocations.record(timeout)
}

public var captureLogsDataInvocations = Invocations<Data>()
public override func captureLogsData(_ data: Data) {
captureLogsDataInvocations.record(data)
public var captureLogsDataInvocations = Invocations<(data: Data, count: NSNumber)>()
public override func captureLogsData(_ data: Data, with count: NSNumber) {
captureLogsDataInvocations.record((data, count))
}
}
14 changes: 14 additions & 0 deletions SentryTestUtils/TestSentryDispatchQueueWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,20 @@ import Foundation
dispatchAfterInvocations.invocations.last?.block()
}

public var dispatchAfterWorkItemInvocations = Invocations<(interval: TimeInterval, workItem: DispatchWorkItem)>()
public override func dispatch(after interval: TimeInterval, workItem: DispatchWorkItem) {
dispatchAfterWorkItemInvocations.record((interval, workItem))
if blockBeforeMainBlock() {
if dispatchAfterExecutesBlock {
workItem.perform()
}
}
}

public func invokeLastDispatchAfterWorkItem() {
dispatchAfterWorkItemInvocations.invocations.last?.workItem.perform()
}

public var dispatchCancelInvocations = 0
public override var shouldDispatchCancel: Bool {
dispatchCancelInvocations += 1
Expand Down
5 changes: 3 additions & 2 deletions Sources/Sentry/SentryClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -1128,12 +1128,13 @@ - (void)removeAttachmentProcessor:(id<SentryClientAttachmentProcessor>)attachmen
return processedAttachments;
}

- (void)captureLogsData:(NSData *)data
- (void)captureLogsData:(NSData *)data with:(NSNumber *)itemCount;
{
SentryEnvelopeItemHeader *header =
[[SentryEnvelopeItemHeader alloc] initWithType:SentryEnvelopeItemTypeLog
length:data.length
contentType:@"application/vnd.sentry.items.log+json"];
contentType:@"application/vnd.sentry.items.log+json"
itemCount:itemCount];

SentryEnvelopeItem *envelopeItem = [[SentryEnvelopeItem alloc] initWithHeader:header data:data];
SentryEnvelope *envelope = [[SentryEnvelope alloc] initWithHeader:[SentryEnvelopeHeader empty]
Expand Down
7 changes: 6 additions & 1 deletion Sources/Sentry/SentrySDK.m
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,19 @@ + (SentryReplayApi *)replay

+ (SentryLogger *)logger
{

@synchronized(currentLoggerLock) {
if (currentLogger == nil) {

SentryLogBatcher *batcher;
if (nil != currentHub.client && currentHub.client.options.experimental.enableLogs) {
batcher = [[SentryLogBatcher alloc] initWithClient:currentHub.client];
batcher = [[SentryLogBatcher alloc]
initWithClient:currentHub.client
dispatchQueue:SentryDependencyContainer.sharedInstance.dispatchQueueWrapper];
} else {
batcher = nil;
}

currentLogger = [[SentryLogger alloc]
initWithHub:currentHub
dateProvider:SentryDependencyContainer.sharedInstance.dateProvider
Expand Down
2 changes: 1 addition & 1 deletion Sources/Sentry/include/SentryClient+Logs.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ NS_ASSUME_NONNULL_BEGIN
/**
* Helper to capture encoded logs, as SentryEnvelope can't be used in the Swift SDK.
*/
- (void)captureLogsData:(NSData *)data;
- (void)captureLogsData:(NSData *)data with:(NSNumber *)itemCount;

@end

Expand Down
4 changes: 4 additions & 0 deletions Sources/Swift/Helper/SentryDispatchQueueWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
internalWrapper.dispatch(after: interval, block: block)
}

public func dispatch(after interval: TimeInterval, workItem: DispatchWorkItem) {
internalWrapper.queue.asyncAfter(deadline: .now() + interval, execute: workItem)
}

public func dispatchOnce(_ predicate: UnsafeMutablePointer<CLong>, block: @escaping () -> Void) {
internalWrapper.dispatchOnce(predicate, block: block)
}
Expand Down
126 changes: 117 additions & 9 deletions Sources/Swift/Tools/SentryLogBatcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,135 @@ import Foundation
@_spi(Private) public class SentryLogBatcher: NSObject {

private let client: SentryClient
private let flushTimeout: TimeInterval
private let maxBufferSizeBytes: Int
private let dispatchQueue: SentryDispatchQueueWrapper

let options: Options
internal let options: Options

// All mutable state is accessed from the same serial dispatch queue.

@_spi(Private) public init(client: SentryClient) {
// Every logs data is added sepratley. They are flushed together in an envelope.
private var encodedLogs: [Data] = []
private var encodedLogsSize: Int = 0
private var timerWorkItem: DispatchWorkItem?

/// Initializes a new SentryLogBatcher.
/// - Parameters:
/// - client: The SentryClient to use for sending logs
/// - flushTimeout: The timeout interval after which buffered logs will be flushed
/// - maxBufferSizeBytes: The maximum buffer size in bytes before triggering an immediate flush
/// - dispatchQueue: A **serial** dispatch queue wrapper for thread-safe access to mutable state
///
/// - Important: The `dispatchQueue` parameter MUST be a serial queue to ensure thread safety.
/// Passing a concurrent queue will result in undefined behavior and potential data races.
@_spi(Private) public init(
client: SentryClient,
flushTimeout: TimeInterval,
maxBufferSizeBytes: Int,
dispatchQueue: SentryDispatchQueueWrapper
) {
self.client = client
self.options = client.options
self.flushTimeout = flushTimeout
self.maxBufferSizeBytes = maxBufferSizeBytes
self.dispatchQueue = dispatchQueue
super.init()
}

func add(_ log: SentryLog) {
dispatch(logs: [log])
/// Convenience initializer with default flush timeout and buffer size.
/// - Parameters:
/// - client: The SentryClient to use for sending logs
/// - dispatchQueue: A **serial** dispatch queue wrapper for thread-safe access to mutable state
///
/// - Important: The `dispatchQueue` parameter MUST be a serial queue to ensure thread safety.
/// Passing a concurrent queue will result in undefined behavior and potential data races.
@_spi(Private) public convenience init(client: SentryClient, dispatchQueue: SentryDispatchQueueWrapper) {
self.init(
client: client,
flushTimeout: 5,
maxBufferSizeBytes: 1_024 * 1_024, // 1MB
dispatchQueue: dispatchQueue
)
}

@_spi(Private) func add(_ log: SentryLog) {
dispatchQueue.dispatchAsync { [weak self] in
self?.encodeAndBuffer(log: log)
}
}

private func dispatch(logs: [SentryLog]) {
@objc
@_spi(Private) func flush() {
dispatchQueue.dispatchAsync { [weak self] in
self?.performFlush()
}
}

// Helper

// Only ever call this from the serial dispatch queue.
private func encodeAndBuffer(log: SentryLog) {
do {
let payload = ["items": logs]
let data = try encodeToJSONData(data: payload)
let encodedLog = try encodeToJSONData(data: log)

let encodedLogsWereEmpty = encodedLogs.isEmpty

client.captureLogsData(data)
encodedLogs.append(encodedLog)
encodedLogsSize += encodedLog.count

if encodedLogsSize >= maxBufferSizeBytes {
performFlush()
} else if encodedLogsWereEmpty && timerWorkItem == nil {
startTimer()
}
} catch {
SentrySDKLog.error("Failed to create logs envelope.")
SentrySDKLog.error("Failed to encode log: \(error)")
}
}

// Only ever call this from the serial dispatch queue.
private func startTimer() {
let timerWorkItem = DispatchWorkItem { [weak self] in
SentrySDKLog.debug("SentryLogBatcher: Timer fired, calling performFlush().")
self?.performFlush()
}
self.timerWorkItem = timerWorkItem
dispatchQueue.dispatch(after: flushTimeout, workItem: timerWorkItem)
}

// Only ever call this from the serial dispatch queue.
private func performFlush() {
// Reset logs on function exit
defer {
encodedLogs.removeAll()
encodedLogsSize = 0
}

// Reset timer state
timerWorkItem?.cancel()
timerWorkItem = nil

guard encodedLogs.count > 0 else {
SentrySDKLog.debug("SentryLogBatcher: No logs to flush.")
return
}

// Create the payload.

var payloadData = Data()
payloadData.append(Data("{\"items\":[".utf8))
let separator = Data(",".utf8)
for (index, encodedLog) in encodedLogs.enumerated() {
if index > 0 {
payloadData.append(separator)
}
payloadData.append(encodedLog)
}
payloadData.append(Data("]}".utf8))

// Send the payload.

client.captureLogsData(payloadData, with: NSNumber(value: encodedLogs.count))
}
}
5 changes: 3 additions & 2 deletions Tests/SentryTests/SentryClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2098,7 +2098,7 @@ class SentryClientTest: XCTestCase {
let sut = fixture.getSut()
let logData = Data("{\"items\":[{\"timestamp\":1627846801,\"level\":\"info\",\"body\":\"Test log message\"}]}".utf8)

sut.captureLogsData(logData)
sut.captureLogsData(logData, with: NSNumber(value: 1))

// Verify that an envelope was sent
XCTAssertEqual(1, fixture.transport.sentEnvelopes.count)
Expand All @@ -2114,6 +2114,7 @@ class SentryClientTest: XCTestCase {
XCTAssertEqual("log", item.header.type)
XCTAssertEqual(UInt(logData.count), item.header.length)
XCTAssertEqual("application/vnd.sentry.items.log+json", item.header.contentType)
XCTAssertEqual(NSNumber(value: 1), item.header.itemCount)

// Verify the envelope item data
XCTAssertEqual(logData, item.data)
Expand All @@ -2123,7 +2124,7 @@ class SentryClientTest: XCTestCase {
let sut = fixture.getSutDisabledSdk()
let logData = Data("{\"items\":[{\"timestamp\":1627846801,\"level\":\"info\",\"body\":\"Test log message\"}]}".utf8)

sut.captureLogsData(logData)
sut.captureLogsData(logData, with: NSNumber(value: 1))

// Verify that no envelope was sent when client is disabled
XCTAssertEqual(0, fixture.transport.sentEnvelopes.count)
Expand Down
Loading
Loading