Skip to content

Commit

Permalink
Trying to get more accurate timestamps when one's not there
Browse files Browse the repository at this point in the history
  • Loading branch information
mbruin-NR committed Oct 21, 2024
1 parent 57c93a9 commit 3865382
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
42 changes: 36 additions & 6 deletions Agent/Utilities/NRAutoLogCollector.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

int saved_stdout;
int saved_stderr;
FILE* fileDescriptor;

@interface NRAutoLogCollector()

Expand Down Expand Up @@ -38,7 +39,9 @@ + (void) redirectStandardOutputAndError {

// Redirect stdout to the file
freopen([[NRAutoLogCollector logFileURL].path cStringUsingEncoding:NSUTF8StringEncoding], "a+", stdout);
freopen([[NRAutoLogCollector logFileURL].path cStringUsingEncoding:NSUTF8StringEncoding], "a+", stderr);
fileDescriptor = freopen([[NRAutoLogCollector logFileURL].path cStringUsingEncoding:NSUTF8StringEncoding], "a+", stderr);

[NRAutoLogCollector monitorFile:[NRAutoLogCollector logFileURL].path];
}

+ (void) restoreStandardOutputAndError {
Expand Down Expand Up @@ -91,10 +94,9 @@ + (BOOL) isValidTimestamp:(NSString *) timestampString {

+ (NSNumber *) extractTimestamp:(NSString *) inputString {
// Define the regular expression pattern to match the t: value
NSString *pattern = @"t:(\\d+\\.\\d+)";
NSString *pattern = @"t:(\\d+(\\.\\d+)?)";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:nil];

// Find matches in the input string
NSTextCheckingResult *match = [regex firstMatchInString:inputString options:0 range:NSMakeRange(0, [inputString length])];

if (match) {
Expand All @@ -107,9 +109,14 @@ + (NSNumber *) extractTimestamp:(NSString *) inputString {
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.numberStyle = NSNumberFormatterDecimalStyle;
NSNumber* originalTimestamp = [formatter numberFromString:timestampString];
double timestampInSeconds = [originalTimestamp doubleValue];
long long timestampInMilliseconds = (long long)(timestampInSeconds * 1000);
return [NSNumber numberWithLongLong:timestampInMilliseconds];
// If the timestamp has a decimal it is in second format, convert it to milliseconds.
if([timestampString containsString:@"."]){
double timestampInSeconds = [originalTimestamp doubleValue];
long long timestampInMilliseconds = (long long)(timestampInSeconds * 1000);
return [NSNumber numberWithLongLong:timestampInMilliseconds];
} else {
return originalTimestamp;
}
}
}

Expand Down Expand Up @@ -142,5 +149,28 @@ + (unsigned int) extractType:(NSString *) inputString {
return NRLogLevelNone;
}

+ (void) monitorFile:(NSString *) filePath {
// Create a dispatch queue for handling log file events
dispatch_queue_t queue = dispatch_queue_create("newrelic.log.monitor.queue", NULL);

// Create a dispatch source to monitor the file descriptor for writes
dispatch_source_t source = dispatch_source_create(DISPATCH_SOURCE_TYPE_VNODE, fileno(fileDescriptor), DISPATCH_VNODE_WRITE, queue);

// Set the event handler block
dispatch_source_set_event_handler(source, ^{
unsigned long flags = dispatch_source_get_data(source);
if (flags & DISPATCH_VNODE_WRITE) {
[NRAutoLogCollector readAndParseLogFile];
}
});

// Set the cancel handler block
dispatch_source_set_cancel_handler(source, ^{
close(fileno(fileDescriptor));
});

// Start monitoring
dispatch_resume(source);
}

@end
1 change: 0 additions & 1 deletion Agent/Utilities/NRLogger.m
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,6 @@ - (void)setLogURL:(NSString*)url {

// Enqueue an upload task for this specific logData , represented by the "formattedData" below.
- (void)enqueueLogUpload {
[NRAutoLogCollector readAndParseLogFile];
@synchronized(self) {
if (self->logFile) {

Expand Down

0 comments on commit 3865382

Please sign in to comment.