diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b0cdb27..3944fbae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ 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] + +- Nothing worth mentioning yet. + ## [0.48.6] - 2026-10-03 ### Fixed diff --git a/playlet-lib/src/components/Telemetry/Sentry.bs b/playlet-lib/src/components/Telemetry/Sentry.bs index 9b1411ac..73965fda 100644 --- a/playlet-lib/src/components/Telemetry/Sentry.bs +++ b/playlet-lib/src/components/Telemetry/Sentry.bs @@ -5,11 +5,57 @@ const SUPPORT_SERVER_ENVELOPE_URL = "https://playlet-v8556.ondigitalocean.app/en const SENTRY_FALLBACK_ENVELOPE_URL = "https://o4509923566288896.ingest.us.sentry.io/api/4512194184544256/envelope/" const SENTRY_FALLBACK_KEY = "a16c510299b23ccc63bdc84f751c9950" -const SUPPORTS_ATTACHMENTS = false - namespace Sentry function SendEvents(events as object) as integer + payload = BuildEnvelope(events, false) + request = HttpClient.Post(SUPPORT_SERVER_ENVELOPE_URL, payload) + request.Header("Content-Type", "application/x-sentry-envelope") + response = request.Await() + ' Only when the server never answered: its own failures hit every device at once and would exhaust the fallback's quota + if response.StatusCode() <= 0 + LogWarnNoTelemetry("Support server unreachable, sending events to the Sentry fallback:", response.ErrorMessage()) + request = HttpClient.Post(SENTRY_FALLBACK_ENVELOPE_URL, BuildEnvelope([CreateSupportServerUnreachableEvent(events[0], payload, response.ErrorMessage())], true)) + request.Header("Content-Type", "application/x-sentry-envelope") + request.QueryParam("sentry_key", SENTRY_FALLBACK_KEY) + response = request.Await() + end if + + if response.IsSuccess() + LogDebug("Sent events to", request.BuildUrl(), "Response:", response.Text()) + return 0 + end if + + retryAfter = ValidString(response.Headers()["Retry-After"]).ToInt() + if retryAfter = 0 + retryAfter = 60 + end if + + if response.StatusCode() = 429 + LogErrorNoTelemetry("Rate limited by", request.BuildUrl(), "Retry after", retryAfter, "seconds") + else + LogErrorNoTelemetry("Failed to send events to", request.BuildUrl(), response.ErrorMessage(), `\nRetry after`, retryAfter, "seconds") + end if + return retryAfter + end function + + function CreateSupportServerUnreachableEvent(originalEvent as object, payload as string, errorMessage as string) as object + return { + level: "warning" + message: `Support server unreachable: ${errorMessage}` + fingerprint: ["support server unreachable"] + release: originalEvent.release + tags: originalEvent.tags + userId: originalEvent.userId + attachments: [{ + filename: "envelope.txt" + content_type: "text/plain" + data: payload + }] + } + end function + + function BuildEnvelope(events as object, supportsAttachments as boolean) as string timestamp = CreateObject("roDateTime").ToISOString() payload = FormatJson({ @@ -69,7 +115,7 @@ namespace Sentry hasAttachments = IsArray(event.attachments) and event.attachments.Count() > 0 - if (not SUPPORTS_ATTACHMENTS) and hasAttachments + if (not supportsAttachments) and hasAttachments extra = {} for each attachment in event.attachments extra[attachment.filename] = `content_type:${attachment.content_type}\n${Redact(attachment.data)}` @@ -83,7 +129,7 @@ namespace Sentry payload += FormatJson(eventPayload) + `\n` - if SUPPORTS_ATTACHMENTS and hasAttachments + if supportsAttachments and hasAttachments tmpBuffer = CreateObject("roByteArray") for each attachment in event.attachments data = Redact(attachment.data) @@ -98,34 +144,7 @@ namespace Sentry end if end for - request = HttpClient.Post(SUPPORT_SERVER_ENVELOPE_URL, payload) - request.Header("Content-Type", "application/x-sentry-envelope") - response = request.Await() - ' Only when the server never answered: its own failures hit every device at once and would exhaust the fallback's quota - if response.StatusCode() <= 0 - LogWarnNoTelemetry("Support server unreachable, sending events to the Sentry fallback:", response.ErrorMessage()) - request = HttpClient.Post(SENTRY_FALLBACK_ENVELOPE_URL, payload) - request.Header("Content-Type", "application/x-sentry-envelope") - request.QueryParam("sentry_key", SENTRY_FALLBACK_KEY) - response = request.Await() - end if - - if response.IsSuccess() - LogDebug("Sent events to", request.BuildUrl(), "Response:", response.Text()) - return 0 - end if - - retryAfter = ValidString(response.Headers()["Retry-After"]).ToInt() - if retryAfter = 0 - retryAfter = 60 - end if - - if response.StatusCode() = 429 - LogErrorNoTelemetry("Rate limited by", request.BuildUrl(), "Retry after", retryAfter, "seconds") - else - LogErrorNoTelemetry("Failed to send events to", request.BuildUrl(), response.ErrorMessage(), `\nRetry after`, retryAfter, "seconds") - end if - return retryAfter + return payload end function function GetFingerprint(message as dynamic) as dynamic @@ -165,7 +184,7 @@ namespace Sentry return text end if - authHeader = /Authorization: [^\r\n]*/i + authHeader = /Authorization: (?!)[^\r\n]*/i for each match in authHeader.MatchAll(text) text = text.Replace(match[0], "Authorization: ") end for diff --git a/playlet-lib/src/components/Telemetry/Telemetry.bs b/playlet-lib/src/components/Telemetry/Telemetry.bs index 3a1a56cf..0274e636 100644 --- a/playlet-lib/src/components/Telemetry/Telemetry.bs +++ b/playlet-lib/src/components/Telemetry/Telemetry.bs @@ -1,4 +1,4 @@ -#const SUPPORTS_BATCHING = false +#const SUPPORTS_BATCHING = true import "pkg:/components/Telemetry/LastExitCrash.bs" import "pkg:/components/Telemetry/Sentry.bs" @@ -72,7 +72,8 @@ function TelemetryLoop() as void "LogError": "error" } - messages = [] + receivedAt = CreateObject("roDateTime") + inbox = [] events = [] while true @@ -86,7 +87,8 @@ function TelemetryLoop() as void end if end if - messages.Push(msg) + receivedAt.Mark() + inbox.Push({ msg: msg, timestamp: receivedAt.ToISOString() }) #if SUPPORTS_BATCHING debounceTimer.Mark() @@ -97,8 +99,9 @@ function TelemetryLoop() as void LogDebug("No more messages") exit while end if - messages.Push(msg) - if messages.Count() >= debounceCountLimit + receivedAt.Mark() + inbox.Push({ msg: msg, timestamp: receivedAt.ToISOString() }) + if inbox.Count() >= debounceCountLimit LogDebug("Reached debounce count limit") exit while end if @@ -110,7 +113,8 @@ function TelemetryLoop() as void #end if events.Clear() - for each msg in messages + for each item in inbox + msg = item.msg msgType = type(msg) if msgType <> "roSGNodeEvent" continue for @@ -122,6 +126,7 @@ function TelemetryLoop() as void events.Push({ level: levelMap[field] message: msg.getData() + timestamp: item.timestamp release: m.release dist: m.dist tags: m.tags @@ -132,6 +137,7 @@ function TelemetryLoop() as void events.Push({ level: data.level message: data.message + timestamp: item.timestamp release: m.release dist: m.dist tags: m.tags @@ -141,7 +147,7 @@ function TelemetryLoop() as void }) end if end for - messages.Clear() + inbox.Clear() if events.Count() = 0 continue while diff --git a/playlet-lib/src/source/tests/RedactTelemetrySecrets.spec.bs b/playlet-lib/src/source/tests/RedactTelemetrySecrets.spec.bs index 425501bf..3c52aeb0 100644 --- a/playlet-lib/src/source/tests/RedactTelemetrySecrets.spec.bs +++ b/playlet-lib/src/source/tests/RedactTelemetrySecrets.spec.bs @@ -44,6 +44,15 @@ namespace Tests m.assertEqual(Sentry.Redact(42), 42) end function + @it("leaves an already-masked Authorization header alone inside a serialized envelope line") + function alreadyRedactedInEnvelopeLine() + line = FormatJson({ + extra: { "request.txt": "curl -H 'Authorization: '" + Chr(10) + " -H 'X-Other: 1'" } + level: "error" + }) + m.assertEqual(Sentry.Redact(line), line) + end function + end class end namespace