Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connect AgentLogEmitterProvider global during OpenTelemetryInstaller #5088

Merged
merged 4 commits into from
Jan 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
}
}