Skip to content

Commit

Permalink
fix: Review features for server telemetry (#1745)
Browse files Browse the repository at this point in the history
Some changes related to the usage telemetry:

- Passing system data as event context (will simplify later integrations)
- Show warning message on server start to clear preventing users
- Include the Telemetry section inside the Reference doc.

Refs: #1687
  • Loading branch information
frascuchon committed Oct 5, 2022
1 parent 9b20810 commit 3a27500
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/index.md
Expand Up @@ -172,6 +172,7 @@ You can join the conversation on Slack! We are a very friendly and inclusive com
:caption: Reference
:hidden:

reference/telemetry
reference/python/index
reference/webapp/index

Expand Down
File renamed without changes.
11 changes: 7 additions & 4 deletions src/rubrix/server/commons/telemetry.py
Expand Up @@ -87,16 +87,19 @@ def __post_init__(self):
"platform": platform.platform(),
"python_version": platform.python_version(),
"sys_version": platform.version(),
"rubrix_version": __version__,
"version": __version__,
}

def track_data(
self, action: str, data: Dict[str, Any], include_system_info: bool = True
):
event_data = data.copy()
if include_system_info:
event_data.update(self.__system_info__)
self.client.track(self.__server_id_str__, action, event_data)
self.client.track(
user_id=self.__server_id_str__,
event=action,
properties=event_data,
context=self.__system_info__ if include_system_info else {},
)


def _process_request_info(request: Request):
Expand Down
33 changes: 33 additions & 0 deletions src/rubrix/server/server.py
Expand Up @@ -16,7 +16,10 @@
"""
This module configures the global fastapi application
"""
import inspect
import os
import sys
import warnings
from pathlib import Path

from brotli_asgi import BrotliMiddleware
Expand Down Expand Up @@ -128,6 +131,35 @@ def configure_app_logging(app: FastAPI):
version=str(rubrix_version),
)


def configure_telemetry(app):
message = "\n"
message += inspect.cleandoc(
"""
Rubrix uses telemetry to report anonymous usage and error information.
You can know more about what information is reported at:
https://rubrix.readthedocs.io/en/stable/reference/telemetry.html
Telemetry is currently enabled. If you want to disable it, you can configure
the environment variable before relaunching the server:
"""
)
message += "\n\n "
message += (
"#set RUBRIX_ENABLE_TELEMETRY=0"
if os.name == "nt"
else "$>export RUBRIX_ENABLE_TELEMETRY=0"
)
message += "\n"

@app.on_event("startup")
async def check_telemetry():
if settings.enable_telemetry:
print(message, flush=True)


for app_configure in [
configure_app_logging,
configure_middleware,
Expand All @@ -136,5 +168,6 @@ def configure_app_logging(app: FastAPI):
configure_api_router,
configure_app_statics,
configure_app_storage,
configure_telemetry,
]:
app_configure(app)
2 changes: 1 addition & 1 deletion tests/conftest.py
Expand Up @@ -38,7 +38,7 @@ def telemetry_track_data(mocker):
client = telemetry._TelemetryClient.get()
if client:
# Disable sending data for tests
client._client = telemetry._configure_analytics(disable_send=True)
client.client = telemetry._configure_analytics(disable_send=True)
spy = mocker.spy(client, "track_data")

return spy
Expand Down

0 comments on commit 3a27500

Please sign in to comment.