From e51dd2710d3013d6b4cbdd3602252c8f1e12cd85 Mon Sep 17 00:00:00 2001 From: Kent Huang Date: Mon, 16 Sep 2024 10:51:42 +0800 Subject: [PATCH 1/3] [Feature] DRC-625 Send telemetry event before server started Signed-off-by: Kent Huang --- recce/event/__init__.py | 6 +++++- recce/event/track.py | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/recce/event/__init__.py b/recce/event/__init__.py index 03b92000..b26ca4fe 100644 --- a/recce/event/__init__.py +++ b/recce/event/__init__.py @@ -191,11 +191,12 @@ def log_load_state(command='server'): ) log_event(prop, 'load_state') + log_event({}, f'{command}_started') if command == 'server': _collector.schedule_flush() -def log_codespaces_events(): +def log_codespaces_events(command): # Only log when the recce is running in GitHub Codespaces if is_github_codespace() is False: return @@ -226,6 +227,9 @@ def log_codespaces_events(): user_properties=user_prop) update_user_profile({'codespace_available_at': available_at.isoformat()}) + # Codespace instance event should be flushed immediately + _collector.send_events() + def capture_exception(e): user_id = load_user_profile().get('user_id') diff --git a/recce/event/track.py b/recce/event/track.py index 4f470012..559bb688 100644 --- a/recce/event/track.py +++ b/recce/event/track.py @@ -55,6 +55,7 @@ def invoke(self, ctx: Context) -> t.Any: start_time = time.time() reason = 'error' event.set_exception_tag('command', ctx.command.name) + event.log_codespaces_events(ctx.command.name) try: ret = super(TrackCommand, self).invoke(ctx) @@ -109,5 +110,4 @@ def invoke(self, ctx: Context) -> t.Any: props['adapter_type'] = 'SQLMesh' event.log_event(props, command, params=ctx.params) - event.log_codespaces_events() event.flush_events() From 03dcb4d2c9634872e9e6dac35bd23a77a8662a24 Mon Sep 17 00:00:00 2001 From: Kent Huang Date: Mon, 16 Sep 2024 16:49:20 +0800 Subject: [PATCH 2/3] [Fix] Handle error when `/tmp/codespaces_logs` folder not found Signed-off-by: Kent Huang --- recce/github.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/recce/github.py b/recce/github.py index a4f23c0e..988504cd 100644 --- a/recce/github.py +++ b/recce/github.py @@ -217,6 +217,10 @@ def extract_datatime(log_line): return None github_codepsce_log_dir = '/tmp/codespaces_logs' - log_file = os.listdir(github_codepsce_log_dir)[-1] # Get the latest log file - start_monitor_line = search_in_file(f'{github_codepsce_log_dir}/{log_file}', 'Starting monitor') - return extract_datatime(start_monitor_line) + try: + log_file = os.listdir(github_codepsce_log_dir)[-1] # Get the latest log file + start_monitor_line = search_in_file(f'{github_codepsce_log_dir}/{log_file}', 'Starting monitor') + return extract_datatime(start_monitor_line) + except Exception: + # If there is any error, suspect the codespace is available at the current time + return datetime.now() From 52c12079fd8ef02bdf080479b30460d0a165158b Mon Sep 17 00:00:00 2001 From: Kent Huang Date: Mon, 16 Sep 2024 17:40:08 +0800 Subject: [PATCH 3/3] [Fix] Edge case when restart the codespace instance Signed-off-by: Kent Huang --- recce/event/__init__.py | 2 +- recce/github.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/recce/event/__init__.py b/recce/event/__init__.py index b26ca4fe..7ad12a24 100644 --- a/recce/event/__init__.py +++ b/recce/event/__init__.py @@ -221,7 +221,7 @@ def log_codespaces_events(command): update_user_profile({'codespace_created_at': codespace.get('created_at')}) # Codespace available event - available_at = get_github_codespace_available_at() + available_at = get_github_codespace_available_at(codespace) if available_at and available_at.isoformat() != load_user_profile().get('codespace_available_at'): _collector.log_event(prop, 'codespace_instance_available', event_triggered_at=available_at, user_properties=user_prop) diff --git a/recce/github.py b/recce/github.py index 988504cd..c572617d 100644 --- a/recce/github.py +++ b/recce/github.py @@ -197,7 +197,7 @@ def get_github_codespace_info(): ) -def get_github_codespace_available_at(): +def get_github_codespace_available_at(codespace): if is_github_codespace() is False: return None @@ -222,5 +222,5 @@ def extract_datatime(log_line): start_monitor_line = search_in_file(f'{github_codepsce_log_dir}/{log_file}', 'Starting monitor') return extract_datatime(start_monitor_line) except Exception: - # If there is any error, suspect the codespace is available at the current time - return datetime.now() + # If there is any error, use the updated_at time from the codespace info + return datetime.fromisoformat(codespace.get('updated_at'))