Skip to content

Commit 3865382

Browse files
committed
Trying to get more accurate timestamps when one's not there
1 parent 57c93a9 commit 3865382

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

Agent/Utilities/NRAutoLogCollector.m

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
int saved_stdout;
1313
int saved_stderr;
14+
FILE* fileDescriptor;
1415

1516
@interface NRAutoLogCollector()
1617

@@ -38,7 +39,9 @@ + (void) redirectStandardOutputAndError {
3839

3940
// Redirect stdout to the file
4041
freopen([[NRAutoLogCollector logFileURL].path cStringUsingEncoding:NSUTF8StringEncoding], "a+", stdout);
41-
freopen([[NRAutoLogCollector logFileURL].path cStringUsingEncoding:NSUTF8StringEncoding], "a+", stderr);
42+
fileDescriptor = freopen([[NRAutoLogCollector logFileURL].path cStringUsingEncoding:NSUTF8StringEncoding], "a+", stderr);
43+
44+
[NRAutoLogCollector monitorFile:[NRAutoLogCollector logFileURL].path];
4245
}
4346

4447
+ (void) restoreStandardOutputAndError {
@@ -91,10 +94,9 @@ + (BOOL) isValidTimestamp:(NSString *) timestampString {
9194

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

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

100102
if (match) {
@@ -107,9 +109,14 @@ + (NSNumber *) extractTimestamp:(NSString *) inputString {
107109
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
108110
formatter.numberStyle = NSNumberFormatterDecimalStyle;
109111
NSNumber* originalTimestamp = [formatter numberFromString:timestampString];
110-
double timestampInSeconds = [originalTimestamp doubleValue];
111-
long long timestampInMilliseconds = (long long)(timestampInSeconds * 1000);
112-
return [NSNumber numberWithLongLong:timestampInMilliseconds];
112+
// If the timestamp has a decimal it is in second format, convert it to milliseconds.
113+
if([timestampString containsString:@"."]){
114+
double timestampInSeconds = [originalTimestamp doubleValue];
115+
long long timestampInMilliseconds = (long long)(timestampInSeconds * 1000);
116+
return [NSNumber numberWithLongLong:timestampInMilliseconds];
117+
} else {
118+
return originalTimestamp;
119+
}
113120
}
114121
}
115122

@@ -142,5 +149,28 @@ + (unsigned int) extractType:(NSString *) inputString {
142149
return NRLogLevelNone;
143150
}
144151

152+
+ (void) monitorFile:(NSString *) filePath {
153+
// Create a dispatch queue for handling log file events
154+
dispatch_queue_t queue = dispatch_queue_create("newrelic.log.monitor.queue", NULL);
155+
156+
// Create a dispatch source to monitor the file descriptor for writes
157+
dispatch_source_t source = dispatch_source_create(DISPATCH_SOURCE_TYPE_VNODE, fileno(fileDescriptor), DISPATCH_VNODE_WRITE, queue);
158+
159+
// Set the event handler block
160+
dispatch_source_set_event_handler(source, ^{
161+
unsigned long flags = dispatch_source_get_data(source);
162+
if (flags & DISPATCH_VNODE_WRITE) {
163+
[NRAutoLogCollector readAndParseLogFile];
164+
}
165+
});
166+
167+
// Set the cancel handler block
168+
dispatch_source_set_cancel_handler(source, ^{
169+
close(fileno(fileDescriptor));
170+
});
171+
172+
// Start monitoring
173+
dispatch_resume(source);
174+
}
145175

146176
@end

Agent/Utilities/NRLogger.m

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,6 @@ - (void)setLogURL:(NSString*)url {
531531

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

0 commit comments

Comments
 (0)