Skip to content

Commit

Permalink
fix: timestampNanos field with nanos precision (#2012)
Browse files Browse the repository at this point in the history
Fixes: #1996
  • Loading branch information
Raf0 authored Jul 10, 2023
1 parent ceff216 commit 2a4c14a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
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 ? nanoseconds : TimeUnit.MILLISECONDS.toNanos(event.getTimeStamp() % 1_000));
}

add(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,23 @@ 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).hasSize(2);
assertThat(logTimestampNanos).anySatisfy(nanos -> assertThat(nanos % 1000000).isPositive());
}

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

0 comments on commit 2a4c14a

Please sign in to comment.