Skip to content

Commit

Permalink
fix: Add logging timestamp for log4j1 appender instrument
Browse files Browse the repository at this point in the history
  • Loading branch information
Pattarapon Kittisrisawai committed Aug 25, 2023
1 parent 660be72 commit 6057861
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.time.Instant;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -107,6 +108,7 @@ public void capture(Category logger, Priority level, Object message, Throwable t
// span context
builder.setContext(Context.current());

builder.setTimestamp(Instant.now());
builder.emit();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.satisfies;
import static java.util.concurrent.TimeUnit.MILLISECONDS;

import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.logs.Severity;
Expand All @@ -17,6 +18,7 @@
import io.opentelemetry.sdk.logs.data.LogRecordData;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import java.lang.reflect.Field;
import java.time.Instant;
import java.util.stream.Stream;
import org.apache.log4j.Logger;
import org.apache.log4j.MDC;
Expand Down Expand Up @@ -79,6 +81,8 @@ private static void test(
String expectedSeverityText)
throws InterruptedException {

Instant start = Instant.now();

// when
if (withParent) {
testing.runWithSpan(
Expand All @@ -99,6 +103,11 @@ private static void test(
.hasInstrumentationScope(InstrumentationScopeInfo.builder(expectedLoggerName).build())
.hasSeverity(expectedSeverity)
.hasSeverityText(expectedSeverityText);

assertThat(log.getTimestampEpochNanos())
.isGreaterThanOrEqualTo(MILLISECONDS.toNanos(start.toEpochMilli()))
.isLessThanOrEqualTo(MILLISECONDS.toNanos(Instant.now().toEpochMilli()));

if (logException) {
assertThat(log)
.hasAttributesSatisfyingExactly(
Expand Down

0 comments on commit 6057861

Please sign in to comment.