Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

airbyte-ci: Add basic segment tracking #31354

Merged
merged 8 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We cool checking this in?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I imagine so. We already have checked in the write key for octavia cli. I dont see the difference here.

Also not too disimilar to how sentry/segment FE libraries work

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
33 changes: 32 additions & 1 deletion 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 @@ -23,6 +23,7 @@ requests = "2.28.2" # Pinned as the requests 2.29.0 version is not compatible wi
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