code: def test_metrics_union(ray_start_regular_shared):
      it1 = from_items([1, 2, 3, 4], num_shards=1)
      it2 = from_items([1, 2, 3, 4], num_shards=1)
  
      def foo_metrics(x):
          metrics = LocalIterator.get_metrics()
          metrics.counters["foo"] += x
          return metrics.counters["foo"]
  
      def bar_metrics(x):
          metrics = LocalIterator.get_metrics()
          metrics.counters["bar"] += 100
          return metrics.counters["bar"]
  
      def verify_metrics(x):
          metrics = LocalIterator.get_metrics()
          metrics.counters["n"] += 1
          # Check the metrics context is shared.
          if metrics.counters["n"] >= 2:
              assert "foo" in metrics.counters
              assert "bar" in metrics.counters
          return x
  
      it1 = it1.gather_async().for_each(foo_metrics)
      it2 = it2.gather_async().for_each(bar_metrics)
      it3 = it1.union(it2, deterministic=True)
      it3 = it3.for_each(verify_metrics)
      assert it3.take(10) == [1, 100, 3, 200, 6, 300, 10, 400]
  
imports: import sys
  import time
  import collections
  from collections import Counter
  import pytest
  import ray
  from ray.util.iter import from_items, from_iterators, from_range, from_actors, ParallelIteratorWorker, LocalIterator
  from ray._private.test_utils import Semaphore
label_desc: Use image node_id 5 for calling a global function with example usage: test_metrics_union(ray_start_regular_shared) and returns: metrics, metrics, x