Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,11 @@ private Function<MetricUpdate<Long>, AttemptedAndCommitted<Long>> committedCount
};
}

@Override
public String toString() {
return queryMetrics(null).toString();
}

@Override
public MetricQueryResults queryMetrics(@Nullable MetricsFilter filter) {
return new QueryResults(filter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,66 @@
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.
*/
@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<MetricResult<Long>> 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<MetricResult<DistributionResult>> 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<MetricResult<GaugeResult>> getGauges();

static <T> void printMetrics(String type, Iterable<MetricResult<T>> metrics, StringBuilder sb) {
List<MetricResult<T>> metricsList = ImmutableList.copyOf(metrics);
if (!metricsList.isEmpty()) {
sb.append(type).append("(");
boolean first = true;
for (MetricResult<T> 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<MetricResult<Long>> counters,
Iterable<MetricResult<DistributionResult>> distributions,
Expand Down
4 changes: 2 additions & 2 deletions sdks/python/apache_beam/examples/wordcount.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__':
Expand Down
7 changes: 7 additions & 0 deletions sdks/python/apache_beam/metrics/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stupid question: What is the difference between the two?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a good question!

I believe "committed" consists only of updates from task-attempts that succeeded, whereas "attempted" contains cumulative updates from all task-attempts; this comment in MetricResult is somewhat canonical. I think this is the original design doc about it, though I think it's out of date now.

Flink only supports "attempted" metrics (in Beam; maybe in general?); here's an example where it only fetches "attempted" metric updates. If you have thoughts about the necessity of this restriction from a Flink PoV I'd love to hear them!

A confusing thing is that MetricContainerImpl discusses "commit"s to MetricCells but I don't think it's referring to the same thing.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for clarifying. Flink does not support committed metrics per se, though it looks like you could work around this by changing the metrics template to include the execution attempt id.



class _MetricsEnvironment(object):
"""Holds the MetricsContainer for every thread and other metric information.
Expand Down