effect-vercel 0.6.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +53 -0
- package/dist/Otlp.d.ts +25 -0
- package/dist/Otlp.js +59 -0
- package/dist/Otlp.js.map +1 -0
- package/dist/PgClient.js +1 -9
- package/dist/PgClient.js.map +1 -1
- package/dist/requestContext-CEUKYTim.js +14 -0
- package/dist/requestContext-CEUKYTim.js.map +1 -0
- package/package.json +10 -7
- package/src/Otlp.ts +80 -0
- package/src/PgClient.ts +1 -19
- package/src/internal/requestContext.ts +18 -0
package/README.md
CHANGED
|
@@ -13,6 +13,7 @@ import * as AiGateway from "effect-vercel/AiGateway"
|
|
|
13
13
|
| `effect-vercel/AiGatewayCredentials` | How AI Gateway requests authenticate |
|
|
14
14
|
| `effect-vercel/Functions` | Bindings for `@vercel/functions`: `waitUntil` |
|
|
15
15
|
| `effect-vercel/Oidc` | The OIDC token Vercel issues to a deployment |
|
|
16
|
+
| `effect-vercel/Otlp` | Keeps a Function running until its OTLP telemetry is exported |
|
|
16
17
|
| `effect-vercel/PgClient` | `@effect/sql-pg` pool that doesn't leave connections open on frozen instances |
|
|
17
18
|
| `effect-vercel/RuntimeCache` | [Runtime Cache](https://vercel.com/docs/runtime-cache) for Effect `Persistence` |
|
|
18
19
|
|
|
@@ -146,6 +147,58 @@ const handler = Effect.gen(function* () {
|
|
|
146
147
|
- Vercel stops the work when the Function reaches its maximum duration.
|
|
147
148
|
- Off Vercel, `waitUntil` does nothing and the fiber runs in the background.
|
|
148
149
|
|
|
150
|
+
## OTLP
|
|
151
|
+
|
|
152
|
+
Effect's OTLP exporters buffer spans, logs and metrics and export them on an interval. Vercel can
|
|
153
|
+
suspend the instance once the response is sent, so the interval may never fire and the telemetry
|
|
154
|
+
is lost. `effect-vercel/Otlp` flushes the exporters when each request ends and keeps the Function
|
|
155
|
+
running until the flush is done.
|
|
156
|
+
|
|
157
|
+
For an `HttpRouter` app, add the `flushAfterRequest` middleware next to your routes. Provide the
|
|
158
|
+
OTLP layer with `Layer.provideMerge`, so its tracer and logger reach each request:
|
|
159
|
+
|
|
160
|
+
```ts
|
|
161
|
+
import * as VercelOtlp from "effect-vercel/Otlp"
|
|
162
|
+
import { Layer } from "effect"
|
|
163
|
+
import { FetchHttpClient, HttpRouter } from "effect/http"
|
|
164
|
+
import { Otlp } from "effect/observability"
|
|
165
|
+
|
|
166
|
+
const { handler } = HttpRouter.toWebHandler(
|
|
167
|
+
Layer.merge(Routes, VercelOtlp.flushAfterRequest).pipe(
|
|
168
|
+
Layer.provideMerge(Otlp.layerJson({ baseUrl, resource })),
|
|
169
|
+
Layer.provide(FetchHttpClient.layer),
|
|
170
|
+
),
|
|
171
|
+
)
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
For another entry point, such as a queue consumer, run `VercelOtlp.flushOnClose` at the start of
|
|
175
|
+
the request in a scope that closes when the request ends:
|
|
176
|
+
|
|
177
|
+
```ts
|
|
178
|
+
// `services` holds the app's services, including the OTLP layer's Flusher.
|
|
179
|
+
const runSync = Effect.runSyncWith(services)
|
|
180
|
+
const scope = runSync(Scope.make())
|
|
181
|
+
runSync(VercelOtlp.flushOnClose.pipe(Scope.provide(scope)))
|
|
182
|
+
|
|
183
|
+
// When the request ends:
|
|
184
|
+
runSync(Scope.close(scope, Exit.void))
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
- The flush starts when the request's scope closes. For an `HttpRouter` app, that is after the
|
|
188
|
+
response body is sent, so telemetry recorded while a body streams is included.
|
|
189
|
+
- Every request flushes, sampled or not, since logs and metrics are exported either way. Spans and
|
|
190
|
+
logs are sent only when some are buffered. Metrics send a snapshot on every flush.
|
|
191
|
+
- The Function waits for the flush for at most 2 seconds.
|
|
192
|
+
- Both need the `OtlpExporter.Flusher` that the OTLP layers provide, so leaving them out fails to
|
|
193
|
+
compile.
|
|
194
|
+
- They register the wait with Vercel when the request starts, while its context is current. Run
|
|
195
|
+
`flushOnClose` first in its scope, so finalizers added later, such as ending a span, run before
|
|
196
|
+
the flush.
|
|
197
|
+
- `flush` can't wait for an export the interval already started, and skips exporters disabled
|
|
198
|
+
after a failure.
|
|
199
|
+
- Off Vercel, or outside a request, they do nothing and the exporters' interval sends the
|
|
200
|
+
telemetry.
|
|
201
|
+
|
|
149
202
|
## Postgres
|
|
150
203
|
|
|
151
204
|
`PgClient.layer` is `PgClient.layer` from `@effect/sql-pg` that keeps the Function instance up
|
package/dist/Otlp.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Effect, Layer, Scope } from "effect";
|
|
2
|
+
import { HttpRouter } from "effect/http";
|
|
3
|
+
import { OtlpExporter } from "effect/observability";
|
|
4
|
+
//#region src/Otlp.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* When the current scope closes, flushes the OTLP exporters and keeps the
|
|
7
|
+
* Function running until the flush ends, for at most 2 seconds.
|
|
8
|
+
*
|
|
9
|
+
* It registers the wait with Vercel right away, so run it while the request
|
|
10
|
+
* is current. Run it first in the scope: finalizers added later, such as
|
|
11
|
+
* ending a span, run before the flush. Off Vercel, or outside a request, it
|
|
12
|
+
* does nothing, and the exporters' interval sends the telemetry.
|
|
13
|
+
*
|
|
14
|
+
* Use `flushAfterRequest` for an `HttpRouter` app. Use this one when the
|
|
15
|
+
* request has another entry point, with a scope that closes when it ends.
|
|
16
|
+
*/
|
|
17
|
+
export declare const flushOnClose: Effect.Effect<void, never, OtlpExporter.Flusher | Scope.Scope>;
|
|
18
|
+
/**
|
|
19
|
+
* Global `HttpRouter` middleware that runs `flushOnClose` in each request's
|
|
20
|
+
* scope. That scope closes after the response body is sent, so telemetry
|
|
21
|
+
* recorded while a body streams is in the flush.
|
|
22
|
+
*/
|
|
23
|
+
export declare const flushAfterRequest: Layer.Layer<never, never, HttpRouter.HttpRouter | OtlpExporter.Flusher>;
|
|
24
|
+
//#endregion
|
|
25
|
+
//# sourceMappingURL=Otlp.d.ts.map
|
package/dist/Otlp.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { t as currentRequest } from "./requestContext-CEUKYTim.js";
|
|
2
|
+
import { Effect } from "effect";
|
|
3
|
+
import { HttpRouter } from "effect/http";
|
|
4
|
+
import { OtlpExporter } from "effect/observability";
|
|
5
|
+
//#region src/Otlp.ts
|
|
6
|
+
/**
|
|
7
|
+
* Keeps a Vercel Function running until its OTLP telemetry is exported.
|
|
8
|
+
*
|
|
9
|
+
* Effect's OTLP exporters buffer spans, logs and metrics and export them on
|
|
10
|
+
* an interval. Vercel can suspend the instance once the response is sent, so
|
|
11
|
+
* the interval may never fire and the telemetry is lost. These flush the
|
|
12
|
+
* exporters when a request ends and keep the Function running until the
|
|
13
|
+
* flush is done.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* import * as VercelOtlp from "effect-vercel/Otlp"
|
|
18
|
+
* import { Layer } from "effect"
|
|
19
|
+
* import { FetchHttpClient, HttpRouter } from "effect/http"
|
|
20
|
+
* import { Otlp } from "effect/observability"
|
|
21
|
+
*
|
|
22
|
+
* const { handler } = HttpRouter.toWebHandler(
|
|
23
|
+
* Layer.merge(Routes, VercelOtlp.flushAfterRequest).pipe(
|
|
24
|
+
* Layer.provideMerge(Otlp.layerJson({ baseUrl, resource })),
|
|
25
|
+
* Layer.provide(FetchHttpClient.layer),
|
|
26
|
+
* ),
|
|
27
|
+
* )
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
const flushOnCloseWith = (flusher) => Effect.suspend(() => {
|
|
31
|
+
const request = currentRequest();
|
|
32
|
+
if (request?.waitUntil === void 0) return Effect.void;
|
|
33
|
+
const { promise, resolve } = Promise.withResolvers();
|
|
34
|
+
request.waitUntil(promise);
|
|
35
|
+
return Effect.addFinalizer(() => flusher.flush.pipe(Effect.timeoutOption("2 seconds"), Effect.ensuring(Effect.sync(resolve)), Effect.forkDetach));
|
|
36
|
+
}).pipe(Effect.uninterruptible);
|
|
37
|
+
/**
|
|
38
|
+
* When the current scope closes, flushes the OTLP exporters and keeps the
|
|
39
|
+
* Function running until the flush ends, for at most 2 seconds.
|
|
40
|
+
*
|
|
41
|
+
* It registers the wait with Vercel right away, so run it while the request
|
|
42
|
+
* is current. Run it first in the scope: finalizers added later, such as
|
|
43
|
+
* ending a span, run before the flush. Off Vercel, or outside a request, it
|
|
44
|
+
* does nothing, and the exporters' interval sends the telemetry.
|
|
45
|
+
*
|
|
46
|
+
* Use `flushAfterRequest` for an `HttpRouter` app. Use this one when the
|
|
47
|
+
* request has another entry point, with a scope that closes when it ends.
|
|
48
|
+
*/
|
|
49
|
+
const flushOnClose = Effect.flatMap(OtlpExporter.Flusher, flushOnCloseWith);
|
|
50
|
+
/**
|
|
51
|
+
* Global `HttpRouter` middleware that runs `flushOnClose` in each request's
|
|
52
|
+
* scope. That scope closes after the response body is sent, so telemetry
|
|
53
|
+
* recorded while a body streams is in the flush.
|
|
54
|
+
*/
|
|
55
|
+
const flushAfterRequest = HttpRouter.middleware(Effect.map(OtlpExporter.Flusher, (flusher) => (httpEffect) => Effect.andThen(flushOnCloseWith(flusher), httpEffect)), { global: true });
|
|
56
|
+
//#endregion
|
|
57
|
+
export { flushAfterRequest, flushOnClose };
|
|
58
|
+
|
|
59
|
+
//# sourceMappingURL=Otlp.js.map
|
package/dist/Otlp.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Otlp.js","names":[],"sources":["../src/Otlp.ts"],"sourcesContent":["/**\n * Keeps a Vercel Function running until its OTLP telemetry is exported.\n *\n * Effect's OTLP exporters buffer spans, logs and metrics and export them on\n * an interval. Vercel can suspend the instance once the response is sent, so\n * the interval may never fire and the telemetry is lost. These flush the\n * exporters when a request ends and keep the Function running until the\n * flush is done.\n *\n * @example\n * ```ts\n * import * as VercelOtlp from \"effect-vercel/Otlp\"\n * import { Layer } from \"effect\"\n * import { FetchHttpClient, HttpRouter } from \"effect/http\"\n * import { Otlp } from \"effect/observability\"\n *\n * const { handler } = HttpRouter.toWebHandler(\n * Layer.merge(Routes, VercelOtlp.flushAfterRequest).pipe(\n * Layer.provideMerge(Otlp.layerJson({ baseUrl, resource })),\n * Layer.provide(FetchHttpClient.layer),\n * ),\n * )\n * ```\n */\n\nimport { Effect, type Layer, type Scope } from \"effect\"\nimport { HttpRouter } from \"effect/http\"\nimport { OtlpExporter } from \"effect/observability\"\nimport { currentRequest } from \"./internal/requestContext.js\"\n\nconst flushOnCloseWith = (flusher: OtlpExporter.Flusher[\"Service\"]) =>\n Effect.suspend(() => {\n const request = currentRequest()\n if (request?.waitUntil === undefined) return Effect.void\n const { promise, resolve } = Promise.withResolvers<void>()\n request.waitUntil(promise)\n // Forked, so a streamed body ends without waiting for the export.\n return Effect.addFinalizer(() =>\n flusher.flush.pipe(\n Effect.timeoutOption(\"2 seconds\"),\n Effect.ensuring(Effect.sync(resolve)),\n Effect.forkDetach,\n ),\n )\n }).pipe(\n // An interrupt before the finalizer is added would leave the wait open.\n Effect.uninterruptible,\n )\n\n/**\n * When the current scope closes, flushes the OTLP exporters and keeps the\n * Function running until the flush ends, for at most 2 seconds.\n *\n * It registers the wait with Vercel right away, so run it while the request\n * is current. Run it first in the scope: finalizers added later, such as\n * ending a span, run before the flush. Off Vercel, or outside a request, it\n * does nothing, and the exporters' interval sends the telemetry.\n *\n * Use `flushAfterRequest` for an `HttpRouter` app. Use this one when the\n * request has another entry point, with a scope that closes when it ends.\n */\nexport const flushOnClose: Effect.Effect<void, never, OtlpExporter.Flusher | Scope.Scope> =\n Effect.flatMap(OtlpExporter.Flusher, flushOnCloseWith)\n\n/**\n * Global `HttpRouter` middleware that runs `flushOnClose` in each request's\n * scope. That scope closes after the response body is sent, so telemetry\n * recorded while a body streams is in the flush.\n */\nexport const flushAfterRequest: Layer.Layer<\n never,\n never,\n HttpRouter.HttpRouter | OtlpExporter.Flusher\n> = HttpRouter.middleware(\n Effect.map(\n OtlpExporter.Flusher,\n (flusher) => (httpEffect) => Effect.andThen(flushOnCloseWith(flusher), httpEffect),\n ),\n { global: true },\n)\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8BA,MAAM,oBAAoB,YACxB,OAAO,cAAc;CACnB,MAAM,UAAU,eAAe;CAC/B,IAAI,SAAS,cAAc,KAAA,GAAW,OAAO,OAAO;CACpD,MAAM,EAAE,SAAS,YAAY,QAAQ,cAAoB;CACzD,QAAQ,UAAU,OAAO;CAEzB,OAAO,OAAO,mBACZ,QAAQ,MAAM,KACZ,OAAO,cAAc,WAAW,GAChC,OAAO,SAAS,OAAO,KAAK,OAAO,CAAC,GACpC,OAAO,UACT,CACF;AACF,CAAC,CAAC,CAAC,KAED,OAAO,eACT;;;;;;;;;;;;;AAcF,MAAa,eACX,OAAO,QAAQ,aAAa,SAAS,gBAAgB;;;;;;AAOvD,MAAa,oBAIT,WAAW,WACb,OAAO,IACL,aAAa,UACZ,aAAa,eAAe,OAAO,QAAQ,iBAAiB,OAAO,GAAG,UAAU,CACnF,GACA,EAAE,QAAQ,KAAK,CACjB"}
|
package/dist/PgClient.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { t as currentRequest } from "./requestContext-CEUKYTim.js";
|
|
1
2
|
import { Duration, Effect, Layer, Redacted } from "effect";
|
|
2
3
|
import * as Net from "node:net";
|
|
3
4
|
import { PgClient } from "@effect/sql-pg";
|
|
@@ -29,15 +30,6 @@ import { constVoid } from "effect/Function";
|
|
|
29
30
|
* ```
|
|
30
31
|
*/
|
|
31
32
|
/**
|
|
32
|
-
* Vercel exposes the current request's context on a well-known global symbol.
|
|
33
|
-
* `@vercel/functions` reads `waitUntil` and the deadline from there. Off
|
|
34
|
-
* Vercel, or outside a request, there is no `waitUntil`.
|
|
35
|
-
*/
|
|
36
|
-
const REQUEST_CONTEXT = Symbol.for("@vercel/request-context");
|
|
37
|
-
const currentRequest = () => {
|
|
38
|
-
return globalThis[REQUEST_CONTEXT]?.get?.();
|
|
39
|
-
};
|
|
40
|
-
/**
|
|
41
33
|
* Keeps the instance up while a tracked socket is open. One `waitUntil`
|
|
42
34
|
* promise holds it, and `follow` moves it to the request running now, as
|
|
43
35
|
* `attachDatabasePool` does, so a busy instance never holds a finished
|
package/dist/PgClient.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PgClient.js","names":[
|
|
1
|
+
{"version":3,"file":"PgClient.js","names":[],"sources":["../src/PgClient.ts"],"sourcesContent":["/**\n * `@effect/sql-pg` for Vercel Functions: a pool that keeps the instance up\n * while it has a connection open.\n *\n * Vercel freezes an instance once no request or `waitUntil` promise is left.\n * A frozen instance keeps its sockets, so a pooler like PgBouncer keeps\n * counting them as clients until the instance wakes up or dies. With many\n * instances, as during a deploy, they fill up `max_client_conn`.\n * `attachDatabasePool` from `@vercel/functions` waits out the idle timeout\n * after each release, but it only supports node-postgres-like pools, and the\n * Effect pool closes idle connections up to twice its `idleTimeout` after\n * their last use. So this layer counts the pool's sockets and waits until\n * the last one closes.\n *\n * @example\n * ```ts\n * import * as PgClient from \"effect-vercel/PgClient\"\n * import { Redacted } from \"effect\"\n *\n * const SqlLive = PgClient.layer({\n * url: Redacted.make(process.env.DATABASE_URL!),\n * idleTimeout: \"5 seconds\",\n * })\n * ```\n */\n\nimport * as Net from \"node:net\"\nimport { PgClient } from \"@effect/sql-pg\"\nimport { Duration, Effect, Layer, Redacted } from \"effect\"\nimport { constVoid } from \"effect/Function\"\nimport { currentRequest } from \"./internal/requestContext.js\"\n\n/**\n * Keeps the instance up while a tracked socket is open. One `waitUntil`\n * promise holds it, and `follow` moves it to the request running now, as\n * `attachDatabasePool` does, so a busy instance never holds a finished\n * request open. The wait also ends a second before the request's deadline.\n * Outside a request, `follow` keeps the wait where it is.\n */\nconst makeKeepAlive = () => {\n let open = 0\n let release = constVoid\n\n const follow = () => {\n const request = currentRequest()\n if (open === 0 || request?.waitUntil === undefined) return\n release()\n const cutoff = Date.parse(request.deadline ?? \"\") - 1_000\n request.waitUntil(\n new Promise<void>((resolve) => {\n const timer = Number.isNaN(cutoff) ? undefined : setTimeout(resolve, cutoff - Date.now())\n release = () => {\n clearTimeout(timer)\n resolve()\n }\n }),\n )\n }\n\n const track = (socket: Net.Socket) => {\n open++\n follow()\n socket.once(\"close\", () => {\n if (--open === 0) release()\n })\n return socket\n }\n\n return { follow, track }\n}\n\n/** `PgClient.PgPoolConfig` without `stream`, which this module sets. */\nexport type Config<R = never> = Omit<PgClient.PgPoolConfig<R>, \"stream\">\n\n/**\n * Opens the TCP or Unix socket `@effect/sql-pg` would open for `config`.\n * It still runs TLS and the startup on it.\n */\nconst connect = (config: Config<unknown>): Net.Socket => {\n const url = config.url && new URL(Redacted.value(config.url))\n const param = (name: string) => url?.searchParams.getAll(name).at(-1)\n const urlHost = url?.hostname && decodeURIComponent(url.hostname.replace(/^\\[(.*)\\]$/, \"$1\"))\n const host = config.host ?? param(\"host\") ?? (urlHost || \"localhost\")\n const port = config.port ?? Number(param(\"port\") ?? (url?.port || 5432))\n const path = config.path ?? (host.startsWith(\"/\") ? `${host}/.s.PGSQL.${port}` : undefined)\n if (path !== undefined) return Net.connect({ path })\n return Net.connect({\n host,\n port,\n noDelay: true,\n ...(config.keepAlive !== undefined && {\n keepAlive: true,\n keepAliveInitialDelay: Duration.toMillis(Duration.fromInputUnsafe(config.keepAlive)),\n }),\n })\n}\n\n/**\n * `PgClient.layer` from `@effect/sql-pg` that keeps the Function instance up\n * until the pool has closed every connection, or the request's deadline\n * comes. Off Vercel it behaves like the plain layer.\n *\n * It opens each socket itself, through the `stream` option, and moves the\n * wait to the request that takes each connection, through `onAcquire`. An\n * `onAcquire` in `config` still runs, after that.\n */\nexport const layer = <R = never>(config: Config<R>) =>\n Layer.suspend(() => {\n const keepAlive = makeKeepAlive()\n const follow = Effect.sync(keepAlive.follow)\n return PgClient.layer({\n ...config,\n stream: () => keepAlive.track(connect(config)),\n onAcquire: config.onAcquire ? Effect.andThen(follow, config.onAcquire) : follow,\n })\n })\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuCA,MAAM,sBAAsB;CAC1B,IAAI,OAAO;CACX,IAAI,UAAU;CAEd,MAAM,eAAe;EACnB,MAAM,UAAU,eAAe;EAC/B,IAAI,SAAS,KAAK,SAAS,cAAc,KAAA,GAAW;EACpD,QAAQ;EACR,MAAM,SAAS,KAAK,MAAM,QAAQ,YAAY,EAAE,IAAI;EACpD,QAAQ,UACN,IAAI,SAAe,YAAY;GAC7B,MAAM,QAAQ,OAAO,MAAM,MAAM,IAAI,KAAA,IAAY,WAAW,SAAS,SAAS,KAAK,IAAI,CAAC;GACxF,gBAAgB;IACd,aAAa,KAAK;IAClB,QAAQ;GACV;EACF,CAAC,CACH;CACF;CAEA,MAAM,SAAS,WAAuB;EACpC;EACA,OAAO;EACP,OAAO,KAAK,eAAe;GACzB,IAAI,EAAE,SAAS,GAAG,QAAQ;EAC5B,CAAC;EACD,OAAO;CACT;CAEA,OAAO;EAAE;EAAQ;CAAM;AACzB;;;;;AASA,MAAM,WAAW,WAAwC;CACvD,MAAM,MAAM,OAAO,OAAO,IAAI,IAAI,SAAS,MAAM,OAAO,GAAG,CAAC;CAC5D,MAAM,SAAS,SAAiB,KAAK,aAAa,OAAO,IAAI,CAAC,CAAC,GAAG,EAAE;CACpE,MAAM,UAAU,KAAK,YAAY,mBAAmB,IAAI,SAAS,QAAQ,cAAc,IAAI,CAAC;CAC5F,MAAM,OAAO,OAAO,QAAQ,MAAM,MAAM,MAAM,WAAW;CACzD,MAAM,OAAO,OAAO,QAAQ,OAAO,MAAM,MAAM,MAAM,KAAK,QAAQ,KAAK;CACvE,MAAM,OAAO,OAAO,SAAS,KAAK,WAAW,GAAG,IAAI,GAAG,KAAK,YAAY,SAAS,KAAA;CACjF,IAAI,SAAS,KAAA,GAAW,OAAO,IAAI,QAAQ,EAAE,KAAK,CAAC;CACnD,OAAO,IAAI,QAAQ;EACjB;EACA;EACA,SAAS;EACT,GAAI,OAAO,cAAc,KAAA,KAAa;GACpC,WAAW;GACX,uBAAuB,SAAS,SAAS,SAAS,gBAAgB,OAAO,SAAS,CAAC;EACrF;CACF,CAAC;AACH;;;;;;;;;;AAWA,MAAa,SAAoB,WAC/B,MAAM,cAAc;CAClB,MAAM,YAAY,cAAc;CAChC,MAAM,SAAS,OAAO,KAAK,UAAU,MAAM;CAC3C,OAAO,SAAS,MAAM;EACpB,GAAG;EACH,cAAc,UAAU,MAAM,QAAQ,MAAM,CAAC;EAC7C,WAAW,OAAO,YAAY,OAAO,QAAQ,QAAQ,OAAO,SAAS,IAAI;CAC3E,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
//#region src/internal/requestContext.ts
|
|
2
|
+
/**
|
|
3
|
+
* Vercel exposes the current request's context on a well-known global symbol.
|
|
4
|
+
* `@vercel/functions` reads `waitUntil` and the deadline from there. Off
|
|
5
|
+
* Vercel, or outside a request, there is no `waitUntil`.
|
|
6
|
+
*/
|
|
7
|
+
const REQUEST_CONTEXT = Symbol.for("@vercel/request-context");
|
|
8
|
+
const currentRequest = () => {
|
|
9
|
+
return globalThis[REQUEST_CONTEXT]?.get?.();
|
|
10
|
+
};
|
|
11
|
+
//#endregion
|
|
12
|
+
export { currentRequest as t };
|
|
13
|
+
|
|
14
|
+
//# sourceMappingURL=requestContext-CEUKYTim.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"requestContext-CEUKYTim.js","names":["holder"],"sources":["../src/internal/requestContext.ts"],"sourcesContent":["/**\n * Vercel exposes the current request's context on a well-known global symbol.\n * `@vercel/functions` reads `waitUntil` and the deadline from there. Off\n * Vercel, or outside a request, there is no `waitUntil`.\n */\nconst REQUEST_CONTEXT = Symbol.for(\"@vercel/request-context\")\n\ntype RequestContext = {\n readonly waitUntil?: (promise: Promise<unknown>) => void\n readonly deadline?: string\n}\n\nexport const currentRequest = (): RequestContext | undefined => {\n const holder = globalThis as typeof globalThis & {\n [REQUEST_CONTEXT]?: { get?: () => RequestContext | undefined }\n }\n return holder[REQUEST_CONTEXT]?.get?.()\n}\n"],"mappings":";;;;;;AAKA,MAAM,kBAAkB,OAAO,IAAI,yBAAyB;AAO5D,MAAa,uBAAmD;CAI9D,OAAOA,WAAO,gBAAgB,EAAE,MAAM;AACxC"}
|
package/package.json
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "effect-vercel",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Effect integrations for Vercel: AI Gateway, OIDC, Postgres pools, Runtime Cache, waitUntil",
|
|
3
|
+
"version": "0.7.0",
|
|
4
|
+
"description": "Effect integrations for Vercel: AI Gateway, OIDC, OTLP flushing, Postgres pools, Runtime Cache, waitUntil",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ai-gateway",
|
|
7
7
|
"effect",
|
|
8
8
|
"effect-ai",
|
|
9
9
|
"oidc",
|
|
10
|
+
"opentelemetry",
|
|
11
|
+
"otlp",
|
|
10
12
|
"postgres",
|
|
11
13
|
"runtime-cache",
|
|
12
14
|
"vercel",
|
|
@@ -33,6 +35,7 @@
|
|
|
33
35
|
"./AiGatewayCredentials": "./dist/AiGatewayCredentials.js",
|
|
34
36
|
"./Functions": "./dist/Functions.js",
|
|
35
37
|
"./Oidc": "./dist/Oidc.js",
|
|
38
|
+
"./Otlp": "./dist/Otlp.js",
|
|
36
39
|
"./PgClient": "./dist/PgClient.js",
|
|
37
40
|
"./RuntimeCache": "./dist/RuntimeCache.js",
|
|
38
41
|
"./package.json": "./package.json"
|
|
@@ -50,13 +53,13 @@
|
|
|
50
53
|
"prepublishOnly": "pnpm check && pnpm test && pnpm build"
|
|
51
54
|
},
|
|
52
55
|
"devDependencies": {
|
|
53
|
-
"@effect/ai-anthropic": "^4.
|
|
56
|
+
"@effect/ai-anthropic": "^4.1.0",
|
|
54
57
|
"@effect/language-service": "^0.87.3",
|
|
55
|
-
"@effect/sql-pg": "^4.0
|
|
56
|
-
"@effect/vitest": "^4.
|
|
58
|
+
"@effect/sql-pg": "^4.1.0",
|
|
59
|
+
"@effect/vitest": "^4.1.0",
|
|
57
60
|
"@types/node": "^24",
|
|
58
61
|
"@vercel/functions": "^3.9.9",
|
|
59
|
-
"effect": "^4.0
|
|
62
|
+
"effect": "^4.1.0",
|
|
60
63
|
"oxfmt": "^0.68.0",
|
|
61
64
|
"oxlint": "^1.83.0",
|
|
62
65
|
"tsdown": "^0.23.0",
|
|
@@ -67,7 +70,7 @@
|
|
|
67
70
|
"@effect/ai-anthropic": "^4.0.0",
|
|
68
71
|
"@effect/sql-pg": "^4.0.6",
|
|
69
72
|
"@vercel/functions": "^3.9.9",
|
|
70
|
-
"effect": "^4.
|
|
73
|
+
"effect": "^4.1.0"
|
|
71
74
|
},
|
|
72
75
|
"peerDependenciesMeta": {
|
|
73
76
|
"@effect/ai-anthropic": {
|
package/src/Otlp.ts
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Keeps a Vercel Function running until its OTLP telemetry is exported.
|
|
3
|
+
*
|
|
4
|
+
* Effect's OTLP exporters buffer spans, logs and metrics and export them on
|
|
5
|
+
* an interval. Vercel can suspend the instance once the response is sent, so
|
|
6
|
+
* the interval may never fire and the telemetry is lost. These flush the
|
|
7
|
+
* exporters when a request ends and keep the Function running until the
|
|
8
|
+
* flush is done.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* import * as VercelOtlp from "effect-vercel/Otlp"
|
|
13
|
+
* import { Layer } from "effect"
|
|
14
|
+
* import { FetchHttpClient, HttpRouter } from "effect/http"
|
|
15
|
+
* import { Otlp } from "effect/observability"
|
|
16
|
+
*
|
|
17
|
+
* const { handler } = HttpRouter.toWebHandler(
|
|
18
|
+
* Layer.merge(Routes, VercelOtlp.flushAfterRequest).pipe(
|
|
19
|
+
* Layer.provideMerge(Otlp.layerJson({ baseUrl, resource })),
|
|
20
|
+
* Layer.provide(FetchHttpClient.layer),
|
|
21
|
+
* ),
|
|
22
|
+
* )
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
import { Effect, type Layer, type Scope } from "effect"
|
|
27
|
+
import { HttpRouter } from "effect/http"
|
|
28
|
+
import { OtlpExporter } from "effect/observability"
|
|
29
|
+
import { currentRequest } from "./internal/requestContext.js"
|
|
30
|
+
|
|
31
|
+
const flushOnCloseWith = (flusher: OtlpExporter.Flusher["Service"]) =>
|
|
32
|
+
Effect.suspend(() => {
|
|
33
|
+
const request = currentRequest()
|
|
34
|
+
if (request?.waitUntil === undefined) return Effect.void
|
|
35
|
+
const { promise, resolve } = Promise.withResolvers<void>()
|
|
36
|
+
request.waitUntil(promise)
|
|
37
|
+
// Forked, so a streamed body ends without waiting for the export.
|
|
38
|
+
return Effect.addFinalizer(() =>
|
|
39
|
+
flusher.flush.pipe(
|
|
40
|
+
Effect.timeoutOption("2 seconds"),
|
|
41
|
+
Effect.ensuring(Effect.sync(resolve)),
|
|
42
|
+
Effect.forkDetach,
|
|
43
|
+
),
|
|
44
|
+
)
|
|
45
|
+
}).pipe(
|
|
46
|
+
// An interrupt before the finalizer is added would leave the wait open.
|
|
47
|
+
Effect.uninterruptible,
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* When the current scope closes, flushes the OTLP exporters and keeps the
|
|
52
|
+
* Function running until the flush ends, for at most 2 seconds.
|
|
53
|
+
*
|
|
54
|
+
* It registers the wait with Vercel right away, so run it while the request
|
|
55
|
+
* is current. Run it first in the scope: finalizers added later, such as
|
|
56
|
+
* ending a span, run before the flush. Off Vercel, or outside a request, it
|
|
57
|
+
* does nothing, and the exporters' interval sends the telemetry.
|
|
58
|
+
*
|
|
59
|
+
* Use `flushAfterRequest` for an `HttpRouter` app. Use this one when the
|
|
60
|
+
* request has another entry point, with a scope that closes when it ends.
|
|
61
|
+
*/
|
|
62
|
+
export const flushOnClose: Effect.Effect<void, never, OtlpExporter.Flusher | Scope.Scope> =
|
|
63
|
+
Effect.flatMap(OtlpExporter.Flusher, flushOnCloseWith)
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Global `HttpRouter` middleware that runs `flushOnClose` in each request's
|
|
67
|
+
* scope. That scope closes after the response body is sent, so telemetry
|
|
68
|
+
* recorded while a body streams is in the flush.
|
|
69
|
+
*/
|
|
70
|
+
export const flushAfterRequest: Layer.Layer<
|
|
71
|
+
never,
|
|
72
|
+
never,
|
|
73
|
+
HttpRouter.HttpRouter | OtlpExporter.Flusher
|
|
74
|
+
> = HttpRouter.middleware(
|
|
75
|
+
Effect.map(
|
|
76
|
+
OtlpExporter.Flusher,
|
|
77
|
+
(flusher) => (httpEffect) => Effect.andThen(flushOnCloseWith(flusher), httpEffect),
|
|
78
|
+
),
|
|
79
|
+
{ global: true },
|
|
80
|
+
)
|
package/src/PgClient.ts
CHANGED
|
@@ -28,25 +28,7 @@ import * as Net from "node:net"
|
|
|
28
28
|
import { PgClient } from "@effect/sql-pg"
|
|
29
29
|
import { Duration, Effect, Layer, Redacted } from "effect"
|
|
30
30
|
import { constVoid } from "effect/Function"
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Vercel exposes the current request's context on a well-known global symbol.
|
|
34
|
-
* `@vercel/functions` reads `waitUntil` and the deadline from there. Off
|
|
35
|
-
* Vercel, or outside a request, there is no `waitUntil`.
|
|
36
|
-
*/
|
|
37
|
-
const REQUEST_CONTEXT = Symbol.for("@vercel/request-context")
|
|
38
|
-
|
|
39
|
-
type RequestContext = {
|
|
40
|
-
readonly waitUntil?: (promise: Promise<unknown>) => void
|
|
41
|
-
readonly deadline?: string
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
const currentRequest = (): RequestContext | undefined => {
|
|
45
|
-
const holder = globalThis as typeof globalThis & {
|
|
46
|
-
[REQUEST_CONTEXT]?: { get?: () => RequestContext | undefined }
|
|
47
|
-
}
|
|
48
|
-
return holder[REQUEST_CONTEXT]?.get?.()
|
|
49
|
-
}
|
|
31
|
+
import { currentRequest } from "./internal/requestContext.js"
|
|
50
32
|
|
|
51
33
|
/**
|
|
52
34
|
* Keeps the instance up while a tracked socket is open. One `waitUntil`
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Vercel exposes the current request's context on a well-known global symbol.
|
|
3
|
+
* `@vercel/functions` reads `waitUntil` and the deadline from there. Off
|
|
4
|
+
* Vercel, or outside a request, there is no `waitUntil`.
|
|
5
|
+
*/
|
|
6
|
+
const REQUEST_CONTEXT = Symbol.for("@vercel/request-context")
|
|
7
|
+
|
|
8
|
+
type RequestContext = {
|
|
9
|
+
readonly waitUntil?: (promise: Promise<unknown>) => void
|
|
10
|
+
readonly deadline?: string
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export const currentRequest = (): RequestContext | undefined => {
|
|
14
|
+
const holder = globalThis as typeof globalThis & {
|
|
15
|
+
[REQUEST_CONTEXT]?: { get?: () => RequestContext | undefined }
|
|
16
|
+
}
|
|
17
|
+
return holder[REQUEST_CONTEXT]?.get?.()
|
|
18
|
+
}
|