Skip to content

Commit f722133

Browse files
dangusevtbarbugli
authored andcommitted
Replace twirp.context.Context with a wrapper class to fix the logging issue
1 parent 64a5aa1 commit f722133

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

getstream/video/rtc/connection_manager.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@
66

77
import aioice
88
import aiortc
9-
from twirp.context import Context
109

1110
from getstream.common import telemetry
1211
from getstream.utils import StreamAsyncIOEventEmitter
1312
from getstream.video.rtc.coordinator.ws import StreamAPIWS
1413
from getstream.video.rtc.pb.stream.video.sfu.event import events_pb2
1514
from getstream.video.rtc.pb.stream.video.sfu.models import models_pb2
1615
from getstream.video.rtc.pb.stream.video.sfu.signal_rpc import signal_pb2
17-
from getstream.video.rtc.twirp_client_wrapper import SfuRpcError, SignalClient
16+
from getstream.video.rtc.twirp_client_wrapper import SfuRpcError, SignalClient, Context
1817

1918
from getstream.video.async_call import Call
2019
from getstream.video.rtc.connection_utils import (

getstream/video/rtc/twirp_client_wrapper.py

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
# -*- coding: utf-8 -*-
2-
import logging
3-
import functools
42
import asyncio # Import asyncio if not already present
3+
import functools
4+
import logging
5+
6+
import structlog
7+
from structlog.stdlib import LoggerFactory, add_log_level
8+
from structlog.types import BindableLogger
9+
from twirp.context import Context as TwirpContext
510

611
from getstream.common import telemetry
712
from getstream.video.rtc.pb.stream.video.sfu.models import models_pb2
@@ -13,6 +18,42 @@
1318
logger = logging.getLogger(__name__)
1419

1520

21+
def _get_twirp_default_logger() -> BindableLogger:
22+
"""Re-generate the default structlog config used inside `twirp` without overriding
23+
the root logger level.
24+
A trimmed down copy of `twirp.logging.configure`"""
25+
26+
structlog.configure(
27+
logger_factory=LoggerFactory(),
28+
processors=[
29+
add_log_level,
30+
# Add timestamp
31+
structlog.processors.TimeStamper("iso"),
32+
# Add stack information
33+
structlog.processors.StackInfoRenderer(),
34+
# Set exception field using exec info
35+
structlog.processors.format_exc_info,
36+
# Render event_dict as JSON
37+
structlog.processors.JSONRenderer(),
38+
],
39+
)
40+
return structlog.get_logger()
41+
42+
43+
_TWIRP_DEFAULT_LOGGER = _get_twirp_default_logger()
44+
45+
46+
class Context(TwirpContext):
47+
"""A wrapper around `twirp.context.Context` to fix the logging issue.
48+
Use it as a replacement.
49+
"""
50+
51+
def __init__(self, *args, headers=None):
52+
# Passing our own logger here because twirp configures the root logger
53+
# under the hood, making every other library noisy.
54+
super().__init__(*args, logger=_TWIRP_DEFAULT_LOGGER, headers=headers)
55+
56+
1657
# Define a custom exception for SFU RPC errors
1758
class SfuRpcError(Exception):
1859
"""Exception raised when an SFU RPC call returns an error."""
@@ -137,4 +178,5 @@ async def wrapped_method(*args, **kwargs):
137178
__all__ = [
138179
"SfuRpcError",
139180
"SignalClient",
181+
"Context",
140182
]

tests/test_twirp_client_wrapper.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@
66
# import twirp.async_client # <-- REMOVE THIS DEBUG IMPORT
77

88
# Modules to test
9-
from getstream.video.rtc.twirp_client_wrapper import SignalClient, SfuRpcError
9+
from getstream.video.rtc.twirp_client_wrapper import SignalClient, SfuRpcError, Context
1010

1111
# Protobufs needed for requests/responses and error codes
1212
from getstream.video.rtc.pb.stream.video.sfu.signal_rpc import signal_pb2
1313
from getstream.video.rtc.pb.stream.video.sfu.models import models_pb2
14-
from twirp.context import Context
1514

1615
# No longer need to patch the base class path
1716
# ASYNC_CLIENT_PATH = "getstream.video.rtc.twirp_client_wrapper.AsyncSignalServerClient"

0 commit comments

Comments
 (0)