Repository navigation
feat(analytics): report usage to Reo alongside Scarf #473
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
base: main
Are you sure you want to change the base?
Changes from all commits
2cbfffe
25caff2
d0187b5
52821e4
f22b4af
a9a2842
bb01be1
8e15c89
a4f66c6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,16 +5,87 @@ | |
|
|
||
| from __future__ import annotations | ||
|
|
||
| import logging | ||
| import os | ||
| import sys | ||
| from typing import Any | ||
|
|
||
| import click | ||
|
|
||
| from diagrid.cli import telemetry_notice | ||
| from diagrid.cli.commands.chaos import chaos | ||
| from diagrid.cli.commands.deploy import deploy | ||
| from diagrid.cli.commands.init import init | ||
| from diagrid.cli.utils.process import set_verbose | ||
| from diagrid.core.config.constants import PROD_API_URL, STAGING_API_URL | ||
|
|
||
| # ``diagrid.core.analytics`` first shipped in diagrid-core 0.5.1, but this | ||
| # package pins ``diagrid-core>=0.1.0``. With an older core the CLI must still | ||
| # run, so fall back to no reporting. | ||
| try: | ||
| from diagrid.core import analytics | ||
| except ImportError: # pragma: no cover - older diagrid-core | ||
| analytics = None # type: ignore[assignment] | ||
|
|
||
|
|
||
| def _is_completion_request(prog_name: str | None, complete_var: str | None) -> bool: | ||
| """Return True when click will treat this invocation as shell completion. | ||
|
|
||
| Mirrors ``click.Command._main_shell_completion``: the variable is | ||
| ``complete_var`` when given, else ``_{PROG_NAME}_COMPLETE`` upper-cased with | ||
| ``-`` and ``.`` in the program name replaced by ``_``. Click then exits | ||
| after printing the script or the completions, so such a call must neither | ||
| report nor block (``eval "$(_DIAGRIDPY_COMPLETE=zsh_source diagridpy)"`` | ||
| runs on every new terminal). | ||
| """ | ||
| try: | ||
| if complete_var is None: | ||
| name = prog_name or os.path.basename(sys.argv[0]) | ||
| name = name.replace("-", "_").replace(".", "_") | ||
| complete_var = f"_{name}_COMPLETE".upper() | ||
| return bool(os.environ.get(complete_var)) | ||
| except Exception: | ||
| return False | ||
|
|
||
|
|
||
| def _report_cli_usage() -> None: | ||
| """Report one anonymous usage event to Reo. Never raises. | ||
|
|
||
| The first run that is about to send prints a one-time notice to stderr | ||
| first; nothing else is ever printed. | ||
| """ | ||
| if analytics is None: | ||
| return | ||
| try: | ||
| if analytics.REO_SINK.configured() and analytics.reo_allowed(): | ||
| telemetry_notice.show_notice_once() | ||
| analytics.report_usage_blocking("diagrid-cli", sinks=(analytics.REO_SINK,)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This makes
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added in a4f66c6. The first time diagridpy is about to send an event, it prints a two-line notice to stderr that names DO_NOT_TRACK and DIAGRID_NO_ANALYTICS. Then it writes ~/.diagrid/telemetry-notice-shown (or the same file under $DIAGRID_HOME), so the notice only shows once. When reporting is skipped (opted out, CI, Kubernetes, a Catalyst target, shell completion) it prints nothing and writes no marker. If the marker can't be written, the command still exits normally and the notice shows again on the next run. I added a line about it to the README too. |
||
| except Exception: | ||
| logging.getLogger(__name__).debug("cli usage report failed", exc_info=True) | ||
|
|
||
|
|
||
| class _UsageReportingGroup(click.Group): | ||
| """A group that reports usage before it parses anything. | ||
|
|
||
| ``--help`` and ``--version`` are eager options: click handles them while | ||
| parsing, before the group callback runs. ``main`` is the one point every | ||
| invocation passes through, including ``CliRunner.invoke``. | ||
| """ | ||
|
|
||
| def main( | ||
| self, | ||
| args: Any = None, | ||
| prog_name: str | None = None, | ||
| complete_var: str | None = None, | ||
| *rest: Any, | ||
| **kwargs: Any, | ||
| ) -> Any: | ||
| if not _is_completion_request(prog_name, complete_var): | ||
| _report_cli_usage() | ||
| return super().main(args, prog_name, complete_var, *rest, **kwargs) | ||
|
|
||
|
|
||
| @click.group() | ||
| @click.group(cls=_UsageReportingGroup) | ||
| # Read the version from the installed ``diagrid-cli`` distribution rather than | ||
| # hardcoding it. The release workflow bumps ``diagrid/cli/pyproject.toml`` but | ||
| # never touched a literal here, so a hardcoded string silently goes stale. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| # Copyright (c) 2026-Present Diagrid Inc. | ||
| # SPDX-License-Identifier: BUSL-1.1 | ||
|
|
||
| """The one-time notice ``diagridpy`` prints before it first reports usage. | ||
|
|
||
| Usage reporting is opt-out, so a CLI user who never reads the README would | ||
| otherwise not know it happens. The first run that is about to send an event | ||
| prints this notice to stderr and records that in a marker file, the way | ||
| ``dotnet``, Homebrew and the Azure CLI do. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import logging | ||
| import os | ||
| from pathlib import Path | ||
|
|
||
| import click | ||
|
|
||
| NOTICE = ( | ||
| "diagridpy sends anonymous usage data to Diagrid (Reo): package version, " | ||
| "OS, architecture and Python version.\n" | ||
| "To turn it off, set DO_NOT_TRACK=1 or DIAGRID_NO_ANALYTICS=1. Details: " | ||
| "https://github.com/diagridio/python-ai#usage-analytics\n" | ||
| "\n" | ||
| ) | ||
|
|
||
| MARKER_NAME = "telemetry-notice-shown" | ||
|
|
||
|
|
||
| def marker_path() -> Path: | ||
| """Return the marker file path, resolved now rather than at import. | ||
|
|
||
| Same rule as ``diagrid.core.config.constants.DIAGRID_DIR``: ``$DIAGRID_HOME`` | ||
| when set, else ``~/.diagrid``. That constant is computed at import, so it | ||
| would miss a ``DIAGRID_HOME`` set afterwards. | ||
| """ | ||
| home = os.environ.get("DIAGRID_HOME") | ||
| base = Path(home) if home else Path.home() / ".diagrid" | ||
| return base / MARKER_NAME | ||
|
|
||
|
|
||
| def show_notice_once() -> None: | ||
| """Print the notice to stderr unless it was shown before. Never raises. | ||
|
|
||
| The marker is written after printing. If it can't be written, the notice | ||
| shows again on the next run, which is accepted. | ||
| """ | ||
| try: | ||
| marker = marker_path() | ||
| if marker.exists(): | ||
| return | ||
| click.echo(NOTICE, err=True, nl=False) | ||
| marker.parent.mkdir(parents=True, exist_ok=True) | ||
| marker.touch() | ||
| except Exception: | ||
| logging.getLogger(__name__).debug( | ||
| "telemetry notice marker could not be written", exc_info=True | ||
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: the runner never passes
sinks(it callsreport_usage("diagrid", kind=..., ...)), so the fallback doesn't need the new parameter, theTYPE_CHECKINGimport ofSinkor theSequenceimport. The old**dimensionssignature still matches every call site.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried that, but mypy rejects it:
runner.py:30: error: All conditional function variants must have identical signatures [misc]. The realreport_usagenow takes thesinkskeyword, so the fallback has to match it even though the runner never passes it. I've left it as it is.