From b657664546b28268a93f7c6748a4e51046ad8f35 Mon Sep 17 00:00:00 2001 From: Brahim Hadriche Date: Fri, 29 Aug 2025 15:22:57 -0400 Subject: [PATCH] Add Telemetry (#704) --- CHANGELOG.md | 6 ++ PRIVACY.md | 2 + playlet-lib/src/components/Logger/Logger.bs | 3 + .../src/components/MainScene.transpiled.xml | 1 + playlet-lib/src/components/MainScene.xml | 3 + .../MainScene_bindings.transpiled.brs | 4 + .../Services/Innertube/InnertubeService.bs | 6 +- .../src/components/Telemetry/Sentry.bs | 62 +++++++++++++++ .../src/components/Telemetry/Telemetry.bs | 75 +++++++++++++++++++ .../src/components/Telemetry/Telemetry.xml | 10 +++ .../components/VideoPlayer/VideoContentJob.bs | 2 + .../src/components/VideoPlayer/VideoPlayer.bs | 1 + playlet-lib/src/config/preferences.json5 | 7 ++ tools/bs-plugins/logger-plugin.ts | 6 +- 14 files changed, 186 insertions(+), 2 deletions(-) create mode 100644 playlet-lib/src/components/Telemetry/Sentry.bs create mode 100644 playlet-lib/src/components/Telemetry/Telemetry.bs create mode 100644 playlet-lib/src/components/Telemetry/Telemetry.xml diff --git a/CHANGELOG.md b/CHANGELOG.md index 39a7c3b0..2a23d5e7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] + +### Added + +- Telemetry to collect anonymous diagnostics data. Can be turned off from the settings. + ## [0.40.15] - 2025-08-28 ### Fixed diff --git a/PRIVACY.md b/PRIVACY.md index 2bc224fa..c4132c8c 100644 --- a/PRIVACY.md +++ b/PRIVACY.md @@ -6,6 +6,8 @@ Playlet, the Roku app, does not collect any user personal information whatsoever Playlet does not show advertising, whether personalized or non personalized. +Playlet may integrate with 3rd party telemetry services in order to capture anonymous diagnostics data. The only purpose of this data is to diagnose issues and improve the app. + ## Invidious Playlet may request information from one or more [Invidious](https://invidious.io/) diff --git a/playlet-lib/src/components/Logger/Logger.bs b/playlet-lib/src/components/Logger/Logger.bs index 29479bff..9d8c7561 100644 --- a/playlet-lib/src/components/Logger/Logger.bs +++ b/playlet-lib/src/components/Logger/Logger.bs @@ -112,6 +112,7 @@ function OnLineSysLog(event as object, dateTime as object, logsFile as string, b print line buffer.FromAsciiString(line + `\n`) buffer.AppendFile(logsFile) + m.global.telemetry.logError = line end function function OnLineLogMonitorMemoryWarning(event as object, dateTime as object, logsFile as string, buffer as object) as void @@ -120,6 +121,7 @@ function OnLineLogMonitorMemoryWarning(event as object, dateTime as object, logs print line buffer.FromAsciiString(line + `\n`) buffer.AppendFile(logsFile) + m.global.telemetry.logWarn = line end function function OnLineLogDeviceInfoMemoryWarning(event as object, dateTime as object, logsFile as string, buffer as object) as void @@ -138,6 +140,7 @@ function OnLineLogDeviceInfoMemoryWarning(event as object, dateTime as object, l print line buffer.FromAsciiString(line + `\n`) buffer.AppendFile(logsFile) + m.global.telemetry.logWarn = line end function ' Note: although the timestamp should be captured in the caller, not the logger, diff --git a/playlet-lib/src/components/MainScene.transpiled.xml b/playlet-lib/src/components/MainScene.transpiled.xml index 22cad120..2744f4f3 100644 --- a/playlet-lib/src/components/MainScene.transpiled.xml +++ b/playlet-lib/src/components/MainScene.transpiled.xml @@ -53,6 +53,7 @@ + diff --git a/playlet-lib/src/components/MainScene.xml b/playlet-lib/src/components/MainScene.xml index 38cf3ff9..0e9bc066 100644 --- a/playlet-lib/src/components/MainScene.xml +++ b/playlet-lib/src/components/MainScene.xml @@ -103,6 +103,9 @@ videoQueue="bind:../VideoQueue" notifications="bind:../Notifications" invidious="bind:../Invidious" /> + \ No newline at end of file diff --git a/playlet-lib/src/components/MainScene_bindings.transpiled.brs b/playlet-lib/src/components/MainScene_bindings.transpiled.brs index 1146d4dd..865fded2 100644 --- a/playlet-lib/src/components/MainScene_bindings.transpiled.brs +++ b/playlet-lib/src/components/MainScene_bindings.transpiled.brs @@ -64,6 +64,10 @@ function InitializeBindings() "videoQueue": "../VideoQueue", "notifications": "../Notifications", "invidious": "../Invidious" + }, + "Telemetry": { + "preferences": "../Preferences", + "applicationInfo": "../ApplicationInfo" } } } diff --git a/playlet-lib/src/components/Services/Innertube/InnertubeService.bs b/playlet-lib/src/components/Services/Innertube/InnertubeService.bs index 62ef19bf..18210770 100644 --- a/playlet-lib/src/components/Services/Innertube/InnertubeService.bs +++ b/playlet-lib/src/components/Services/Innertube/InnertubeService.bs @@ -107,9 +107,13 @@ namespace InnertubeService try parsedResponse = Innertube.ParseInnertubePlayerResponse(playerResponse.Json(), playerRequest.signatureTimestamp, cancellation) catch exception - if ValidString(exception.code) = "STS_MISMATCH" + code = ValidString(exception.code) + if code = "STS_MISMATCH" return GetVideoMetadata(videoId, options) end if + if code = "MISSING_FORMAT_FIELD" + m.global.telemetry.LogError = code + `\n` + playerRequest.ToCurlCommand() + `\n` + playerResponse.Text() + end if throw exception end try if parsedResponse.error <> invalid diff --git a/playlet-lib/src/components/Telemetry/Sentry.bs b/playlet-lib/src/components/Telemetry/Sentry.bs new file mode 100644 index 00000000..afc0b4e3 --- /dev/null +++ b/playlet-lib/src/components/Telemetry/Sentry.bs @@ -0,0 +1,62 @@ +import "pkg:/source/services/HttpClient.bs" + +const SENTRY_ENVELOPE_URL = "https://o4509923566288896.ingest.us.sentry.io/api/4509923579265024/envelope/" +const SENTRY_KEY = "0bda23ffe3634745f57a0edbffdd18be" + +namespace Sentry + + function SendLogEvent(level as string, message as string, tags as object) as void + eventId = GenerateEventId() + timestamp = CreateObject("roDateTime").ToISOString() + + header = FormatJson({ + event_id: eventId + sent_at: timestamp + sdk: { + name: "sentry.roku.playlet" + version: "1.0.0" + } + }) + event = `{"type":"event"}` + eventPayload = FormatJson({ + event_id: eventId + timestamp: timestamp + platform: "roku" + #if DEBUG + environment: "development" + #else + environment: "production" + #end if + tags: tags + level: level + logentry: { + formatted: message + } + }) + + payload = header + `\n` + event + `\n` + eventPayload + + request = HttpClient.Post(SENTRY_ENVELOPE_URL, payload) + request.QueryParam("sentry_key", SENTRY_KEY) + request.Header("Content-Type", "text/plain;charset=UTF-8") + response = request.Await() + if not response.IsSuccess() + #if DEBUG + LogError("Failed to send event to Sentry:", response.ErrorMessage()) + #end if + return + end if + + LogDebug("Sent event to Sentry with ID:", eventId, "Response:", response.Text()) + end function + + function GenerateEventId() as string + hexChars = "0123456789abcdef" + eventId = "" + for i = 0 to 31 + eventId = eventId + hexChars.Mid(rnd(16) - 1, 1) + end for + return eventId + end function + +end namespace diff --git a/playlet-lib/src/components/Telemetry/Telemetry.bs b/playlet-lib/src/components/Telemetry/Telemetry.bs new file mode 100644 index 00000000..7c9fb227 --- /dev/null +++ b/playlet-lib/src/components/Telemetry/Telemetry.bs @@ -0,0 +1,75 @@ +import "pkg:/components/Telemetry/Sentry.bs" +import "pkg:/source/utils/Logging.bs" +import "pkg:/source/utils/Types.bs" + +function Init() as void + if m.global.telemetry <> invalid + return + end if + m.global.addFields({ telemetry: m.top }) + m.top.functionName = "TelemetryLoop" +end function + +function OnNodeReady() as void + m.tags = GetTags() + m.top.ObserveField("enabled", FuncName(OnEnabledChanged)) + m.preferences.ObserveFieldScoped("dev.diagnostics.enabled", FuncName(OnPreferencesChanged)) + enabled = m.preferences["dev.diagnostics.enabled"] + OnPreferencesChanged({ + enabled: enabled + getData: function() as boolean + return m.enabled + end function + }) +end function + +function OnPreferencesChanged(event as object) as void + m.top.enabled = event.getData() +end function + +function OnEnabledChanged(event as object) as void + enabled = event.getData() + LogInfo("Telemetry enabled:", enabled) + if enabled + m.top.control = "run" + else + m.top.control = "stop" + end if +end function + +function TelemetryLoop() as void + msgPort = CreateObject("roMessagePort") + m.top.observeField("LogInfo", msgPort) + m.top.observeField("LogWarn", msgPort) + m.top.observeField("LogError", msgPort) + + levelMap = { + "LogInfo": "info" + "LogWarn": "warning" + "LogError": "error" + } + + while true + msg = wait(0, msgPort) + msgType = type(msg) + + if msgType = "roSGNodeEvent" + level = levelMap[msg.getField()] + if level <> invalid + Sentry.SendLogEvent(level, msg.getData(), m.tags) + end if + end if + end while +end function + +function GetTags() as object + appInfo = m.applicationInfo + tags = appInfo@.GetAppInfo() + tags.Append(appInfo@.GetDeviceInfo()) + for each key in tags + if not IsString(tags[key]) + tags[key] = ToString(tags[key]) + end if + end for + return tags +end function diff --git a/playlet-lib/src/components/Telemetry/Telemetry.xml b/playlet-lib/src/components/Telemetry/Telemetry.xml new file mode 100644 index 00000000..e1ef7207 --- /dev/null +++ b/playlet-lib/src/components/Telemetry/Telemetry.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/playlet-lib/src/components/VideoPlayer/VideoContentJob.bs b/playlet-lib/src/components/VideoPlayer/VideoContentJob.bs index e8fa13d9..290d98fa 100644 --- a/playlet-lib/src/components/VideoPlayer/VideoContentJob.bs +++ b/playlet-lib/src/components/VideoPlayer/VideoContentJob.bs @@ -105,6 +105,8 @@ function ExecuteJob() as void if metadata.liveNow = true contentNode.Live = true end if + + m.global.telemetry.logInfo = "[VideoContentJob] success" end function function CreateStreamUrls(metadata as object, service as Invidious.InvidiousService, preferences as object, backend as string, playletServerPort as integer) as object diff --git a/playlet-lib/src/components/VideoPlayer/VideoPlayer.bs b/playlet-lib/src/components/VideoPlayer/VideoPlayer.bs index d047f562..92445d4c 100644 --- a/playlet-lib/src/components/VideoPlayer/VideoPlayer.bs +++ b/playlet-lib/src/components/VideoPlayer/VideoPlayer.bs @@ -431,6 +431,7 @@ function OnVideoPlayerStateChange() as void ' This is to reduce false positives, as we do not want retry another link if the first link is working fine. if state = "playing" and not m.videoPlayingSuccess LogInfo(`Now playing video ${content.url}`) + m.global.telemetry.logInfo = "[VideoPlayer] state=playing" m.videoPlayingSuccess = true end if diff --git a/playlet-lib/src/config/preferences.json5 b/playlet-lib/src/config/preferences.json5 index ff4bb53d..64148d80 100644 --- a/playlet-lib/src/config/preferences.json5 +++ b/playlet-lib/src/config/preferences.json5 @@ -297,6 +297,13 @@ visibility: "tv", rokuComponent: "ClearCacheControl", }, + { + displayText: "Enable Diagnostics", + key: "dev.diagnostics.enabled", + description: "Help improve Playlet by enabling diagnostics. This will send anonymous diagnotics data to the developer.", + type: "boolean", + defaultValue: true, + }, ], }, ] diff --git a/tools/bs-plugins/logger-plugin.ts b/tools/bs-plugins/logger-plugin.ts index 30ef751a..f880ec78 100644 --- a/tools/bs-plugins/logger-plugin.ts +++ b/tools/bs-plugins/logger-plugin.ts @@ -180,6 +180,8 @@ export class LoggerPlugin implements CompilerPlugin { let logLine = ''; + const telemetryEnabled = level === 'LogError' || level === 'LogWarn'; + if (isDebug) { const RED = '[31m'; const YELLOW = '[33m'; @@ -217,7 +219,9 @@ function ${newFunctionName}(${args.join(', ')}) as void if logger.logLevel < ${func.level} return end if - logger.logLine = ${logLine} + line = ${logLine} + logger.logLine = line + ${(telemetryEnabled ? `m.global.telemetry.${level} = line` : '')} end function `; }