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

Add CI usage to telemetry #1717

Merged
merged 3 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
17 changes: 17 additions & 0 deletions src/snowflake/cli/_app/telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from __future__ import annotations

import os
import platform
import sys
from enum import Enum, unique
Expand Down Expand Up @@ -59,6 +60,7 @@ class CLITelemetryField(Enum):
COMMAND_RESULT_STATUS = "command_result_status"
COMMAND_OUTPUT_TYPE = "command_output_type"
COMMAND_EXECUTION_TIME = "command_execution_time"
COMMAND_CI_ENVIRONMENT = "command_ci_environment"
# Configuration
CONFIG_FEATURE_FLAGS = "config_feature_flags"
# Metrics
Expand Down Expand Up @@ -127,6 +129,20 @@ def _get_installation_source() -> CLIInstallationSource:
return CLIInstallationSource.PYPI


def _get_ci_environment_type() -> str:
if "GITHUB_ACTIONS" in os.environ:
return "GITHUB_ACTIONS"
if "GITLAB_CI" in os.environ:
return "GITLAB_CI"
if "CIRCLECI" in os.environ:
return "CIRCLECI"
if "JENKINS_URL" in os.environ or "HUDSON_URL" in os.environ:
return "JENKINS"
if "TF_BUILD" in os.environ:
return "AZURE_DEVOPS"
return "UNKNOWN"


def command_info() -> str:
info = _find_command_info()
command = ".".join(info[CLITelemetryField.COMMAND])
Expand All @@ -153,6 +169,7 @@ def generate_telemetry_data_dict(
CLITelemetryField.VERSION_CLI: VERSION,
CLITelemetryField.VERSION_OS: platform.platform(),
CLITelemetryField.VERSION_PYTHON: python_version(),
CLITelemetryField.COMMAND_CI_ENVIRONMENT: _get_ci_environment_type(),
CLITelemetryField.CONFIG_FEATURE_FLAGS: {
k: str(v) for k, v in get_feature_flags_section().items()
},
Expand Down
34 changes: 34 additions & 0 deletions tests/app/test_telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import uuid
from unittest import mock

import pytest
from snowflake.connector.version import VERSION as DRIVER_VERSION


Expand Down Expand Up @@ -46,6 +47,9 @@ def test_executing_command_sends_telemetry_usage_data(
.to_dict()
)

del usage_command_event["message"][
"command_ci_environment"
] # to avoid side effect from CI
assert usage_command_event == {
"message": {
"driver_type": "PythonConnector",
Expand All @@ -72,6 +76,36 @@ def test_executing_command_sends_telemetry_usage_data(
}


@pytest.mark.parametrize(
"ci_type, env_var",
[
("GITHUB_ACTIONS", "GITHUB_ACTIONS"),
("GITLAB_CI", "GITLAB_CI"),
("CIRCLECI", "CIRCLECI"),
("JENKINS", "JENKINS_URL"),
("JENKINS", "HUDSON_URL"),
("AZURE_DEVOPS", "TF_BUILD"),
],
)
@mock.patch("snowflake.connector.connect")
@mock.patch("snowflake.cli._plugins.connection.commands.ObjectManager")
def test_executing_command_sends_ci_usage_data(_, mock_conn, runner, env_var, ci_type):
with mock.patch.dict(os.environ, {env_var: "true"}, clear=True):
result = runner.invoke(["connection", "test"], catch_exceptions=False)

assert result.exit_code == 0, result.output
# The method is called with a TelemetryData type, so we cast it to dict for simpler comparison
usage_command_event = (
mock_conn.return_value._telemetry.try_add_log_to_batch.call_args_list[ # noqa: SLF001
0
]
.args[0]
.to_dict()
)

assert usage_command_event["message"]["command_ci_environment"] == ci_type


@mock.patch(
"snowflake.cli._app.telemetry.python_version",
)
Expand Down
Loading