Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 80 additions & 52 deletions src/deepgram/extensions/core/instrumented_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,33 +140,47 @@ def request(
force_multipart=force_multipart,
)
duration_ms = (time.perf_counter() - start) * 1000.0
try:
if self._events is not None:
response_headers = typing.cast(typing.Union[typing.Mapping[str, str], None], getattr(resp, "headers", None))
# Filter response headers for telemetry extras
try:
from .telemetry_events import (
capture_response_details,
# filter_sensitive_headers, # No longer needed - using privacy-focused capture
)
# No longer filter response headers - use privacy-focused response_details instead
extras = None
response_details = capture_response_details(resp)
except Exception:
extras = None
response_details = None

# Capture response details in a background thread to avoid blocking
# We capture minimal sync data here and defer detailed processing
if self._events is not None:
try:
import threading
Copy link

Copilot AI Nov 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Threading module is imported inside the function on every request. Consider importing at the module level to avoid repeated import overhead.

Copilot uses AI. Check for mistakes.

self._events.on_http_response(
method=method,
url=url or "",
status_code=resp.status_code,
duration_ms=duration_ms,
headers=response_headers,
extras=extras,
response_details=response_details,
)
except Exception:
pass
def capture_and_emit():
try:
response_headers = typing.cast(typing.Union[typing.Mapping[str, str], None], getattr(resp, "headers", None))
# Filter response headers for telemetry extras
try:
from .telemetry_events import (
capture_response_details,
# filter_sensitive_headers, # No longer needed - using privacy-focused capture
)
# No longer filter response headers - use privacy-focused response_details instead
extras = None
response_details = capture_response_details(resp)
except Exception:
extras = None
response_details = None

self._events.on_http_response(
method=method,
url=url or "",
status_code=resp.status_code,
duration_ms=duration_ms,
headers=response_headers,
extras=extras,
response_details=response_details,
)
except Exception:
pass

# Emit telemetry in background thread so we don't block the response
thread = threading.Thread(target=capture_and_emit, daemon=True, name="dg-telemetry-capture")
thread.start()
except Exception:
pass

return resp
except Exception as exc:
duration_ms = (time.perf_counter() - start) * 1000.0
Expand Down Expand Up @@ -311,33 +325,47 @@ async def request(
force_multipart=force_multipart,
)
duration_ms = (time.perf_counter() - start) * 1000.0
try:
if self._events is not None:
response_headers = typing.cast(typing.Union[typing.Mapping[str, str], None], getattr(resp, "headers", None))
# Filter response headers for telemetry extras
try:
from .telemetry_events import (
capture_response_details,
# filter_sensitive_headers, # No longer needed - using privacy-focused capture
)
# No longer filter response headers - use privacy-focused response_details instead
extras = None
response_details = capture_response_details(resp)
except Exception:
extras = None
response_details = None

# Capture response details in a background thread to avoid blocking
# We capture minimal sync data here and defer detailed processing
if self._events is not None:
try:
import threading

self._events.on_http_response(
method=method,
url=url or "",
status_code=resp.status_code,
duration_ms=duration_ms,
headers=response_headers,
extras=extras,
response_details=response_details,
)
except Exception:
pass
def capture_and_emit():
try:
response_headers = typing.cast(typing.Union[typing.Mapping[str, str], None], getattr(resp, "headers", None))
# Filter response headers for telemetry extras
try:
from .telemetry_events import (
capture_response_details,
# filter_sensitive_headers, # No longer needed - using privacy-focused capture
)
# No longer filter response headers - use privacy-focused response_details instead
extras = None
response_details = capture_response_details(resp)
except Exception:
extras = None
response_details = None

self._events.on_http_response(
method=method,
url=url or "",
status_code=resp.status_code,
duration_ms=duration_ms,
headers=response_headers,
extras=extras,
response_details=response_details,
)
except Exception:
pass

# Emit telemetry in background thread so we don't block the response
thread = threading.Thread(target=capture_and_emit, daemon=True, name="dg-telemetry-capture")
thread.start()
except Exception:
pass

return resp
except Exception as exc:
duration_ms = (time.perf_counter() - start) * 1000.0
Expand Down
Loading