Skip to content

feat(analytics): report usage to Reo alongside Scarf - #473

Open
marcduiker wants to merge 9 commits into
mainfrom
dev-783-reo-tracking-pypi-diagrid-and-diagrid-cli
Open

marcduiker wants to merge 9 commits into
mainfrom
dev-783-reo-tracking-pypi-diagrid-and-diagrid-cli

Conversation

@marcduiker

Copy link
Copy Markdown
Contributor

What and why

This adds Reo as a second usage-analytics destination next to Scarf. Agent runs and diagridpy invocations can then be attributed to accounts in Reo. Tracked in DEV-783.

diagrid/core/analytics.py already sends one anonymous event per package per process to Scarf. This PR adds a second destination to that reporter. It does not add a new tracker, and it adds no dependency. reo-census was considered and rejected: it would add a fourth runtime dependency, and it can't see the gate described below.

What changes

  • analytics.py sends through a destination list. Each destination has its own endpoint, gate and request builder, and its own once-per-process dedupe.

    • Scarf is the existing behaviour moved over. Its bytes on the wire are unchanged. Two golden tests pin its full URL and its User-Agent.
    • Setting a destination's endpoint to an empty string turns that destination off. USAGE_ENDPOINT already worked this way, and REO_ENDPOINT now does too.
  • Reo gets an unauthenticated GET. No API key and no auth header:

    https://telemetry.diagrid.io/data?activity_type=pypi_download&product=diagrid&version=…&python=…&platform=…&arch=…&kind=agent&framework=…&framework_version=…&core_version=…
    
    • The builder sends only keys on a fixed allowlist, mapped to the names registered in Reo. For example, os becomes platform and python_version becomes python.
    • target and ci are not sent to Reo. The gate already filters on both, so their values would always be the same.
  • The Reo gate is stricter than Scarf's, and it fails closed. Reo is skipped when any of these hold:

    • any opt-out is set (DO_NOT_TRACK, SCARF_NO_ANALYTICS, DIAGRID_NO_ANALYTICS)
    • KUBERNETES_SERVICE_HOST is set
    • the process points at Catalyst
    • a CI variable is set
    • any of those checks raises

    Scarf's gate is still opt-out only. The two services will report different totals, and the README explains why.

  • diagridpy now reports to Reo only, once per invocation. That includes --help and --version.

    • The CLI exits right after the call, so the send blocks. It has a hard 1-second wall-clock cap, which includes DNS. A send still running at the cap is abandoned.
    • Shell-completion requests (_DIAGRIDPY_COMPLETE) are not reported. Otherwise every Tab press and every shell start would wait on the network.
    • diagrid-cli allows diagrid-core>=0.1.0, and analytics only exists from 0.5.1. The import is guarded the same way runner.py guards it, so an older core turns reporting off instead of crashing the CLI.
  • The README "Usage analytics" section is rewritten. It covers:

    • both services, and which fields each receives
    • when Reo is skipped
    • how diagridpy reports
    • the three opt-out variables, which are unchanged and turn off both services

How it was tested

  • uv run pytest tests -m "not integration": 1057 passed, 4 skipped. The analytics and CLI tests also pass under CI=true. Every test that expects Reo to send first clears the CI variables, so none of them passes vacuously on GitHub Actions.

  • No test can reach a real host. The root conftest.py blanks both endpoints. The Reo tests use request recorders, example.invalid or 127.0.0.1 listeners.

  • Opt-outs, against a local listener:

    Setting Requests
    DO_NOT_TRACK=1 0
    SCARF_NO_ANALYTICS=1 0
    DIAGRID_NO_ANALYTICS=1 0
    Control, no variable set 3 (1 Scarf, 2 Reo)
  • Cost of diagridpy --help:

    Situation Time
    Reo endpoint never replies 1.01 s
    Send stalled for 5 s 1.00 s
    DO_NOT_TRACK=1 0.001 s

Worth a look in review

  • Tab completion is detected the way click detects it (complete_var, or _{PROG}_COMPLETE from the program name). An unrelated tool's exported _*_COMPLETE variable therefore doesn't switch reporting off.
  • Sends are sequential on one daemon thread. On the agent path, a hung Scarf send can delay the Reo send by up to the 1 s timeout. This happens off the caller's thread.
  • Timing tests. Two of them assert under 1.5 s against the 1 s cap. If they flake on a loaded Windows runner, widen them to 2 s.

Before release (owner checks, not code)

  • Confirm a real pypi_download event shows up in the Reo dashboard. A 200 from the endpoint proves nothing. No nuget_runtime test event has shown up yet either, and that's being followed up with Reo.
  • Register core_version on pypi_download in Reo, or Reo will drop it. arch, kind, framework and framework_version are already registered.
  • Confirm two README claims: that Reo derives company and location from the request IP, and the privacy-policy link https://www.reo.dev/privacy-policy.

Checklist

  • uv run ruff format (no path arguments, so examples/ is covered too)
  • uv run flake8 diagrid tests --ignore=E501,F401,W503,E203,E704
  • uv run mypy --config-file mypy.ini
  • uv run pytest tests -m "not integration"
  • New or changed behaviour has tests
  • New files under diagrid/ carry the license header (no new files)
  • Commits are signed off (git commit -s)

🤖 Generated with Claude Code

marcduiker and others added 8 commits November 17, 2026 12:53
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Marc Duiker <4191277+marcduiker@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Marc Duiker <4191277+marcduiker@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Marc Duiker <4191277+marcduiker@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Marc Duiker <4191277+marcduiker@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Marc Duiker <4191277+marcduiker@users.noreply.github.com>
…tics

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Marc Duiker <4191277+marcduiker@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Marc Duiker <4191277+marcduiker@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Marc Duiker <4191277+marcduiker@users.noreply.github.com>
@marcduiker
marcduiker requested a review from a team as a code owner November 17, 2026 11:53
@CasperGN CasperGN self-assigned this Nov 17, 2026

@CasperGN CasperGN left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @marcduiker 🙌 Routing both destinations through one Sink list with a shared _select_sinks is a clean way to keep the dedupe and gates from drifting apart. The Reo gate failing closed, and the golden tests that pin Scarf's exact URL and User-Agent, make this easy to trust 🧪 I left a couple of comments inline, please take a look.

Drafted with AI assistance and reviewed by me.

Comment thread diagrid/cli/main.py
if analytics is None:
return
try:
analytics.report_usage_blocking("diagrid-cli", sinks=(analytics.REO_SINK,))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes diagridpy report on every run, including --help and --version, and it blocks for up to a second. The README covers it, but a CLI user who never reads the README sees nothing. What do you think about printing a one-time notice to stderr on first run that names the opt-out variables (DO_NOT_TRACK, DIAGRID_NO_ANALYTICS)? It could be recorded with a marker file under the user's config dir, the way dotnet, Homebrew and the Azure CLI handle telemetry. It's a small change, and it saves the first person who notices the outbound request from opening an issue about it. Not blocking.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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.

def report_usage(package: str, **dimensions: object) -> None:
# ``sinks`` is accepted and ignored: an older core has no destination list.
def report_usage(
package: str, *, sinks: Sequence["Sink"] = (), **dimensions: object

Copy link
Copy Markdown
Contributor

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 calls report_usage("diagrid", kind=..., ...)), so the fallback doesn't need the new parameter, the TYPE_CHECKING import of Sink or the Sequence import. The old **dimensions signature still matches every call site.

Copy link
Copy Markdown
Contributor Author

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 real report_usage now takes the sinks keyword, so the fallback has to match it even though the runner never passes it. I've left it as it is.

…eports

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants