apache / apache/beam

[Bug]: Fn Logging doesn't work in ParDo#with_exception_handling(use_subprocess=True)

Open
#25,573 0 comments 0 reactions 0 assignees View on GitHub
bug P2 python
Dominant language
Java
Stars
8.7k
Forks
4.7k
Avg merge
1d 20h
Merged PRs (30d)
196

Description

### What happened?

In the Python SDK, logging (via Fn Logging API) doesn't work when running a ParDo in a subprocess with `ParDo#with_exception_handling(use_subprocess=True)`. This is because the Fn Log API gRPC channel breaks when the DoFn gets pickled and sent to the subprocess.

Logging doesn't work here:
```py
import logging

logging.basicConfig(level=logging.INFO)

class MyDoFn(beam.DoFn):
def process(self, element):
logging.getLogger().info(element)
```

But if I reestablish the Fn Log API gRPC channel, it works:

```py
import logging

logging.basicConfig(level=logging.INFO)

class MyDoFn(beam.DoFn):
def setup(self):
import os
from apache_beam.portability.api import endpoints_pb2
from google.protobuf import text_format

# Copied from https://github.com/apache/beam/blob/master/sdks/python/apache_beam/runners/worker/sdk_worker_main.py#L82-L83
if "LOGGING_API_SERVICE_DESCRIPTOR" in os.environ:
logging_service_descriptor = endpoints_pb2.ApiServiceDescriptor()
text_format.Merge(
os.environ["LOGGING_API_SERVICE_DESCRIPTOR"], logging_service_descriptor
)

fn_log_handler = FnApiLogRecordHandler(logging_service_descriptor)
logging.getLogger().setLevel(logging.INFO)
logging.getLogger().addHandler(fn_log_handler)

def process(self, element):
logging.info(element)
```

### Issue Priority

Priority: 2 (default / most bugs should be filed as P2)

### Issue Components

- [X] Component: Python SDK
- [ ] Component: Java SDK
- [ ] Component: Go SDK
- [ ] Component: Typescript SDK
- [ ] Component: IO connector
- [ ] Component: Beam examples
- [ ] Component: Beam playground
- [ ] Component: Beam katas
- [ ] Component: Website
- [ ] Component: Spark Runner
- [ ] Component: Flink Runner
- [ ] Component: Samza Runner
- [ ] Component: Twister2 Runner
- [ ] Component: Hazelcast Jet Runner
- [ ] Component: Google Cloud Dataflow Runner

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.