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