Telemetry fix
This commit is contained in:
1 parent
8c22152d8e
commit
8da62890f9
3 files changed
+130
-95
No files matched your search
@@ -2,42 +2,63 @@ import "pkg:/source/services/HttpClient.bs"
|
||||
|
||||
const SUPPORT_SERVER_ENVELOPE_URL = "https://playlet-v8556.ondigitalocean.app/envelope"
|
||||
' A fallback is needed to investigate https://github.com/iBicha/playlet/issues/906
|
||||
const SENTRY_FALLBACK_ENVELOPE_URL = "https://o4509923566288896.ingest.us.sentry.io/api/4512194184544256/envelope/"
|
||||
const SENTRY_FALLBACK_KEY = "a16c510299b23ccc63bdc84f751c9950"
|
||||
const SENTRY_FALLBACK_ENVELOPE_URL = "https://o4509923566288896.ingest.us.sentry.io/api/4512194184544256/envelope/?sentry_key=a16c510299b23ccc63bdc84f751c9950"
|
||||
|
||||
namespace Sentry
|
||||
|
||||
function SendEvents(events as object) as integer
|
||||
payload = BuildEnvelope(events, false)
|
||||
request = HttpClient.Post(SUPPORT_SERVER_ENVELOPE_URL).TextBody(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).TextBody(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
|
||||
class Delivery
|
||||
private events as object
|
||||
private payload as string
|
||||
private request as HttpClient.Request
|
||||
private pending as HttpClient.PendingResponse
|
||||
private sentToFallback as boolean = false
|
||||
|
||||
if response.IsSuccess()
|
||||
LogDebug("Sent events to", request.GetFullUrl(), "Response:", response.Text())
|
||||
return 0
|
||||
end if
|
||||
function new(events as object)
|
||||
m.events = events
|
||||
m.payload = BuildEnvelope(events, false)
|
||||
m.Send(SUPPORT_SERVER_ENVELOPE_URL, m.payload)
|
||||
end function
|
||||
|
||||
retryAfter = response.RetryAfterSeconds()
|
||||
if retryAfter = invalid or retryAfter = 0
|
||||
retryAfter = 60
|
||||
end if
|
||||
' invalid while it is being sent; then how long to hold events back, in seconds: 0 once delivered.
|
||||
function Poll() as dynamic
|
||||
response = m.pending.Poll()
|
||||
if response = invalid
|
||||
return invalid
|
||||
end if
|
||||
|
||||
if response.StatusCode() = 429
|
||||
LogErrorNoTelemetry("Rate limited by", request.GetFullUrl(), "Retry after", retryAfter, "seconds")
|
||||
else
|
||||
LogErrorNoTelemetry("Failed to send events to", request.GetFullUrl(), response.ErrorMessage(), `\nRetry after`, retryAfter, "seconds")
|
||||
end if
|
||||
return retryAfter
|
||||
end function
|
||||
' Only when the server never answered: its own failures hit every device at once and would exhaust the fallback's quota
|
||||
if not m.sentToFallback and response.StatusCode() <= 0
|
||||
errorMessage = response.ErrorMessage()
|
||||
LogWarnNoTelemetry("Support server unreachable, sending events to the Sentry fallback:", errorMessage)
|
||||
m.sentToFallback = true
|
||||
unreachableEvent = CreateSupportServerUnreachableEvent(m.events[0], m.payload, errorMessage)
|
||||
m.Send(SENTRY_FALLBACK_ENVELOPE_URL, BuildEnvelope([unreachableEvent], true))
|
||||
return invalid
|
||||
end if
|
||||
|
||||
if response.IsSuccess()
|
||||
LogDebug("Sent events to", m.request.GetFullUrl(), "Response:", response.Text())
|
||||
return 0
|
||||
end if
|
||||
|
||||
retryAfter = response.RetryAfterSeconds()
|
||||
if retryAfter = invalid or retryAfter = 0
|
||||
retryAfter = 60
|
||||
end if
|
||||
|
||||
if response.StatusCode() = 429
|
||||
LogErrorNoTelemetry("Rate limited by", m.request.GetFullUrl(), "Retry after", retryAfter, "seconds")
|
||||
else
|
||||
LogErrorNoTelemetry("Failed to send events to", m.request.GetFullUrl(), response.ErrorMessage(), `\nRetry after`, retryAfter, "seconds")
|
||||
end if
|
||||
return retryAfter
|
||||
end function
|
||||
|
||||
private function Send(url as string, envelope as string) as void
|
||||
m.request = HttpClient.Post(url).TextBody(envelope).Header("Content-Type", "application/x-sentry-envelope")
|
||||
m.pending = m.request.Send()
|
||||
end function
|
||||
end class
|
||||
|
||||
function CreateSupportServerUnreachableEvent(originalEvent as object, payload as string, errorMessage as string) as object
|
||||
return {
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
#const SUPPORTS_BATCHING = true
|
||||
|
||||
import "pkg:/components/Telemetry/LastExitCrash.bs"
|
||||
import "pkg:/components/Telemetry/Sentry.bs"
|
||||
import "pkg:/source/utils/Logging.bs"
|
||||
import "pkg:/source/utils/Types.bs"
|
||||
|
||||
const DELIVERY_POLL_MS = 500
|
||||
const MAX_EVENTS_PER_DELIVERY = 20
|
||||
' A batch is sent once it holds MAX_EVENTS_PER_DELIVERY events, once no event came for BATCH_IDLE_MS, or BATCH_MAX_AGE_MS after the inbox was last empty.
|
||||
const BATCH_IDLE_MS = 2000
|
||||
const BATCH_MAX_AGE_MS = 5000
|
||||
|
||||
function Init() as void
|
||||
if m.global.telemetry <> invalid
|
||||
return
|
||||
@@ -53,18 +57,9 @@ function TelemetryLoop() as void
|
||||
m.top.observeField("LogError", msgPort)
|
||||
m.top.observeField("LogEvent", msgPort)
|
||||
|
||||
#if SUPPORTS_BATCHING
|
||||
debouceMs = 2000
|
||||
debounceCountLimit = 20
|
||||
debounceTimeLimit = 5000
|
||||
debounceTimer = CreateObject("roTimeSpan")
|
||||
#end if
|
||||
|
||||
retryAfter = 0
|
||||
retryAfterTimer = CreateObject("roTimeSpan")
|
||||
retryAfter = Sentry.SendEvents(CreateLaunchEvents())
|
||||
if retryAfter > 0
|
||||
retryAfterTimer.Mark()
|
||||
end if
|
||||
delivery = new Sentry.Delivery(CreateLaunchEvents())
|
||||
|
||||
levelMap = {
|
||||
"LogInfo": "info"
|
||||
@@ -73,51 +68,66 @@ function TelemetryLoop() as void
|
||||
}
|
||||
|
||||
receivedAt = CreateObject("roDateTime")
|
||||
lastEventTimer = CreateObject("roTimeSpan")
|
||||
batchTimer = CreateObject("roTimeSpan")
|
||||
inbox = []
|
||||
events = []
|
||||
|
||||
while true
|
||||
msg = wait(0, msgPort)
|
||||
waitMs = 0
|
||||
if delivery <> invalid
|
||||
waitMs = DELIVERY_POLL_MS
|
||||
else if inbox.Count() > 0
|
||||
waitMs = BATCH_IDLE_MS - lastEventTimer.TotalMilliseconds()
|
||||
batchAgeLeftMs = BATCH_MAX_AGE_MS - batchTimer.TotalMilliseconds()
|
||||
if batchAgeLeftMs < waitMs
|
||||
waitMs = batchAgeLeftMs
|
||||
end if
|
||||
' wait(0) would block until the next event.
|
||||
if waitMs < 1
|
||||
waitMs = 1
|
||||
end if
|
||||
end if
|
||||
msg = wait(waitMs, msgPort)
|
||||
|
||||
if retryAfter > 0
|
||||
if retryAfterTimer.TotalMilliseconds() < retryAfter * 1000
|
||||
continue while
|
||||
else
|
||||
retryAfter = 0
|
||||
if delivery <> invalid
|
||||
deliveryRetryAfter = delivery.Poll()
|
||||
if deliveryRetryAfter <> invalid
|
||||
delivery = invalid
|
||||
retryAfter = deliveryRetryAfter
|
||||
if retryAfter > 0
|
||||
retryAfterTimer.Mark()
|
||||
inbox.Clear()
|
||||
end if
|
||||
end if
|
||||
end if
|
||||
|
||||
receivedAt.Mark()
|
||||
inbox.Push({ msg: msg, timestamp: receivedAt.ToISOString() })
|
||||
if retryAfter > 0 and retryAfterTimer.TotalMilliseconds() < retryAfter * 1000
|
||||
continue while
|
||||
end if
|
||||
|
||||
#if SUPPORTS_BATCHING
|
||||
debounceTimer.Mark()
|
||||
while true
|
||||
LogDebug("Waiting for more messages...")
|
||||
msg = wait(debouceMs, msgPort)
|
||||
if msg = invalid
|
||||
LogDebug("No more messages")
|
||||
exit while
|
||||
end if
|
||||
receivedAt.Mark()
|
||||
inbox.Push({ msg: msg, timestamp: receivedAt.ToISOString() })
|
||||
if inbox.Count() >= debounceCountLimit
|
||||
LogDebug("Reached debounce count limit")
|
||||
exit while
|
||||
end if
|
||||
if debounceTimer.TotalMilliseconds() >= debounceTimeLimit
|
||||
LogDebug("Reached debounce time limit")
|
||||
exit while
|
||||
end if
|
||||
end while
|
||||
#end if
|
||||
if msg <> invalid
|
||||
receivedAt.Mark()
|
||||
inbox.Push({ msg: msg, timestamp: receivedAt.ToISOString() })
|
||||
lastEventTimer.Mark()
|
||||
if inbox.Count() = 1
|
||||
batchTimer.Mark()
|
||||
end if
|
||||
end if
|
||||
|
||||
events.Clear()
|
||||
for each item in inbox
|
||||
if delivery <> invalid or inbox.Count() = 0
|
||||
continue while
|
||||
end if
|
||||
if inbox.Count() < MAX_EVENTS_PER_DELIVERY and lastEventTimer.TotalMilliseconds() < BATCH_IDLE_MS and batchTimer.TotalMilliseconds() < BATCH_MAX_AGE_MS
|
||||
continue while
|
||||
end if
|
||||
|
||||
events = []
|
||||
while events.Count() < MAX_EVENTS_PER_DELIVERY and inbox.Count() > 0
|
||||
item = inbox.Shift()
|
||||
msg = item.msg
|
||||
msgType = type(msg)
|
||||
if msgType <> "roSGNodeEvent"
|
||||
continue for
|
||||
continue while
|
||||
end if
|
||||
|
||||
field = msg.getField()
|
||||
@@ -146,17 +156,13 @@ function TelemetryLoop() as void
|
||||
attachments: data.attachments
|
||||
})
|
||||
end if
|
||||
end for
|
||||
inbox.Clear()
|
||||
end while
|
||||
|
||||
if events.Count() = 0
|
||||
continue while
|
||||
end if
|
||||
|
||||
retryAfter = Sentry.SendEvents(events)
|
||||
if retryAfter > 0
|
||||
retryAfterTimer.Mark()
|
||||
end if
|
||||
delivery = new Sentry.Delivery(events)
|
||||
end while
|
||||
end function
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ namespace Tests
|
||||
' Stands in for HttpClient.CreateUrlTransfer, sending to the urls Serve set in place of the support server's and the fallback's.
|
||||
function SentryUrlTransfer(url as string, headers as object, messagePort as object) as object
|
||||
urls = GetGlobalAA().sentryEnvelopeUrls
|
||||
url = url.Replace(SUPPORT_SERVER_ENVELOPE_URL, urls.supportServer).Replace(SENTRY_FALLBACK_ENVELOPE_URL, urls.fallback)
|
||||
url = url.Replace(SUPPORT_SERVER_ENVELOPE_URL, urls.supportServer).Replace(SENTRY_FALLBACK_ENVELOPE_URL.Split("?")[0], urls.fallback)
|
||||
urlTransfer = CreateObject("roUrlTransfer")
|
||||
urlTransfer.RetainBodyOnError(true)
|
||||
urlTransfer.SetUrl(url)
|
||||
@@ -20,7 +20,7 @@ namespace Tests
|
||||
end function
|
||||
|
||||
@suite
|
||||
class SentrySendEventsTests extends Rooibos.BaseTestSuite
|
||||
class SentryDeliveryTests extends Rooibos.BaseTestSuite
|
||||
|
||||
protected override function setup()
|
||||
m.server = Tests.StartHttpTestServer()
|
||||
@@ -34,11 +34,6 @@ namespace Tests
|
||||
m.server = invalid
|
||||
end function
|
||||
|
||||
protected override function beforeEach()
|
||||
Tests.SetUpLoggingGlobals()
|
||||
m.stubCall(HttpClient.CreateUrlTransfer, Tests.SentryUrlTransfer)
|
||||
end function
|
||||
|
||||
protected override function afterEach()
|
||||
GetGlobalAA().Delete("sentryEnvelopeUrls")
|
||||
end function
|
||||
@@ -61,7 +56,7 @@ namespace Tests
|
||||
GetGlobalAA().sentryEnvelopeUrls = urls
|
||||
end function
|
||||
|
||||
' SendEvents' result for one event, and the lines it logged.
|
||||
' The delivery's result for one event, and the lines it logged.
|
||||
function Send(message as string) as object
|
||||
result = {
|
||||
events: [{
|
||||
@@ -74,7 +69,12 @@ namespace Tests
|
||||
retryAfter: invalid
|
||||
}
|
||||
result.lines = Tests.LogLines(function(result as object) as void
|
||||
result.retryAfter = Sentry.SendEvents(result.events)
|
||||
delivery = new Sentry.Delivery(result.events)
|
||||
result.retryAfter = delivery.Poll()
|
||||
while result.retryAfter = invalid
|
||||
sleep(10)
|
||||
result.retryAfter = delivery.Poll()
|
||||
end while
|
||||
end function, result)
|
||||
return result
|
||||
end function
|
||||
@@ -84,7 +84,7 @@ namespace Tests
|
||||
end function
|
||||
|
||||
function FallbackHits() as integer
|
||||
return Tests.HttpTestServerHits(m.server, `${m.fallbackPath}?sentry_key=${SENTRY_FALLBACK_KEY}`)
|
||||
return Tests.HttpTestServerHits(m.server, `${m.fallbackPath}?sentry_key=a16c510299b23ccc63bdc84f751c9950`)
|
||||
end function
|
||||
|
||||
function Messages(lines as string[], level as string) as string[]
|
||||
@@ -112,7 +112,15 @@ namespace Tests
|
||||
return request.body.Split(`\n`)
|
||||
end function
|
||||
|
||||
@describe("Sentry.SendEvents")
|
||||
@describe("Sentry.Delivery")
|
||||
|
||||
' Annotated rather than an override: Rooibos instruments a global function only for a stubCall it finds in a test
|
||||
' or an annotated hook, and an uninstrumented HttpClient.CreateUrlTransfer sends to the real support server.
|
||||
@beforeEach
|
||||
function StubEnvelopeUrls()
|
||||
Tests.SetUpLoggingGlobals()
|
||||
m.stubCall(HttpClient.CreateUrlTransfer, Tests.SentryUrlTransfer)
|
||||
end function
|
||||
|
||||
@it("sends the events to the support server, holding nothing back")
|
||||
function _()
|
||||
@@ -186,7 +194,7 @@ namespace Tests
|
||||
result = m.Send("rate-limited event")
|
||||
m.assertEqual(result.retryAfter, 45)
|
||||
m.assertEqual(m.FallbackHits(), 1)
|
||||
m.assertEqual(m.ErrorLogged(result.lines), `Rate limited by ${SENTRY_FALLBACK_ENVELOPE_URL}?sentry_key=${SENTRY_FALLBACK_KEY} Retry after 45 seconds`)
|
||||
m.assertEqual(m.ErrorLogged(result.lines), `Rate limited by ${SENTRY_FALLBACK_ENVELOPE_URL} Retry after 45 seconds`)
|
||||
end function
|
||||
|
||||
@it("holds the events back for 60 seconds when the fallback fails too")
|
||||
@@ -200,7 +208,7 @@ namespace Tests
|
||||
result = m.Send("failed event")
|
||||
m.assertEqual(result.retryAfter, 60, testCase.id)
|
||||
errorLogged = m.ErrorLogged(result.lines)
|
||||
m.assertTrue(errorLogged.StartsWith(`Failed to send events to ${SENTRY_FALLBACK_ENVELOPE_URL}?sentry_key=${SENTRY_FALLBACK_KEY} `), errorLogged)
|
||||
m.assertTrue(errorLogged.StartsWith(`Failed to send events to ${SENTRY_FALLBACK_ENVELOPE_URL} `), errorLogged)
|
||||
m.assertTrue(errorLogged.EndsWith(` \nRetry after 60 seconds`), errorLogged)
|
||||
end for
|
||||
end function
|
||||
|
||||
Reference in new issue
Block a user