From b3d31dd28979cd27652f7cc73cbd0fc9d653cb83 Mon Sep 17 00:00:00 2001 From: Ryan Fu Date: Fri, 30 Aug 2024 17:59:11 -0700 Subject: [PATCH] Memoize toString() for MetricKey and MetricName (#32379) (cherry picked from commit 64b4aa6fdde6a65b8157578036374623a47c18c2) --- .../src/main/java/org/apache/beam/sdk/metrics/MetricKey.java | 4 +++- .../src/main/java/org/apache/beam/sdk/metrics/MetricName.java | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/sdks/java/core/src/main/java/org/apache/beam/sdk/metrics/MetricKey.java b/sdks/java/core/src/main/java/org/apache/beam/sdk/metrics/MetricKey.java index e7dcebd14833..05f1d736d8a5 100644 --- a/sdks/java/core/src/main/java/org/apache/beam/sdk/metrics/MetricKey.java +++ b/sdks/java/core/src/main/java/org/apache/beam/sdk/metrics/MetricKey.java @@ -18,6 +18,7 @@ package org.apache.beam.sdk.metrics; import com.google.auto.value.AutoValue; +import com.google.auto.value.extension.memoized.Memoized; import java.io.Serializable; import org.apache.beam.sdk.annotations.Experimental; import org.apache.beam.sdk.annotations.Experimental.Kind; @@ -35,7 +36,8 @@ public abstract class MetricKey implements Serializable { public abstract MetricName metricName(); @Override - public final String toString() { + @Memoized + public String toString() { return stepName() + ":" + metricName(); } diff --git a/sdks/java/core/src/main/java/org/apache/beam/sdk/metrics/MetricName.java b/sdks/java/core/src/main/java/org/apache/beam/sdk/metrics/MetricName.java index 329e44a5d714..258aa4827c50 100644 --- a/sdks/java/core/src/main/java/org/apache/beam/sdk/metrics/MetricName.java +++ b/sdks/java/core/src/main/java/org/apache/beam/sdk/metrics/MetricName.java @@ -20,6 +20,7 @@ import static org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkArgument; import com.google.auto.value.AutoValue; +import com.google.auto.value.extension.memoized.Memoized; import java.io.Serializable; import org.apache.beam.sdk.annotations.Experimental; import org.apache.beam.sdk.annotations.Experimental.Kind; @@ -42,6 +43,7 @@ public abstract class MetricName implements Serializable { public abstract String getName(); @Override + @Memoized public String toString() { return getNamespace() + ":" + getName(); }