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

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

### Features

- Add experimental support for capturing structured logs via `SentrySDK.logger` (#5532)
- Add experimental support for capturing structured logs via `SentrySDK.logger` (#5532, #5593)

### Improvements

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
82 changes: 80 additions & 2 deletions Sources/Swift/Tools/SentryLogBatcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,92 @@ import Foundation
@_spi(Private) public class SentryLogBatcher: NSObject {

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

@_spi(Private) public init(client: SentryClient) {
private var logBuffer: [SentryLog] = []
private let logBufferLock = NSLock()
private var currentFlushId: UUID?

@_spi(Private) public init(
client: SentryClient,
flushTimeout: TimeInterval,
maxBufferSize: Int,
dispatchQueue: SentryDispatchQueueWrapper
) {
self.client = client
self.flushTimeout = flushTimeout
self.maxBufferSize = maxBufferSize
self.dispatchQueue = dispatchQueue
super.init()
}

@_spi(Private) public convenience init(client: SentryClient, dispatchQueue: SentryDispatchQueueWrapper) {
self.init(
client: client,
flushTimeout: 5,
maxBufferSize: 100,
dispatchQueue: dispatchQueue
)
}

func add(_ log: SentryLog) {
dispatch(logs: [log])
cancelFlush()

let shouldFlush = logBufferLock.synchronized {
logBuffer.append(log)
return logBuffer.count >= maxBufferSize
}

if !shouldFlush {
scheduleFlush()
} else {
flush()
}
}

@objc
public func flush() {
cancelFlush()

let logs = logBufferLock.synchronized {
let logs = Array(logBuffer)
logBuffer.removeAll()
return logs
}

if !logs.isEmpty {
dispatch(logs: logs)
}
}

private func scheduleFlush() {
let flushId = UUID()

logBufferLock.synchronized {
currentFlushId = flushId
}

dispatchQueue.dispatch(after: flushTimeout) { [weak self] in
self?.executeFlushIfMatching(flushId: flushId)
}
}

private func executeFlushIfMatching(flushId: UUID) {
let shouldFlush = logBufferLock.synchronized {
return currentFlushId == flushId
}

if shouldFlush {
flush()
}
}

private func cancelFlush() {
logBufferLock.synchronized {
currentFlushId = nil
}
}

private func dispatch(logs: [SentryLog]) {
Expand Down
Loading
Loading