@lunora/container 1.0.0-alpha.11 → 1.0.0-alpha.110

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.
Files changed (38) hide show
  1. package/LICENSE.md +201 -104
  2. package/README.md +32 -10
  3. package/dist/bridge.d.mts +58 -33
  4. package/dist/bridge.d.ts +58 -33
  5. package/dist/bridge.mjs +1 -79
  6. package/dist/do/index.d.mts +362 -78
  7. package/dist/do/index.d.ts +362 -78
  8. package/dist/do/index.mjs +1 -321
  9. package/dist/index.d.mts +590 -151
  10. package/dist/index.d.ts +590 -151
  11. package/dist/index.mjs +1 -2
  12. package/dist/otel.d.mts +146 -47
  13. package/dist/otel.d.ts +146 -47
  14. package/dist/otel.mjs +1 -220
  15. package/dist/packem_shared/CONTAINER_EXEC_PATH-7Hcd6-zs.mjs +1 -0
  16. package/dist/packem_shared/CUSTOM_INSTANCE_MIN_MEMORY_MIB_PER_VCPU-D8Hy9Rxv.mjs +1 -0
  17. package/dist/packem_shared/ContainerProxy-CfXjS6ZI.mjs +27 -0
  18. package/dist/packem_shared/DURABLE_OBJECT_POLICY_FORBIDDEN_KEYS-gYi8RBUO.mjs +1 -0
  19. package/dist/packem_shared/LunoraSandboxContainer-DPnhJUu1.mjs +1 -0
  20. package/dist/packem_shared/abort-deadline-jwbW9Ala.mjs +1 -0
  21. package/dist/packem_shared/createContainerContext-p6QxWjsm.mjs +1 -0
  22. package/dist/packem_shared/exec-BYLW70sR.mjs +1 -0
  23. package/dist/packem_shared/jurisdiction-DtE9s70w.mjs +1 -0
  24. package/dist/packem_shared/jurisdiction.d-BwpauCoI.d.mts +471 -0
  25. package/dist/packem_shared/jurisdiction.d-BwpauCoI.d.ts +471 -0
  26. package/dist/packem_shared/sampling-D21rpu_s.mjs +1 -0
  27. package/dist/packem_shared/sandbox-types.d-DXlHnsEg.d.mts +110 -0
  28. package/dist/packem_shared/sandbox-types.d-DXlHnsEg.d.ts +110 -0
  29. package/dist/sandbox/index.d.mts +66 -0
  30. package/dist/sandbox/index.d.ts +66 -0
  31. package/dist/sandbox/index.mjs +1 -0
  32. package/package.json +9 -3
  33. package/dist/packem_shared/ContainerProxy-DWqUX_re.mjs +0 -1474
  34. package/dist/packem_shared/containerBindingName-BiTrAF1J.mjs +0 -224
  35. package/dist/packem_shared/createContainerContext-Cg53QGdf.mjs +0 -223
  36. package/dist/packem_shared/jurisdiction-CuPNcLDt.mjs +0 -13
  37. package/dist/packem_shared/jurisdiction.d-TwTGkgTg.d.mts +0 -266
  38. package/dist/packem_shared/jurisdiction.d-TwTGkgTg.d.ts +0 -266
