Skip to content

Commit 82952c8

Browse files
authored
Update python metrics example in programming guide (#36334)
1 parent 800e2d7 commit 82952c8

2 files changed

Lines changed: 40 additions & 9 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import unittest
2+
3+
import apache_beam as beam
4+
from apache_beam import metrics
5+
from apache_beam.runners.direct.direct_runner import BundleBasedDirectRunner
6+
7+
8+
class ProgrammingGuideTest(unittest.TestCase):
9+
def test_metrics_example(self):
10+
class MyMetricsDoFn(beam.DoFn):
11+
def __init__(self):
12+
super().__init__()
13+
self.counter = metrics.Metrics.counter("namespace", "counter1")
14+
15+
def process(self, element):
16+
self.counter.inc()
17+
yield element
18+
19+
with beam.Pipeline(runner=BundleBasedDirectRunner()) as p:
20+
_ = (p | beam.Create([1, 2, 3]) | beam.ParDo(MyMetricsDoFn()))
21+
22+
metrics_filter = metrics.MetricsFilter().with_name("counter1")
23+
query_result = p.result.metrics().query(metrics_filter)
24+
25+
for metric in query_result["counters"]:
26+
print(metric)
27+
28+
# Not in example but just to confirm that anything is returned
29+
assert query_result["counters"]
30+
31+
32+
if __name__ == '__main__':
33+
unittest.main()

website/www/site/content/en/documentation/programming-guide.md

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6422,22 +6422,20 @@ public class MyMetricsDoFn extends DoFn<Integer, Integer> {
64226422
{{< highlight py >}}
64236423
class MyMetricsDoFn(beam.DoFn):
64246424
def __init__(self):
6425+
super().__init__()
64256426
self.counter = metrics.Metrics.counter("namespace", "counter1")
64266427

64276428
def process(self, element):
6428-
counter.inc()
6429+
self.counter.inc()
64296430
yield element
64306431

6431-
pipeline = beam.Pipeline()
6432-
6433-
pipeline | beam.ParDo(MyMetricsDoFn())
6434-
6435-
result = pipeline.run().wait_until_finish()
6432+
with beam.Pipeline() as p:
6433+
p | beam.Create([1, 2, 3]) | beam.ParDo(MyMetricsDoFn())
64366434

6437-
metrics = result.metrics().query(
6438-
metrics.MetricsFilter.with_namespace("namespace").with_name("counter1"))
6435+
metrics_filter = metrics.MetricsFilter().with_name("counter1")
6436+
query_result = p.result.metrics().query(metrics_filter)
64396437

6440-
for metric in metrics["counters"]:
6438+
for metric in query_result["counters"]:
64416439
print(metric)
64426440
{{< /highlight >}}
64436441

0 commit comments

Comments
 (0)