From 0c21b1791e9b3f3252249e53450cb347cc8a386a Mon Sep 17 00:00:00 2001 From: jason plumb <75337021+breedx-splk@users.noreply.github.com> Date: Thu, 13 Jan 2022 08:31:02 -0800 Subject: [PATCH] Connect AgentLogEmitterProvider global during OpenTelemetryInstaller (#5088) * during install, hook up the log emitter provider for instrumentation to use. * spotless * Fix tests * Default instrumentation name to ROOT when logger name null/empty Co-authored-by: Trask Stalnaker --- .../instrumentation/jul/JavaUtilLoggingHelper.java | 9 +++++---- .../log4j/appender/v1_2/Log4jHelper.java | 6 +++++- .../log4j/appender/v2_16/Log4jHelper.java | 7 +++++-- .../log4j/appender/v2_16/OpenTelemetryAppender.java | 10 +++++----- .../appender/v1_0/internal/LoggingEventMapper.java | 6 +++++- .../appender/internal/AgentLogEmitterProvider.java | 12 +++++++++++- .../javaagent/tooling/OpenTelemetryInstaller.java | 10 ++++++++++ .../testing/exporter/AgentTestingLogsCustomizer.java | 1 + 8 files changed, 47 insertions(+), 14 deletions(-) diff --git a/instrumentation/java-util-logging/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jul/JavaUtilLoggingHelper.java b/instrumentation/java-util-logging/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jul/JavaUtilLoggingHelper.java index 1747778cbc31..96ebe2cf981e 100644 --- a/instrumentation/java-util-logging/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jul/JavaUtilLoggingHelper.java +++ b/instrumentation/java-util-logging/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jul/JavaUtilLoggingHelper.java @@ -31,11 +31,12 @@ public static void capture(Logger logger, LogRecord logRecord) { return; } + String instrumentationName = logRecord.getLoggerName(); + if (instrumentationName == null || instrumentationName.isEmpty()) { + instrumentationName = "ROOT"; + } LogBuilder builder = - AgentLogEmitterProvider.get() - .logEmitterBuilder(logRecord.getLoggerName()) - .build() - .logBuilder(); + AgentLogEmitterProvider.get().logEmitterBuilder(instrumentationName).build().logBuilder(); mapLogRecord(builder, logRecord); builder.emit(); } diff --git a/instrumentation/log4j/log4j-appender-1.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/log4j/appender/v1_2/Log4jHelper.java b/instrumentation/log4j/log4j-appender-1.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/log4j/appender/v1_2/Log4jHelper.java index cc691f804dc8..7f2b0192d40c 100644 --- a/instrumentation/log4j/log4j-appender-1.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/log4j/appender/v1_2/Log4jHelper.java +++ b/instrumentation/log4j/log4j-appender-1.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/log4j/appender/v1_2/Log4jHelper.java @@ -24,8 +24,12 @@ public final class Log4jHelper { // TODO (trask) capture MDC public static void capture(Category logger, Priority level, Object message, Throwable throwable) { + String instrumentationName = logger.getName(); + if (instrumentationName == null || instrumentationName.isEmpty()) { + instrumentationName = "ROOT"; + } LogBuilder builder = - AgentLogEmitterProvider.get().logEmitterBuilder(logger.getName()).build().logBuilder(); + AgentLogEmitterProvider.get().logEmitterBuilder(instrumentationName).build().logBuilder(); // message if (message != null) { diff --git a/instrumentation/log4j/log4j-appender-2.16/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/log4j/appender/v2_16/Log4jHelper.java b/instrumentation/log4j/log4j-appender-2.16/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/log4j/appender/v2_16/Log4jHelper.java index 2a80a379d3ef..864e95d56955 100644 --- a/instrumentation/log4j/log4j-appender-2.16/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/log4j/appender/v2_16/Log4jHelper.java +++ b/instrumentation/log4j/log4j-appender-2.16/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/log4j/appender/v2_16/Log4jHelper.java @@ -23,9 +23,12 @@ public final class Log4jHelper { new LogEventMapper<>(ContextDataAccessorImpl.INSTANCE); public static void capture(Logger logger, Level level, Message message, Throwable throwable) { - + String instrumentationName = logger.getName(); + if (instrumentationName == null || instrumentationName.isEmpty()) { + instrumentationName = "ROOT"; + } LogBuilder builder = - AgentLogEmitterProvider.get().logEmitterBuilder(logger.getName()).build().logBuilder(); + AgentLogEmitterProvider.get().logEmitterBuilder(instrumentationName).build().logBuilder(); Map contextData = ThreadContext.getImmutableContext(); mapper.mapLogEvent(builder, message, level, throwable, null, contextData); builder.emit(); diff --git a/instrumentation/log4j/log4j-appender-2.16/library/src/main/java/io/opentelemetry/instrumentation/log4j/appender/v2_16/OpenTelemetryAppender.java b/instrumentation/log4j/log4j-appender-2.16/library/src/main/java/io/opentelemetry/instrumentation/log4j/appender/v2_16/OpenTelemetryAppender.java index f04c888591a2..d37248abd8dc 100644 --- a/instrumentation/log4j/log4j-appender-2.16/library/src/main/java/io/opentelemetry/instrumentation/log4j/appender/v2_16/OpenTelemetryAppender.java +++ b/instrumentation/log4j/log4j-appender-2.16/library/src/main/java/io/opentelemetry/instrumentation/log4j/appender/v2_16/OpenTelemetryAppender.java @@ -66,12 +66,12 @@ private OpenTelemetryAppender( @Override public void append(LogEvent event) { + String instrumentationName = event.getLoggerName(); + if (instrumentationName == null || instrumentationName.isEmpty()) { + instrumentationName = "ROOT"; + } LogBuilder builder = - logEmitterProviderHolder - .get() - .logEmitterBuilder(event.getLoggerName()) - .build() - .logBuilder(); + logEmitterProviderHolder.get().logEmitterBuilder(instrumentationName).build().logBuilder(); ReadOnlyStringMap contextData = event.getContextData(); mapper.mapLogEvent( builder, 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 14b7363f3eb9..cc9da1213743 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 @@ -53,8 +53,12 @@ private LoggingEventMapper() { } public void emit(LogEmitterProvider logEmitterProvider, ILoggingEvent event) { + String instrumentationName = event.getLoggerName(); + if (instrumentationName == null || instrumentationName.isEmpty()) { + instrumentationName = "ROOT"; + } LogBuilder builder = - logEmitterProvider.logEmitterBuilder(event.getLoggerName()).build().logBuilder(); + logEmitterProvider.logEmitterBuilder(instrumentationName).build().logBuilder(); mapLoggingEvent(builder, event); builder.emit(); } diff --git a/javaagent-instrumentation-api/src/main/java/io/opentelemetry/javaagent/instrumentation/api/appender/internal/AgentLogEmitterProvider.java b/javaagent-instrumentation-api/src/main/java/io/opentelemetry/javaagent/instrumentation/api/appender/internal/AgentLogEmitterProvider.java index 7313967c77a2..19110bc7b9ad 100644 --- a/javaagent-instrumentation-api/src/main/java/io/opentelemetry/javaagent/instrumentation/api/appender/internal/AgentLogEmitterProvider.java +++ b/javaagent-instrumentation-api/src/main/java/io/opentelemetry/javaagent/instrumentation/api/appender/internal/AgentLogEmitterProvider.java @@ -20,11 +20,21 @@ public static LogEmitterProvider get() { /** * Sets the {@link LogEmitterProvider} that should be used by the agent. Future calls to {@link * #get()} will return the provided {@link LogEmitterProvider} instance. It should only be called - * once - an attempt to call it a second time will result in an error. + * once - an attempt to call it a second time will result in an error. If trying to set the + * instance {@link LogEmitterProvider} multiple times in tests, use {@link + * LogEmitterProviderHolder#resetForTest()} between them. */ public static void set(LogEmitterProvider logEmitterProvider) { delegate.set(logEmitterProvider); } + /** + * Unsets the {@link LogEmitterProvider}. This is only meant to be used from tests which need to + * reconfigure {@link LogEmitterProvider}. + */ + public static void resetForTest() { + delegate.resetForTest(); + } + private AgentLogEmitterProvider() {} } diff --git a/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/OpenTelemetryInstaller.java b/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/OpenTelemetryInstaller.java index 653cd2ef6b23..558d0e2a5fe1 100644 --- a/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/OpenTelemetryInstaller.java +++ b/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/OpenTelemetryInstaller.java @@ -5,13 +5,17 @@ package io.opentelemetry.javaagent.tooling; +import io.opentelemetry.instrumentation.api.appender.internal.LogEmitterProvider; import io.opentelemetry.instrumentation.api.config.Config; +import io.opentelemetry.instrumentation.sdk.appender.internal.DelegatingLogEmitterProvider; import io.opentelemetry.javaagent.bootstrap.AgentInitializer; import io.opentelemetry.javaagent.instrumentation.api.OpenTelemetrySdkAccess; +import io.opentelemetry.javaagent.instrumentation.api.appender.internal.AgentLogEmitterProvider; import io.opentelemetry.sdk.OpenTelemetrySdk; import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk; import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdkBuilder; import io.opentelemetry.sdk.common.CompletableResultCode; +import io.opentelemetry.sdk.logs.SdkLogEmitterProvider; import java.util.Arrays; public class OpenTelemetryInstaller { @@ -47,6 +51,12 @@ static AutoConfiguredOpenTelemetrySdk installOpenTelemetrySdk(Config config) { .join(timeout, unit); }); + SdkLogEmitterProvider sdkLogEmitterProvider = + autoConfiguredSdk.getOpenTelemetrySdk().getSdkLogEmitterProvider(); + LogEmitterProvider logEmitterProvider = + DelegatingLogEmitterProvider.from(sdkLogEmitterProvider); + AgentLogEmitterProvider.set(logEmitterProvider); + return autoConfiguredSdk; } } diff --git a/testing/agent-exporter/src/main/java/io/opentelemetry/javaagent/testing/exporter/AgentTestingLogsCustomizer.java b/testing/agent-exporter/src/main/java/io/opentelemetry/javaagent/testing/exporter/AgentTestingLogsCustomizer.java index 9f62b0165398..6b0cb83c6f1b 100644 --- a/testing/agent-exporter/src/main/java/io/opentelemetry/javaagent/testing/exporter/AgentTestingLogsCustomizer.java +++ b/testing/agent-exporter/src/main/java/io/opentelemetry/javaagent/testing/exporter/AgentTestingLogsCustomizer.java @@ -28,6 +28,7 @@ public void beforeAgent( BatchLogProcessor.builder(AgentTestingExporterFactory.logExporter).build()) .build(); + AgentLogEmitterProvider.resetForTest(); AgentLogEmitterProvider.set(DelegatingLogEmitterProvider.from(logEmitterProvider)); } }