-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce telemetry in datachain (#411)
* Introduce telemetry in datachain * Add test and implement telemetry to api call * Env * Try new alternative * Remove unused log param * Reset telemetry sent * Address PR comments * Fix tests * Fix test * Update telemetry.py Co-authored-by: skshetry <18718008+skshetry@users.noreply.github.com> * Add import --------- Co-authored-by: skshetry <18718008+skshetry@users.noreply.github.com>
- Loading branch information
1 parent
27ce790
commit 320360f
Showing
9 changed files
with
90 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import logging | ||
import os | ||
from importlib.metadata import PackageNotFoundError, version | ||
|
||
from iterative_telemetry import IterativeTelemetryLogger | ||
|
||
from datachain.utils import env2bool | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def is_enabled(): | ||
""" | ||
Determine if telemetry is enabled based on environment variables and configuration. | ||
""" | ||
# Disable telemetry if running in test mode | ||
if env2bool("DATACHAIN_TEST"): | ||
return False | ||
|
||
# Check if telemetry is disabled by environment variable | ||
disabled = bool(os.getenv("DATACHAIN_NO_ANALYTICS")) | ||
if disabled: | ||
logger.debug("Telemetry is disabled by environment variable.") | ||
return False | ||
|
||
logger.debug("Telemetry is enabled.") | ||
return True | ||
|
||
|
||
# Try to get the version of the datachain package | ||
try: | ||
__version__ = version("datachain") | ||
except PackageNotFoundError: | ||
__version__ = "unknown" | ||
|
||
# Initialize telemetry logger | ||
telemetry = IterativeTelemetryLogger("datachain", __version__, is_enabled) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from datachain.lib.dc import DataChain | ||
from datachain.telemetry import telemetry | ||
|
||
|
||
def test_is_enabled(): | ||
assert not telemetry.is_enabled() | ||
|
||
|
||
def test_telemetry_api_call(mocker, tmp_dir): | ||
patch_send = mocker.patch("iterative_telemetry.IterativeTelemetryLogger.send") | ||
telemetry._event_sent = False | ||
|
||
DataChain.from_storage(tmp_dir.as_uri()) | ||
assert patch_send.call_count == 1 | ||
args = patch_send.call_args_list[0].args[0] | ||
extra = args.pop("extra") | ||
|
||
assert args == {"interface": "class", "action": "datachain_init", "error": None} | ||
|
||
assert "name" in extra |