Skip to content

Commit

Permalink
Merge pull request #421 from DataRecce/feature/drc-625-send-telemetry…
Browse files Browse the repository at this point in the history
…-event-before-server-start

[Feature] DRC-625 Send telemetry event before server started
  • Loading branch information
kentwelcome authored Sep 16, 2024
2 parents 7fc9dd7 + 52c1207 commit 5109617
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
8 changes: 6 additions & 2 deletions recce/event/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -220,12 +221,15 @@ def log_codespaces_events():
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)
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')
Expand Down
2 changes: 1 addition & 1 deletion recce/event/track.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
12 changes: 8 additions & 4 deletions recce/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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, use the updated_at time from the codespace info
return datetime.fromisoformat(codespace.get('updated_at'))

0 comments on commit 5109617

Please sign in to comment.