package/dist/index.mjs CHANGED
@@ -1,2 +1 @@
1
- export { createContainerContext, createContainerTestContext } from './packem_shared/createContainerContext-Cg53QGdf.mjs';
2
- export { containerBindingName, containerBuildTag, containerClassName, defineContainer, isContainerDefinition, normalizeContainerImage, resolveContainerEnvVars } from './packem_shared/containerBindingName-BiTrAF1J.mjs';
1
+ import{createContainerContext as r,createContainerTestContext as t,getContainer as a}from"./packem_shared/createContainerContext-p6QxWjsm.mjs";import{DURABLE_OBJECT_POLICY_FORBIDDEN_KEYS as i,containerBindingName as C,containerBuildTag as _,containerClassName as E,defineContainer as s,isCloudflareRegistryDigest as I,isContainerDefinition as T,isManagedImage as N,normalizeContainerImage as m,resolveContainerEnvVars as g}from"./packem_shared/DURABLE_OBJECT_POLICY_FORBIDDEN_KEYS-gYi8RBUO.mjs";import{C as f}from"./packem_shared/exec-BYLW70sR.mjs";import{CUSTOM_INSTANCE_MIN_MEMORY_MIB_PER_VCPU as l,CUSTOM_INSTANCE_TYPE_LIMITS as x}from"./packem_shared/CUSTOM_INSTANCE_MIN_MEMORY_MIB_PER_VCPU-D8Hy9Rxv.mjs";export{f as CONTAINER_EXEC_PATH,l as CUSTOM_INSTANCE_MIN_MEMORY_MIB_PER_VCPU,x as CUSTOM_INSTANCE_TYPE_LIMITS,i as DURABLE_OBJECT_POLICY_FORBIDDEN_KEYS,C as containerBindingName,_ as containerBuildTag,E as containerClassName,r as createContainerContext,t as createContainerTestContext,s as defineContainer,a as getContainer,I as isCloudflareRegistryDigest,T as isContainerDefinition,N as isManagedImage,m as normalizeContainerImage,g as resolveContainerEnvVars};
package/dist/otel.d.mts CHANGED
@@ -1,12 +1,14 @@
1
- /** An attribute value carried on a span or log. */
1
+ /**
2
+ * An attribute value carried on a span or log.
3
+ */
2
4
  type ContainerAttributeValue = boolean | number | string;
