Skip to content
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

fix: timestampNanos nanos precision. #2003

Closed
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,10 @@ protected Map<String, Object> toJsonMap(ILoggingEvent event) {
map.put(
StackdriverTraceConstants.TIMESTAMP_SECONDS_ATTRIBUTE,
TimeUnit.MILLISECONDS.toSeconds(event.getTimeStamp()));
int nanoseconds = event.getNanoseconds();
map.put(
StackdriverTraceConstants.TIMESTAMP_NANOS_ATTRIBUTE,
TimeUnit.MILLISECONDS.toNanos(event.getTimeStamp() % 1_000));
nanoseconds == -1 ? TimeUnit.MILLISECONDS.toNanos(event.getTimeStamp() % 1_000) : nanoseconds);
}

add(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,22 @@ void testJsonSeverityLevelMapping() {
.containsExactly("DEBUG", "DEBUG", "INFO", "WARNING", "ERROR", "ERROR");
}

@Test
void testTimestampNanos() {
LOGGER.warn("test1");
LOGGER.warn("test2");

List<String> jsonLogRecords = Arrays.asList(new String(logOutput.toByteArray()).split("\n"));

List<Double> logTimestampNanos =
jsonLogRecords.stream()
.map(record -> GSON.fromJson(record, Map.class))
.map(data -> (Double) data.get(StackdriverTraceConstants.TIMESTAMP_NANOS_ATTRIBUTE))
.collect(Collectors.toList());

assertThat(logTimestampNanos).anySatisfy(nanos -> assertThat(nanos % 1000000).isGreaterThan(0));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't nanos be 0 though?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I wanted to create test to ensure that there is nano precision and not just mili.
I am testing 2 logs with any greater than 0 to lower false negative possibility (when all micros and nanos are 0) to 1:1e+12.

Not sure if you like it this way or maybe just simple 1 log with nanos greaterOrEqual(0).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the way you have it is fine. As you point out, the likelihood of flakiness due to this is tiny. You might just want to make sure you are getting 2 timestamps in the array. You can even make the number of logs a variable.

}

@Test
void testJsonLayoutEnhancer_missing() {
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
Expand Down