Skip to content

Commit

Permalink
Merge pull request #335 from DataRecce/feature/drc-477-add-a-runner-t…
Browse files Browse the repository at this point in the history
…ype-in-the-recce-telemetry

[Chore] Track running environment and repository
  • Loading branch information
wcchang1115 authored Jun 6, 2024
2 parents fb682a3 + c14af14 commit f3acf5a
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 11 deletions.
16 changes: 16 additions & 0 deletions recce/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,22 @@ def is_ci_env():
return False


def get_runner():
# GitHub Action
if os.environ.get('GITHUB_ACTIONS', 'false') == 'true':
return 'github actions'

# GitHub Codespace
if os.environ.get('CODESPACES', 'false') == 'true':
return 'github codespaces'

# CircleCI
if os.environ.get('CIRCLECI', 'false') == 'true':
return 'circleci'

return None


def get_version():
version_file = os.path.normpath(os.path.join(os.path.dirname(__file__), 'VERSION'))
with open(version_file) as fh:
Expand Down
9 changes: 0 additions & 9 deletions recce/event/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,6 @@ def log_event(prop, event_type, **kwargs):
**prop,
)

# only 'server' and 'run' commands carry the adapter info
params = kwargs.get('params')
adapter_type_sqlmesh = None
if params is not None:
adapter_type_sqlmesh = params.get('sqlmesh')

if adapter_type_sqlmesh is not None:
payload['adapter_type'] = 'SQLMesh' if adapter_type_sqlmesh else 'DBT'

_collector.log_event(payload, event_type)


Expand Down
25 changes: 24 additions & 1 deletion recce/event/track.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
import time
import traceback
import typing as t
from hashlib import sha256

from click import Context
from click.core import Command
from rich.console import Console
from rich.markup import escape

from recce import event
from recce import event, get_runner
from recce.core import load_context
from recce.git import hosting_repo

console = Console()

Expand Down Expand Up @@ -77,6 +80,8 @@ def invoke(self, ctx: Context) -> t.Any:
finally:
end_time = time.time()
duration = end_time - start_time
runner = get_runner()
repo = hosting_repo()
command = ctx.command.name
props = dict(
command=command,
Expand All @@ -85,5 +90,23 @@ def invoke(self, ctx: Context) -> t.Any:
duration=duration,
)

if runner is not None:
props['runner_type'] = runner

if repo is not None:
props['repository'] = sha256(repo.encode()).hexdigest()

try:
recce_context = load_context()
except Exception:
# it's not a ready-for-use project
recce_context = None

if recce_context is not None:
if recce_context.adapter_type == "dbt":
props['adapter_type'] = 'DBT'
elif recce_context.adapter_type == "sqlmesh":
props['adapter_type'] = 'SQLMesh'

event.log_event(props, command, params=ctx.params)
event.flush_events()
28 changes: 27 additions & 1 deletion recce/git.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from git import Repo
import os

from git import Repo, InvalidGitRepositoryError


def current_branch():
Expand All @@ -12,3 +14,27 @@ def current_branch():
return None
except Exception:
return None


def hosting_repo():
try:
repo = Repo(search_parent_directories=True)
origin_url = repo.remote().url
remote_repo = None

if origin_url.startswith('git@'):
# Handle git@github.com:user/repo.git
remote_repo = origin_url.split(':')[1].replace('.git', '')

elif origin_url.startswith('https://') or origin_url.startswith('http://'):
# Handle https://github.com/user/repo.git or http://github.com/user/repo.git
remote_repo = '/'.join(origin_url.split('/')[-2:]).replace('.git', '')

return remote_repo
except ValueError:
repo = Repo(search_parent_directories=True)
toplevel_dir = repo.git.rev_parse("--show-toplevel")

return os.path.basename(toplevel_dir)
except InvalidGitRepositoryError:
return None

0 comments on commit f3acf5a

Please sign in to comment.