1818package org .apache .beam .it .common .dataflow ;
1919
2020import static org .apache .beam .it .common .logging .LogStrings .formatForLogging ;
21- import static org .apache .beam .sdk .testing .TestPipeline .PROPERTY_BEAM_TEST_PIPELINE_OPTIONS ;
2221import static org .apache .beam .vendor .guava .v32_1_2_jre .com .google .common .base .Preconditions .checkState ;
2322
2423import com .fasterxml .jackson .databind .ObjectMapper ;
4140import java .util .stream .StreamSupport ;
4241import org .apache .beam .it .common .PipelineLauncher ;
4342import org .apache .beam .it .common .utils .PipelineUtils ;
44- import org .apache .beam .it .common .IOLoadTestBase ;
4543import org .apache .beam .runners .dataflow .DataflowPipelineJob ;
4644import org .apache .beam .sdk .PipelineResult ;
4745import org .apache .beam .sdk .metrics .DistributionResult ;
@@ -73,6 +71,8 @@ public class DefaultPipelineLauncher extends AbstractPipelineLauncher {
7371 private static final String READ_PIPELINE_NAME_OVERWRITE = "readPipelineNameOverride" ;
7472 private static final String WRITE_PIPELINE_NAME_OVERWRITE = "writePipelineNameOverride" ;
7573 private static final Pattern JOB_ID_PATTERN = Pattern .compile ("Submitted job: (\\ S+)" );
74+ /** Namespace for Beam provided pipeline metrics (set up by Metrics transform). */
75+ public static final String BEAM_METRICS_NAMESPACE = "BEAM_METRICS" ;
7676
7777 // For unsupported runners (other than dataflow), implement launcher methods by operating with
7878 // PipelineResult.
@@ -101,6 +101,18 @@ public class DefaultPipelineLauncher extends AbstractPipelineLauncher {
101101 .put (PipelineResult .State .UNRECOGNIZED , JobState .UNKNOWN )
102102 .build ();
103103
104+ // To make PipelineLauncher.getMetric work in a unified way for both runner provided metrics and
105+ // pipeline defined
106+ // metrics, here we wrap Beam provided metrics as a pre-defined metrics name
107+ // [name_space:metric_type:metric_name
108+ // which will be recognized by getMetric method
109+ public enum PipelineMetricsType {
110+ COUNTER ,
111+ STARTTIME ,
112+ ENDTIME ,
113+ RUNTIME ,
114+ }
115+
104116 private DefaultPipelineLauncher (Builder builder ) {
105117 super (
106118 new Dataflow (
@@ -169,7 +181,7 @@ private static <T> void checkIfMetricResultIsUnique(
169181 resultCount <= 1 ,
170182 "More than one metric result matches name: %s in namespace %s. Metric results count: %s" ,
171183 name ,
172- IOLoadTestBase . BEAM_METRICS_NAMESPACE ,
184+ BEAM_METRICS_NAMESPACE ,
173185 resultCount );
174186 }
175187
@@ -181,14 +193,14 @@ private static Iterable<MetricResult<DistributionResult>> getDistributions(
181193 .queryMetrics (
182194 MetricsFilter .builder ()
183195 .addNameFilter (
184- MetricNameFilter .named (IOLoadTestBase . BEAM_METRICS_NAMESPACE , metricName ))
196+ MetricNameFilter .named (BEAM_METRICS_NAMESPACE , metricName ))
185197 .build ());
186198 return metrics .getDistributions ();
187199 }
188200
189201 /** Pull Beam pipeline defined metrics given the jobId. */
190202 public Long getBeamMetric (
191- String jobId , IOLoadTestBase . PipelineMetricsType metricType , String metricName ) {
203+ String jobId , PipelineMetricsType metricType , String metricName ) {
192204 PipelineResult pipelineResult =
193205 MANAGED_JOBS .getOrDefault (jobId , UNMANAGED_JOBS .getOrDefault (jobId , null ));
194206 if (pipelineResult != null ) {
@@ -198,7 +210,7 @@ public Long getBeamMetric(
198210 .queryMetrics (
199211 MetricsFilter .builder ()
200212 .addNameFilter (
201- MetricNameFilter .named (IOLoadTestBase . BEAM_METRICS_NAMESPACE , metricName ))
213+ MetricNameFilter .named (BEAM_METRICS_NAMESPACE , metricName ))
202214 .build ());
203215
204216 switch (metricType ) {
@@ -212,7 +224,7 @@ public Long getBeamMetric(
212224 LOG .error (
213225 "Failed to get metric {}, from namespace {}" ,
214226 metricName ,
215- IOLoadTestBase . BEAM_METRICS_NAMESPACE );
227+ BEAM_METRICS_NAMESPACE );
216228 }
217229 return UNKNOWN_METRIC_VALUE ;
218230 case STARTTIME :
@@ -230,9 +242,9 @@ public Long getBeamMetric(
230242 .map (element -> Objects .requireNonNull (element .getAttempted ()).getMax ())
231243 .max (Long ::compareTo )
232244 .orElse (UNKNOWN_METRIC_VALUE );
233- if (metricType == IOLoadTestBase . PipelineMetricsType .STARTTIME ) {
245+ if (metricType == PipelineMetricsType .STARTTIME ) {
234246 return lowestMin ;
235- } else if (metricType == IOLoadTestBase . PipelineMetricsType .ENDTIME ) {
247+ } else if (metricType == PipelineMetricsType .ENDTIME ) {
236248 return greatestMax ;
237249 } else {
238250 if (lowestMin != UNKNOWN_METRIC_VALUE && greatestMax != UNKNOWN_METRIC_VALUE ) {
@@ -254,15 +266,15 @@ public Long getBeamMetric(
254266 @ Override
255267 public Double getMetric (String project , String region , String jobId , String metricName )
256268 throws IOException {
257- if (metricName .startsWith (IOLoadTestBase . BEAM_METRICS_NAMESPACE )) {
269+ if (metricName .startsWith (BEAM_METRICS_NAMESPACE )) {
258270 String [] nameSpacedMetrics = metricName .split (":" , 3 );
259271 Preconditions .checkState (
260272 nameSpacedMetrics .length == 3 ,
261273 String .format (
262274 "Invalid Beam metrics name: %s, expected: '%s:metric_type:metric_name'" ,
263- metricName , IOLoadTestBase . BEAM_METRICS_NAMESPACE ));
264- IOLoadTestBase . PipelineMetricsType metricType =
265- IOLoadTestBase . PipelineMetricsType .valueOf (nameSpacedMetrics [1 ]);
275+ metricName , BEAM_METRICS_NAMESPACE ));
276+ PipelineMetricsType metricType =
277+ PipelineMetricsType .valueOf (nameSpacedMetrics [1 ]);
266278
267279 // Pipeline defined metrics are long values. Have to cast to double that is what the base
268280 // class defined.
@@ -437,16 +449,15 @@ private List<String> extractOptions(String project, String region, LaunchConfig
437449 // add pipeline options from beamTestPipelineOptions system property to preserve the
438450 // pipeline options already set in TestPipeline.
439451 @ Nullable
440- String beamTestPipelineOptions = System .getProperty (PROPERTY_BEAM_TEST_PIPELINE_OPTIONS );
452+ String beamTestPipelineOptions = System .getProperty (org . apache . beam . sdk . testing . TestPipeline . PROPERTY_BEAM_TEST_PIPELINE_OPTIONS );
441453 if (!Strings .isNullOrEmpty (beamTestPipelineOptions )) {
442454 try {
443455 additionalOptions .addAll (MAPPER .readValue (beamTestPipelineOptions , List .class ));
444456 } catch (IOException e ) {
445457 throw new RuntimeException (
446458 "Unable to instantiate test options from system property "
447- + PROPERTY_BEAM_TEST_PIPELINE_OPTIONS
448- + ":"
449- + System .getProperty (PROPERTY_BEAM_TEST_PIPELINE_OPTIONS ),
459+ + org .apache .beam .sdk .testing .TestPipeline .PROPERTY_BEAM_TEST_PIPELINE_OPTIONS
460+ + ":" + beamTestPipelineOptions ,
450461 e );
451462 }
452463 }
0 commit comments