Skip to content

Commit df533ea

Browse files
committed
Send the telemetry event with the CLI token
Delivery now takes its token from cli_token: MAPBOX_CLI_TOKEN, else the user's login for the run's profile, else the bundled token. This replaces the telemetry-only MAPBOX_CLI_TELEMETRY_TOKEN.
1 parent dd63d3d commit df533ea

6 files changed

Lines changed: 26 additions & 26 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,10 @@ that may never merge. They are not releases and are not listed here.
256256
`mapbox config list` now reports a second key, `history`.
257257

258258
- Each run sends one `cli.command` telemetry event to Mapbox, from a
259-
background process the command doesn't wait for. A build from source sends
260-
nothing and appends it to `~/.mapbox/.telemetry/<date>.jsonl` instead,
261-
kept for 7 days. It never touches stdout, the exit code or how long a
259+
background process the command doesn't wait for, with your `mapbox auth
260+
login` token or, when you're logged out, one built into the CLI. A build
261+
from source has no built-in token, so logged out it appends the event to
262+
`~/.mapbox/.telemetry/<date>.jsonl` instead, kept for 7 days. It never touches stdout, the exit code or how long a
262263
command takes, so a script sees no difference; a CI job does find a
263264
`.mapbox/.telemetry` directory it didn't before. `MAPBOX_CLI_NO_TELEMETRY=1`
264265
turns it off.

‎README.md‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,9 +291,11 @@ size, and for a JSON body only the field names the API defines), how it
291291
ended, and how long it took — never a token, a file path or free text you
292292
typed. It carries a random ID that is replaced on the first run a day after
293293
it was created. It is sent in the background, so a command never waits for
294-
it, and dropped if it can't be delivered. A build from source sends nothing
295-
and keeps the event in `~/.mapbox/.telemetry/<date>.jsonl` (kept for 7 days)
296-
instead. `MAPBOX_CLI_NO_TELEMETRY=1` turns it off.
294+
it, and dropped if it can't be delivered. It is sent with your
295+
`mapbox auth login` token when you're logged in, and with a token built into
296+
the CLI otherwise. A build from source has no built-in token, so when you're
297+
logged out it keeps the event in `~/.mapbox/.telemetry/<date>.jsonl` (kept
298+
for 7 days) instead. `MAPBOX_CLI_NO_TELEMETRY=1` turns it off.
297299

298300
### Diagnostics and settings
299301

‎src/cli_token.rs‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ pub(crate) enum Source {
3535

3636
/// The token a background request should carry, or `None` when there is
3737
/// nothing to send it with.
38-
// The first caller is telemetry delivery, which lands separately.
39-
#[allow(dead_code)]
4038
pub(crate) fn for_background(profile: Option<&str>) -> Option<String> {
4139
let override_token = std::env::var("MAPBOX_CLI_TOKEN").ok();
4240
// Read-only: a background request must not be what creates or
@@ -77,7 +75,6 @@ fn choose(
7775
usable(bundled).map(|token| (Source::Bundled, token.to_owned()))
7876
}
7977

80-
#[allow(dead_code)]
8178
fn now_secs() -> u64 {
8279
std::time::SystemTime::now()
8380
.duration_since(std::time::UNIX_EPOCH)

‎src/telemetry_event.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,8 @@ pub(crate) fn deliver(record: &Record) {
308308
}
309309
let event = build(record);
310310
if let Ok(line) = serde_json::to_string(&event) {
311-
telemetry_sink::deliver(&line);
311+
let profile = record.options.as_ref().and_then(|o| o.profile.as_deref());
312+
telemetry_sink::deliver(&line, profile);
312313
}
313314
}
314315

‎src/telemetry_sink.rs‎

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
//! delivery change — a new endpoint, batching — happen without touching them.
77
//!
88
//! [`deliver`] is the one place that picks the [`Sink`]. [`HttpSink`] sends
9-
//! the event to Mapbox Events when this build has somewhere to send it — a
10-
//! token compiled in, or [`URL_ENV`]. Otherwise [`FileSink`] keeps it on this
11-
//! machine, which is what `cargo build` and `cargo test` get.
9+
//! the event to Mapbox Events when there is a token to send it with — from
10+
//! [`crate::cli_token`] — or a [`URL_ENV`]. Otherwise [`FileSink`] keeps it
11+
//! on this machine, which is what `cargo test` and a logged-out source build
12+
//! get.
1213
//!
1314
//! It also owns the directory, `~/.mapbox/.telemetry` (or under
1415
//! `$MAPBOX_CONFIG_DIR`), written only once the config directory exists,
@@ -25,6 +26,7 @@ use reqwest::header::{CONTENT_TYPE, USER_AGENT};
2526
use serde_json::json;
2627

2728
use crate::auth;
29+
use crate::cli_token;
2830
use crate::dated_jsonl;
2931
use crate::http;
3032
use crate::telemetry;
@@ -48,19 +50,14 @@ const _: () = assert!(
4850
"telemetry_sink::ENDPOINT is still staging; switch it to PRODUCTION_ENDPOINT before a production release"
4951
);
5052

51-
/// The CLI's own `pk.` token for Mapbox Events, compiled into release
52-
/// builds. Never the user's: the event must not be tied to their account.
53-
const TOKEN: Option<&str> = option_env!("MAPBOX_CLI_TELEMETRY_TOKEN");
54-
5553
/// Set by [`HttpSink`] on the child it spawns, and read by [`is_send_child`]
5654
/// at the top of `main` — a mode of this binary rather than a hidden
5755
/// subcommand, for the reason `update_check` gives.
5856
const SEND_ENV: &str = "MAPBOX_INTERNAL_TELEMETRY_SEND";
5957

60-
/// The full URL to send to, token included, overriding [`ENDPOINT`] and
61-
/// [`TOKEN`]. The parent hands it to the child; it is also the seam
62-
/// `tests/telemetry_events.rs` points at a loopback server, and how a
63-
/// development build sends to staging without a compiled-in token.
58+
/// The full URL to send to, token included, overriding [`ENDPOINT`] and the
59+
/// token. The parent hands it to the child; it is also the seam
60+
/// `tests/telemetry_events.rs` points at a loopback server.
6461
const URL_ENV: &str = "MAPBOX_INTERNAL_TELEMETRY_URL";
6562

6663
/// Turns on the delivery log: one line when an event is handed to the child
@@ -151,19 +148,20 @@ impl Sink for HttpSink {
151148
}
152149

153150
/// Hands `line` to [`HttpSink`] when there is a URL to send to, and to
154-
/// [`FileSink`] otherwise.
155-
pub(crate) fn deliver(line: &str) {
156-
match send_url() {
151+
/// [`FileSink`] otherwise. `profile` is the run's `--profile`, whose login
152+
/// [`crate::cli_token`] may send with.
153+
pub(crate) fn deliver(line: &str, profile: Option<&str>) {
154+
match send_url(profile) {
157155
Some(url) => HttpSink { url }.deliver(line),
158156
None => FileSink.deliver(line),
159157
}
160158
}
161159

162-
fn send_url() -> Option<String> {
160+
fn send_url(profile: Option<&str>) -> Option<String> {
163161
if let Some(url) = from_environment(URL_ENV) {
164162
return Some(url);
165163
}
166-
TOKEN.map(|token| format!("{ENDPOINT}?access_token={token}"))
164+
cli_token::for_background(profile).map(|token| format!("{ENDPOINT}?access_token={token}"))
167165
}
168166

169167
/// Whether this process is the sender rather than a command.

‎tests/telemetry_events.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ fn command(home: &Path) -> Command {
4545
.env_remove("MAPBOX_OUTPUT")
4646
.env_remove("MAPBOX_CLI_NO_TELEMETRY")
4747
.env_remove("MAPBOX_INTERNAL_TELEMETRY_URL")
48+
.env_remove("MAPBOX_CLI_TOKEN")
4849
.env_remove("MAPBOX_INTERNAL_TELEMETRY_SEND")
4950
.env_remove("MAPBOX_INTERNAL_TELEMETRY_LOG")
5051
.env("MAPBOX_NO_UPDATE_CHECK", "1")

0 commit comments

Comments
 (0)