pushpendu1991 commented on code in PR #64900: URL: https://github.com/apache/airflow/pull/64900#discussion_r3287991990
########## providers/google/tests/system/google/cloud/dataflow/example_dataflow_get_metrics.py: ########## @@ -0,0 +1,133 @@ +# 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. + +""" +Example Airflow DAG for Google Cloud Dataflow Get Metrics Operator. + +This DAG demonstrates how to use DataflowJobMetricsOperator to: +1. Collect metrics from a Dataflow job +2. Pass metrics to a callback function for processing +3. Return metrics directly for XCom consumption when no callback is provided +4. Use deferrable mode for async execution +5. Consume metrics from XCom in downstream tasks +""" + +from __future__ import annotations + +import os +from datetime import datetime + +from airflow import DAG +from airflow.operators.empty import EmptyOperator +from airflow.operators.python import PythonOperator +from airflow.providers.google.cloud.operators.dataflow import DataflowJobMetricsOperator + +ENV_ID = os.environ.get("SYSTEM_TESTS_ENV_ID", "default") +PROJECT_ID = os.environ.get("SYSTEM_TESTS_GCP_PROJECT") or "test-project" +DAG_ID = "dataflow_get_metrics" +LOCATION = "us-central1" + + +def process_metrics_callback(metrics): + """Callback function that processes metrics returned by the operator.""" + metric_list = metrics if isinstance(metrics, list) else [] + print(f"Metrics count from callback: {len(metric_list)}") + return {"processed_metrics_count": len(metric_list)} + + +def consume_metrics_from_xcom(**context): + """Consume and display metrics count from XCom.""" + task_instance = context["task_instance"] + metrics = task_instance.xcom_pull(task_ids="collect_metrics_no_callback", key="metrics") Review Comment: extracting from return_value now -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
