Repository navigation
[BEAM-6165] send metrics to Flink in portable Flink runner #7183
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| /* | ||
| * 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.flink.metrics; | ||
|
|
||
| import java.io.FileNotFoundException; | ||
| import java.io.FileOutputStream; | ||
| import java.io.PrintStream; | ||
| import org.apache.flink.metrics.Metric; | ||
| import org.apache.flink.metrics.MetricConfig; | ||
| import org.apache.flink.metrics.MetricGroup; | ||
| import org.apache.flink.metrics.reporter.AbstractReporter; | ||
|
|
||
| /** | ||
| * Flink {@link org.apache.flink.metrics.reporter.MetricReporter metrics reporter} for writing | ||
| * metrics to a file specified via the "metrics.reporter.file.path" config key (assuming an alias of | ||
| * "file" for this reporter in the "metrics.reporters" setting). | ||
| */ | ||
| public class FileReporter extends AbstractReporter { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rename this to
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I originally gave it a name like that, however I talked myself in to that this is a potentially more general-purpose abstraction, and it is Flink specific, so that this ( If it was lmk what you think!
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I get why it's not practical to have it in the test scope. That's fine. Still, I think
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
ah, I left the reporter alias as (update: I did this, and also left a placeholder in the This is a reporter that writes the final state of all metrics to a file of the user's choosing. It can do this in test or production environments. It doesn't perform any assertions, or otherwise do things that only make sense in a testing context. It is fairly similar to Slf4jReporter, but its output is easier to parse (for machines or humans), and would normally be configured like: To me, it feels weirder to name it However I understand if the possible non-test uses feel contrived, and perhaps having I just want to make sure the vestigial
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That sounds reasonable. But shouldn't we use a more distinct name to avoid confusion, like
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suppose it could come in handy when you just want to dump metrics locally. Since package is
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (woops, I'd not seen Max's comment above when I posted this, but seems like we're saying the same thing) fwiw: "beam", "flink", "metrics", "file", and "reporter" are already in this class' name: I generally dislike the Java convention of repeating package-name segments in class' basenames, but recognize that it's common and people seem to like it, so happy to add here, just wanted to float the idea that lmk if you still prefer it and I'll change it.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reason I brought it up is discoverability. Names like |
||
| @Override | ||
| public String filterCharacters(String input) { | ||
| return input; | ||
| } | ||
|
|
||
| private String path; | ||
| private PrintStream ps; | ||
|
|
||
| @Override | ||
| public void open(MetricConfig config) { | ||
| synchronized (this) { | ||
| if (path == null) { | ||
| path = config.getString("path", null); | ||
| log.info("Opening file: {}", path); | ||
| if (path == null) { | ||
| throw new IllegalStateException("FileReporter metrics config needs 'path' key"); | ||
| } | ||
| try { | ||
| FileOutputStream fos = new FileOutputStream(path); | ||
| ps = new PrintStream(fos); | ||
| } catch (FileNotFoundException e) { | ||
| throw new IllegalStateException("FileReporter couldn't open file", e); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void notifyOfRemovedMetric(Metric metric, String metricName, MetricGroup group) { | ||
| final String name = group.getMetricIdentifier(metricName, this); | ||
| super.notifyOfRemovedMetric(metric, metricName, group); | ||
| synchronized (this) { | ||
| ps.printf("%s: %s%n", name, Metrics.toString(metric)); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| ps.close(); | ||
| log.info("wrote metrics to {}", path); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -17,14 +17,19 @@ | |||||
| */ | ||||||
| package org.apache.beam.runners.flink.metrics; | ||||||
|
|
||||||
| import static org.apache.beam.model.fnexecution.v1.BeamFnApi.MonitoringInfoUrns.Enum.USER_COUNTER_URN_PREFIX; | ||||||
| import static org.apache.beam.runners.core.metrics.MetricsContainerStepMap.asAttemptedOnlyMetricResults; | ||||||
|
|
||||||
| import java.util.HashMap; | ||||||
| import java.util.List; | ||||||
| import java.util.Map; | ||||||
| import org.apache.beam.model.fnexecution.v1.BeamFnApi; | ||||||
| import org.apache.beam.runners.core.metrics.MetricsContainerImpl; | ||||||
| import org.apache.beam.runners.core.metrics.MetricsContainerStepMap; | ||||||
| import org.apache.beam.sdk.metrics.Distribution; | ||||||
| import org.apache.beam.sdk.metrics.DistributionResult; | ||||||
| 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.MetricResult; | ||||||
| import org.apache.beam.sdk.metrics.MetricResults; | ||||||
|
|
@@ -77,12 +82,72 @@ public FlinkMetricContainer(RuntimeContext runtimeContext) { | |||||
| this.metricsAccumulator = (MetricsAccumulator) metricsAccumulator; | ||||||
| } | ||||||
|
|
||||||
| MetricsContainer getMetricsContainer(String stepName) { | ||||||
| public MetricsContainer getMetricsContainer(String stepName) { | ||||||
| return metricsAccumulator != null | ||||||
| ? metricsAccumulator.getLocalValue().getContainer(stepName) | ||||||
| : null; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Parse a {@link MetricName} from a {@link | ||||||
| * org.apache.beam.model.fnexecution.v1.BeamFnApi.MonitoringInfoUrns.Enum} | ||||||
| * | ||||||
| * <p>Should be consistent with {@code parse_namespace_and_name} in monitoring_infos.py | ||||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wanted to flag this for @ajamato in particular as the author of the python code I mimicked here. How important is it that the python and java SDKs parse metric URNs into a {namespace, name} pairs in the same way? From what I can tell, It may not be critical, though we should still probably agree on one semantic interpretation of what is "namespace" vs "name" of e.g. With |
||||||
| * | ||||||
| * <p>TODO: not flink-specific; where should it live? | ||||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Flagging this as well, in case anyone has ideas. I would normally view this as a
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe that should be changed in
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure I follow; how could I put this |
||||||
| */ | ||||||
| public static MetricName parseUrn(String urn) { | ||||||
| if (urn.startsWith(USER_COUNTER_URN_PREFIX.toString())) { | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does calling toString on this work? The usage I have seen before involved extracting the attached object and pulling out a field from the enum. See BeamUrns.getUrns Line 27 in a5a139d
You could reference this file which pulls it in. SimpleMonitoringInfoBuilder Line 64 in dc10f75
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good catch, thanks! I just filed #7408 to fix |
||||||
| urn = urn.substring(USER_COUNTER_URN_PREFIX.toString().length()); | ||||||
| } | ||||||
| // 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); | ||||||
| } | ||||||
| return MetricName.named(pieces[0], pieces[1]); | ||||||
| } | ||||||
|
|
||||||
| public void updateMetrics(String stepName, List<BeamFnApi.MonitoringInfo> monitoringInfos) { | ||||||
| MetricsContainer metricsContainer = getMetricsContainer(stepName); | ||||||
| monitoringInfos.forEach( | ||||||
| monitoringInfo -> { | ||||||
| if (monitoringInfo.hasMetric()) { | ||||||
| String urn = monitoringInfo.getUrn(); | ||||||
| MetricName metricName = parseUrn(urn); | ||||||
| BeamFnApi.Metric metric = monitoringInfo.getMetric(); | ||||||
| if (metric.hasCounterData()) { | ||||||
| BeamFnApi.CounterData counterData = metric.getCounterData(); | ||||||
| org.apache.beam.sdk.metrics.Counter counter = metricsContainer.getCounter(metricName); | ||||||
| if (counterData.getValueCase() == BeamFnApi.CounterData.ValueCase.INT64_VALUE) { | ||||||
| counter.inc(counterData.getInt64Value()); | ||||||
| } else { | ||||||
| throw new IllegalArgumentException("Unsupported CounterData type: " + counterData); | ||||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||
| } | ||||||
| } else if (metric.hasDistributionData()) { | ||||||
| BeamFnApi.DistributionData distributionData = metric.getDistributionData(); | ||||||
| Distribution distribution = metricsContainer.getDistribution(metricName); | ||||||
| if (distributionData.hasIntDistributionData()) { | ||||||
| BeamFnApi.IntDistributionData intDistributionData = | ||||||
| distributionData.getIntDistributionData(); | ||||||
| distribution.update( | ||||||
| intDistributionData.getSum(), | ||||||
| intDistributionData.getCount(), | ||||||
| intDistributionData.getMin(), | ||||||
| intDistributionData.getMax()); | ||||||
| } else { | ||||||
| throw new IllegalArgumentException( | ||||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. likewise, |
||||||
| "Unsupported DistributionData type: " + distributionData); | ||||||
| } | ||||||
| } else if (metric.hasExtremaData()) { | ||||||
| BeamFnApi.ExtremaData extremaData = metric.getExtremaData(); | ||||||
| throw new IllegalArgumentException("Extrema metric unsupported: " + extremaData); | ||||||
| } | ||||||
| } | ||||||
| }); | ||||||
| updateMetrics(stepName); | ||||||
| } | ||||||
|
|
||||||
| void updateMetrics(String stepName) { | ||||||
| MetricResults metricResults = asAttemptedOnlyMetricResults(metricsAccumulator.getLocalValue()); | ||||||
| MetricQueryResults metricQueryResults = | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| /* | ||
| * 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.flink.metrics; | ||
|
|
||
| import org.apache.flink.metrics.Counter; | ||
| import org.apache.flink.metrics.Gauge; | ||
| import org.apache.flink.metrics.Histogram; | ||
| import org.apache.flink.metrics.HistogramStatistics; | ||
| import org.apache.flink.metrics.Meter; | ||
| import org.apache.flink.metrics.Metric; | ||
|
|
||
| /** Helper for pretty-printing {@link Metric Flink metrics}. */ | ||
| public class Metrics { | ||
| public static String toString(Metric metric) { | ||
| if (metric instanceof Counter) { | ||
| return Long.toString(((Counter) metric).getCount()); | ||
| } else if (metric instanceof Gauge) { | ||
| return ((Gauge) metric).getValue().toString(); | ||
| } else if (metric instanceof Meter) { | ||
| return Double.toString(((Meter) metric).getRate()); | ||
| } else if (metric instanceof Histogram) { | ||
| HistogramStatistics stats = ((Histogram) metric).getStatistics(); | ||
| return String.format( | ||
| "count=%d, min=%d, max=%d, mean=%f, stddev=%f, p50=%f, p75=%f, p95=%f", | ||
| stats.size(), | ||
| stats.getMin(), | ||
| stats.getMax(), | ||
| stats.getMean(), | ||
| stats.getStdDev(), | ||
| stats.getQuantile(0.5), | ||
| stats.getQuantile(0.75), | ||
| stats.getQuantile(0.95)); | ||
| } else { | ||
| throw new IllegalStateException( | ||
| String.format( | ||
| "Cannot remove unknown metric type %s. This indicates that the reporter " | ||
| + "does not support this metric type.", | ||
| metric.getClass().getName())); | ||
| } | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.