Skip to content

About

One call to report that a program was used — a standard-library-only Python 3.8+ client for a trace server

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

38 Commits

Folders and files

Repository files navigation

trace-client (Python)

One call to report that a program was used.

A standard-library-only Python 3.8+ client for a trace server — the central place a fleet of programs reports usage events to. The whole library is one file, trace_client/trace_client.py, and the integration on the program side is meant to stay one call. The Java counterpart is trace-client-java; the two speak the same wire format and make the same promises.

from trace_client import TraceClient, allows_reporting

trace = TraceClient("https://trace.danielstephenson.dev", "roam", __version__,
                    key=settings.usage_reporting_key,
                    enabled=allows_reporting(settings.usage_reporting_enabled))
trace.report("startup")
trace.report("world-load", tags={"kind": "procedural"})

# on shutdown -- also before a short-lived program exits, so the event is sent
trace.close()

Terminal output of examples/demo.py: three events reach a stand-in trace server as POST /api/metrics with a Bearer key, each tagged with version and install; then DO_NOT_TRACK=1, enabled="false" and an empty key each give a client that sends nothing

The real output of python examples/demo.py, which runs the client against a stand-in trace server on a loopback port. Nothing leaves the machine.

How a report travels

report() only builds the JSON body and puts it on a bounded queue; one daemon thread the client owns does the HTTP. The opt-outs are decided once, in the constructor, and a disabled client never starts that thread.

sequenceDiagram
    participant P as Program
    participant C as TraceClient
    participant Q as Queue (max 256)
    participant S as Sender thread
    participant T as trace server

    P->>C: TraceClient(base_url, application, version, key=..., enabled=...)
    Note over C: opt-outs, first match wins:<br/>environment, then enabled=, then key<br/>disabled: no thread, no install ID, nothing sent
    C->>S: start daemon thread (enabled only)
    P->>C: report("startup", value, tags)
    Note over C: add tags version and install,<br/>drop None tags and non-real values
    C->>Q: put_nowait(JSON body)
    C-->>P: returns at once (full queue: dropped, logged at DEBUG)
    Q->>S: get()
    S->>T: POST /api/metrics<br/>Authorization: Bearer key
    alt 201
        T-->>S: 201 Created
    else any other status (redirects not followed)
        T-->>S: 3xx / 4xx / 5xx
    end
    Note over S: anything but 201, or no answer in 5 s:<br/>logged at DEBUG, report dropped
    P->>C: close(timeout=5)
    C->>Q: put stop signal behind queued reports
    Q->>S: remaining reports, then stop
    C-->>P: returns when drained or after timeout
Loading

Screenshots

Terminal output of examples/demo.py showing the three events the stand-in server received and the three opt-outs python examples/demo.py. The client reports startup, world-load and level-complete; every event arrives at POST /api/metrics with the key as a Bearer token, the trace-client-python/<version> User-Agent, and the version and install tags added by the client. Then DO_NOT_TRACK=1, enabled="false" and an empty key each give a client with enabled=False and the matching disabled_reason. (The install ID is a fresh random UUID each run; the example prints its first eight characters.)

Regenerating screenshots: darkroom shoot docs/screenshots/media.json (the darkroom shot list runs the example and screenshots its output).

Every event carries the program's version

The third argument to TraceClient is the program's own version, and it is required. Every event the client sends — startup, command, anything else — carries it as the tag version, so every event can be tied to a release, not just startup. An event that passes its own version tag keeps it, and the dict passed as tags is never modified. There is no need to tag startup by hand any more.

A missing or blank version, one over 255 characters after trimming, or one that is not a string, raises ValueError. So does a value that is not a string for base_url, application, key or install_id=, naming the argument, whether or not the client ends up enabled, so a settings file that turns version: 1.4 into a number is caught at startup. An empty value of another type (0, False, []) is taken as no value, as it was before that check: no key, no installation ID, or "<argument> is required" for the three positional arguments.

Before 0.3.0, the constructor took two positional arguments and only events tagged by hand carried a version. Upgrading is one argument — TraceClient(base_url, application, __version__, key=..., enabled=...) — and any tags={"version": __version__} passed to report can be dropped.

Every event carries a random installation ID

Since 0.4.0, every event can also carry the tag install: a random ID for the installation, so the trace server can count distinct installations ("active installs in the last 30 days") rather than raw events — the same idea as trace-client-java's server ID and bStats' serverUuid. It is said out loud here because it is the one thing the client sends that is the same from one event to the next.

What it is. A random uuid.uuid4(). It is not derived from anything — not a hostname, an IP address, a MAC address, a player, an account or a path. It identifies no person and no address; all it can say is "these events came from the same installation". (The trace server still sees the IP address of every HTTP request, as every web server does.)

Where it lives. Wherever the program says — there is no default location and no hidden file. Without install_id= or install_id_file=, no ID is made up, nothing is written, and no install tag is sent. The simplest way in is a file next to the program's own settings:

trace = TraceClient("https://trace.danielstephenson.dev", "roam", __version__,
                    key=settings.usage_reporting_key,
                    enabled=allows_reporting(settings.usage_reporting_enabled),
                    install_id_file=os.path.join(settings_dir, "trace-install-id"))

The first time an enabled client starts, it writes a new random UUID to that file (creating parent directories) and reuses it on every later run. The first line that is an ID ([A-Za-z0-9_.-], at most 255 characters) is the one used. If the file cannot be read or written, a fresh ID is used in memory for that run only — the constructor never raises over it, and a file that exists but cannot be read is never overwritten.

