Repository: beam Updated Branches: refs/heads/master 4bba380cb -> 3082178b3
Remove periods from step name in SparkBeamMetric to avoid inconsistent hierarchies in Graphite metric sink Project: http://git-wip-us.apache.org/repos/asf/beam/repo Commit: http://git-wip-us.apache.org/repos/asf/beam/commit/b74f8fa3 Tree: http://git-wip-us.apache.org/repos/asf/beam/tree/b74f8fa3 Diff: http://git-wip-us.apache.org/repos/asf/beam/diff/b74f8fa3 Branch: refs/heads/master Commit: b74f8fa30aea130f990c152a1c91a509c3b6d6c9 Parents: 4bba380 Author: Aviem Zur <aviem...@gmail.com> Authored: Thu Feb 23 07:09:55 2017 +0200 Committer: Stas Levin <stasle...@apache.org> Committed: Mon Feb 27 14:15:43 2017 +0200 ---------------------------------------------------------------------- .../runners/spark/metrics/SparkBeamMetric.java | 13 ++++- .../spark/metrics/SparkBeamMetricTest.java | 60 ++++++++++++++++++++ 2 files changed, 70 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/beam/blob/b74f8fa3/runners/spark/src/main/java/org/apache/beam/runners/spark/metrics/SparkBeamMetric.java ---------------------------------------------------------------------- diff --git a/runners/spark/src/main/java/org/apache/beam/runners/spark/metrics/SparkBeamMetric.java b/runners/spark/src/main/java/org/apache/beam/runners/spark/metrics/SparkBeamMetric.java index 8e31b22..8328a1a 100644 --- a/runners/spark/src/main/java/org/apache/beam/runners/spark/metrics/SparkBeamMetric.java +++ b/runners/spark/src/main/java/org/apache/beam/runners/spark/metrics/SparkBeamMetric.java @@ -19,6 +19,7 @@ package org.apache.beam.runners.spark.metrics; import com.codahale.metrics.Metric; +import com.google.common.annotations.VisibleForTesting; import java.util.HashMap; import java.util.Map; import org.apache.beam.sdk.metrics.DistributionResult; @@ -33,6 +34,7 @@ import org.apache.beam.sdk.metrics.MetricsFilter; */ class SparkBeamMetric implements Metric { private static final String ILLEGAL_CHARACTERS = "[^A-Za-z0-9\\._-]"; + private static final String ILLEGAL_CHARACTERS_AND_PERIOD = "[^A-Za-z0-9_-]"; private final SparkMetricResults metricResults = new SparkMetricResults(); @@ -54,9 +56,14 @@ class SparkBeamMetric implements Metric { return metrics; } - private String renderName(MetricResult<?> metricResult) { + @VisibleForTesting + String renderName(MetricResult<?> metricResult) { + String renderedStepName = metricResult.step().replaceAll(ILLEGAL_CHARACTERS_AND_PERIOD, "_"); + if (renderedStepName.endsWith("_")) { + renderedStepName = renderedStepName.substring(0, renderedStepName.length() - 1); + } MetricName metricName = metricResult.name(); - String rendered = metricResult.step() + "." + metricName.namespace() + "." + metricName.name(); - return rendered.replaceAll(ILLEGAL_CHARACTERS, "_"); + return (renderedStepName + "." + metricName.namespace() + "." + metricName.name()) + .replaceAll(ILLEGAL_CHARACTERS, "_"); } } http://git-wip-us.apache.org/repos/asf/beam/blob/b74f8fa3/runners/spark/src/test/java/org/apache/beam/runners/spark/metrics/SparkBeamMetricTest.java ---------------------------------------------------------------------- diff --git a/runners/spark/src/test/java/org/apache/beam/runners/spark/metrics/SparkBeamMetricTest.java b/runners/spark/src/test/java/org/apache/beam/runners/spark/metrics/SparkBeamMetricTest.java new file mode 100644 index 0000000..9426b2c --- /dev/null +++ b/runners/spark/src/test/java/org/apache/beam/runners/spark/metrics/SparkBeamMetricTest.java @@ -0,0 +1,60 @@ +/* + * 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.spark.metrics; + +import static org.hamcrest.Matchers.equalTo; +import static org.junit.Assert.assertThat; + +import org.apache.beam.sdk.metrics.MetricName; +import org.apache.beam.sdk.metrics.MetricResult; +import org.junit.Test; + + +/** + * Test SparkBeamMetric. + */ +public class SparkBeamMetricTest { + @Test + public void testRenderName() throws Exception { + MetricResult<Object> metricResult = new MetricResult<Object>() { + @Override + public MetricName name() { + return MetricName.named("myNameSpace//", "myName()"); + } + + @Override + public String step() { + return "myStep.one.two(three)"; + } + + @Override + public Object committed() { + return null; + } + + @Override + public Object attempted() { + return null; + } + }; + String renderedName = new SparkBeamMetric().renderName(metricResult); + assertThat("Metric name was not rendered correctly", renderedName, + equalTo("myStep_one_two_three.myNameSpace__.myName__")); + } +}