-
Notifications
You must be signed in to change notification settings - Fork 389
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: task item review the telemetry client for both client and s…
…erver side (#4480) <!-- Thanks for your contribution! As part of our Community Growers initiative 🌱, we're donating Justdiggit bunds in your name to reforest sub-Saharan Africa. To claim your Community Growers certificate, please contact David Berenstein in our Slack community or fill in this form https://tally.so/r/n9XrxK once your PR has been merged. --> # Description This PR separates telemetry clients for server and client modules. Closes #4479 **Type of change** (Please delete options that are not relevant. Remember to title the PR according to the type of change) - [ ] New feature (non-breaking change which adds functionality) - [X] Refactor (change restructuring the codebase without changing functionality) - [ ] Improvement (change adding some improvement to an existing functionality) **How Has This Been Tested** (Please describe the tests that you ran to verify your changes. And ideally, reference `tests`) **Checklist** - [ ] I added relevant documentation - [X] I followed the style guidelines of this project - [X] I did a self-review of my code - [ ] I made corresponding changes to the documentation - [X] My changes generate no new warnings - [X] I have added tests that prove my fix is effective or that my feature works - [ ] I filled out [the contributor form](https://tally.so/r/n9XrxK) (see text above) - [] I have added relevant notes to the `CHANGELOG.md` file (See https://keepachangelog.com/) --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
0b91c8f
commit 4290369
Showing
13 changed files
with
154 additions
and
57 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,106 @@ | ||
# Copyright 2021-present, the Recognai S.L. team. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import dataclasses | ||
import logging | ||
import platform | ||
import uuid | ||
from typing import Any, Dict, Optional | ||
|
||
from fastapi import Request | ||
|
||
from argilla.server.commons.models import TaskType | ||
from argilla.server.settings import settings | ||
|
||
try: | ||
from analytics import Client # This import works only for version 2.2.0 | ||
except (ImportError, ModuleNotFoundError): | ||
# TODO: show some warning info | ||
settings.enable_telemetry = False | ||
Client = None | ||
|
||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
@dataclasses.dataclass | ||
class TelemetryClient: | ||
enable_telemetry: dataclasses.InitVar[bool] = settings.enable_telemetry | ||
disable_send: dataclasses.InitVar[bool] = False | ||
api_key: dataclasses.InitVar[str] = settings.telemetry_key | ||
host: dataclasses.InitVar[str] = "https://api.segment.io" | ||
|
||
_server_id: Optional[uuid.UUID] = dataclasses.field(init=False, default=None) | ||
|
||
@property | ||
def server_id(self) -> uuid.UUID: | ||
return self._server_id | ||
|
||
def __post_init__(self, enable_telemetry: bool, disable_send: bool, api_key: str, host: str): | ||
from argilla import __version__ | ||
|
||
self.client = None | ||
if enable_telemetry: | ||
try: | ||
self.client = Client(write_key=api_key, gzip=True, host=host, send=not disable_send, max_retries=10) | ||
except Exception as err: | ||
_LOGGER.warning(f"Cannot initialize telemetry. Error: {err}. Disabling...") | ||
|
||
self._server_id = uuid.UUID(int=uuid.getnode()) | ||
self._system_info = { | ||
"system": platform.system(), | ||
"machine": platform.machine(), | ||
"platform": platform.platform(), | ||
"python_version": platform.python_version(), | ||
"sys_version": platform.version(), | ||
"version": __version__, | ||
} | ||
|
||
def track_data(self, action: str, data: Dict[str, Any], include_system_info: bool = True): | ||
if not self.client: | ||
return | ||
|
||
event_data = data.copy() | ||
self.client.track( | ||
user_id=str(self._server_id), | ||
event=action, | ||
properties=event_data, | ||
context=self._system_info if include_system_info else {}, | ||
) | ||
|
||
|
||
_CLIENT = TelemetryClient() | ||
|
||
|
||
def _process_request_info(request: Request): | ||
return {header: request.headers.get(header) for header in ["user-agent", "accept-language"]} | ||
|
||
|
||
async def track_bulk(task: TaskType, records: int): | ||
_CLIENT.track_data(action="LogRecordsRequested", data={"task": task, "records": records}) | ||
|
||
|
||
async def track_login(request: Request, username: str): | ||
_CLIENT.track_data( | ||
action="UserInfoRequested", | ||
data={ | ||
"is_default_user": username == "argilla", | ||
"user_hash": str(uuid.uuid5(namespace=_CLIENT.server_id, name=username)), | ||
**_process_request_info(request), | ||
}, | ||
) | ||
|
||
|
||
def get_telemetry_client() -> TelemetryClient: | ||
return _CLIENT |
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
Oops, something went wrong.