Skip to content

Commit

Permalink
Connect AgentLogEmitterProvider global during OpenTelemetryInstaller (o…
Browse files Browse the repository at this point in the history
…pen-telemetry#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 <[email protected]>
  • Loading branch information
2 people authored and RashmiRam committed May 23, 2022
1 parent cc533c6 commit 0c21b17
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> contextData = ThreadContext.getImmutableContext();
mapper.mapLogEvent(builder, message, level, throwable, null, contextData);
builder.emit();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public void beforeAgent(
BatchLogProcessor.builder(AgentTestingExporterFactory.logExporter).build())
.build();

AgentLogEmitterProvider.resetForTest();
AgentLogEmitterProvider.set(DelegatingLogEmitterProvider.from(logEmitterProvider));
}
}

0 comments on commit 0c21b17

Please sign in to comment.