Skip to content

Commit

Permalink
[SPARK-48011][CORE] Store LogKey name as a value to avoid generating …
Browse files Browse the repository at this point in the history
…new string instances

### What changes were proposed in this pull request?

Store LogKey name as a value to avoid generating new string instances
### Why are the changes needed?

To save memory usage on getting the names of `LogKey`s.

### Does this PR introduce _any_ user-facing change?

No

### How was this patch tested?

Existing tests

### Was this patch authored or co-authored using generative AI tooling?

No

Closes apache#46249 from gengliangwang/addKeyName.

Authored-by: Gengliang Wang <gengliang@apache.org>
Signed-off-by: Dongjoon Hyun <dhyun@apple.com>
  • Loading branch information
gengliangwang authored and dongjoon-hyun committed Apr 26, 2024
1 parent 6098bd9 commit 2b2a33c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@
*/
package org.apache.spark.internal

import java.util.Locale

/**
* All structured logging `keys` used in `MDC` must be extends `LogKey`
*/
trait LogKey
trait LogKey {
val name: String = this.toString.toLowerCase(Locale.ROOT)
}

/**
* Various keys used for mapped diagnostic contexts(MDC) in logging.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

package org.apache.spark.internal

import java.util.Locale

import scala.jdk.CollectionConverters._

import org.apache.logging.log4j.{CloseableThreadContext, Level, LogManager}
Expand Down Expand Up @@ -110,7 +108,7 @@ trait Logging {
val value = if (mdc.value != null) mdc.value.toString else null
sb.append(value)
if (Logging.isStructuredLoggingEnabled) {
context.put(mdc.key.toString.toLowerCase(Locale.ROOT), value)
context.put(mdc.key.name, value)
}

if (processedParts.hasNext) {
Expand Down

0 comments on commit 2b2a33c

Please sign in to comment.