Skip to content

Commit

Permalink
Chore: Revamp anonymous usage stats (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronsteers authored Feb 28, 2024
1 parent d1d12d6 commit edf2314
Show file tree
Hide file tree
Showing 17 changed files with 620 additions and 299 deletions.
19 changes: 0 additions & 19 deletions airbyte/_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

from airbyte import exceptions as exc
from airbyte.registry import ConnectorMetadata
from airbyte.telemetry import SourceTelemetryInfo, SourceType


if TYPE_CHECKING:
Expand Down Expand Up @@ -64,10 +63,6 @@ def ensure_installation(self, *, auto_fix: bool = True) -> None:
def install(self) -> None:
pass

@abstractmethod
def _get_telemetry_info(self) -> SourceTelemetryInfo:
pass

@abstractmethod
def uninstall(self) -> None:
pass
Expand Down Expand Up @@ -388,13 +383,6 @@ def execute(self, args: list[str]) -> Iterator[str]:
with _stream_from_subprocess([str(connector_path), *args]) as stream:
yield from stream

def _get_telemetry_info(self) -> SourceTelemetryInfo:
return SourceTelemetryInfo(
name=self.name,
type=SourceType.VENV,
version=self.reported_version,
)


class PathExecutor(Executor):
def __init__(
Expand Down Expand Up @@ -448,10 +436,3 @@ def uninstall(self) -> NoReturn:
def execute(self, args: list[str]) -> Iterator[str]:
with _stream_from_subprocess([str(self.path), *args]) as stream:
yield from stream

def _get_telemetry_info(self) -> SourceTelemetryInfo:
return SourceTelemetryInfo(
str(self.name),
SourceType.LOCAL_INSTALL,
version=self.reported_version,
)
6 changes: 0 additions & 6 deletions airbyte/_processors/sql/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from __future__ import annotations

import abc
import enum
from contextlib import contextmanager
from functools import cached_property
Expand Down Expand Up @@ -53,7 +52,6 @@
)

from airbyte.caches.base import CacheBase
from airbyte.telemetry import CacheTelemetryInfo


DEBUG_MODE = False # Set to True to enable additional debug logging.
Expand Down Expand Up @@ -908,7 +906,3 @@ def _table_exists(
Subclasses may override this method to provide a more efficient implementation.
"""
return table_name in self._get_tables_list()

@abc.abstractmethod
def _get_telemetry_info(self) -> CacheTelemetryInfo:
pass
6 changes: 0 additions & 6 deletions airbyte/_processors/sql/bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from airbyte import exceptions as exc
from airbyte._processors.file.jsonl import JsonlWriter
from airbyte._processors.sql.base import SqlProcessorBase
from airbyte.telemetry import CacheTelemetryInfo
from airbyte.types import SQLTypeConverter


Expand Down Expand Up @@ -79,11 +78,6 @@ def _quote_identifier(self, identifier: str) -> str:
"""Return the identifier name as is. BigQuery does not require quoting identifiers"""
return f"{identifier}"

@final
@overrides
def _get_telemetry_info(self) -> CacheTelemetryInfo:
return CacheTelemetryInfo("bigquery")

def _write_files_to_new_table(
self,
files: list[Path],
Expand Down
5 changes: 0 additions & 5 deletions airbyte/_processors/sql/duckdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

from airbyte._processors.file import JsonlWriter
from airbyte._processors.sql.base import SqlProcessorBase
from airbyte.telemetry import CacheTelemetryInfo


if TYPE_CHECKING:
Expand Down Expand Up @@ -123,7 +122,3 @@ def _write_files_to_new_table(
)
self._execute_sql(insert_statement)
return temp_table_name

@overrides
def _get_telemetry_info(self) -> CacheTelemetryInfo:
return CacheTelemetryInfo("duckdb")
7 changes: 0 additions & 7 deletions airbyte/_processors/sql/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@

from __future__ import annotations

from overrides import overrides

from airbyte._processors.file import JsonlWriter
from airbyte._processors.sql.base import SqlProcessorBase
from airbyte.telemetry import CacheTelemetryInfo


class PostgresSqlProcessor(SqlProcessorBase):
Expand All @@ -23,7 +20,3 @@ class PostgresSqlProcessor(SqlProcessorBase):

file_writer_class = JsonlWriter
supports_merge_insert = False # TODO: Add native implementation for merge insert

@overrides
def _get_telemetry_info(self) -> CacheTelemetryInfo:
return CacheTelemetryInfo("postgres")
12 changes: 2 additions & 10 deletions airbyte/_processors/sql/snowflake.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

from airbyte._processors.file.jsonl import JsonlWriter
from airbyte._processors.sql.base import SqlProcessorBase
from airbyte.telemetry import CacheTelemetryInfo
from airbyte.types import SQLTypeConverter


Expand Down Expand Up @@ -42,11 +41,8 @@ def to_sql_type(
return sql_type


class SnowflakeSQLSqlProcessor(SqlProcessorBase):
"""A Snowflake implementation of the cache.
Parquet is used for local file storage before bulk loading.
"""
class SnowflakeSqlProcessor(SqlProcessorBase):
"""A Snowflake implementation of the cache."""

file_writer_class = JsonlWriter
type_converter_class = SnowflakeTypeConverter
Expand Down Expand Up @@ -114,7 +110,3 @@ def _init_connection_settings(self, connection: Connection) -> None:
MULTI_STATEMENT_COUNT = 0
"""
)

@overrides
def _get_telemetry_info(self) -> CacheTelemetryInfo:
return CacheTelemetryInfo("snowflake")
115 changes: 115 additions & 0 deletions airbyte/_util/meta.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
# Copyright (c) 2023 Airbyte, Inc., all rights reserved.
"""Environment meta utils and globals.
This module contains functions for detecting environment and runtime information.
"""
from __future__ import annotations

import os
import sys
from contextlib import suppress
from functools import lru_cache
from pathlib import Path
from platform import python_implementation, python_version, system

import requests


COLAB_SESSION_URL = "http://172.28.0.12:9000/api/sessions"
"""URL to get the current Google Colab session information."""


def get_colab_release_version() -> str | None:
if "COLAB_RELEASE_TAG" in os.environ:
return os.environ["COLAB_RELEASE_TAG"]

return None


def is_ci() -> bool:
return "CI" in os.environ


@lru_cache
def is_colab() -> bool:
return bool(get_colab_release_version())


@lru_cache
def is_jupyter() -> bool:
"""Return True if running in a Jupyter notebook or qtconsole.
Will return False in Colab (use is_colab() instead).
"""
try:
shell = get_ipython().__class__.__name__ # type: ignore # noqa: PGH003
except NameError:
return False # If 'get_ipython' undefined, we're probably in a standard Python interpreter.

if shell == "ZMQInteractiveShell":
return True # Jupyter notebook or qtconsole.

if shell == "TerminalInteractiveShell":
return False # Terminal running IPython

return False # Other type (?)


@lru_cache
def get_notebook_name() -> str | None:
if is_colab():
session_info = None
response = None
with suppress(Exception):
response = requests.get(COLAB_SESSION_URL)
if response.status_code == 200: # noqa: PLR2004 # Magic number
session_info = response.json()

if session_info and "name" in session_info:
return session_info["name"]

return None


@lru_cache
def get_vscode_notebook_name() -> str | None:
with suppress(Exception):
import IPython

return Path(
IPython.extract_module_locals()[1]["__vsc_ipynb_file__"],
).name

return None


def is_vscode_notebook() -> bool:
return get_vscode_notebook_name() is not None


@lru_cache
def get_python_script_name() -> str | None:
script_name = None
with suppress(Exception):
script_name = sys.argv[0] # When running a python script, this is the script name.

if script_name:
return Path(script_name).name

return None


@lru_cache
def get_application_name() -> str | None:
return get_notebook_name() or get_python_script_name() or get_vscode_notebook_name() or None


def get_python_version() -> str:
return f"{python_version()} ({python_implementation()})"


def get_os() -> str:
if is_colab():
return f"Google Colab ({get_colab_release_version()})"

return f"{system()}"
Loading

0 comments on commit edf2314

Please sign in to comment.