A program that already keeps its own settings can pass the ID instead:

trace = TraceClient(url, "roam", __version__, key=key,
                    install_id=settings.get("install_id"))  # None or blank: none sent

install_id= is trimmed, wins over install_id_file=, and over 255 characters raises ValueError, like the version. An event that passes its own install tag keeps it, and install is never added to an event that already has 32 tags, version included (the server's limit). trace.install_id returns the ID in use (None when disabled or when there is none), so a program can print it.

TraceClient.install_id_from_file(path) is the same load-or-create step on its own, for a program that wants the ID for something else. Called directly, it writes the file whatever the opt-outs say — pass the path as install_id_file= to keep the guarantee below.

Resetting it. Delete the file; the next start writes a new one. Or put your own value on its first line.

Opting out. Every opt-out also stops the ID: a disabled client never generates one, never reads or writes the file, and sends nothing.

What report promises

Property Meaning
Returns immediately The HTTP call runs on one daemon thread the client owns. A game loop can report from its main thread and no frame waits on the network.
Never raises A server that is down, slow, or rejecting the key is a dropped report, not an exception in your program. Drops are logged at DEBUG on the trace logger, otherwise not at all.
Bounded At most 256 reports wait to be sent; past that, new ones are dropped. A trace server that is unreachable for a week costs a few kilobytes, not your memory.
close() drains Reports already queued get up to the client timeout (5 s total, or close(timeout=...)) to be sent before close() returns, so a CLI that reports and exits at once does not lose its event. Still bounded: an unreachable server delays exit by at most the timeout. Reports still queued when the timeout runs out are not discarded: the daemon thread keeps sending them after close() returns, then stops; whatever it has not sent when the program exits is lost. On a full queue, close() drops the oldest queued report (logged at DEBUG) to make room for the signal that stops the thread.

Turning it off

Reporting is opt-out, and the person running the program always has the last word. Every switch fails closed: a value that does not clearly mean "on" — a typo, "false" as a string, an empty value, None — turns reporting off, and nothing in this client ever turns a switch back on. Any one of these yields a client that does nothing and costs nothing; the first that applies is the reason:

  • Environment, for every trace-reporting program at once (case-insensitive, surrounding space ignored): TRACE_USAGE_REPORTING set to anything but blank, true, on, yes or 1 (so off, false, 0, no, disabled, …); or DO_NOT_TRACK set to anything but blank, 0, false, off or no (so 1, true, yes, …; the consoledonottrack.com convention). An environment that cannot be read counts as opting out. The constructor checks these before enabled and the key, so they win over the program's own setting. (It checks its arguments before either, so a bad argument still raises ValueError with reporting switched off.) An unset or blank variable leaves that setting in charge. environment_opts_out() answers the same question for the program.
  • The program's own setting: enabled= anything but True or a string true/on/yes/1. Read the setting with allows_reporting(value), which fails closed the same way: False, "false", "off", a typo, a number and None (a missing setting) are all off. Pass the raw value, not bool(value) — bool("false") is True. The constructor reads enabled= through allows_reporting as well, so enabled="false" is off.
  • No key (or a blank one).

client.disabled_reason says which one applied — "environment", "config" or "no key" — and is None when the client reports, so a program can log it. A program that runs on other people's machines should expose the enabled switch in its settings — and say so once, the first time it runs, so the player knows reporting is on, that TRACE_USAGE_REPORTING=off turns it off, and where the details are: https://danielstephenson.dev/usage-reporting.

Getting it

Copy the file. trace_client/trace_client.py has no dependencies. Drop it into your source tree (as trace_client.py), keep the header so it can be found again, and you are done.

Or vendor the package — copy the trace_client/ directory — if you prefer from trace_client import TraceClient unchanged.

There is no PyPI package yet; the file is the distribution.

The wire format

POST {base_url}/api/metrics with Authorization: Bearer <key> and a body of

{"application":"roam","name":"command","value":1.0,"tags":{"name":"home","version":"1.4.0"}}

value is omitted when not given, when it is NaN or infinite (JSON has no such numbers), and when it is not a real number at all — a string such as "3", or a bool — though the event itself is still sent. Any other real number (a Decimal, a Fraction) is sent as a float. tags always holds at least version, and install when the client has an installation ID. A tag whose key or value is None is left out, and every other key and value is sent as a string. An event with a blank name is not sent at all. The server assigns the timestamp. A 201 is success; anything else is logged at DEBUG and dropped.

Redirects are not followed: a 3xx answer is logged at DEBUG with its own status and the report is dropped, so the key is never re-sent to whatever host a Location header names. Point base_url at the server's final address.

The User-Agent is trace-client-python/<client version> (<application>). Any character of the application's name that a header cannot carry (outside printable Latin-1) becomes ? there; application in the body is always the exact name.

Keys

A key identifies the program to the server and lets the operator revoke it; it is scoped to reporting only. Because it ships inside the program, it cannot prove anything — treat trace data as best-effort telemetry, which is what it is. Ask the trace operator for a key for your program.

In the browser (Pyodide)

Pyodide has no real threads and no outbound sockets, so build the client with enabled=False there. Nothing is lost: the desktop build reports.

Building

python -m unittest -v

To see the client work end to end without a trace server, run python examples/demo.py: it starts a stand-in server on a loopback port and prints what it receives.

Tests run the client against the standard library's own ThreadingHTTPServer on a loopback port. CI runs them on Python 3.8, 3.10 and 3.12.

License

MIT.

About

One call to report that a program was used — a standard-library-only Python 3.8+ client for a trace server

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages