-
Notifications
You must be signed in to change notification settings - Fork 4
Add telemetry events #152
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
Draft
jonathanmendez
wants to merge
35
commits into
enterprise-main
Choose a base branch
from
add-more-telemetry-events
base: enterprise-main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Add telemetry events #152
Changes from 24 commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
3b4a633
WIP
gcp e38d646
feat: add enterprise download security telemetry with MOZ_ENTERPRISE …
gcp ea2854a
feat: add enterprise download telemetry with policy controls
gcp e7f5908
chore: update Cargo.lock
gcp 02f5632
feat: add TELEMETRY_ENDPOINT env var support for telemetry endpoints
gcp 61dd192
refactor: unify TELEMETRY_ENDPOINT handling across components
gcp 3205509
refactor: use lazy loading for DownloadsTelemetry
gcp 4caf74b
feat: add localhost support for telemetry testing and debugging
gcp 274abf1
feat: prioritize TELEMETRY_ENDPOINT env var over localhost config
gcp 150fcc7
feat: add debug logging for download telemetry
gcp 038925f
fix: Improve error handling and resource path resolution in downloads…
gcp 47cea1a
feat: wrap file_downloaded telemetry in MOZ_ENTERPRISE ifdef
gcp e484c38
fix: use MOZ_TELEMETRY_REPORTING instead of MOZILLA_OFFICIAL for tele…
gcp b1f9540
feat: reset telemetry localhost port and expose MOZ_TELEMETRY_REPORTI…
gcp cc580c9
refactor: move download telemetry to dedicated enterprise ping
gcp ca80612
fix: use PathUtils as global instead of importing as ES module
gcp 6997c65
POC metric for page printed
19b231e
Update metric to have better data
72a29ea
Put print telemetry behind #ifdef MOZ_ENTERPRISE
f824376
Add and check prefs for metric
384b4a4
Add content titles to print metric payload
e6a22fb
Add existing add-on install metric to enterprise pings
f98868d
Include info section in enterprise ping so console can consume
da7695d
Allow enterprise prefix policies to be set via policy
0bd4adb
Enterprise glean client
fiji-flo 6ee43ad
Add metric for browsing a blocked URL
f3bf7a2
Remove debugging force-enable of download telemetry
65185b4
Add policy and pref for telemetry of browsing blocked domains
2090a56
Protect against telemetry exceptions for printing
eaaa96a
Protect against telemetry exceptions during website filtering
9b74d2f
Add BlocklistDomainBrowsedTelemetry policy to schema
9e4617a
Revert defunct changes to redirect telemetry
d72789d
Remove defunct cargo dependency on local glean
7da0388
Remove obsolete pref
4b6aecb
Remove trace log statements
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
268 changes: 268 additions & 0 deletions
268
browser/components/downloads/DownloadsTelemetry.enterprise.sys.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,268 @@ | ||
| /* This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
|
||
| /** | ||
| * Enterprise Downloads Telemetry Implementation | ||
| * | ||
| * This module is only included in MOZ_ENTERPRISE builds and provides | ||
| * security telemetry for completed downloads. | ||
| * | ||
| * ENTERPRISE POLICY CONFIGURATION: | ||
| * ================================ | ||
| * | ||
| * The telemetry collection can be configured via enterprise policy to control | ||
| * the level of URL information collected. In the enterprise policies.json file: | ||
| * | ||
| * { | ||
| * "policies": { | ||
| * "DownloadTelemetry": { | ||
| * "Enabled": true, | ||
| * "UrlLogging": "full" | ||
| * } | ||
| * } | ||
| * } | ||
| * | ||
| * Configuration Options: | ||
| * - Enabled (boolean): Enable/disable download telemetry collection | ||
| * - UrlLogging (string): URL logging level with values: | ||
|
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. I think this wants FileLogging as well, where there is an intermediate level that just logs the extension and MIME type. |
||
| * - "full" (default): Collect complete download URLs including paths and parameters | ||
| * - "domain": Collect only the hostname portion of URLs | ||
| * - "none": Do not collect any URL information | ||
| * | ||
| * SECURITY CONSIDERATIONS: | ||
| * ======================= | ||
| * | ||
| * - "full" mode provides maximum visibility for security analysis but may | ||
| * contain sensitive information in URL paths/parameters | ||
| * - "domain" mode balances security monitoring with privacy by limiting | ||
| * collection to hostnames only | ||
| * - "none" mode disables URL collection entirely for high-privacy environments | ||
| * | ||
| * The default "full" mode is appropriate for most enterprise environments where | ||
| * comprehensive security monitoring is prioritized. | ||
| */ | ||
|
|
||
| import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs"; | ||
|
|
||
| const lazy = {}; | ||
|
|
||
| XPCOMUtils.defineLazyServiceGetter( | ||
| lazy, | ||
| "gMIMEService", | ||
| "@mozilla.org/mime;1", | ||
| "nsIMIMEService" | ||
| ); | ||
|
|
||
| // PathUtils will be imported lazily when needed | ||
|
|
||
| export const DownloadsTelemetryEnterprise = { | ||
| /** | ||
| * Checks if download telemetry is enabled via enterprise policy. | ||
| * | ||
| * @returns {boolean} True if telemetry should be collected | ||
| */ | ||
| _isEnabled() { | ||
| return Services.prefs.getBoolPref( | ||
| "browser.download.enterprise.telemetry.enabled", | ||
| true | ||
| ); | ||
| }, | ||
|
|
||
| /** | ||
| * Gets the configured URL logging level from enterprise policy preferences. | ||
| * | ||
| * @returns {string} One of: "full", "domain", "none" | ||
| */ | ||
| _getUrlLoggingPolicy() { | ||
| const urlLogging = Services.prefs.getCharPref( | ||
| "browser.download.enterprise.telemetry.urlLogging", | ||
| "full" | ||
| ); | ||
|
|
||
| // Validate policy value | ||
| if (["full", "domain", "none"].includes(urlLogging)) { | ||
| return urlLogging; | ||
| } | ||
|
|
||
| return "full"; // Default to full URL for enterprise environments | ||
| }, | ||
|
|
||
| /** | ||
| * Processes the source URL based on the configured logging policy. | ||
| * | ||
| * @param {string} sourceUrl - The original download URL | ||
| * @returns {string|null} Processed URL or null based on policy | ||
| */ | ||
| _processSourceUrl(sourceUrl) { | ||
| if (!sourceUrl) { | ||
| return null; | ||
| } | ||
|
|
||
| const policy = this._getUrlLoggingPolicy(); | ||
|
|
||
| switch (policy) { | ||
| case "none": | ||
| return null; | ||
|
|
||
| case "domain": | ||
| try { | ||
| const url = new URL(sourceUrl); | ||
| return url.hostname || null; | ||
| } catch (ex) { | ||
| return null; | ||
| } | ||
|
|
||
| case "full": | ||
| default: | ||
| return sourceUrl; | ||
| } | ||
| }, | ||
|
|
||
| /** | ||
| * Records a telemetry event for a completed file download. | ||
| * | ||
| * @param {object} download - The Download object containing download information | ||
| */ | ||
| recordFileDownloaded(download) { | ||
| console.log("[DownloadsTelemetryEnterprise] recordFileDownloaded called"); | ||
|
|
||
| // DEBUG: Force enable telemetry for debugging purposes | ||
| console.log( | ||
| "[DownloadsTelemetryEnterprise] DEBUG: Force enabling telemetry for debugging" | ||
| ); | ||
| try { | ||
| Services.prefs.setBoolPref( | ||
jonathanmendez marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| "browser.download.enterprise.telemetry.enabled", | ||
| true | ||
| ); | ||
| Services.prefs.setCharPref( | ||
| "browser.download.enterprise.telemetry.urlLogging", | ||
| "full" | ||
| ); | ||
| console.log( | ||
| "[DownloadsTelemetryEnterprise] DEBUG: Telemetry preferences set" | ||
| ); | ||
| } catch (e) { | ||
| console.error( | ||
| "[DownloadsTelemetryEnterprise] DEBUG: Failed to set prefs:", | ||
| e | ||
| ); | ||
| } | ||
|
|
||
| // Check if telemetry is enabled via enterprise policy | ||
| const isEnabled = this._isEnabled(); | ||
| console.log( | ||
| `[DownloadsTelemetryEnterprise] Telemetry enabled: ${isEnabled}` | ||
| ); | ||
| console.log( | ||
| `[DownloadsTelemetryEnterprise] Checking pref: browser.download.enterprise.telemetry.enabled = ${Services.prefs.getBoolPref("browser.download.enterprise.telemetry.enabled", false)}` | ||
| ); | ||
|
|
||
| if (!isEnabled) { | ||
| console.log( | ||
| "[DownloadsTelemetryEnterprise] Telemetry disabled, not recording" | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| console.log( | ||
| "[DownloadsTelemetryEnterprise] Processing download for telemetry" | ||
| ); | ||
|
|
||
| // Extract filename from target path | ||
| let filename = null; | ||
| if (download.target?.path) { | ||
| filename = PathUtils.filename(download.target.path); | ||
| } | ||
| console.log(`[DownloadsTelemetryEnterprise] Filename: ${filename}`); | ||
|
|
||
| // Extract file extension | ||
| let extension = null; | ||
| if (filename) { | ||
| const lastDotIndex = filename.lastIndexOf("."); | ||
| if (lastDotIndex > 0) { | ||
| extension = filename.substring(lastDotIndex + 1).toLowerCase(); | ||
| } | ||
| } | ||
| console.log(`[DownloadsTelemetryEnterprise] Extension: ${extension}`); | ||
|
|
||
| // Get MIME type with fallback to extension-based detection | ||
| let mimeType = download.contentType || null; | ||
| if (!mimeType && extension) { | ||
| try { | ||
| mimeType = lazy.gMIMEService.getTypeFromExtension(extension); | ||
| } catch (ex) { | ||
| // MIME service failed, leave null | ||
| console.log( | ||
| `[DownloadsTelemetryEnterprise] MIME service failed: ${ex.message}` | ||
| ); | ||
| } | ||
| } | ||
| console.log(`[DownloadsTelemetryEnterprise] MIME type: ${mimeType}`); | ||
|
|
||
| // Process source URL based on enterprise policy configuration | ||
| let sourceUrl = this._processSourceUrl(download.source?.url); | ||
| const urlPolicy = this._getUrlLoggingPolicy(); | ||
| console.log( | ||
| `[DownloadsTelemetryEnterprise] URL policy: ${urlPolicy}, processed URL: ${sourceUrl}` | ||
| ); | ||
|
|
||
| // Get file size | ||
| let sizeBytes = download.target?.size; | ||
| if (typeof sizeBytes !== "number" || sizeBytes < 0) { | ||
| sizeBytes = null; | ||
| } | ||
| console.log(`[DownloadsTelemetryEnterprise] File size: ${sizeBytes}`); | ||
|
|
||
| const telemetryData = { | ||
| filename: filename || "", | ||
| extension: extension || "", | ||
| mime_type: mimeType || "", | ||
| size_bytes: sizeBytes, | ||
| source_url: sourceUrl || "", | ||
| }; | ||
|
|
||
| console.log( | ||
| `[DownloadsTelemetryEnterprise] Recording Glean event with data:`, | ||
| telemetryData | ||
| ); | ||
|
|
||
| // Record the Glean event | ||
| console.log( | ||
| `[DownloadsTelemetryEnterprise] Glean object available: ${typeof Glean !== "undefined"}` | ||
| ); | ||
| console.log( | ||
| `[DownloadsTelemetryEnterprise] Glean.downloads available: ${typeof Glean?.downloads !== "undefined"}` | ||
| ); | ||
| console.log( | ||
| `[DownloadsTelemetryEnterprise] Glean.downloads.fileDownloaded available: ${typeof Glean?.downloads?.fileDownloaded !== "undefined"}` | ||
| ); | ||
|
|
||
| Glean.downloads.fileDownloaded.record(telemetryData); | ||
| console.log( | ||
| `[DownloadsTelemetryEnterprise] Glean event recorded successfully` | ||
| ); | ||
|
|
||
| // Submit the enterprise ping | ||
| GleanPings.enterprise.submit(); | ||
jonathanmendez marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| console.log(`[DownloadsTelemetryEnterprise] Enterprise ping submitted`); | ||
| } catch (ex) { | ||
| // Silently fail - telemetry errors should not break downloads | ||
| console.error( | ||
| `[DownloadsTelemetryEnterprise] Download telemetry recording failed:`, | ||
| ex | ||
| ); | ||
| try { | ||
| ChromeUtils.reportError(`Download telemetry recording failed: ${ex}`); | ||
| } catch (reportEx) { | ||
| // ChromeUtils.reportError may not be available in all contexts | ||
| console.error( | ||
| `[DownloadsTelemetryEnterprise] Could not report error:`, | ||
| reportEx | ||
| ); | ||
| } | ||
| } | ||
| }, | ||
| }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Should we remove the console.log debugging as part of the PR?
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.
Yeah that can all be reduced to the minimum, a lot of this was tracing to see where things were going.