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
2 changes: 2 additions & 0 deletions model/fn-execution/src/main/proto/beam_fn_api.proto
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,8 @@ message Annotation {
// MonitoringInfo protos.
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) = {
urn: "beam:metric:user:",
type_urn: "beam:metrics:sum_int_64",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import com.google.auto.value.AutoValue;
import java.io.Serializable;
import javax.annotation.Nullable;
import org.apache.beam.sdk.annotations.Experimental;
import org.apache.beam.sdk.annotations.Experimental.Kind;
import org.apache.beam.sdk.metrics.MetricName;
Expand All @@ -28,7 +29,8 @@
@AutoValue
public abstract class MetricKey implements Serializable {

/** The step name that is associated with this metric. */
/** The step name that is associated with this metric or Null if none is associated. */
@Nullable
public abstract String stepName();

/** The name of the metric. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
@Experimental(Kind.METRICS)
public class MetricsContainerImpl implements Serializable, MetricsContainer {

private final String stepName;
@Nullable private final String stepName;

private MetricsMap<MetricName, CounterCell> counters = new MetricsMap<>(CounterCell::new);

Expand All @@ -60,7 +60,7 @@ public class MetricsContainerImpl implements Serializable, MetricsContainer {
private MetricsMap<MetricName, GaugeCell> gauges = new MetricsMap<>(GaugeCell::new);

/** Create a new {@link MetricsContainerImpl} associated with the given {@code stepName}. */
public MetricsContainerImpl(String stepName) {
public MetricsContainerImpl(@Nullable String stepName) {
this.stepName = stepName;
}

Expand Down Expand Up @@ -143,6 +143,7 @@ public MetricUpdates getUpdates() {
* @param metricUpdate
* @return The MonitoringInfo generated from the metricUpdate.
*/
@Nullable
private MonitoringInfo counterUpdateToMonitoringInfo(MetricUpdate<Long> metricUpdate) {
SimpleMonitoringInfoBuilder builder = new SimpleMonitoringInfoBuilder(true);
MetricName metricName = metricUpdate.getKey().metricName();
Expand All @@ -153,11 +154,17 @@ private MonitoringInfo counterUpdateToMonitoringInfo(MetricUpdate<Long> metricUp
for (Entry<String, String> e : monitoringInfoName.getLabels().entrySet()) {
builder.setLabel(e.getKey(), e.getValue());
}
} else {
} else { // Note: (metricName instanceof MetricName) is always True.
// Represents a user counter.
builder.setUrnForUserMetric(
metricUpdate.getKey().metricName().getNamespace(),
metricUpdate.getKey().metricName().getName());
// Drop if the stepname is not set. All user counters must be
// defined for a PTransform. They must be defined on a container bound to a step.
if (this.stepName == null) {
return null;
}
builder.setPTransformLabel(metricUpdate.getKey().stepName());
}
builder.setInt64Value(metricUpdate.getUpdate());
builder.setTimestampToNow();
Expand All @@ -171,7 +178,10 @@ public Iterable<MonitoringInfo> getMonitoringInfos() {
MetricUpdates metricUpdates = this.getUpdates();

for (MetricUpdate<Long> metricUpdate : metricUpdates.counterUpdates()) {
monitoringInfos.add(counterUpdateToMonitoringInfo(metricUpdate));
MonitoringInfo mi = counterUpdateToMonitoringInfo(metricUpdate);
if (mi != null) {
monitoringInfos.add(counterUpdateToMonitoringInfo(metricUpdate));
}
}
return monitoringInfos;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@
package org.apache.beam.runners.core.metrics;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.annotation.Nullable;
import org.apache.beam.model.fnexecution.v1.BeamFnApi.MonitoringInfo;
import org.apache.beam.runners.core.construction.metrics.MetricFiltering;
import org.apache.beam.runners.core.construction.metrics.MetricKey;
import org.apache.beam.runners.core.metrics.MetricUpdates.MetricUpdate;
Expand All @@ -35,6 +38,7 @@
import org.apache.beam.vendor.guava.v20_0.com.google.common.base.Function;
import org.apache.beam.vendor.guava.v20_0.com.google.common.base.Predicate;
import org.apache.beam.vendor.guava.v20_0.com.google.common.collect.FluentIterable;
import org.apache.beam.vendor.guava.v20_0.com.google.common.collect.Iterables;

/**
* Metrics containers by step.
Expand All @@ -43,13 +47,23 @@
*/
public class MetricsContainerStepMap implements Serializable {
private Map<String, MetricsContainerImpl> metricsContainers;
private MetricsContainerImpl unboundContainer = new MetricsContainerImpl(null);

public MetricsContainerStepMap() {
this.metricsContainers = new ConcurrentHashMap<>();
}

/* Returns the container that is not bound to any step name. */
public MetricsContainerImpl getUnboundContainer() {
return this.unboundContainer;
}

/** Returns the container for the given step name. */
public MetricsContainerImpl getContainer(String stepName) {
if (stepName == null) {
// TODO(BEAM-6538): Disallow this in the future, some tests rely on an empty step name today.
return getUnboundContainer();
}
if (!metricsContainers.containsKey(stepName)) {
metricsContainers.put(stepName, new MetricsContainerImpl(stepName));
}
Expand All @@ -64,6 +78,7 @@ public void updateAll(MetricsContainerStepMap other) {
for (Map.Entry<String, MetricsContainerImpl> container : other.metricsContainers.entrySet()) {
getContainer(container.getKey()).update(container.getValue());
}
getUnboundContainer().update(other.getUnboundContainer());
}

/**
Expand All @@ -85,7 +100,8 @@ public boolean equals(Object o) {

MetricsContainerStepMap that = (MetricsContainerStepMap) o;

return metricsContainers.equals(that.metricsContainers);
// TODO(BEAM-6546): The underlying MetricContainerImpls do not implement equals().
return getMetricsContainers().equals(that.getMetricsContainers());
}

@Override
Expand All @@ -106,6 +122,18 @@ public static MetricResults asMetricResults(
attemptedMetricsContainers, committedMetricsContainers);
}

/** Return the cumulative values for any metrics in this container as MonitoringInfos. */
public Iterable<MonitoringInfo> getMonitoringInfos() {
// Extract user metrics and store as MonitoringInfos.
ArrayList<MonitoringInfo> monitoringInfos = new ArrayList<MonitoringInfo>();
for (MetricsContainerImpl container : getMetricsContainers()) {
for (MonitoringInfo mi : container.getMonitoringInfos()) {
monitoringInfos.add(mi);
}
}
return monitoringInfos;
}

/**
* Returns {@link MetricResults} based on given {@link MetricsContainerStepMap} of attempted
* metrics.
Expand All @@ -119,8 +147,8 @@ public static MetricResults asAttemptedOnlyMetricResults(
return new MetricsContainerStepMapMetricResults(attemptedMetricsContainers);
}

private Map<String, MetricsContainerImpl> getMetricsContainers() {
return metricsContainers;
private Iterable<MetricsContainerImpl> getMetricsContainers() {
return Iterables.concat(metricsContainers.values(), Collections.singleton(unboundContainer));
}

private static class MetricsContainerStepMapMetricResults extends MetricResults {
Expand All @@ -145,16 +173,14 @@ private MetricsContainerStepMapMetricResults(
MetricsContainerStepMap attemptedMetricsContainers,
MetricsContainerStepMap committedMetricsContainers,
boolean isCommittedSupported) {
for (MetricsContainerImpl container :
attemptedMetricsContainers.getMetricsContainers().values()) {
for (MetricsContainerImpl container : attemptedMetricsContainers.getMetricsContainers()) {
MetricUpdates cumulative = container.getCumulative();
mergeCounters(counters, cumulative.counterUpdates(), attemptedCounterUpdateFn());
mergeDistributions(
distributions, cumulative.distributionUpdates(), attemptedDistributionUpdateFn());
mergeGauges(gauges, cumulative.gaugeUpdates(), attemptedGaugeUpdateFn());
}
for (MetricsContainerImpl container :
committedMetricsContainers.getMetricsContainers().values()) {
for (MetricsContainerImpl container : committedMetricsContainers.getMetricsContainers()) {
MetricUpdates cumulative = container.getCumulative();
mergeCounters(counters, cumulative.counterUpdates(), committedCounterUpdateFn());
mergeDistributions(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ public HashMap<String, String> getLabels() {
return this.labels;
}

/** @return a MetricName for a specific urn and labels map. */
public static MonitoringInfoMetricName named(String urn, HashMap<String, String> labels) {
return new MonitoringInfoMetricName(urn, labels);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,12 @@ public SimpleMonitoringInfoBuilder setLabel(String labelName, String labelValue)
return this;
}

/** Clear the builder and merge from the provided monitoringInfo. */
public void clearAndMerge(MonitoringInfo monitoringInfo) {
this.builder = MonitoringInfo.newBuilder();
this.builder.mergeFrom(monitoringInfo);
}

/**
* @return A copy of the MonitoringInfo with the timestamp cleared, to allow comparing two
* MonitoringInfos.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,13 @@ public void testMonitoringInfosArePopulatedForUserCounters() {
SimpleMonitoringInfoBuilder builder1 = new SimpleMonitoringInfoBuilder();
builder1.setUrnForUserMetric("ns", "name1");
builder1.setInt64Value(5);
builder1.setPTransformLabel("step1");
builder1.build();

SimpleMonitoringInfoBuilder builder2 = new SimpleMonitoringInfoBuilder();
builder2.setUrnForUserMetric("ns", "name2");
builder2.setInt64Value(4);
builder2.setPTransformLabel("step1");
builder2.build();

ArrayList<MonitoringInfo> actualMonitoringInfos = new ArrayList<MonitoringInfo>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,21 @@
import static org.apache.beam.runners.core.metrics.MetricsContainerStepMap.asAttemptedOnlyMetricResults;
import static org.apache.beam.runners.core.metrics.MetricsContainerStepMap.asMetricResults;
import static org.apache.beam.sdk.metrics.MetricResultsMatchers.metricsResult;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.hasItem;
import static org.junit.Assert.assertThat;

import java.io.Closeable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.beam.model.fnexecution.v1.BeamFnApi.MonitoringInfo;
import org.apache.beam.sdk.metrics.Counter;
import org.apache.beam.sdk.metrics.Distribution;
import org.apache.beam.sdk.metrics.DistributionResult;
import org.apache.beam.sdk.metrics.Gauge;
import org.apache.beam.sdk.metrics.GaugeResult;
import org.apache.beam.sdk.metrics.MetricName;
import org.apache.beam.sdk.metrics.MetricQueryResults;
import org.apache.beam.sdk.metrics.MetricResults;
import org.apache.beam.sdk.metrics.Metrics;
Expand Down Expand Up @@ -172,6 +177,51 @@ public void testGaugeCommittedUnsupportedInAttemptedAccumulatedMetricResults() {
assertGauge(GAUGE_NAME, step1res, STEP1, GaugeResult.empty(), true);
}

@Test
public void testUserMetricDroppedOnUnbounded() {
MetricsContainerStepMap testObject = new MetricsContainerStepMap();
CounterCell c1 = testObject.getUnboundContainer().getCounter(MetricName.named("ns", "name1"));
c1.inc(5);

List<MonitoringInfo> expected = new ArrayList<MonitoringInfo>();
assertThat(testObject.getMonitoringInfos(), containsInAnyOrder(expected.toArray()));
}

@Test
public void testUpdateAllUpdatesUnboundedAndBoundedContainers() {
MetricsContainerStepMap baseMetricContainerRegistry = new MetricsContainerStepMap();

CounterCell c1 =
baseMetricContainerRegistry.getContainer(STEP1).getCounter(MetricName.named("ns", "name1"));
CounterCell c2 =
baseMetricContainerRegistry
.getUnboundContainer()
.getCounter(MonitoringInfoTestUtil.testElementCountName());

c1.inc(7);
c2.inc(14);

MetricsContainerStepMap testObject = new MetricsContainerStepMap();
testObject.updateAll(baseMetricContainerRegistry);

List<MonitoringInfo> expected = new ArrayList<MonitoringInfo>();

SimpleMonitoringInfoBuilder builder = new SimpleMonitoringInfoBuilder();
builder.setUrnForUserMetric("ns", "name1");
builder.setPTransformLabel(STEP1);
builder.setInt64Value(7);
expected.add(builder.build());

expected.add(MonitoringInfoTestUtil.testElementCountMonitoringInfo(14));

ArrayList<MonitoringInfo> actual = new ArrayList<MonitoringInfo>();

for (MonitoringInfo mi : testObject.getMonitoringInfos()) {
actual.add(SimpleMonitoringInfoBuilder.clearTimestamp(mi));
}
assertThat(actual, containsInAnyOrder(expected.toArray()));
}

@Test
public void testAttemptedAndCommittedAccumulatedMetricResults() {
MetricsContainerStepMap attemptedMetrics = new MetricsContainerStepMap();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.beam.runners.core.metrics;

import java.util.HashMap;
import org.apache.beam.model.fnexecution.v1.BeamFnApi.MonitoringInfo;

/**
* Provides convenient one line factories for unit tests that need to generate test MonitoringInfos.
*/
public class MonitoringInfoTestUtil {
/** @return A basic MonitoringInfoMetricName to test. */
public static MonitoringInfoMetricName testElementCountName() {
HashMap labels = new HashMap<String, String>();
labels.put(SimpleMonitoringInfoBuilder.PCOLLECTION_LABEL, "testPCollection");
MonitoringInfoMetricName name =
MonitoringInfoMetricName.named(SimpleMonitoringInfoBuilder.ELEMENT_COUNT_URN, labels);
return name;
}

/** @return A basic MonitoringInfo which matches the testElementCountName. */
public static MonitoringInfo testElementCountMonitoringInfo(long value) {
SimpleMonitoringInfoBuilder builder = new SimpleMonitoringInfoBuilder();
builder.setUrn(SimpleMonitoringInfoBuilder.ELEMENT_COUNT_URN);
builder.setPCollectionLabel("testPCollection");
builder.setInt64Value(value);
return builder.build();
}
}
Loading