Skip to content
Closed
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
18 changes: 9 additions & 9 deletions model/pipeline/src/main/proto/metrics.proto
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,12 @@ import "beam_runner_api.proto";
import "google/protobuf/descriptor.proto";
import "google/protobuf/timestamp.proto";

// A specification containing required set of fields and labels required
// to be set on a MonitoringInfo for the specific URN for SDK->RunnerHarness
// ProcessBundleResponse reporting.
// Specifications containing required fields and labels for a given
// MonitoringInfo URN, for SDK->RunnerHarness ProcessBundleResponse reporting.
message MonitoringInfoSpec {
string urn = 1;
string type_urn = 2;
// The list of required
// The list of required labels for this URN
repeated string required_labels = 3;
// Extra non functional parts of the spec for descriptive purposes.
// i.e. description, units, etc.
Expand All @@ -61,14 +60,16 @@ message MonitoringInfoSpecs {
enum Enum {
// TODO(ajamato): Add the PTRANSFORM name as a required label after
// upgrading the python SDK.
USER_COUNTER = 0 [(monitoring_info_spec) = {
USER_METRIC = 0 [(monitoring_info_spec) = {
urn: "beam:metric:user:",
type_urn: "beam:metrics:sum_int_64",
}];

ELEMENT_COUNT = 1 [(monitoring_info_spec) = {
urn: "beam:metric:element_count:v1",
type_urn: "beam:metrics:sum_int_64",
// TODO(ryan): we currently also generate this metric with ["PTRANSFORM"] labels, but it fails validation in
// Java's SimpleMonitoringInfoBuilder (and emits a WARN); should ["PTRANSFORM"] and ["PCOLLEcTION"] both work?
required_labels: [ "PCOLLECTION" ],
annotations: [ {
key: "description",
Expand Down Expand Up @@ -158,8 +159,7 @@ message MonitoringInfo {
}

enum MonitoringInfoLabels {
// TODO(ajamato): Rename all references to TRANSFORM to PTRANSFORM
TRANSFORM = 0 [(label_props) = { name: "PTRANSFORM" }];
PTRANSFORM = 0 [(label_props) = { name: "PTRANSFORM" }];
PCOLLECTION = 1 [(label_props) = { name: "PCOLLECTION" }];
WINDOWING_STRATEGY = 2 [(label_props) = { name: "WINDOWING_STRATEGY" }];
CODER = 3 [(label_props) = { name: "CODER" }];
Expand All @@ -182,8 +182,8 @@ message MonitoringInfo {

message MonitoringInfoUrns {
enum Enum {
// User counter have this format: 'beam:metric:user:<namespace>:<name>'.
USER_COUNTER_URN_PREFIX = 0
// User-metric URNs have this format: 'beam:metric:user:<namespace>:<name>'
USER_METRIC_URN_PREFIX = 0
[(org.apache.beam.model.pipeline.v1.beam_urn) = "beam:metric:user:"];

ELEMENT_COUNT = 1 [(org.apache.beam.model.pipeline.v1.beam_urn) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
*/
package org.apache.beam.runners.core.metrics;

import static org.apache.beam.runners.core.metrics.SimpleMonitoringInfoBuilder.USER_COUNTER_URN_PREFIX;
import static org.apache.beam.runners.core.metrics.SimpleMonitoringInfoBuilder.USER_METRIC_URN_PREFIX;

import javax.annotation.Nullable;
import org.apache.beam.sdk.metrics.MetricName;

/** Utility for parsing a URN to a {@link MetricName}. */
Expand All @@ -29,14 +30,17 @@ public class MetricUrns {
*
* <p>Should be consistent with {@code parse_namespace_and_name} in monitoring_infos.py.
*/
public static MetricName parseUrn(String urn) {
if (urn.startsWith(USER_COUNTER_URN_PREFIX)) {
urn = urn.substring(USER_COUNTER_URN_PREFIX.length());
@Nullable
public static MetricName parseUserMetricUrn(String urn) {
if (urn.startsWith(USER_METRIC_URN_PREFIX)) {
urn = urn.substring(USER_METRIC_URN_PREFIX.length());
} else {
return null;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Did you encounter this somewhere? Hopefully this isn't called if the metric isn't actually starting with USER_METRIC_URN_PREFIX

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.

As of this PR, this helper is called in two places:

  • MonitoringInfoMetricName.parseUrn:
    • use the parsed user-MetricName, if possible
    • throw if it's not a user-metric (signaled by null return here)
  • FlinkMetricContainer.updateMetrics:
    • use the parsed user-MetricName, if possible
    • skip the metric if it's not a user-metric (again, signaled by null return)
      • in later PRs I make Flink support system-metrics as well.

This was the most direct way to support those uses. The Flink metrics do actually see system metrics, and should gracefully skip them instead of throwing (for now).

}
// If it is not a user counter, just use the first part of the URN, i.e. 'beam'
String[] pieces = urn.split(":", 2);
if (pieces.length != 2) {
throw new IllegalArgumentException("Invalid metric URN: " + urn);
throw new IllegalArgumentException("Invalid user-metric URN: " + urn);
}
return MetricName.named(pieces[0], pieces[1]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@

import static org.apache.beam.vendor.guava.v20_0.com.google.common.base.Preconditions.checkArgument;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import javax.annotation.Nullable;
Expand All @@ -41,7 +39,7 @@ public class MonitoringInfoMetricName extends MetricName {
@Nullable private String namespace;
private HashMap<String, String> labels = new HashMap<String, String>();

private MonitoringInfoMetricName(String urn, HashMap<String, String> labels) {
private MonitoringInfoMetricName(String urn, Map<String, String> labels) {
checkArgument(!Strings.isNullOrEmpty(urn), "MonitoringInfoMetricName urn must be non-empty");
checkArgument(labels != null, "MonitoringInfoMetricName labels must be non-null");
// TODO(ajamato): Move SimpleMonitoringInfoBuilder to beam-runner-core-construction-java
Expand All @@ -53,19 +51,21 @@ private MonitoringInfoMetricName(String urn, HashMap<String, String> labels) {
}

/** Parse the urn field into a name and namespace field. */
private void parseUrn() {
if (this.urn.startsWith(SimpleMonitoringInfoBuilder.USER_COUNTER_URN_PREFIX)) {
List<String> split = new ArrayList<String>(Arrays.asList(this.getUrn().split(":")));
this.name = split.get(split.size() - 1);
this.namespace = split.get(split.size() - 2);
private void parseUserMetricUrn() {
MetricName metricName = MetricUrns.parseUserMetricUrn(getUrn());
if (metricName == null) {
throw new IllegalStateException(
"Attempting to access namespace/name of a non-user metric: " + getUrn());
}
namespace = metricName.getNamespace();
name = metricName.getName();
}

/** @return the parsed namespace from the user metric URN, otherwise null. */
@Override
public String getNamespace() {
if (this.namespace == null) {
parseUrn();
parseUserMetricUrn();
}
return this.namespace;
}
Expand All @@ -74,7 +74,7 @@ public String getNamespace() {
@Override
public String getName() {
if (this.name == null) {
parseUrn();
parseUserMetricUrn();
}
return this.name;
}
Expand All @@ -89,7 +89,7 @@ public HashMap<String, String> getLabels() {
return this.labels;
}

public static MonitoringInfoMetricName named(String urn, HashMap<String, String> labels) {
public static MonitoringInfoMetricName named(String urn, Map<String, String> labels) {
return new MonitoringInfoMetricName(urn, labels);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
*/
public class SimpleExecutionState extends ExecutionState {
private long totalMillis = 0;
private HashMap<String, String> labelsMetadata;
private Map<String, String> labelsMetadata;
private String urn;

private static final Logger LOG = LoggerFactory.getLogger(SimpleExecutionState.class);
Expand All @@ -59,13 +59,12 @@ public class SimpleExecutionState extends ExecutionState {
* @param urn A optional string urn for an execution time metric.
* @param labelsMetadata arbitrary metadata to use for reporting purposes.
*/
public SimpleExecutionState(
String stateName, String urn, HashMap<String, String> labelsMetadata) {
public SimpleExecutionState(String stateName, String urn, Map<String, String> labelsMetadata) {
super(stateName);
this.urn = urn;
this.labelsMetadata = labelsMetadata;
if (this.labelsMetadata == null) {
this.labelsMetadata = new HashMap<String, String>();
this.labelsMetadata = new HashMap<>();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import java.time.Instant;
import java.util.HashMap;
import java.util.Optional;
import javax.annotation.Nullable;
import org.apache.beam.model.pipeline.v1.MetricsApi.MonitoringInfo;
import org.apache.beam.model.pipeline.v1.MetricsApi.MonitoringInfo.MonitoringInfoLabels;
Expand Down Expand Up @@ -68,16 +69,16 @@ public class SimpleMonitoringInfoBuilder {
BeamUrns.getUrn(MonitoringInfoUrns.Enum.PROCESS_BUNDLE_MSECS);
public static final String FINISH_BUNDLE_MSECS_URN =
BeamUrns.getUrn(MonitoringInfoUrns.Enum.FINISH_BUNDLE_MSECS);
public static final String USER_COUNTER_URN_PREFIX =
BeamUrns.getUrn(MonitoringInfoUrns.Enum.USER_COUNTER_URN_PREFIX);
public static final String USER_METRIC_URN_PREFIX =
BeamUrns.getUrn(MonitoringInfoUrns.Enum.USER_METRIC_URN_PREFIX);
public static final String SUM_INT64_TYPE_URN =
BeamUrns.getUrn(MonitoringInfoTypeUrns.Enum.SUM_INT64_TYPE);

private static final HashMap<String, MonitoringInfoSpec> specs =
new HashMap<String, MonitoringInfoSpec>();

public static final String PCOLLECTION_LABEL = getLabelString(MonitoringInfoLabels.PCOLLECTION);
public static final String PTRANSFORM_LABEL = getLabelString(MonitoringInfoLabels.TRANSFORM);
public static final String PTRANSFORM_LABEL = getLabelString(MonitoringInfoLabels.PTRANSFORM);

private final boolean validateAndDropInvalid;

Expand Down Expand Up @@ -120,7 +121,7 @@ public static String userMetricUrn(String metricNamespace, String metricName) {
String fixedMetricNamespace = metricNamespace.replace(':', '_');
String fixedMetricName = metricName.replace(':', '_');
StringBuilder sb = new StringBuilder();
sb.append(USER_COUNTER_URN_PREFIX);
sb.append(USER_METRIC_URN_PREFIX);
sb.append(fixedMetricNamespace);
sb.append(':');
sb.append(fixedMetricName);
Expand Down Expand Up @@ -212,8 +213,13 @@ public static MonitoringInfo clearTimestamp(MonitoringInfo input) {
@Nullable
public MonitoringInfo build() {
final MonitoringInfo result = this.builder.build();
if (validateAndDropInvalid && this.validator.validate(result).isPresent()) {
return null;
if (validateAndDropInvalid) {
Optional<String> error = this.validator.validate(result);
if (error.isPresent()) {
// TODO(ryan): throw in this case; remove nullability
LOG.warn("Dropping invalid partial MonitoringInfo: {}\n{}", error, result);
return null;
}
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public SpecMonitoringInfoValidator() {
// Filtering default value for "unknown" Enums. Coming from proto implementation.
.filter(x -> !x.name().equals("UNRECOGNIZED"))
.map(x -> x.getValueDescriptor().getOptions().getExtension(monitoringInfoSpec))
.toArray(size -> new MonitoringInfoSpec[size]);
.toArray(MonitoringInfoSpec[]::new);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ public void testElementCountConstruction() {
HashMap<String, String> labels = new HashMap<String, String>();
String urn = SimpleMonitoringInfoBuilder.ELEMENT_COUNT_URN;
MonitoringInfoMetricName name = MonitoringInfoMetricName.named(urn, labels);
assertEquals(null, name.getName());
assertEquals(null, name.getNamespace());
assertEquals(labels, name.getLabels());
assertEquals(urn, name.getUrn());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void testUserCounter() {
MonitoringInfo monitoringInfo = builder.build();
assertTrue(monitoringInfo != null);
assertEquals(
SimpleMonitoringInfoBuilder.USER_COUNTER_URN_PREFIX + "myNamespace:myName",
SimpleMonitoringInfoBuilder.USER_METRIC_URN_PREFIX + "myNamespace:myName",
monitoringInfo.getUrn());
assertEquals(SimpleMonitoringInfoBuilder.SUM_INT64_TYPE_URN, monitoringInfo.getType());
assertEquals(1, monitoringInfo.getMetric().getCounterData().getInt64Value());
Expand All @@ -77,7 +77,7 @@ public void testUserMetricWithInvalidDelimiterCharacterIsReplaced() {
MonitoringInfo monitoringInfo = builder.build();
assertTrue(monitoringInfo != null);
assertEquals(
SimpleMonitoringInfoBuilder.USER_COUNTER_URN_PREFIX + "myNamespace_withInvalidChar:myName",
SimpleMonitoringInfoBuilder.USER_METRIC_URN_PREFIX + "myNamespace_withInvalidChar:myName",
monitoringInfo.getUrn());
assertEquals(SimpleMonitoringInfoBuilder.SUM_INT64_TYPE_URN, monitoringInfo.getType());
assertEquals(1, monitoringInfo.getMetric().getCounterData().getInt64Value());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
*/
package org.apache.beam.runners.core.metrics;

import static org.apache.beam.runners.core.metrics.SimpleMonitoringInfoBuilder.ELEMENT_COUNT_URN;
import static org.apache.beam.runners.core.metrics.SimpleMonitoringInfoBuilder.SUM_INT64_TYPE_URN;
import static org.apache.beam.runners.core.metrics.SimpleMonitoringInfoBuilder.USER_METRIC_URN_PREFIX;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

Expand All @@ -36,6 +39,16 @@ public void setUp() throws Exception {

@Test
public void validateReturnsErrorOnInvalidMonitoringInfoType() {
MonitoringInfo testInput =
MonitoringInfo.newBuilder()
.setUrn(ELEMENT_COUNT_URN)
.setType("beam:metrics:bad_value")
.build();
assertTrue(testObject.validate(testInput).isPresent());
}

@Test
public void validatePassesThroughInvalidTypeOnUserMetric() {
MonitoringInfo testInput =
MonitoringInfo.newBuilder()
.setUrn("beam:metric:user:someCounter")
Expand All @@ -48,16 +61,16 @@ public void validateReturnsErrorOnInvalidMonitoringInfoType() {
public void validateReturnsNoErrorOnValidMonitoringInfo() {
MonitoringInfo testInput =
MonitoringInfo.newBuilder()
.setUrn(SimpleMonitoringInfoBuilder.USER_COUNTER_URN_PREFIX + "someCounter")
.setType(SimpleMonitoringInfoBuilder.SUM_INT64_TYPE_URN)
.setUrn(USER_METRIC_URN_PREFIX + "someCounter")
.setType(SUM_INT64_TYPE_URN)
.putLabels("dummy", "value")
.build();
assertFalse(testObject.validate(testInput).isPresent());

testInput =
MonitoringInfo.newBuilder()
.setUrn(SimpleMonitoringInfoBuilder.ELEMENT_COUNT_URN)
.setType(SimpleMonitoringInfoBuilder.SUM_INT64_TYPE_URN)
.setUrn(ELEMENT_COUNT_URN)
.setType(SUM_INT64_TYPE_URN)
.putLabels("PTRANSFORM", "value")
.putLabels("PCOLLECTION", "anotherValue")
.build();
Expand All @@ -68,8 +81,8 @@ public void validateReturnsNoErrorOnValidMonitoringInfo() {
public void validateReturnsErrorOnInvalidMonitoringInfoLabels() {
MonitoringInfo testInput =
MonitoringInfo.newBuilder()
.setUrn(SimpleMonitoringInfoBuilder.ELEMENT_COUNT_URN)
.setType(SimpleMonitoringInfoBuilder.SUM_INT64_TYPE_URN)
.setUrn(ELEMENT_COUNT_URN)
.setType(SUM_INT64_TYPE_URN)
.putLabels("PTRANSFORM", "unexpectedLabel")
.build();
assertTrue(testObject.validate(testInput).isPresent());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
package org.apache.beam.runners.flink.metrics;

import static org.apache.beam.runners.core.metrics.MetricUrns.parseUrn;
import static org.apache.beam.runners.core.metrics.MetricUrns.parseUserMetricUrn;
import static org.apache.beam.runners.core.metrics.MetricsContainerStepMap.asAttemptedOnlyMetricResults;

import java.util.HashMap;
Expand Down Expand Up @@ -105,7 +105,11 @@ public void updateMetrics(String stepName, List<MonitoringInfo> monitoringInfos)
monitoringInfo -> {
if (monitoringInfo.hasMetric()) {
String urn = monitoringInfo.getUrn();
MetricName metricName = parseUrn(urn);
MetricName metricName = parseUserMetricUrn(urn);
if (metricName == null) {
LOG.info("Skipping non-user metric: {}", urn);
return;
}
Metric metric = monitoringInfo.getMetric();
if (metric.hasCounterData()) {
CounterData counterData = metric.getCounterData();
Expand Down
Loading