diff --git a/runners/core-construction-java/src/main/java/org/apache/beam/runners/core/construction/metrics/MetricKey.java b/runners/core-construction-java/src/main/java/org/apache/beam/runners/core/construction/metrics/MetricKey.java index f4d1e9d4acb6..4bfabe0c125e 100644 --- a/runners/core-construction-java/src/main/java/org/apache/beam/runners/core/construction/metrics/MetricKey.java +++ b/runners/core-construction-java/src/main/java/org/apache/beam/runners/core/construction/metrics/MetricKey.java @@ -34,6 +34,11 @@ public abstract class MetricKey implements Serializable { /** The name of the metric. */ public abstract MetricName metricName(); + @Override + public String toString() { + return String.format("%s:%s", stepName(), metricName()); + } + public static MetricKey create(String stepName, MetricName metricName) { return new AutoValue_MetricKey(stepName, metricName); } diff --git a/runners/core-java/src/main/java/org/apache/beam/runners/core/metrics/MetricsContainerStepMap.java b/runners/core-java/src/main/java/org/apache/beam/runners/core/metrics/MetricsContainerStepMap.java index f2c6f03da738..6a73e679aa05 100644 --- a/runners/core-java/src/main/java/org/apache/beam/runners/core/metrics/MetricsContainerStepMap.java +++ b/runners/core-java/src/main/java/org/apache/beam/runners/core/metrics/MetricsContainerStepMap.java @@ -212,6 +212,11 @@ private Function, AttemptedAndCommitted> committedCount }; } + @Override + public String toString() { + return queryMetrics(null).toString(); + } + @Override public MetricQueryResults queryMetrics(@Nullable MetricsFilter filter) { return new QueryResults(filter); 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 3ffea6ec80a5..ae23ff0c23bd 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 @@ -50,6 +50,11 @@ public String namespace() { /** The name of this metric. */ public abstract String getName(); + @Override + public String toString() { + return String.format("%s:%s", getNamespace(), getName()); + } + /** * The name of this metric. * diff --git a/sdks/java/core/src/main/java/org/apache/beam/sdk/metrics/MetricQueryResults.java b/sdks/java/core/src/main/java/org/apache/beam/sdk/metrics/MetricQueryResults.java index baae446174eb..e79989ef8c58 100644 --- a/sdks/java/core/src/main/java/org/apache/beam/sdk/metrics/MetricQueryResults.java +++ b/sdks/java/core/src/main/java/org/apache/beam/sdk/metrics/MetricQueryResults.java @@ -18,8 +18,10 @@ package org.apache.beam.sdk.metrics; import com.google.auto.value.AutoValue; +import java.util.List; import org.apache.beam.sdk.annotations.Experimental; import org.apache.beam.sdk.annotations.Experimental.Kind; +import org.apache.beam.vendor.grpc.v1p13p1.com.google.common.collect.ImmutableList; /** * The results of a query for metrics. Allows accessing all of the metrics that matched the filter. @@ -27,15 +29,55 @@ @AutoValue @Experimental(Kind.METRICS) public abstract class MetricQueryResults { - /** Return the metric results for the getCounters that matched the filter. */ + /** Return the metric results for the counters that matched the filter. */ public abstract Iterable> getCounters(); - /** Return the metric results for the getDistributions that matched the filter. */ + /** Return the metric results for the distributions that matched the filter. */ public abstract Iterable> getDistributions(); - /** Return the metric results for the getGauges that matched the filter. */ + /** Return the metric results for the gauges that matched the filter. */ public abstract Iterable> getGauges(); + static void printMetrics(String type, Iterable> metrics, StringBuilder sb) { + List> metricsList = ImmutableList.copyOf(metrics); + if (!metricsList.isEmpty()) { + sb.append(type).append("("); + boolean first = true; + for (MetricResult metricResult : metricsList) { + if (first) { + first = false; + } else { + sb.append(", "); + } + MetricName name = metricResult.getName(); + sb.append(metricResult.getStep()) + .append(":") + .append(name.getNamespace()) + .append(":") + .append(name.getName()) + .append(": ") + .append(metricResult.getAttempted()); + try { + T committed = metricResult.getCommitted(); + sb.append(", ").append(committed); + } catch (UnsupportedOperationException ignored) { + } + } + sb.append(")"); + } + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("MetricQueryResults("); + printMetrics("Counters", getCounters(), sb); + printMetrics("Distributions", getDistributions(), sb); + printMetrics("Gauges", getGauges(), sb); + sb.append(")"); + return sb.toString(); + } + public static MetricQueryResults create( Iterable> counters, Iterable> distributions, diff --git a/sdks/python/apache_beam/examples/wordcount.py b/sdks/python/apache_beam/examples/wordcount.py index e23f46ccbfbd..0945985fa8c7 100644 --- a/sdks/python/apache_beam/examples/wordcount.py +++ b/sdks/python/apache_beam/examples/wordcount.py @@ -122,13 +122,13 @@ def format_result(word_count): query_result = result.metrics().query(empty_lines_filter) if query_result['counters']: empty_lines_counter = query_result['counters'][0] - logging.info('number of empty lines: %d', empty_lines_counter.committed) + logging.info('number of empty lines: %d', empty_lines_counter.result) word_lengths_filter = MetricsFilter().with_name('word_len_dist') query_result = result.metrics().query(word_lengths_filter) if query_result['distributions']: word_lengths_dist = query_result['distributions'][0] - logging.info('average word length: %d', word_lengths_dist.committed.mean) + logging.info('average word length: %d', word_lengths_dist.result.mean) if __name__ == '__main__': diff --git a/sdks/python/apache_beam/metrics/execution.py b/sdks/python/apache_beam/metrics/execution.py index 77e0a884b74b..ba190ff981e9 100644 --- a/sdks/python/apache_beam/metrics/execution.py +++ b/sdks/python/apache_beam/metrics/execution.py @@ -122,6 +122,13 @@ def __repr__(self): return 'MetricResult(key={}, committed={}, attempted={})'.format( self.key, str(self.committed), str(self.attempted)) + @property + def result(self): + """Short-hand for falling back to attempted metrics if it seems that + committed was not populated (e.g. due to not being supported on a given + runner""" + return self.committed if self.committed else self.attempted + class _MetricsEnvironment(object): """Holds the MetricsContainer for every thread and other metric information.