From 88aec29ba4953d2b141fece345d06d0532993eb5 Mon Sep 17 00:00:00 2001 From: Joey Tran Date: Tue, 30 Sep 2025 18:16:54 +0100 Subject: [PATCH 1/5] update programming guide metrics example --- .../content/en/documentation/programming-guide.md | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/website/www/site/content/en/documentation/programming-guide.md b/website/www/site/content/en/documentation/programming-guide.md index 14029b9babaf..05d7dccd78ba 100644 --- a/website/www/site/content/en/documentation/programming-guide.md +++ b/website/www/site/content/en/documentation/programming-guide.md @@ -6327,17 +6327,14 @@ class MyMetricsDoFn(beam.DoFn): self.counter = metrics.Metrics.counter("namespace", "counter1") def process(self, element): - counter.inc() + self.counter.inc() yield element -pipeline = beam.Pipeline() - -pipeline | beam.ParDo(MyMetricsDoFn()) - -result = pipeline.run().wait_until_finish() +with beam.Pipeline(runner=BundleBasedDirectRunner()) as p: + p | beam.Create([1, 2, 3]) | beam.ParDo(MyMetricsDoFn()) -metrics = result.metrics().query( - metrics.MetricsFilter.with_namespace("namespace").with_name("counter1")) +metrics = p.result.metrics().query( +metrics.MetricsFilter().with_namespace("namespace").with_name("counter1")) for metric in metrics["counters"]: print(metric) From 764138b4c4cc18e7c59a33c9a555f445c66dad61 Mon Sep 17 00:00:00 2001 From: Joey Tran Date: Tue, 30 Sep 2025 18:22:05 +0100 Subject: [PATCH 2/5] add test --- .../apache_beam/programming_guide_test.py | 30 +++++++++++++++++++ .../en/documentation/programming-guide.md | 4 +-- 2 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 sdks/python/apache_beam/programming_guide_test.py diff --git a/sdks/python/apache_beam/programming_guide_test.py b/sdks/python/apache_beam/programming_guide_test.py new file mode 100644 index 000000000000..339226e70268 --- /dev/null +++ b/sdks/python/apache_beam/programming_guide_test.py @@ -0,0 +1,30 @@ +import apache_beam as beam +from apache_beam import metrics +from apache_beam.runners.direct.direct_runner import BundleBasedDirectRunner + +import unittest + + +class ProgrammingGuideTest(unittest.TestCase): + def test_metrics_example(self): + class MyMetricsDoFn(beam.DoFn): + def __init__(self): + super().__init__() + self.counter = metrics.Metrics.counter("namespace", "counter1") + + def process(self, element): + self.counter.inc() + yield element + + with beam.Pipeline(runner=BundleBasedDirectRunner()) as p: + _ = p | beam.Create([1, 2, 3]) | beam.ParDo(MyMetricsDoFn()) + + metrics_ = p.result.metrics().query( + metrics.MetricsFilter().with_namespace("namespace").with_name( + "counter1")) + + for metric in metrics_["counters"]: + print(metric) + + # Not in example but just to confirm that anything is returned + assert metrics_["counters"] diff --git a/website/www/site/content/en/documentation/programming-guide.md b/website/www/site/content/en/documentation/programming-guide.md index 05d7dccd78ba..ef12a611837e 100644 --- a/website/www/site/content/en/documentation/programming-guide.md +++ b/website/www/site/content/en/documentation/programming-guide.md @@ -6333,10 +6333,10 @@ class MyMetricsDoFn(beam.DoFn): with beam.Pipeline(runner=BundleBasedDirectRunner()) as p: p | beam.Create([1, 2, 3]) | beam.ParDo(MyMetricsDoFn()) -metrics = p.result.metrics().query( +metrics_ = p.result.metrics().query( metrics.MetricsFilter().with_namespace("namespace").with_name("counter1")) -for metric in metrics["counters"]: +for metric in metrics_["counters"]: print(metric) {{< /highlight >}} From ca9c3b33631bfd44d3197d314628be0bc426d694 Mon Sep 17 00:00:00 2001 From: Joey Tran Date: Tue, 30 Sep 2025 20:11:28 -0400 Subject: [PATCH 3/5] isort --- sdks/python/apache_beam/programming_guide_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sdks/python/apache_beam/programming_guide_test.py b/sdks/python/apache_beam/programming_guide_test.py index 339226e70268..93beeb50c889 100644 --- a/sdks/python/apache_beam/programming_guide_test.py +++ b/sdks/python/apache_beam/programming_guide_test.py @@ -1,9 +1,9 @@ +import unittest + import apache_beam as beam from apache_beam import metrics from apache_beam.runners.direct.direct_runner import BundleBasedDirectRunner -import unittest - class ProgrammingGuideTest(unittest.TestCase): def test_metrics_example(self): From 02549970e17717c425222ab68ed2bf41da0bc2eb Mon Sep 17 00:00:00 2001 From: Joey Tran Date: Tue, 30 Sep 2025 21:25:59 -0400 Subject: [PATCH 4/5] add unitt test main call --- sdks/python/apache_beam/programming_guide_test.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/sdks/python/apache_beam/programming_guide_test.py b/sdks/python/apache_beam/programming_guide_test.py index 93beeb50c889..a110c6e3b0c1 100644 --- a/sdks/python/apache_beam/programming_guide_test.py +++ b/sdks/python/apache_beam/programming_guide_test.py @@ -28,3 +28,7 @@ def process(self, element): # Not in example but just to confirm that anything is returned assert metrics_["counters"] + + +if __name__ == '__main__': + unittest.main() From 1191496e6786bb68ad3ef59fab1127bac01952ad Mon Sep 17 00:00:00 2001 From: Joey Tran Date: Wed, 1 Oct 2025 14:23:23 -0400 Subject: [PATCH 5/5] update example --- sdks/python/apache_beam/programming_guide_test.py | 11 +++++------ .../content/en/documentation/programming-guide.md | 9 +++++---- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/sdks/python/apache_beam/programming_guide_test.py b/sdks/python/apache_beam/programming_guide_test.py index a110c6e3b0c1..28d4bddd798e 100644 --- a/sdks/python/apache_beam/programming_guide_test.py +++ b/sdks/python/apache_beam/programming_guide_test.py @@ -17,17 +17,16 @@ def process(self, element): yield element with beam.Pipeline(runner=BundleBasedDirectRunner()) as p: - _ = p | beam.Create([1, 2, 3]) | beam.ParDo(MyMetricsDoFn()) + _ = (p | beam.Create([1, 2, 3]) | beam.ParDo(MyMetricsDoFn())) - metrics_ = p.result.metrics().query( - metrics.MetricsFilter().with_namespace("namespace").with_name( - "counter1")) + metrics_filter = metrics.MetricsFilter().with_name("counter1") + query_result = p.result.metrics().query(metrics_filter) - for metric in metrics_["counters"]: + for metric in query_result["counters"]: print(metric) # Not in example but just to confirm that anything is returned - assert metrics_["counters"] + assert query_result["counters"] if __name__ == '__main__': diff --git a/website/www/site/content/en/documentation/programming-guide.md b/website/www/site/content/en/documentation/programming-guide.md index ef12a611837e..93b8a367e0ee 100644 --- a/website/www/site/content/en/documentation/programming-guide.md +++ b/website/www/site/content/en/documentation/programming-guide.md @@ -6324,19 +6324,20 @@ public class MyMetricsDoFn extends DoFn { {{< highlight py >}} class MyMetricsDoFn(beam.DoFn): def __init__(self): + super().__init__() self.counter = metrics.Metrics.counter("namespace", "counter1") def process(self, element): self.counter.inc() yield element -with beam.Pipeline(runner=BundleBasedDirectRunner()) as p: +with beam.Pipeline() as p: p | beam.Create([1, 2, 3]) | beam.ParDo(MyMetricsDoFn()) -metrics_ = p.result.metrics().query( -metrics.MetricsFilter().with_namespace("namespace").with_name("counter1")) +metrics_filter = metrics.MetricsFilter().with_name("counter1") +query_result = p.result.metrics().query(metrics_filter) -for metric in metrics_["counters"]: +for metric in query_result["counters"]: print(metric) {{< /highlight >}}