Skip to content

Commit 6010ba3

Browse files
committed
fix: use single context var
1 parent 5102cfb commit 6010ba3

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

src/isolate/connections/grpc/agent.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,23 @@
4444
from isolate.connections.grpc.interface import from_grpc
4545

4646
IDLE_TIMEOUT_SECONDS = int(os.getenv("ISOLATE_AGENT_IDLE_TIMEOUT_SECONDS", "0"))
47+
CONTEXT_VAR_NAME_LOG = os.getenv("ISOLATE_CONTEXT_VAR_NAME_LOG", "")
4748

4849

4950
def get_log_context() -> dict[str, Any]:
50-
"""Extract all contextvars that start with LOG_ prefix."""
51-
result = {}
52-
for var in contextvars.copy_context():
53-
if var.name.startswith("LOG_"):
54-
key = var.name[4:].lower()
55-
try:
56-
value = var.get()
57-
# Ellipsis is a placeholder to indicate it's not set
58-
if value is not Ellipsis:
59-
result[key] = var.get()
60-
except LookupError:
61-
pass
62-
return result
51+
"""Extract the contextvar that is set to the ISOLATE_CONTEXT_VAR_NAME_LOG."""
52+
if not CONTEXT_VAR_NAME_LOG:
53+
return {}
54+
55+
log_context = next(
56+
contextvars.ContextVar(CONTEXT_VAR_NAME_LOG).get(),
57+
None,
58+
)
59+
60+
if not isinstance(log_context, dict):
61+
return {}
62+
63+
return log_context
6364

6465

6566
class JsonStdoutProxy:

0 commit comments

Comments
 (0)