Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/quiet-otters-retry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": patch
---

Honor HTTP-date `Retry-After` values when retrying OTLP exports.
24 changes: 19 additions & 5 deletions packages/effect/src/unstable/observability/OtlpExporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,24 @@ import * as HttpClientError from "../../unstable/http/HttpClientError.ts"
import * as HttpClientRequest from "../../unstable/http/HttpClientRequest.ts"
import type { HttpBody } from "../http/HttpBody.ts"

const retryAfterDelay = (value: string | undefined): Effect.Effect<Duration.Duration> => {
const seconds = Option.fromUndefinedOr(value).pipe(Option.flatMap(Num.parse))
if (Option.isSome(seconds)) {
return Effect.succeed(Duration.seconds(seconds.value))
}
if (value === undefined) {
return Effect.succeed(Duration.seconds(5))
}
const timestamp = Date.parse(value)
if (Number.isNaN(timestamp)) {
return Effect.succeed(Duration.seconds(5))
}
return Effect.map(
Clock,
(clock) => Duration.millis(Math.max(timestamp - clock.currentTimeMillisUnsafe(), 1))
)
}

const policy = Schedule.forever.pipe(
Schedule.passthrough,
Schedule.addDelay(({ output: error }) => {
Expand All @@ -34,11 +52,7 @@ const policy = Schedule.forever.pipe(
&& error.reason._tag === "StatusCodeError"
&& error.reason.response.status === 429
) {
const retryAfter = Option.fromUndefinedOr(error.reason.response.headers["retry-after"]).pipe(
Option.flatMap(Num.parse),
Option.getOrElse(() => 5)
)
return Effect.succeed(Duration.seconds(retryAfter))
return retryAfterDelay(error.reason.response.headers["retry-after"])
}
return Effect.succeed(Duration.seconds(1))
})
Expand Down
21 changes: 21 additions & 0 deletions packages/effect/test/unstable/observability/OtlpExporter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,27 @@ describe("OtlpExporter", () => {
})
))

it.effect("retries status 429 with HTTP-date retry-after delay", () =>
Effect.scoped(
Effect.gen(function*() {
const { attempts, httpClient } = yield* makeHttpClient("Thu, 01 Jan 1970 00:01:00 GMT")
const exporter = yield* makeExporter(httpClient)

exporter.push({ value: 1 })
yield* yieldNowN(3)

assert.strictEqual(yield* Ref.get(attempts), 1)

yield* TestClock.adjust("5 seconds")
yield* yieldNowN(2)
assert.strictEqual(yield* Ref.get(attempts), 1)

yield* TestClock.adjust("55 seconds")
yield* yieldNowN(2)
assert.strictEqual(yield* Ref.get(attempts), 2)
})
))

it.effect("uses fallback retry-after delay when header is non-numeric", () =>
Effect.scoped(
Effect.gen(function*() {
Expand Down
Loading