Skip to content

Commit d3aae15

Browse files
committed
use json format for spans for elastic logging option
1 parent f243106 commit d3aae15

File tree

3 files changed

+35
-14
lines changed

3 files changed

+35
-14
lines changed

internal-logging/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ dependencies {
1212
compileOnly("io.opentelemetry.javaagent:opentelemetry-javaagent-bootstrap")
1313
compileOnly("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure-spi")
1414
compileOnly("io.opentelemetry:opentelemetry-exporter-logging")
15+
compileOnly("io.opentelemetry:opentelemetry-exporter-logging-otlp")
1516
compileOnly(libs.slf4j.api)
1617
implementation(libs.bundles.log4j2) {
1718
// Workaround for https://github.com/apache/logging-log4j2/issues/3754

internal-logging/src/main/java/co/elastic/otel/logging/AgentLog.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import static java.util.Collections.emptyList;
2222

2323
import io.opentelemetry.exporter.logging.LoggingSpanExporter;
24+
import io.opentelemetry.exporter.logging.otlp.OtlpJsonLoggingSpanExporter;
2425
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
2526
import io.opentelemetry.sdk.common.CompletableResultCode;
2627
import io.opentelemetry.sdk.trace.SdkTracerProviderBuilder;
@@ -45,16 +46,32 @@ public class AgentLog {
4546
/** root logger is an empty string */
4647
private static final String ROOT_LOGGER_NAME = "";
4748

49+
private static boolean logPlainText = false;
50+
4851
/**
4952
* debug span logging exporter that can be controlled at runtime, only used when logging span
5053
* exporter has not been explicitly configured.
5154
*/
52-
private static final DebugLogSpanExporter debugLogSpanExporter =
53-
new DebugLogSpanExporter(LoggingSpanExporter.create());
55+
private static DebugLogSpanExporter debugLogSpanExporter = null;
5456

5557
private AgentLog() {}
5658

57-
public static void init() {
59+
/**
60+
* Initializes agent logging
61+
*
62+
* @param usePlainTextLog {@literal true} to use plain text logging, `{@literal false} to use JSON
63+
* @param initialLevel initial log level to configure
64+
*/
65+
public static void init(boolean usePlainTextLog, Level initialLevel) {
66+
internalInit();
67+
setLevel(initialLevel);
68+
logPlainText = usePlainTextLog;
69+
debugLogSpanExporter =
70+
new DebugLogSpanExporter(
71+
logPlainText ? LoggingSpanExporter.create() : OtlpJsonLoggingSpanExporter.create());
72+
}
73+
74+
private static void internalInit() {
5875

5976
ConfigurationBuilder<BuiltConfiguration> conf =
6077
ConfigurationBuilderFactory.newConfigurationBuilder();
@@ -74,8 +91,11 @@ public static void addSpanLoggingIfRequired(
7491
// Replicate behavior of the upstream agent: span logging exporter is automatically added when
7592
// not already present when debugging. When logging exporter has been explicitly configured,
7693
// spans logging will be done by the explicitly configured logging exporter instance.
94+
95+
String exporterName = logPlainText ? "logging" : "otlp-logging";
96+
7797
boolean loggingExporterNotAlreadyConfigured =
78-
!config.getList("otel.traces.exporter", emptyList()).contains("logging");
98+
!config.getList("otel.traces.exporter", emptyList()).contains(exporterName);
7999
if (loggingExporterNotAlreadyConfigured) {
80100
providerBuilder.addSpanProcessor(SimpleSpanProcessor.create(debugLogSpanExporter));
81101
}
@@ -114,7 +134,7 @@ public static void setLevel(String level) {
114134
*
115135
* @param level log level
116136
*/
117-
public static void setLevel(Level level) {
137+
public static synchronized void setLevel(Level level) {
118138
// Using log4j2 implementation allows to change the log level programmatically at runtime
119139
// which is not directly possible through the slf4j API and simple implementation used in
120140
// upstream distribution.

internal-logging/src/main/java/co/elastic/otel/logging/ElasticLoggingCustomizer.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import io.opentelemetry.javaagent.bootstrap.InternalLogger;
2323
import io.opentelemetry.javaagent.tooling.LoggingCustomizer;
2424
import io.opentelemetry.javaagent.tooling.config.EarlyInitAgentConfig;
25+
import java.util.Optional;
2526
import org.apache.logging.log4j.Level;
2627
import org.slf4j.LoggerFactory;
2728

@@ -43,20 +44,19 @@ public void init(EarlyInitAgentConfig earlyConfig) {
4344
// make the agent internal logger delegate to slf4j, which will delegate to log4j
4445
InternalLogger.initialize(Slf4jInternalLogger::create);
4546

46-
AgentLog.init();
47-
48-
Level level = null;
49-
if (earlyConfig.getBoolean(AgentLog.OTEL_JAVAAGENT_DEBUG, false)) {
47+
boolean upstreamDebugEnabled = earlyConfig.getBoolean(AgentLog.OTEL_JAVAAGENT_DEBUG, false);
48+
Level level;
49+
if (upstreamDebugEnabled) {
5050
// set debug logging when enabled through configuration to behave like the upstream
5151
// distribution
5252
level = Level.DEBUG;
5353
} else {
54-
String levelConfig = earlyConfig.getString("elastic.otel.javaagent.log.level");
55-
if (levelConfig != null) {
56-
level = Level.getLevel(levelConfig);
57-
}
54+
level =
55+
Optional.ofNullable(earlyConfig.getString("elastic.otel.javaagent.log.level"))
56+
.map(Level::getLevel)
57+
.orElse(Level.INFO);
5858
}
59-
AgentLog.setLevel(level != null ? level : Level.INFO);
59+
AgentLog.init(upstreamDebugEnabled, level);
6060
}
6161

6262
@Override

0 commit comments

Comments
 (0)