You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@tez.apache.org by GitBox <gi...@apache.org> on 2022/10/24 12:03:44 UTC

[GitHub] [tez] abstractdog commented on a diff in pull request #98: TEZ-4039: Tez should inject dag id, query id into MDC

abstractdog commented on code in PR #98:
URL: https://github.com/apache/tez/pull/98#discussion_r1003232833


##########
tez-common/src/main/java/org/apache/tez/util/LoggingUtils.java:
##########
@@ -0,0 +1,147 @@
+/**
+* Licensed to the Apache Software Foundation (ASF) under one
+* or more contributor license agreements.  See the NOTICE file
+* distributed with this work for additional information
+* regarding copyright ownership.  The ASF licenses this file
+* to you under the Apache License, Version 2.0 (the
+* "License"); you may not use this file except in compliance
+* with the License.  You may obtain a copy of the License at
+*
+*     http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+
+package org.apache.tez.util;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
+import java.util.Arrays;
+import java.util.Hashtable;
+
+import org.apache.hadoop.conf.Configuration;
+import org.apache.log4j.helpers.ThreadLocalMap;
+import org.apache.tez.dag.api.TezConfiguration;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public final class LoggingUtils {
+  private static final Logger LOG = LoggerFactory.getLogger(LoggingUtils.class);
+
+  private LoggingUtils() {}
+
+  @SuppressWarnings("unchecked")
+  public static void initLoggingContext(ThreadLocalMap threadLocalMap, Configuration conf,
+      String dagId, String taskAttemptId) {
+    Hashtable<String, String> data = (Hashtable<String, String>) threadLocalMap.get();
+    if (data == null) {
+      data = new NonClonableHashtable<String, String>();
+      threadLocalMap.set(data);
+    }
+    data.put("dagId", dagId == null ? "" : dagId);
+    data.put("taskAttemptId", taskAttemptId == null ? "" : taskAttemptId);
+
+    String[] mdcKeys = conf.getStrings(TezConfiguration.TEZ_MDC_CUSTOM_KEYS,
+        TezConfiguration.TEZ_MDC_CUSTOM_KEYS_DEFAULT);
+
+    if (mdcKeys.length == 0) {
+      return;
+    }
+
+    String[] mdcKeysValuesFrom = conf.getStrings(TezConfiguration.TEZ_MDC_CUSTOM_KEYS_CONF_PROPS,
+        TezConfiguration.TEZ_MDC_CUSTOM_KEYS_CONF_PROPS_DEFAULT);
+    LOG.info("MDC_LOGGING: setting up MDC keys: keys: {} / conf: {}", Arrays.asList(mdcKeys),
+        Arrays.asList(mdcKeysValuesFrom));
+
+    int i = 0;
+    for (String mdcKey : mdcKeys) {
+      // don't want to fail on incorrect mdc key settings, but warn in app logs
+      if (mdcKey.isEmpty() || mdcKeysValuesFrom.length < i + 1) {
+        LOG.warn("cannot set mdc key: {}", mdcKey);
+        break;

Review Comment:
   if we hit this it means that mdcKey is most probably empty, so we had:
   ```
   tez.mdc.custom.keys=
   ```
   as it's very unlikely that the user set something like this:
   ```
   tez.mdc.custom.keys=,something_which_matters
   ```
   if we split string by commas and find empty string, it makes no sense to loop further, or at least I cannot figure out a valid use-case



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@tez.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org