Skip to content

Commit

Permalink
airbyte-ci: Add basic segment tracking (#31354)
Browse files Browse the repository at this point in the history
Co-authored-by: bnchrch <bnchrch@users.noreply.github.com>
  • Loading branch information
bnchrch and bnchrch authored Oct 17, 2023
1 parent ebf566d commit ba48460
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
"""This module is the CLI entrypoint to the airbyte-ci commands."""

import importlib
import os
import subprocess
from typing import List

import click
from github import PullRequest
from pipelines import github, main_logger
from pipelines.bases import CIContext
from pipelines.consts import LOCAL_PIPELINE_PACKAGE_PATH
from pipelines.telemetry import track_command
from pipelines.utils import (
get_current_epoch_time,
get_current_git_branch,
Expand Down Expand Up @@ -119,6 +118,7 @@ def get_modified_files(
@click.option("--ci-job-key", envvar="CI_JOB_KEY", type=str)
@click.option("--show-dagger-logs/--hide-dagger-logs", default=False, type=bool)
@click.pass_context
@track_command
def airbyte_ci(
ctx: click.Context,
is_local: bool,
Expand Down
60 changes: 60 additions & 0 deletions airbyte-ci/connectors/pipelines/pipelines/telemetry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import getpass
import hashlib
import os
import platform
import sys

import segment.analytics as analytics

analytics.write_key = "G6G7whgro81g9xM00kN2buclGKvcOjFd"
analytics.send = True
analytics.debug = True


def _is_airbyte_user():
"""Returns True if the user is airbyter, False otherwise."""
return os.getenv("AIRBYTE_ROLE") == "airbyter"


def _get_anonymous_system_id():
"""Returns a unique anonymous hashid of the current system info."""
# Collect machine-specific information
machine_info = platform.node()
username = getpass.getuser()

unique_system_info = f"{machine_info}-{username}"

# Generate a unique hash
unique_id = hashlib.sha256(unique_system_info.encode()).hexdigest()

return unique_id


def track_command(f):
"""
Decorator to track CLI commands with segment.io
"""

def wrapper(*args, **kwargs):
ctx = args[0]
top_level_command = ctx.command_path
full_cmd = " ".join(sys.argv)

# remove anything prior to the command name f.__name__
# to avoid logging inline secrets
santized_cmd = full_cmd[full_cmd.find(top_level_command) :]

sys_id = _get_anonymous_system_id()
sys_user_name = f"anonymous:{sys_id}"
airbyter = _is_airbyte_user()

is_local = kwargs.get("is_local", False)
user_id = "local-user" if is_local else "ci-user"
event = f"airbyte-ci:{f.__name__}"

# IMPORTANT! do not log kwargs as they may contain secrets
analytics.track(user_id, event, {"username": sys_user_name, "command": santized_cmd, "airbyter": airbyter})

return f(*args, **kwargs)

return wrapper
107 changes: 69 additions & 38 deletions airbyte-ci/connectors/pipelines/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions airbyte-ci/connectors/pipelines/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ airbyte-connectors-base-images = {path = "../base_images", develop = true}
connector-ops = {path = "../connector_ops", develop = true}
toml = "^0.10.2"
sentry-sdk = "^1.28.1"
segment-analytics-python = "^2.2.3"

[tool.poetry.group.test.dependencies]
pytest = "^6.2.5"
Expand Down

0 comments on commit ba48460

Please sign in to comment.