diff --git a/instrumentation/logback/logback-appender-1.0/library/src/main/java/io/opentelemetry/instrumentation/logback/appender/v1_0/internal/LoggingEventMapper.java b/instrumentation/logback/logback-appender-1.0/library/src/main/java/io/opentelemetry/instrumentation/logback/appender/v1_0/internal/LoggingEventMapper.java index 5ff92f6672b7..1fd6b4eda3b3 100644 --- a/instrumentation/logback/logback-appender-1.0/library/src/main/java/io/opentelemetry/instrumentation/logback/appender/v1_0/internal/LoggingEventMapper.java +++ b/instrumentation/logback/logback-appender-1.0/library/src/main/java/io/opentelemetry/instrumentation/logback/appender/v1_0/internal/LoggingEventMapper.java @@ -45,6 +45,7 @@ public final class LoggingEventMapper { private static final AttributeKey THREAD_ID = AttributeKey.longKey("thread.id"); private static final AttributeKey THREAD_NAME = AttributeKey.stringKey("thread.name"); + private static final boolean supportsInstant = supportsInstant(); private static final boolean supportsKeyValuePairs = supportsKeyValuePairs(); private static final boolean supportsMultipleMarkers = supportsMultipleMarkers(); private static final Cache> mdcAttributeKeys = Cache.bounded(100); @@ -106,8 +107,12 @@ private void mapLoggingEvent( } // time - long timestamp = loggingEvent.getTimeStamp(); - builder.setTimestamp(timestamp, TimeUnit.MILLISECONDS); + if (supportsInstant && hasInstant(loggingEvent)) { + setTimestampFromInstant(builder, loggingEvent); + } else { + long timestamp = loggingEvent.getTimeStamp(); + builder.setTimestamp(timestamp, TimeUnit.MILLISECONDS); + } // level Level level = loggingEvent.getLevel(); @@ -174,6 +179,28 @@ private void mapLoggingEvent( builder.setContext(Context.current()); } + // getInstant is available since Logback 1.3 + private static boolean supportsInstant() { + try { + ILoggingEvent.class.getMethod("getInstant"); + } catch (NoSuchMethodException e) { + return false; + } + + return true; + } + + @NoMuzzle + private static boolean hasInstant(ILoggingEvent loggingEvent) { + return loggingEvent.getInstant() != null; + } + + @NoMuzzle + private static void setTimestampFromInstant( + LogRecordBuilder builder, ILoggingEvent loggingEvent) { + builder.setTimestamp(loggingEvent.getInstant()); + } + // visible for testing void captureMdcAttributes(AttributesBuilder attributes, Map mdcProperties) { if (captureAllMdcAttributes) {