Skip to content

Commit 7e64a16

Browse files
committed
cleanup
1 parent 3ce89d8 commit 7e64a16

File tree

2 files changed

+20
-59
lines changed

2 files changed

+20
-59
lines changed

getstream/common/telemetry.py

Lines changed: 17 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44
import os
55
import time
66
from contextlib import contextmanager
7-
from typing import Any, Dict, Optional, Callable, Awaitable
7+
from typing import Any, Dict, Optional, Callable, Awaitable, TYPE_CHECKING
88
import inspect
99

10+
if TYPE_CHECKING:
11+
from getstream.chat.channel import Channel
12+
from getstream.video import BaseCall
13+
1014
# Optional OpenTelemetry imports with graceful fallback
1115
try:
1216
from opentelemetry import baggage, context as otel_context, metrics, trace
@@ -38,11 +42,6 @@
3842
"authorization,password,token,secret,api_key,authorization_bearer,auth",
3943
).split(",")
4044
}
41-
REDACT_API_KEY = os.getenv("STREAM_OTEL_REDACT_API_KEY", "true").lower() in {
42-
"1",
43-
"true",
44-
"yes",
45-
}
4645

4746

4847
def _noop_cm(): # pragma: no cover - used when OTel missing
@@ -205,22 +204,6 @@ def span_request(
205204
pass
206205

207206

208-
@contextmanager
209-
def with_call_context(call_cid: str):
210-
"""Attach `stream.call_cid` baggage for the scope of the context (no-op if OTel missing)."""
211-
if not _HAS_OTEL or baggage is None or otel_context is None: # pragma: no cover
212-
yield
213-
return
214-
ctx = baggage.set_baggage(
215-
"stream.call_cid", call_cid, context=otel_context.get_current()
216-
)
217-
token = otel_context.attach(ctx)
218-
try:
219-
yield
220-
finally:
221-
otel_context.detach(token)
222-
223-
224207
def current_operation(default: Optional[str] = None) -> Optional[str]:
225208
"""Return current logical operation name from baggage (stream.operation)."""
226209
if not _HAS_OTEL or baggage is None:
@@ -231,18 +214,12 @@ def current_operation(default: Optional[str] = None) -> Optional[str]:
231214

232215
# Decorators for auto-attaching baggage around method calls
233216
def attach_call_cid(func: Callable[..., Any]) -> Callable[..., Any]:
234-
def wrapper(self, *args, **kwargs):
235-
cid = None
236-
try:
237-
cid = f"{getattr(self, 'call_type', None)}:{getattr(self, 'id', None)}"
238-
except Exception:
239-
cid = None
240-
op = f"call.{getattr(func, '__name__', 'unknown')}"
241-
if cid and _HAS_OTEL and baggage is not None and otel_context is not None:
217+
def wrapper(self: BaseCall, *args, **kwargs):
218+
cid = f"{self.call_type}:{self.id}"
219+
if _HAS_OTEL and baggage is not None and otel_context is not None:
242220
ctx = baggage.set_baggage(
243221
"stream.call_cid", cid, context=otel_context.get_current()
244222
)
245-
ctx = baggage.set_baggage("stream.operation", op, context=ctx)
246223
token = otel_context.attach(ctx)
247224
try:
248225
return func(self, *args, **kwargs)
@@ -256,18 +233,12 @@ def wrapper(self, *args, **kwargs):
256233
def attach_call_cid_async(
257234
func: Callable[..., Awaitable[Any]],
258235
) -> Callable[..., Awaitable[Any]]:
259-
async def wrapper(self, *args, **kwargs):
260-
cid = None
261-
try:
262-
cid = f"{getattr(self, 'call_type', None)}:{getattr(self, 'id', None)}"
263-
except Exception:
264-
cid = None
265-
op = f"call.{getattr(func, '__name__', 'unknown')}"
266-
if cid and _HAS_OTEL and baggage is not None and otel_context is not None:
236+
async def wrapper(self: BaseCall, *args, **kwargs):
237+
cid = f"{self.call_type}:{self.id}"
238+
if _HAS_OTEL and baggage is not None and otel_context is not None:
267239
ctx = baggage.set_baggage(
268240
"stream.call_cid", cid, context=otel_context.get_current()
269241
)
270-
ctx = baggage.set_baggage("stream.operation", op, context=ctx)
271242
token = otel_context.attach(ctx)
272243
try:
273244
return await func(self, *args, **kwargs)
@@ -279,19 +250,13 @@ async def wrapper(self, *args, **kwargs):
279250

280251

281252
def attach_channel_cid(func: Callable[..., Any]) -> Callable[..., Any]:
282-
def wrapper(self, *args, **kwargs):
283-
cid = None
284-
try:
285-
cid = f"{getattr(self, 'channel_type', None)}:{getattr(self, 'channel_id', None)}"
286-
except Exception:
287-
cid = None
288-
op = f"channel.{getattr(func, '__name__', 'unknown')}"
289-
if cid and _HAS_OTEL and baggage is not None and otel_context is not None:
253+
def wrapper(self: Channel, *args, **kwargs):
254+
cid = f"{self.channel_type}:{self.channel_id}"
255+
if _HAS_OTEL and baggage is not None and otel_context is not None:
290256
# Attach channel_cid to baggage under a different key than call_cid
291257
ctx = baggage.set_baggage(
292258
"stream.channel_cid", cid, context=otel_context.get_current()
293259
)
294-
ctx = baggage.set_baggage("stream.operation", op, context=ctx)
295260
token = otel_context.attach(ctx)
296261
try:
297262
return func(self, *args, **kwargs)
@@ -305,18 +270,12 @@ def wrapper(self, *args, **kwargs):
305270
def attach_channel_cid_async(
306271
func: Callable[..., Awaitable[Any]],
307272
) -> Callable[..., Awaitable[Any]]:
308-
async def wrapper(self, *args, **kwargs):
309-
cid = None
310-
try:
311-
cid = f"{getattr(self, 'channel_type', None)}:{getattr(self, 'channel_id', None)}"
312-
except Exception:
313-
cid = None
314-
op = f"channel.{getattr(func, '__name__', 'unknown')}"
315-
if cid and _HAS_OTEL and baggage is not None and otel_context is not None:
273+
async def wrapper(self: Channel, *args, **kwargs):
274+
cid = f"{self.channel_type}:{self.channel_id}"
275+
if _HAS_OTEL and baggage is not None and otel_context is not None:
316276
ctx = baggage.set_baggage(
317277
"stream.channel_cid", cid, context=otel_context.get_current()
318278
)
319-
ctx = baggage.set_baggage("stream.operation", op, context=ctx)
320279
token = otel_context.attach(ctx)
321280
try:
322281
return await func(self, *args, **kwargs)

getstream/stream.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from functools import cached_property
24
import time
35
from typing import List, Optional
@@ -227,7 +229,7 @@ class Stream(BaseStream, CommonClient):
227229

228230
@classmethod
229231
@deprecated("from_env is deprecated, use __init__ instead")
230-
def from_env(cls, timeout: float = 6.0) -> "Stream":
232+
def from_env(cls, timeout: float = 6.0) -> Stream:
231233
"""
232234
Construct a StreamClient by loading its credentials and base_url
233235
from environment variables (via our pydantic Settings).

0 commit comments

Comments
 (0)