3
5
  /**
4
- * A `fetch` implementation — defaults to the runtime global. The exporter passes
5
- * an abort `signal` (for the per-request timeout) and, once the promise settles,
6
- * cancels the response `body` so Node/undici can release the socket for
7
- * keep-alive reuse instead of leaving it occupied by an unread stream. It reads
8
- * `ok`/`status` to detect a rejected export and nothing else from the response.
9
- */
6
+ * A `fetch` implementation — defaults to the runtime global. The exporter passes
7
+ * an abort `signal` (for the per-request timeout) and, once the promise settles,
8
+ * cancels the response `body` so Node/undici can release the socket for
9
+ * keep-alive reuse instead of leaving it occupied by an unread stream. It reads
10
+ * `ok`/`status` to detect a rejected export and nothing else from the response.
11
+ */
10
12
  type OtelFetchLike = (input: string, init: {
11
13
  body: string;
12
14
  headers: Record<string, string>;
@@ -19,7 +21,9 @@ type OtelFetchLike = (input: string, init: {
19
21
  ok: boolean;
20
22
  status: number;
21
23
  }>;
22
- /** A single span the container process asks the exporter to record. */
24
+ /**
25
+ * A single span the container process asks the exporter to record.
26
+ */
23
27
  interface ContainerSpanInput {
24
28
  /** Attributes attached to the span (rendered under the OTLP `attributes` list). */
25
29
  attributes?: Record<string, ContainerAttributeValue>;
@@ -35,7 +39,9 @@ interface ContainerSpanInput {
35
39
  /** Wall-clock millis when the operation started. */
36
40
  startMs: number;
37
41
  }
38
- /** A single log line the container process asks the exporter to record. */
42
+ /**
43
+ * A single log line the container process asks the exporter to record.
44
+ */
39
45
  interface ContainerLogInput {
40
46
  /** Attributes attached to the log record. */
41
47
  attributes?: Record<string, ContainerAttributeValue>;
@@ -46,8 +52,42 @@ interface ContainerLogInput {
46
52
  /** Wall-clock millis the line was emitted; defaults to now. */
47
53
  ts?: number;
48
54
  }
49
- /** Options for {@link createContainerTelemetry}. */
55
+ /**
56
+ * Options for {@link createContainerTelemetry}.
57
+ */
50
58
  interface ContainerTelemetryOptions {
59
+ /**
60
+ * Keep a span that ERRORED even when the inbound `traceparent` says the trace
61
+ * was sampled out — the **tail bias**, the same rule `@lunora/runtime`'s
62
+ * `sampling.alwaysSampleErrors` and the shard apply.
63
+ *
64
+ * Normally you do not set this: the dispatch's verdict arrives on the
65
+ * inbound request (see {@link ContainerTelemetryOptions.request}), so all
66
+ * three tiers agree because they were told the same thing. Set it to
67
+ * override that. With neither, the `LUNORA_SAMPLE_ERRORS` env var applies
68
+ * (`"0"` turns it off) — which is what a one-shot container with no request
69
+ * to read has — and failing that, the tier default `true`.
70
+ *
71
+ * Per SPAN, not per trace: the container is a long-running process with no
72
+ * dispatch boundary to re-decide at, so a span that already settled `ok`
73
+ * before a sibling failed is not retro-exported. That is the same shape the
74
+ * worker applies to its own dispatch events.
75
+ */
76
+ alwaysSampleErrors?: boolean;
77
+ /**
78
+ * Value of the `deployment.environment` resource attribute. Falls back to
79
+ * the `DEPLOYMENT_ENVIRONMENT` / `ENVIRONMENT` / `NODE_ENV` env vars **only
80
+ * when {@link ContainerTelemetryOptions.detectResources} is `true`** —
81
+ * unlike `serviceName`, env detection here is opt-in so a stray `NODE_ENV`
82
+ * never silently labels a deployment.
83
+ */
84
+ deploymentEnvironment?: string;
85
+ /**
86
+ * When `true`, auto-detect OTLP resource attributes from the container
87
+ * environment (`HOSTNAME`, `KUBERNETES_*`, `SERVICE_VERSION`, etc.).
88
+ * Explicit options and `resourceAttributes` win on collision.
89
+ */
90
+ detectResources?: boolean;
51
91
  /** Base OTLP collector endpoint; defaults to the `LUNORA_OTLP_ENDPOINT` env var. */
52
92
  endpoint?: string;
53
93
  /** Injectable `fetch` (tests / non-global runtimes). Defaults to `globalThis.fetch`. */
@@ -56,37 +96,81 @@ interface ContainerTelemetryOptions {
56
96
  headers?: Record<string, string>;
57
97
  /** Called with any send failure so the caller can surface it; the export itself always swallows. */
58
98
  onError?: (error: unknown) => void;
99
+ /**
100
+ * The inbound container request, read for the telemetry context the caller's
101
+ * Worker dispatch propagated onto it: the W3C `traceparent` and the
102
+ * tail-bias toggle.
103
+ *
104
+ * The one-call form of the per-request pattern. A container that serves many
105
+ * requests must build a telemetry instance per request anyway (the trace
106
+ * context differs each call), and passing the request means the handler does
107
+ * not have to know which headers carry what — which is the only way the
108
+ * toggle's header name would otherwise reach user code.
109
+ *
110
+ * ```ts
111
+ * const telemetry = createContainerTelemetry({ request });
112
+ * ```
113
+ */
114
+ request?: {
115
+ headers: {
116
+ get: (name: string) => null | string;
117
+ };
118
+ };
119
+ /** Additional resource attributes merged onto every signal. */
120
+ resourceAttributes?: Record<string, ContainerAttributeValue>;
59
121
  /** `service.name` resource attribute; defaults to the `LUNORA_SERVICE_NAME` env var then `"lunora-container"`. */
60
122
  serviceName?: string;
123
+ /**
124
+ * `service.version` resource attribute. Falls back to `SERVICE_VERSION` /
125
+ * `CF_VERSION_METADATA` / `VERCEL_GIT_COMMIT_SHA` / `GITHUB_SHA` /
126
+ * `COMMIT_SHA` env vars **only when
127
+ * {@link ContainerTelemetryOptions.detectResources} is `true`**.
128
+ */
129
+ serviceVersion?: string;
61
130
  /** Per-POST timeout in ms; a collector that never responds aborts after this so a stuck send can't stall `flush()`. Defaults to {@link DEFAULT_TIMEOUT_MS} (10s). */
62
131
  timeoutMs?: number;
63
132
  /** Bearer token sent as an `Authorization: Bearer` header; defaults to the `LUNORA_OTLP_TOKEN` env var. */
64
133
  token?: string;
65
134
  /**
66
- * W3C `traceparent` of the Worker RPC that invoked this container; defaults to
67
- * the `LUNORA_TRACEPARENT` env var. When present (and well-formed) every span
68
- * inherits its trace id and hangs off its span id, so container spans stitch
69
- * under the Worker's trace instead of forming a fresh, disconnected trace.
70
- *
71
- * `@lunora/container` stamps this trace context as the **`traceparent` request
72
- * header** on every proxied fetch (`ctx.containers.&lt;name>.…`), so a container
73
- * that serves many requests should read it per request and create a telemetry
74
- * instance scoped to that request — the trace context differs each call, so a
75
- * single process-lifetime instance can't carry it:
76
- *
77
- * ```ts
78
- * // inside the container's request handler
79
- * const telemetry = createContainerTelemetry({ traceparent: request.headers.get("traceparent") ?? undefined });
80
- * await telemetry.trace("transcode", () => transcode(job));
81
- * await telemetry.flush();
82
- * ```
83
- *
84
- * The `LUNORA_TRACEPARENT` env fallback fits a one-shot container that
85
- * processes a single job per start (the value is fixed for the process).
86
- */
135
+ * W3C `traceparent` of the Worker RPC that invoked this container; defaults to
136
+ * the `LUNORA_TRACEPARENT` env var. When present (and well-formed) every span
137
+ * inherits its trace id and hangs off its span id, so container spans stitch
138
+ * under the Worker's trace instead of forming a fresh, disconnected trace, and
139
+ * every log record is stamped with the same ids so a request's container logs
140
+ * are reachable from its trace.
141
+ *
142
+ * It also carries the trace's settled **sampling verdict**, which this exporter
143
+ * OBEYS rather than re-derives: a `traceparent` whose flags say the trace was
144
+ * sampled out (`…-00`) suppresses span export, because the worker and shard
145
+ * spans of that trace were dropped and shipping ours would leave the collector
146
+ * holding the middle of a trace. An ERRORED span is the exception — see
147
+ * {@link ContainerTelemetryOptions.alwaysSampleErrors}. Logs are never sampled
148
+ * and are unaffected.
149
+ *
150
+ * `@lunora/container` stamps this trace context on every proxied fetch
151
+ * (`ctx.containers.<name>.…`), so a container that serves many requests
152
+ * should create a telemetry instance per request — the trace context differs
153
+ * each call, so a single process-lifetime instance can't carry it. Prefer
154
+ * {@link ContainerTelemetryOptions.request}, which takes this AND the
155
+ * tail-bias toggle off the same request:
156
+ *
157
+ * ```ts
158
+ * // inside the container's request handler
159
+ * const telemetry = createContainerTelemetry({ request });
160
+ * await telemetry.trace("transcode", () => transcode(job));
161
+ * await telemetry.flush();
162
+ * ```
163
+ *
164
+ * Pass this option explicitly only to override what the request carries. The
165
+ * `LUNORA_TRACEPARENT` env fallback fits a one-shot container that processes
166
+ * a single job per start, where there is no request to read (the value is
167
+ * fixed for the process).
168
+ */
87
169
  traceparent?: string;
88
170
  }
89
- /** The exporter handle {@link createContainerTelemetry} returns. */
171
+ /**
172
+ * The exporter handle {@link createContainerTelemetry} returns.
173
+ */
90
174
  interface ContainerTelemetry {
91
175
  /** Record one log line (no-op when disabled). */
92
176
  emitLog: (log: ContainerLogInput) => void;
@@ -94,25 +178,40 @@ interface ContainerTelemetry {
94
178
  emitSpan: (span: ContainerSpanInput) => void;
95
179
  /** True when an endpoint resolved and exports are actually sent. */
96
180
  readonly enabled: boolean;
97
- /** Await all in-flight sends — call before the process exits. */
181
+ /** Drain the span/log buffers and await every in-flight send — call before the process exits, or buffered signals are lost. */
98
182
  flush: () => Promise<void>;
99
183
  /** Time `run()`, recording a span named `name` (ok, or errored if it throws). Always runs `run()`, even when disabled. */
100
184
  trace: <T>(name: string, run: () => Promise<T>, attributes?: Record<string, ContainerAttributeValue>) => Promise<T>;
101
185
  }
102
186
  /**
103
- * Create a zero-config OTLP exporter for the container process.
104
- *
105
- * ```ts
106
- * const telemetry = createContainerTelemetry(); // reads LUNORA_OTLP_ENDPOINT / _TOKEN
107
- * await telemetry.trace("transcode", () => transcode(job), { jobId: job.id });
108
- * telemetry.emitLog({ level: "info", message: "done", attributes: { jobId: job.id } });
109
- * await telemetry.flush(); // before the process exits
110
- * ```
111
- *
112
- * With no endpoint resolvable the returned exporter is disabled (`enabled ===
113
- * false`): `emitSpan`/`emitLog` no-op and `trace` still runs its work but records
114
- * nothing — so the same code runs unchanged locally and in the cloud.
115
- * @param options Exporter options; every field falls back to a `LUNORA_*` env var.
116
- */
187
+ * Create a zero-config OTLP exporter for the container process.
188
+ *
189
+ * ```ts
190
+ * const telemetry = createContainerTelemetry(); // reads LUNORA_OTLP_ENDPOINT / _TOKEN
191
+ * await telemetry.trace("transcode", () => transcode(job), { jobId: job.id });
192
+ * telemetry.emitLog({ level: "info", message: "done", attributes: { jobId: job.id } });
193
+ * await telemetry.flush(); // REQUIRED before the process exits — see below
194
+ * ```
195
+ *
196
+ * `flush()` is mandatory, not an optimisation. Spans and log records are
197
+ * BATCHED: nothing leaves the process until the batcher's timer elapses or
198
+ * `flush()` drains it, so a job that finishes and exits inside that window
199
+ * reports NOTHING without it. A long job between flushes can also reach the
200
+ * batcher's item cap, which drops the OLDEST buffered records — flush at
201
+ * checkpoints, not only at exit.
202
+ *
203
+ * With no endpoint resolvable the returned exporter is disabled (`enabled ===
204
+ * false`): `emitSpan`/`emitLog` no-op and `trace` still runs its work but records
205
+ * nothing — so the same code runs unchanged locally and in the cloud.
206
+ *
207
+ * With a trace anchor (`traceparent`, the `request`'s header, or
208
+ * `LUNORA_TRACEPARENT`) every span hangs off it and every log is stamped with
209
+ * it. Without one each span starts its own trace — so in a long-running server,
210
+ * create the instance per request (`{ request }`), not once per process.
211
+ * @param options Exporter options. Connection fields (`endpoint`, `token`,
212
+ * `serviceName`, `traceparent`) always fall back to their `LUNORA_*` env var;
213
+ * resource fields (`serviceVersion`, `deploymentEnvironment`) only do so under
214
+ * `detectResources: true`.
215
+ */
117
216
  declare const createContainerTelemetry: (options?: ContainerTelemetryOptions) => ContainerTelemetry;
118
217
  export { type ContainerAttributeValue, type ContainerLogInput, type ContainerSpanInput, type ContainerTelemetry, type ContainerTelemetryOptions, type OtelFetchLike, createContainerTelemetry };
package/dist/otel.d.ts CHANGED
@@ -1,12 +1,14 @@
1
- /** An attribute value carried on a span or log. */
1
+ /**
2
+ * An attribute value carried on a span or log.
3
+ */
2
4
  type ContainerAttributeValue = boolean | number | string;
3
5
  /**
4
- * A `fetch` implementation — defaults to the runtime global. The exporter passes
5
- * an abort `signal` (for the per-request timeout) and, once the promise settles,
6
- * cancels the response `body` so Node/undici can release the socket for
7
- * keep-alive reuse instead of leaving it occupied by an unread stream. It reads
8
- * `ok`/`status` to detect a rejected export and nothing else from the response.
9
- */
6
+ * A `fetch` implementation — defaults to the runtime global. The exporter passes
7
+ * an abort `signal` (for the per-request timeout) and, once the promise settles,
8
+ * cancels the response `body` so Node/undici can release the socket for
9
+ * keep-alive reuse instead of leaving it occupied by an unread stream. It reads
10
+ * `ok`/`status` to detect a rejected export and nothing else from the response.
11
+ */
10
12
  type OtelFetchLike = (input: string, init: {
11
13
  body: string;
12
14
  headers: Record<string, string>;
@@ -19,7 +21,9 @@ type OtelFetchLike = (input: string, init: {
19
21
  ok: boolean;
20
22
  status: number;
21
23
  }>;
22
- /** A single span the container process asks the exporter to record. */
24
+ /**
25
+ * A single span the container process asks the exporter to record.
26
+ */
23
27
  interface ContainerSpanInput {
24
28
  /** Attributes attached to the span (rendered under the OTLP `attributes` list). */
25
29
  attributes?: Record<string, ContainerAttributeValue>;
@@ -35,7 +39,9 @@ interface ContainerSpanInput {
35
39
  /** Wall-clock millis when the operation started. */
36
40
  startMs: number;
37
41
  }
38
- /** A single log line the container process asks the exporter to record. */
42
+ /**
43
+ * A single log line the container process asks the exporter to record.
44
+ */
39
45
  interface ContainerLogInput {
40
46
  /** Attributes attached to the log record. */
41
47
  attributes?: Record<string, ContainerAttributeValue>;
@@ -46,8 +52,42 @@ interface ContainerLogInput {
46
52
  /** Wall-clock millis the line was emitted; defaults to now. */
47
53
  ts?: number;
48
54
  }
49
- /** Options for {@link createContainerTelemetry}. */
55
+ /**
56
+ * Options for {@link createContainerTelemetry}.
57
+ */
50
58
  interface ContainerTelemetryOptions {
59
+ /**
60
+ * Keep a span that ERRORED even when the inbound `traceparent` says the trace
61
+ * was sampled out — the **tail bias**, the same rule `@lunora/runtime`'s
62
+ * `sampling.alwaysSampleErrors` and the shard apply.
63
+ *
64
+ * Normally you do not set this: the dispatch's verdict arrives on the
65
+ * inbound request (see {@link ContainerTelemetryOptions.request}), so all
66
+ * three tiers agree because they were told the same thing. Set it to
67
+ * override that. With neither, the `LUNORA_SAMPLE_ERRORS` env var applies
68
+ * (`"0"` turns it off) — which is what a one-shot container with no request
69
+ * to read has — and failing that, the tier default `true`.
70
+ *
71
+ * Per SPAN, not per trace: the container is a long-running process with no
72
+ * dispatch boundary to re-decide at, so a span that already settled `ok`
73
+ * before a sibling failed is not retro-exported. That is the same shape the
74
+ * worker applies to its own dispatch events.
75
+ */
76
+ alwaysSampleErrors?: boolean;
77
+ /**
78
+ * Value of the `deployment.environment` resource attribute. Falls back to
79
+ * the `DEPLOYMENT_ENVIRONMENT` / `ENVIRONMENT` / `NODE_ENV` env vars **only
80
+ * when {@link ContainerTelemetryOptions.detectResources} is `true`** —
81
+ * unlike `serviceName`, env detection here is opt-in so a stray `NODE_ENV`
82
+ * never silently labels a deployment.
83
+ */
84
+ deploymentEnvironment?: string;
85
+ /**
86
+ * When `true`, auto-detect OTLP resource attributes from the container
87
+ * environment (`HOSTNAME`, `KUBERNETES_*`, `SERVICE_VERSION`, etc.).
88
+ * Explicit options and `resourceAttributes` win on collision.
89
+ */
90
+ detectResources?: boolean;
51
91
  /** Base OTLP collector endpoint; defaults to the `LUNORA_OTLP_ENDPOINT` env var. */
52
92
  endpoint?: string;
53
93
  /** Injectable `fetch` (tests / non-global runtimes). Defaults to `globalThis.fetch`. */
@@ -56,37 +96,81 @@ interface ContainerTelemetryOptions {
56
96
  headers?: Record<string, string>;
57
97
  /** Called with any send failure so the caller can surface it; the export itself always swallows. */
58
98
  onError?: (error: unknown) => void;
99
+ /**
100
+ * The inbound container request, read for the telemetry context the caller's
101
+ * Worker dispatch propagated onto it: the W3C `traceparent` and the
102
+ * tail-bias toggle.
103
+ *
104
+ * The one-call form of the per-request pattern. A container that serves many
105
+ * requests must build a telemetry instance per request anyway (the trace
106
+ * context differs each call), and passing the request means the handler does
107
+ * not have to know which headers carry what — which is the only way the
108
+ * toggle's header name would otherwise reach user code.
109
+ *
110
+ * ```ts
111
+ * const telemetry = createContainerTelemetry({ request });
112
+ * ```
113
+ */
114
+ request?: {
115
+ headers: {
116
+ get: (name: string) => null | string;
117
+ };
118
+ };
119
+ /** Additional resource attributes merged onto every signal. */
120
+ resourceAttributes?: Record<string, ContainerAttributeValue>;
59
121
  /** `service.name` resource attribute; defaults to the `LUNORA_SERVICE_NAME` env var then `"lunora-container"`. */
60
122
  serviceName?: string;
123
+ /**
124
+ * `service.version` resource attribute. Falls back to `SERVICE_VERSION` /
125
+ * `CF_VERSION_METADATA` / `VERCEL_GIT_COMMIT_SHA` / `GITHUB_SHA` /
126
+ * `COMMIT_SHA` env vars **only when
127
+ * {@link ContainerTelemetryOptions.detectResources} is `true`**.
128
+ */
129
+ serviceVersion?: string;
61
130
  /** Per-POST timeout in ms; a collector that never responds aborts after this so a stuck send can't stall `flush()`. Defaults to {@link DEFAULT_TIMEOUT_MS} (10s). */
62
131
  timeoutMs?: number;
63
132
  /** Bearer token sent as an `Authorization: Bearer` header; defaults to the `LUNORA_OTLP_TOKEN` env var. */
64
133
  token?: string;
65
134
  /**
66
- * W3C `traceparent` of the Worker RPC that invoked this container; defaults to
67
- * the `LUNORA_TRACEPARENT` env var. When present (and well-formed) every span
68
- * inherits its trace id and hangs off its span id, so container spans stitch
69
- * under the Worker's trace instead of forming a fresh, disconnected trace.
70
- *
71
- * `@lunora/container` stamps this trace context as the **`traceparent` request
72
- * header** on every proxied fetch (`ctx.containers.&lt;name>.…`), so a container
73
- * that serves many requests should read it per request and create a telemetry
74
- * instance scoped to that request — the trace context differs each call, so a
75
- * single process-lifetime instance can't carry it:
76
- *
77
- * ```ts
78
- * // inside the container's request handler
79
- * const telemetry = createContainerTelemetry({ traceparent: request.headers.get("traceparent") ?? undefined });
80
- * await telemetry.trace("transcode", () => transcode(job));
81
- * await telemetry.flush();
82
- * ```
83
- *
84
- * The `LUNORA_TRACEPARENT` env fallback fits a one-shot container that
85
- * processes a single job per start (the value is fixed for the process).
86
- */
135
+ * W3C `traceparent` of the Worker RPC that invoked this container; defaults to
136
+ * the `LUNORA_TRACEPARENT` env var. When present (and well-formed) every span
137
+ * inherits its trace id and hangs off its span id, so container spans stitch
138
+ * under the Worker's trace instead of forming a fresh, disconnected trace, and
139
+ * every log record is stamped with the same ids so a request's container logs
140
+ * are reachable from its trace.
141
+ *
142
+ * It also carries the trace's settled **sampling verdict**, which this exporter
143
+ * OBEYS rather than re-derives: a `traceparent` whose flags say the trace was
144
+ * sampled out (`…-00`) suppresses span export, because the worker and shard
145
+ * spans of that trace were dropped and shipping ours would leave the collector
146
+ * holding the middle of a trace. An ERRORED span is the exception — see
147
+ * {@link ContainerTelemetryOptions.alwaysSampleErrors}. Logs are never sampled
148
+ * and are unaffected.
149
+ *
150
+ * `@lunora/container` stamps this trace context on every proxied fetch
151
+ * (`ctx.containers.<name>.…`), so a container that serves many requests
152
+ * should create a telemetry instance per request — the trace context differs
153
+ * each call, so a single process-lifetime instance can't carry it. Prefer
154
+ * {@link ContainerTelemetryOptions.request}, which takes this AND the
155
+ * tail-bias toggle off the same request:
156
+ *
157
+ * ```ts
158
+ * // inside the container's request handler
159
+ * const telemetry = createContainerTelemetry({ request });
160
+ * await telemetry.trace("transcode", () => transcode(job));
161
+ * await telemetry.flush();
162
+ * ```
163
+ *
164
+ * Pass this option explicitly only to override what the request carries. The
165
+ * `LUNORA_TRACEPARENT` env fallback fits a one-shot container that processes
166
+ * a single job per start, where there is no request to read (the value is
167
+ * fixed for the process).
168
+ */
87
169
  traceparent?: string;
88
170
  }
89
- /** The exporter handle {@link createContainerTelemetry} returns. */
171
+ /**
172
+ * The exporter handle {@link createContainerTelemetry} returns.
173
+ */
90
174
  interface ContainerTelemetry {
91
175
  /** Record one log line (no-op when disabled). */
92
176
  emitLog: (log: ContainerLogInput) => void;
@@ -94,25 +178,40 @@ interface ContainerTelemetry {
94
178
  emitSpan: (span: ContainerSpanInput) => void;
95
179
  /** True when an endpoint resolved and exports are actually sent. */
96
180
  readonly enabled: boolean;
97
- /** Await all in-flight sends — call before the process exits. */
181
+ /** Drain the span/log buffers and await every in-flight send — call before the process exits, or buffered signals are lost. */
98
182
  flush: () => Promise<void>;
99
183
  /** Time `run()`, recording a span named `name` (ok, or errored if it throws). Always runs `run()`, even when disabled. */
100
184
  trace: <T>(name: string, run: () => Promise<T>, attributes?: Record<string, ContainerAttributeValue>) => Promise<T>;
101
185
  }
102
186
  /**
103
- * Create a zero-config OTLP exporter for the container process.
104
- *
105
- * ```ts
106
- * const telemetry = createContainerTelemetry(); // reads LUNORA_OTLP_ENDPOINT / _TOKEN
107
- * await telemetry.trace("transcode", () => transcode(job), { jobId: job.id });
108
- * telemetry.emitLog({ level: "info", message: "done", attributes: { jobId: job.id } });
109
- * await telemetry.flush(); // before the process exits
110
- * ```
111
- *
112
- * With no endpoint resolvable the returned exporter is disabled (`enabled ===
113
- * false`): `emitSpan`/`emitLog` no-op and `trace` still runs its work but records
114
- * nothing — so the same code runs unchanged locally and in the cloud.
115
- * @param options Exporter options; every field falls back to a `LUNORA_*` env var.
116
- */
187
+ * Create a zero-config OTLP exporter for the container process.
188
+ *
189
+ * ```ts
190
+ * const telemetry = createContainerTelemetry(); // reads LUNORA_OTLP_ENDPOINT / _TOKEN
191
+ * await telemetry.trace("transcode", () => transcode(job), { jobId: job.id });
192
+ * telemetry.emitLog({ level: "info", message: "done", attributes: { jobId: job.id } });
193
+ * await telemetry.flush(); // REQUIRED before the process exits — see below
194
+ * ```
195
+ *
196
+ * `flush()` is mandatory, not an optimisation. Spans and log records are
197
+ * BATCHED: nothing leaves the process until the batcher's timer elapses or
198
+ * `flush()` drains it, so a job that finishes and exits inside that window
199
+ * reports NOTHING without it. A long job between flushes can also reach the
200
+ * batcher's item cap, which drops the OLDEST buffered records — flush at
201
+ * checkpoints, not only at exit.
202
+ *
203
+ * With no endpoint resolvable the returned exporter is disabled (`enabled ===
204
+ * false`): `emitSpan`/`emitLog` no-op and `trace` still runs its work but records
205
+ * nothing — so the same code runs unchanged locally and in the cloud.
206
+ *
207
+ * With a trace anchor (`traceparent`, the `request`'s header, or
208
+ * `LUNORA_TRACEPARENT`) every span hangs off it and every log is stamped with
209
+ * it. Without one each span starts its own trace — so in a long-running server,
210
+ * create the instance per request (`{ request }`), not once per process.
211
+ * @param options Exporter options. Connection fields (`endpoint`, `token`,
212
+ * `serviceName`, `traceparent`) always fall back to their `LUNORA_*` env var;
213
+ * resource fields (`serviceVersion`, `deploymentEnvironment`) only do so under
214
+ * `detectResources: true`.
215
+ */
117
216
  declare const createContainerTelemetry: (options?: ContainerTelemetryOptions) => ContainerTelemetry;
118
217
  export { type ContainerAttributeValue, type ContainerLogInput, type ContainerSpanInput, type ContainerTelemetry, type ContainerTelemetryOptions, type OtelFetchLike, createContainerTelemetry };