Skip to content

Commit

Permalink
Merge pull request #616 from launchableinc/add-telemetry-more
Browse files Browse the repository at this point in the history
Add telemetry for other CLI commands
  • Loading branch information
ono-max authored Aug 23, 2023
2 parents 2d4a5a3 + eed0e45 commit c595b84
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 36 deletions.
10 changes: 6 additions & 4 deletions launchable/commands/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import click

from launchable.utils.no_build import NO_BUILD_BUILD_NAME
from launchable.utils.tracking import TrackingClient

from ..utils.launchable_client import LaunchableClient
from ..utils.session import read_build, read_session
Expand All @@ -12,6 +13,7 @@ def find_or_create_session(
context: click.core.Context,
session: Optional[str],
build_name: Optional[str],
tracking_client: TrackingClient,
flavor=[],
is_observation: bool = False,
links: List[str] = [],
Expand All @@ -38,7 +40,7 @@ def find_or_create_session(
from .record.session import session as session_command

if session:
_check_observation_mode_status(session, is_observation)
_check_observation_mode_status(session, is_observation, tracking_client=tracking_client)
return session

if is_no_build:
Expand Down Expand Up @@ -78,7 +80,7 @@ def find_or_create_session(

session_id = read_session(saved_build_name)
if session_id:
_check_observation_mode_status(session_id, is_observation)
_check_observation_mode_status(session_id, is_observation, tracking_client=tracking_client)
return session_id
else:
context.invoke(
Expand All @@ -95,11 +97,11 @@ def find_or_create_session(
return read_session(saved_build_name)


def _check_observation_mode_status(session: str, is_observation: bool):
def _check_observation_mode_status(session: str, is_observation: bool, tracking_client: TrackingClient):
if not is_observation:
return

client = LaunchableClient()
client = LaunchableClient(tracking_client=tracking_client)
res = client.request("get", session)

# only check when the status code is 200 not to stop the command
Expand Down
11 changes: 10 additions & 1 deletion launchable/commands/record/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from launchable.utils.key_value_type import normalize_key_value_types
from launchable.utils.link import CIRCLECI_KEY, GITHUB_ACTIONS_KEY, JENKINS_URL_KEY, LinkKind, capture_link
from launchable.utils.tracking import Tracking, TrackingClient

from ...utils import subprocess
from ...utils.authentication import get_org_workspace
Expand Down Expand Up @@ -203,6 +204,7 @@ def build(ctx: click.core.Context, build_name: str, source: List[str], max_days:
err=True)

build_id = None
tracking_client = TrackingClient(Tracking.Command.RECORD_BUILD)
try:
commitHashes = [{
'repositoryName': name,
Expand All @@ -228,14 +230,21 @@ def build(ctx: click.core.Context, build_name: str, source: List[str], max_days:
})
payload["links"] = _links

client = LaunchableClient(dry_run=ctx.obj.dry_run)
client = LaunchableClient(dry_run=ctx.obj.dry_run, tracking_client=tracking_client)

res = client.request("post", "builds", payload=payload)
res.raise_for_status()

build_id = res.json().get("id", None)

except Exception as e:
org, workspace = get_org_workspace()
tracking_client.send_error_event(
event_name=Tracking.ErrorEvent.INTERNAL_CLI_ERROR,
stack_trace=str(e),
organization=org or "",
workspace=workspace or "",
)
if os.getenv(REPORT_ERROR_KEY):
raise e
else:
Expand Down
41 changes: 36 additions & 5 deletions launchable/commands/record/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
from typing import List, Optional

import click
from launchable.utils.authentication import get_org_workspace

from launchable.utils.key_value_type import normalize_key_value_types
from launchable.utils.link import LinkKind, capture_link
from launchable.utils.tracking import Tracking, TrackingClient

from ...utils.click import KeyValueType, ignorable_error
from ...utils.env_keys import REPORT_ERROR_KEY
Expand Down Expand Up @@ -123,7 +125,8 @@ def session(

build_name = NO_BUILD_BUILD_NAME

client = LaunchableClient(dry_run=ctx.obj.dry_run)
tracking_client = TrackingClient(Tracking.Command.RECORD_SESSION)
client = LaunchableClient(dry_run=ctx.obj.dry_run, tracking_client=tracking_client)

if session_name:
sub_path = "builds/{}/test_session_names/{}".format(build_name, session_name)
Expand All @@ -138,6 +141,13 @@ def session(
err=True)
sys.exit(2)
except Exception as e:
org, workspace = get_org_workspace()
tracking_client.send_error_event(
event_name=Tracking.ErrorEvent.INTERNAL_CLI_ERROR,
stack_trace=str(e),
organization=org or "",
workspace=workspace or "",
)
if os.getenv(REPORT_ERROR_KEY):
raise e
else:
Expand Down Expand Up @@ -169,12 +179,19 @@ def session(
res = client.request("post", sub_path, payload=payload)

if res.status_code == HTTPStatus.NOT_FOUND:
msg = "Build {} was not found." \
"Make sure to run `launchable record build --name {}` before you run this command.".format(
build_name, build_name)
org, workspace = get_org_workspace()
tracking_client.send_error_event(
event_name=Tracking.ErrorEvent.INTERNAL_CLI_ERROR,
stack_trace=msg,
organization=org or "",
workspace=workspace or "",
)
click.echo(
click.style(
"Build {} was not found. "
"Make sure to run `launchable record build --name {}` before you run this command.".format(
build_name,
build_name),
msg,
'yellow'),
err=True,
)
Expand All @@ -195,6 +212,13 @@ def session(
click.echo("{}/{}".format(sub_path, session_id), nl=False)

except Exception as e:
org, workspace = get_org_workspace()
tracking_client.send_error_event(
event_name=Tracking.ErrorEvent.INTERNAL_CLI_ERROR,
stack_trace=str(e),
organization=org or "",
workspace=workspace or "",
)
if os.getenv(REPORT_ERROR_KEY):
raise e
else:
Expand All @@ -209,6 +233,13 @@ def session(
session_name=session_name,
)
except Exception as e:
org, workspace = get_org_workspace()
tracking_client.send_error_event(
event_name=Tracking.ErrorEvent.INTERNAL_CLI_ERROR,
stack_trace=str(e),
organization=org or "",
workspace=workspace or "",
)
if os.getenv(REPORT_ERROR_KEY):
raise e
else:
Expand Down
35 changes: 30 additions & 5 deletions launchable/commands/record/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
from more_itertools import ichunked
from tabulate import tabulate

from launchable.utils.authentication import ensure_org_workspace
from launchable.utils.authentication import ensure_org_workspace, get_org_workspace
from launchable.utils.tracking import Tracking, TrackingClient

from ...testpath import FilePathNormalizer, TestPathComponent, unparse_test_path
from ...utils.click import KeyValueType
Expand Down Expand Up @@ -175,13 +176,22 @@ def tests(

test_runner = context.invoked_subcommand

client = LaunchableClient(test_runner=test_runner, dry_run=context.obj.dry_run)
tracking_client = TrackingClient(Tracking.Command.RECORD_TESTS)
client = LaunchableClient(test_runner=test_runner, dry_run=context.obj.dry_run, tracking_client=tracking_client)

file_path_normalizer = FilePathNormalizer(base_path, no_base_path_inference=no_base_path_inference)

if is_no_build and (read_build() and read_build() != ""):
raise click.UsageError(
'The cli already created `.launchable` file. If you want to use `--no-build` option, please remove `.launchable` file before executing.') # noqa: E501
msg = 'The cli already created `.launchable` file.' \
'If you want to use `--no-build` option, please remove `.launchable` file before executing.'
org, workspace = get_org_workspace()
tracking_client.send_error_event(
event_name=Tracking.ErrorEvent.INTERNAL_CLI_ERROR,
stack_trace=msg,
organization=org or "",
workspace=workspace or "",
)
raise click.UsageError(message=msg) # noqa: E501

if is_no_build and session:
click.echo(
Expand Down Expand Up @@ -218,12 +228,20 @@ def tests(
build_name=build_name,
flavor=flavor,
links=links,
lineage=lineage))
lineage=lineage,
tracking_client=tracking_client))
build_name = read_build()
record_start_at = get_record_start_at(session_id, client)

build_name, test_session_id = parse_session(session_id)
except Exception as e:
org, workspace = get_org_workspace()
tracking_client.send_error_event(
event_name=Tracking.ErrorEvent.INTERNAL_CLI_ERROR,
stack_trace=str(e),
organization=org or "",
workspace=workspace or "",
)
if os.getenv(REPORT_ERROR_KEY):
raise e
else:
Expand Down Expand Up @@ -509,6 +527,13 @@ def recorded_result() -> Tuple[int, int, int, float]:
raise Exception(exceptions)

except Exception as e:
orgn, ws = get_org_workspace()
tracking_client.send_error_event(
event_name=Tracking.ErrorEvent.INTERNAL_CLI_ERROR,
stack_trace=str(e),
organization=orgn or "",
workspace=ws or "",
)
if os.getenv(REPORT_ERROR_KEY):
raise e
else:
Expand Down
25 changes: 23 additions & 2 deletions launchable/commands/subset.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from launchable.utils.authentication import get_org_workspace
from launchable.utils.session import parse_session
from launchable.utils.tracking import Tracking, TrackingClient

from ..testpath import FilePathNormalizer, TestPath
from ..utils.click import DURATION, PERCENTAGE, DurationType, KeyValueType, PercentageType, ignorable_error
Expand Down Expand Up @@ -207,6 +208,7 @@ def subset(
is_no_build = False

session_id = None
tracking_client = TrackingClient(Tracking.Command.SUBSET)
try:
session_id = find_or_create_session(
context=context,
Expand All @@ -216,9 +218,17 @@ def subset(
is_observation=is_observation,
links=links,
is_no_build=is_no_build,
lineage=lineage
lineage=lineage,
tracking_client=tracking_client
)
except Exception as e:
org, workspace = get_org_workspace()
tracking_client.send_error_event(
event_name=Tracking.ErrorEvent.INTERNAL_CLI_ERROR,
stack_trace=str(e),
organization=org or "",
workspace=workspace or "",
)
if os.getenv(REPORT_ERROR_KEY):
raise e
else:
Expand Down Expand Up @@ -407,7 +417,10 @@ def run(self):
else:
try:
test_runner = context.invoked_subcommand
client = LaunchableClient(test_runner=test_runner, dry_run=context.obj.dry_run)
client = LaunchableClient(
test_runner=test_runner,
dry_run=context.obj.dry_run,
tracking_client=tracking_client)

# temporarily extend the timeout because subset API response has become slow
# TODO: remove this line when API response return respose
Expand All @@ -427,6 +440,14 @@ def run(self):
is_observation = res.json().get("isObservation", False)

except Exception as e:
org, workspace = get_org_workspace()
tracking_client.send_error_event(
event_name=Tracking.ErrorEvent.INTERNAL_CLI_ERROR,
stack_trace=str(e),
organization=org or "",
workspace=workspace or "",
)

if os.getenv(REPORT_ERROR_KEY):
raise e
else:
Expand Down
Loading

0 comments on commit c595b84

Please sign in to comment.