You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@beam.apache.org by yh...@apache.org on 2023/05/26 02:46:25 UTC
[beam] branch master updated: Structured logging support - Java SDK (#26848)
This is an automated email from the ASF dual-hosted git repository.
yhu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/beam.git
The following commit(s) were added to refs/heads/master by this push:
new fc5f2f51350 Structured logging support - Java SDK (#26848)
fc5f2f51350 is described below
commit fc5f2f5135071757df1aef180633d1b4b9fa4c2d
Author: Udi Meiri <ud...@users.noreply.github.com>
AuthorDate: Thu May 25 19:46:12 2023 -0700
Structured logging support - Java SDK (#26848)
* Add support for structured logging
Benchmark shows negligible performance changes.
Part of #26460
---
.../apache/beam/sdk/options/SdkHarnessOptions.java | 10 ++++++++++
.../jmh/logging/BeamFnLoggingClientBenchmark.java | 14 ++++++++++++++
.../fn/harness/logging/BeamFnLoggingClient.java | 22 ++++++++++++++++++++++
.../harness/logging/BeamFnLoggingClientTest.java | 6 ++++++
4 files changed, 52 insertions(+)
diff --git a/sdks/java/core/src/main/java/org/apache/beam/sdk/options/SdkHarnessOptions.java b/sdks/java/core/src/main/java/org/apache/beam/sdk/options/SdkHarnessOptions.java
index 2771eb20eee..70ba18e614c 100644
--- a/sdks/java/core/src/main/java/org/apache/beam/sdk/options/SdkHarnessOptions.java
+++ b/sdks/java/core/src/main/java/org/apache/beam/sdk/options/SdkHarnessOptions.java
@@ -99,6 +99,16 @@ public interface SdkHarnessOptions extends PipelineOptions {
void setSdkHarnessLogLevelOverrides(SdkHarnessLogLevelOverrides value);
+ /** Whether to include SLF4J MDC in log entries. */
+ @Description(
+ "This option controls whether SLF4J MDC keys and values will be appended to log entries. "
+ + "This used by Beam to add structured data to log entries, such as quota events and "
+ + "return statuses.")
+ @Default.Boolean(true)
+ boolean getLogMdc();
+
+ void setLogMdc(boolean value);
+
/**
* Size (in MB) of each grouping table used to pre-combine elements. Larger values may reduce the
* amount of data shuffled. If unset, defaults to 100 MB.
diff --git a/sdks/java/harness/jmh/src/main/java/org/apache/beam/fn/harness/jmh/logging/BeamFnLoggingClientBenchmark.java b/sdks/java/harness/jmh/src/main/java/org/apache/beam/fn/harness/jmh/logging/BeamFnLoggingClientBenchmark.java
index 3719460e531..3732bdfb189 100644
--- a/sdks/java/harness/jmh/src/main/java/org/apache/beam/fn/harness/jmh/logging/BeamFnLoggingClientBenchmark.java
+++ b/sdks/java/harness/jmh/src/main/java/org/apache/beam/fn/harness/jmh/logging/BeamFnLoggingClientBenchmark.java
@@ -43,6 +43,7 @@ import org.openjdk.jmh.annotations.TearDown;
import org.openjdk.jmh.annotations.Threads;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.slf4j.MDC;
/** Benchmarks for {@link BeamFnLoggingClient}. */
public class BeamFnLoggingClientBenchmark {
@@ -192,6 +193,19 @@ public class BeamFnLoggingClientBenchmark {
BeamFnLoggingMDC.setInstructionId(null);
}
+ @Benchmark
+ @Threads(16) // Use several threads since we expect contention during logging
+ public void testLoggingWithCustomData(
+ ManyExpectedCallsLoggingClientAndService client, ManageExecutionState executionState)
+ throws Exception {
+ try (Closeable state =
+ executionState.executionStateTracker.enterState(executionState.simpleExecutionState)) {
+ try (Closeable mdc = MDC.putCloseable("key", "value")) {
+ LOG.warn("log me");
+ }
+ }
+ }
+
@Benchmark
@Threads(16) // Use several threads since we expect contention during logging
public void testSkippedLogging(ZeroExpectedCallsLoggingClientAndService client) {
diff --git a/sdks/java/harness/src/main/java/org/apache/beam/fn/harness/logging/BeamFnLoggingClient.java b/sdks/java/harness/src/main/java/org/apache/beam/fn/harness/logging/BeamFnLoggingClient.java
index 2cca7f25c3c..f2ec67c2ac5 100644
--- a/sdks/java/harness/src/main/java/org/apache/beam/fn/harness/logging/BeamFnLoggingClient.java
+++ b/sdks/java/harness/src/main/java/org/apache/beam/fn/harness/logging/BeamFnLoggingClient.java
@@ -23,6 +23,7 @@ import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
+import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CancellationException;
@@ -49,7 +50,9 @@ import org.apache.beam.model.pipeline.v1.Endpoints;
import org.apache.beam.sdk.options.ExecutorOptions;
import org.apache.beam.sdk.options.PipelineOptions;
import org.apache.beam.sdk.options.SdkHarnessOptions;
+import org.apache.beam.vendor.grpc.v1p54p0.com.google.protobuf.Struct;
import org.apache.beam.vendor.grpc.v1p54p0.com.google.protobuf.Timestamp;
+import org.apache.beam.vendor.grpc.v1p54p0.com.google.protobuf.Value;
import org.apache.beam.vendor.grpc.v1p54p0.io.grpc.ManagedChannel;
import org.apache.beam.vendor.grpc.v1p54p0.io.grpc.Status;
import org.apache.beam.vendor.grpc.v1p54p0.io.grpc.stub.CallStreamObserver;
@@ -58,6 +61,7 @@ import org.apache.beam.vendor.grpc.v1p54p0.io.grpc.stub.ClientResponseObserver;
import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.MoreObjects;
import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap;
import org.checkerframework.checker.nullness.qual.Nullable;
+import org.slf4j.MDC;
/**
* Configures {@link java.util.logging} to send all {@link LogRecord}s via the Beam Fn Logging API.
@@ -122,6 +126,7 @@ public class BeamFnLoggingClient implements AutoCloseable {
logRecordHandler.setLevel(Level.ALL);
logRecordHandler.setFormatter(DEFAULT_FORMATTER);
logRecordHandler.executeOn(options.as(ExecutorOptions.class).getScheduledExecutorService());
+ logRecordHandler.setLogMdc(options.as(SdkHarnessOptions.class).getLogMdc());
outboundObserver = (CallStreamObserver<BeamFnApi.LogEntry.List>) stub.logging(inboundObserver);
rootLogger.addHandler(logRecordHandler);
}
@@ -171,6 +176,12 @@ public class BeamFnLoggingClient implements AutoCloseable {
*/
private @Nullable Thread logEntryHandlerThread = null;
+ private boolean logMdc = true;
+
+ private void setLogMdc(boolean value) {
+ logMdc = value;
+ }
+
private void executeOn(ExecutorService executorService) {
bufferedLogWriter = executorService.submit(this);
}
@@ -217,6 +228,17 @@ public class BeamFnLoggingClient implements AutoCloseable {
}
}
+ if (logMdc) {
+ Map<String, String> mdc = MDC.getCopyOfContextMap();
+ if (mdc != null) {
+ Struct.Builder customDataBuilder = builder.getCustomDataBuilder();
+ mdc.forEach(
+ (k, v) -> {
+ customDataBuilder.putFields(k, Value.newBuilder().setStringValue(v).build());
+ });
+ }
+ }
+
// The thread that sends log records should never perform a blocking publish and
// only insert log records best effort.
if (Thread.currentThread() != logEntryHandlerThread) {
diff --git a/sdks/java/harness/src/test/java/org/apache/beam/fn/harness/logging/BeamFnLoggingClientTest.java b/sdks/java/harness/src/test/java/org/apache/beam/fn/harness/logging/BeamFnLoggingClientTest.java
index fad306a81a9..66b9246d619 100644
--- a/sdks/java/harness/src/test/java/org/apache/beam/fn/harness/logging/BeamFnLoggingClientTest.java
+++ b/sdks/java/harness/src/test/java/org/apache/beam/fn/harness/logging/BeamFnLoggingClientTest.java
@@ -41,7 +41,9 @@ import org.apache.beam.model.fnexecution.v1.BeamFnLoggingGrpc;
import org.apache.beam.model.pipeline.v1.Endpoints;
import org.apache.beam.sdk.fn.test.TestStreams;
import org.apache.beam.sdk.options.PipelineOptionsFactory;
+import org.apache.beam.vendor.grpc.v1p54p0.com.google.protobuf.Struct;
import org.apache.beam.vendor.grpc.v1p54p0.com.google.protobuf.Timestamp;
+import org.apache.beam.vendor.grpc.v1p54p0.com.google.protobuf.Value;
import org.apache.beam.vendor.grpc.v1p54p0.io.grpc.ManagedChannel;
import org.apache.beam.vendor.grpc.v1p54p0.io.grpc.Server;
import org.apache.beam.vendor.grpc.v1p54p0.io.grpc.Status;
@@ -94,6 +96,10 @@ public class BeamFnLoggingClientTest {
.setInstructionId("instruction-1")
.setSeverity(BeamFnApi.LogEntry.Severity.Enum.DEBUG)
.setMessage("testMdcValue:Message")
+ .setCustomData(
+ Struct.newBuilder()
+ .putFields(
+ "testMdcKey", Value.newBuilder().setStringValue("testMdcValue").build()))
.setThread("12345")
.setTimestamp(Timestamp.newBuilder().setSeconds(1234567).setNanos(890000000).build())
.setLogLocation("LoggerName")