github-actions[bot] commented on code in PR #40885:
URL: https://github.com/apache/beam/pull/40885#discussion_r4413970068
##########
sdks/python/apache_beam/yaml/integration_tests.py:
##########
@@ -1214,6 +1214,130 @@ def
temp_pubsub_emulator(project_id="apache-beam-testing"):
yield created_topic_object.name
[email protected]
+def temp_pubsub_subscriptions(project_id="apache-beam-testing"):
+ """Context manager providing a temporary Pub/Sub emulator with input and
+ deadletter topics and subscriptions, plus bounded ReadFromPubSub and
+ WriteToPubSub transforms compatible with FnApiRunner for E2E testing.
+ """
+ from google.api_core import exceptions as gcp_exceptions
+
+ with PubSubContainer(project=project_id) as pubsub_container:
+ emulator_host = pubsub_container.get_pubsub_emulator_host()
+ with mock.patch.dict(os.environ, {"PUBSUB_EMULATOR_HOST": emulator_host}):
+ publisher = pubsub_v1.PublisherClient()
+ subscriber = pubsub_v1.SubscriberClient()
+ suffix = uuid.uuid4().hex[:8]
+
+ input_topic =
f"projects/{pubsub_container.project}/topics/input-{suffix}"
+ input_sub = (
+ f"projects/{pubsub_container.project}/subscriptions/"
+ f"input-sub-{suffix}")
+ dlq_topic = (
+ f"projects/{pubsub_container.project}/topics/deadletter-{suffix}")
+ dlq_sub = (
+ f"projects/{pubsub_container.project}/subscriptions/"
+ f"deadletter-sub-{suffix}")
+
+ publisher.create_topic(name=input_topic)
+ subscriber.create_subscription(name=input_sub, topic=input_topic)
+ publisher.create_topic(name=dlq_topic)
+ subscriber.create_subscription(name=dlq_sub, topic=dlq_topic)
Review Comment:
**🟡 Medium**: `publisher` and `subscriber` gRPC clients are instantiated
here to create the topics and subscriptions, but are never closed.
Additionally, `_PublishFn` (lines 1315-1328) creates `self._pub_client =
pubsub_v1.PublisherClient()` in `setup()` without closing it in a `teardown()`
method. Please ensure all `PublisherClient` and `SubscriberClient` instances
are properly closed (e.g., via context managers or `teardown()`) to avoid
leaking gRPC channels and background threads.
##########
sdks/python/apache_beam/yaml/integration_tests.py:
##########
@@ -1214,6 +1214,130 @@ def
temp_pubsub_emulator(project_id="apache-beam-testing"):
yield created_topic_object.name
[email protected]
+def temp_pubsub_subscriptions(project_id="apache-beam-testing"):
+ """Context manager providing a temporary Pub/Sub emulator with input and
+ deadletter topics and subscriptions, plus bounded ReadFromPubSub and
+ WriteToPubSub transforms compatible with FnApiRunner for E2E testing.
+ """
+ from google.api_core import exceptions as gcp_exceptions
+
+ with PubSubContainer(project=project_id) as pubsub_container:
+ emulator_host = pubsub_container.get_pubsub_emulator_host()
+ with mock.patch.dict(os.environ, {"PUBSUB_EMULATOR_HOST": emulator_host}):
+ publisher = pubsub_v1.PublisherClient()
+ subscriber = pubsub_v1.SubscriberClient()
+ suffix = uuid.uuid4().hex[:8]
+
+ input_topic =
f"projects/{pubsub_container.project}/topics/input-{suffix}"
+ input_sub = (
+ f"projects/{pubsub_container.project}/subscriptions/"
+ f"input-sub-{suffix}")
+ dlq_topic = (
+ f"projects/{pubsub_container.project}/topics/deadletter-{suffix}")
+ dlq_sub = (
+ f"projects/{pubsub_container.project}/subscriptions/"
+ f"deadletter-sub-{suffix}")
+
+ publisher.create_topic(name=input_topic)
+ subscriber.create_subscription(name=input_sub, topic=input_topic)
+ publisher.create_topic(name=dlq_topic)
+ subscriber.create_subscription(name=dlq_sub, topic=dlq_topic)
+
+ class _BoundedReadFromPubSub(beam.PTransform):
+ def __init__(
+ self,
+ topic=None,
+ subscription=None,
+ id_label=None,
+ with_attributes=False,
+ timestamp_attribute=None):
+ super().__init__()
+ self._topic = topic
+ self._subscription = subscription
+ self._with_attributes = with_attributes
+
+ def expand(self, pbegin):
+ sub_name = self._subscription
Review Comment:
**🟡 Medium**: `ReadFromPubSub` allows specifying either `topic` or
`subscription` (as validated in `yaml_io.read_from_pubsub`), but
`_BoundedReadFromPubSub` ignores `self._topic` and passes `self._subscription`
(`None` when `topic` is used) directly to `sub_client.pull()`. If `self._topic`
is provided instead of `self._subscription`, this should either raise a clear
`ValueError` or create a temporary subscription for the topic.
##########
sdks/python/apache_beam/yaml/extended_tests/e2e/pubsub_to_splunk.yaml:
##########
@@ -0,0 +1,143 @@
+#
+# 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.
+#
+
+fixtures:
+ - name: PUBSUB_VARS
+ type: "apache_beam.yaml.integration_tests.temp_pubsub_subscriptions"
+ config:
+ project_id: "apache-beam-testing"
+ - name: FAKE_SPLUNK_SERVER
+ type:
"apache_beam.yaml.test_utils.splunk_test_utils.temp_fake_splunk_server"
+ config:
+ expected_records:
+ - { time: 1700000000, host: "test-host", source: "apache-beam-11a",
sourcetype: "test-type", index: "test-index", event: "Event for label 11a with
rank 1" }
+ - { time: 1700000000, host: "test-host", source: "apache-beam-37a",
sourcetype: "test-type", index: "test-index", event: "Event for label 37a with
rank 2" }
+ - { time: 1700000000, host: "test-host", source: "apache-beam-389a",
sourcetype: "test-type", index: "test-index", event: "Event for label 389a with
rank 3" }
+ - { time: 1700000000, host: "test-host", source: "apache-beam-3821b",
sourcetype: "test-type", index: "test-index", event: "Event for label 3821b
with rank 4" }
+
+pipelines:
+ # Pipeline 1: Publish test messages (valid and invalid) to Pub/Sub input
topic
+ - pipeline:
+ type: chain
+ transforms:
+ - type: Create
+ config:
+ elements:
+ - {label: "11a", rank: 1}
+ - {label: "37a", rank: 2}
+ - {label: "389a", rank: 3}
+ - {label: "3821b", rank: 4}
+ - {label: "broken", rank: -1}
+ - type: WriteToPubSub
+ config:
+ topic: "{PUBSUB_VARS[INPUT_TOPIC]}"
+ format: JSON
+
+ # Pipeline 2: Read from Pub/Sub subscription, transform and write to Splunk
HEC,
+ # and route failed records to Pub/Sub deadletter topic
+ - pipeline:
+ type: composite
+ transforms:
+ - type: ReadFromPubSub
+ name: ReadMessages
+ config:
+ subscription: "{PUBSUB_VARS[INPUT_SUBSCRIPTION]}"
+ format: JSON
+ schema:
+ type: object
+ properties:
+ label: {type: string}
+ rank: {type: integer}
+ required:
+ - label
+ - rank
+
+ - type: MapToFields
+ name: ConvertToSplunkEvents
+ input: ReadMessages
+ config:
+ language: python
+ fields:
+ time: 1700000000
+ host: "'test-host'"
+ source:
+ expression: "'apache-beam-' + label"
+ output_type: string
+ sourcetype: "'test-type'"
+ index: "'test-index'"
+ event:
+ callable: |
+ from typing import Optional
+ def format_event(row) -> Optional[str]:
+ if row.rank < 0:
+ return None
+ return str("Event for label " + row.label + " with rank "
+ str(row.rank))
+
+ - type: WriteToSplunk
+ name: WriteToSplunk
+ input: ConvertToSplunkEvents
+ config:
+ url: "{FAKE_SPLUNK_SERVER.url}"
+ token: "{FAKE_SPLUNK_SERVER.token}"
+ batch_count: 2
+ parallelism: 1
+ disable_certificate_validation: false
+ enable_batch_logs: true
+ enable_gzip_http_compression: true
+ error_handling:
+ output: write_errors
+
+ - type: MapToFields
+ name: FormatWriteErrors
+ input: WriteToSplunk.write_errors
+ config:
+ language: python
+ fields:
+ failed_source:
Review Comment:
**🟡 Medium**: In `SplunkWriteSchemaTransformProvider`, `failed_row` is a
nullable field that is only populated for row-to-`SplunkEvent` conversion
errors and is `null` (`None` in Python) when an actual HTTP write error occurs
against Splunk HEC. Accessing `failed_row.source` unconditionally will raise an
`AttributeError: 'NoneType' object has no attribute 'source'` on HTTP write
failures. Consider guarding against `None` (e.g., `failed_row.source if
failed_row else None`).
```suggestion
expression: "failed_row.source if failed_row else ''"
```
--
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]