Skip to content

Commit 31ff38e

Browse files
committed
fix: add user email to sentry
1 parent 6c0af01 commit 31ff38e

4 files changed

Lines changed: 24 additions & 4 deletions

File tree

plain2code.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ def setup_logging(
140140
return logging.getLevelName(configured_log_level)
141141

142142

143-
def _check_connection(codeplainAPI: codeplain_api.CodeplainAPI):
144-
"""Check API connectivity and validate API key and client version."""
143+
def _check_connection(codeplainAPI: codeplain_api.CodeplainAPI) -> Optional[str]:
144+
"""Check API connectivity, validate API key and client version, and return the user email."""
145145
response = codeplainAPI.connection_check(system_config.client_version)
146146

147147
if not response.get("api_key_valid", False):
@@ -158,6 +158,8 @@ def _check_connection(codeplainAPI: codeplain_api.CodeplainAPI):
158158
"Please update using: uv tool upgrade codeplain"
159159
)
160160

161+
return response.get("user_email")
162+
161163

162164
def warn_if_acceptance_tests_without_conformance_script(plain_module, args) -> None:
163165
"""Warn when any loaded module (including required modules) defines acceptance tests
@@ -201,7 +203,7 @@ def render( # noqa: C901
201203
assert args.api is not None and args.api != "", "API URL is required"
202204
codeplainAPI.api_url = args.api
203205

204-
_check_connection(codeplainAPI)
206+
run_state.user_email = _check_connection(codeplainAPI)
205207

206208
stop_event = threading.Event()
207209
enter_pause_event = threading.Event()

plain2code_state.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def __init__(self, spec_filename: str, replay_with: Optional[str] = None):
2727
self.current_module: Optional[str] = None
2828
self.current_frid: Optional[str] = None
2929
self.current_render_state: Optional[str] = None
30+
self.user_email: Optional[str] = None
3031

3132
def increment_call_count(self):
3233
self.call_count += 1

plain2code_telemetry.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ def capture_crash(exc_info, run_state: Optional[RunState], args) -> bool:
9898
scope.set_tag("render_state", run_state.current_render_state)
9999
scope.set_tag("current_module", run_state.current_module)
100100
scope.set_tag("current_frid", run_state.current_frid)
101+
if run_state.user_email:
102+
scope.set_user({"email": run_state.user_email})
101103
scope.set_tag("headless", bool(getattr(args, "headless", False)))
102104
scope.set_tag("unittests_script_provided", bool(getattr(args, "unittests_script", None)))
103105
scope.set_tag("conformance_tests_script_provided", bool(getattr(args, "conformance_tests_script", None)))

tests/test_telemetry.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,14 @@ def test_capture_crash_sends_event_with_tags(transport):
8080
run_state.current_module = "my_module"
8181
run_state.current_frid = "2.1"
8282
run_state.current_render_state = "IMPLEMENTING_FRID"
83+
run_state.user_email = "user@codeplain.ai"
8384

8485
assert capture_crash(make_exc_info(KeyError("boom")), run_state, make_args(headless=True))
8586
sentry_sdk.flush(timeout=2)
8687

8788
assert len(transport.events) == 1
88-
tags = transport.events[0]["tags"]
89+
event = transport.events[0]
90+
tags = event["tags"]
8991
assert tags["render_id"] == run_state.render_id
9092
assert tags["current_module"] == "my_module"
9193
assert tags["current_frid"] == "2.1"
@@ -94,6 +96,19 @@ def test_capture_crash_sends_event_with_tags(transport):
9496
assert tags["unittests_script_provided"] is True
9597
assert tags["conformance_tests_script_provided"] is False
9698
assert tags["prepare_environment_script_provided"] is False
99+
assert event["user"]["email"] == "user@codeplain.ai"
100+
101+
102+
def test_capture_crash_without_user_email(transport):
103+
init_with_transport(transport)
104+
105+
run_state = RunState(spec_filename="test.plain")
106+
107+
assert capture_crash(make_exc_info(KeyError("boom")), run_state, make_args())
108+
sentry_sdk.flush(timeout=2)
109+
110+
assert len(transport.events) == 1
111+
assert "email" not in transport.events[0].get("user", {})
97112

98113

99114
def test_capture_crash_without_run_state(transport):

0 commit comments

Comments
 (0)