-
-
Notifications
You must be signed in to change notification settings - Fork 736
Improve analytics #214
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
Merged
Merged
Improve analytics #214
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e4a0f70
Capture key value name in set config value
wonderwhy-er 96421e3
Capture more about containers enviroment
wonderwhy-er b0d340a
Bunch of analytics upgrades
wonderwhy-er 34c3f6d
Remove analytics audit logging to local file
wonderwhy-er 8c464ee
Merge remote-tracking branch 'origin/main' into improve-analytics
wonderwhy-er 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
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
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
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.
🛠️ Refactor suggestion
Audit file may capture unsanitized PII and “unknown” client_id in some paths.
Make logAnalyticsAudit robust: sanitize properties internally and lazily ensure a clientId before writing. This protects privacy even if callers forget to sanitize.
Apply this diff:
async function logAnalyticsAudit(event: string, properties: any, success: boolean, error?: string) { try { + // Ensure we have a client id for auditing + if (uniqueUserId === 'unknown') { + try { + uniqueUserId = await getOrCreateUUID(); + } catch { + // Best-effort; if it fails, we'll keep 'unknown' + } + } + // Check if analytics audit logging is enabled (default: false for production) const analyticsAuditEnabled = await configManager.getValue('analyticsAuditEnabled'); if (!analyticsAuditEnabled) { return; // Skip logging if not explicitly enabled } // Ensure analytics audit directory exists const auditDir = path.dirname(ANALYTICS_AUDIT_FILE); if (!fs.existsSync(auditDir)) { await fs.promises.mkdir(auditDir, { recursive: true }); } // Check if file size is approaching limit and rotate if needed let fileSize = 0; try { const stats = await fs.promises.stat(ANALYTICS_AUDIT_FILE); fileSize = stats.size; } catch (error) { // File doesn't exist yet, size remains 0 } // If file size is at limit, rotate the log file if (fileSize >= ANALYTICS_AUDIT_FILE_MAX_SIZE) { const fileExt = path.extname(ANALYTICS_AUDIT_FILE); const fileBase = path.basename(ANALYTICS_AUDIT_FILE, fileExt); const dirName = path.dirname(ANALYTICS_AUDIT_FILE); // Create a timestamp-based filename for the old log const date = new Date(); const rotateTimestamp = `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}_${String(date.getHours()).padStart(2, '0')}-${String(date.getMinutes()).padStart(2, '0')}-${String(date.getSeconds()).padStart(2, '0')}`; const newFileName = path.join(dirName, `${fileBase}_${rotateTimestamp}${fileExt}`); // Rename the current file await fs.promises.rename(ANALYTICS_AUDIT_FILE, newFileName); } // Prepare audit log entry const timestamp = new Date().toISOString(); + // Last line of defense: sanitize before persisting locally + const safeProperties = sanitizeProperties(properties || {}); const auditEntry = { timestamp, event, - properties, + properties: safeProperties, success, error: error || null, client_id: uniqueUserId }; // Format as readable JSON log entry const logLine = `${timestamp} | ${success ? 'SUCCESS' : 'FAILED'} | ${event} | ${JSON.stringify(auditEntry)}\n`; // Append to audit log file await fs.promises.appendFile(ANALYTICS_AUDIT_FILE, logLine, 'utf8'); } catch (auditError) { // Don't let audit logging errors affect the main functionality console.error(`Analytics audit logging error: ${auditError instanceof Error ? auditError.message : String(auditError)}`); } }Additionally, add this small helper (outside the selected range) so callers and audit have a single, correct sanitizer: