@decocms/blocks 8.1.0-next.0 → 8.1.0-next.1

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 (114) hide show
  1. package/package.json +6 -92
  2. package/src/index.ts +2 -12
  3. package/src/protocol/storage/memory.ts +1 -1
  4. package/src/v8/background.ts +3 -3
  5. package/src/v8/builtins/legacy.ts +1 -1
  6. package/src/v8/cli/index.ts +1 -1
  7. package/src/v8/fetch.ts +8 -27
  8. package/src/v8/remoteLoader.ts +1 -1
  9. package/src/v8/telemetry.ts +1 -1
  10. package/src/cms/applySectionConventions.ts +0 -148
  11. package/src/cms/blockSource.ts +0 -228
  12. package/src/cms/client.ts +0 -85
  13. package/src/cms/deferredTrigger.ts +0 -58
  14. package/src/cms/draftSource.ts +0 -476
  15. package/src/cms/index.ts +0 -180
  16. package/src/cms/loadDecofileDirectory.ts +0 -89
  17. package/src/cms/loader.ts +0 -563
  18. package/src/cms/registry.ts +0 -264
  19. package/src/cms/renderJson.ts +0 -179
  20. package/src/cms/resolve.ts +0 -2583
  21. package/src/cms/schema.ts +0 -1219
  22. package/src/cms/sectionLoaderContext.ts +0 -113
  23. package/src/cms/sectionLoaders.ts +0 -686
  24. package/src/cms/sectionMixins.ts +0 -153
  25. package/src/flags/audience.ts +0 -56
  26. package/src/flags/everyone.ts +0 -29
  27. package/src/flags/flag.ts +0 -15
  28. package/src/flags/multivariate/image.ts +0 -11
  29. package/src/flags/multivariate/message.ts +0 -11
  30. package/src/flags/multivariate/page.ts +0 -16
  31. package/src/flags/multivariate/section.ts +0 -16
  32. package/src/flags/multivariate.ts +0 -1
  33. package/src/flags/types.ts +0 -59
  34. package/src/flags/utils/multivariate.ts +0 -20
  35. package/src/hooks/Image.tsx +0 -314
  36. package/src/hooks/JsonLd.tsx +0 -353
  37. package/src/hooks/LazySection.tsx +0 -121
  38. package/src/hooks/LiveControls.tsx +0 -144
  39. package/src/hooks/Picture.tsx +0 -127
  40. package/src/hooks/RenderSection.tsx +0 -91
  41. package/src/hooks/SectionErrorFallback.tsx +0 -97
  42. package/src/hooks/Stats.tsx +0 -134
  43. package/src/hooks/index.ts +0 -35
  44. package/src/hooks/useLoadMore.ts +0 -100
  45. package/src/matchers/builtins.ts +0 -459
  46. package/src/matchers/countryNames.ts +0 -104
  47. package/src/matchers/override.ts +0 -136
  48. package/src/matchers/posthog.ts +0 -154
  49. package/src/middleware/decoState.ts +0 -55
  50. package/src/middleware/healthMetrics.ts +0 -133
  51. package/src/middleware/hydrationContext.ts +0 -79
  52. package/src/middleware/index.ts +0 -88
  53. package/src/middleware/liveness.ts +0 -21
  54. package/src/middleware/observability.ts +0 -800
  55. package/src/middleware/validateSection.ts +0 -100
  56. package/src/preview/DraftPreviewBadge.tsx +0 -306
  57. package/src/preview/decoMark.ts +0 -11
  58. package/src/preview/index.ts +0 -8
  59. package/src/sdk/abTesting.ts +0 -556
  60. package/src/sdk/analytics.ts +0 -147
  61. package/src/sdk/cacheHeaders.ts +0 -633
  62. package/src/sdk/cacheStorage.ts +0 -287
  63. package/src/sdk/cachedLoader.ts +0 -556
  64. package/src/sdk/clx.ts +0 -5
  65. package/src/sdk/cn.ts +0 -28
  66. package/src/sdk/composite.ts +0 -114
  67. package/src/sdk/cookie.ts +0 -129
  68. package/src/sdk/crypto.ts +0 -199
  69. package/src/sdk/csp.ts +0 -59
  70. package/src/sdk/deepOmit.ts +0 -52
  71. package/src/sdk/detectDevice.ts +0 -45
  72. package/src/sdk/djb2.ts +0 -20
  73. package/src/sdk/encoding.ts +0 -44
  74. package/src/sdk/env.ts +0 -54
  75. package/src/sdk/experiments.ts +0 -314
  76. package/src/sdk/fetchCache.ts +0 -269
  77. package/src/sdk/fetchTimeout.ts +0 -58
  78. package/src/sdk/flags.ts +0 -138
  79. package/src/sdk/http.ts +0 -124
  80. package/src/sdk/index.ts +0 -97
  81. package/src/sdk/inflightTimeout.ts +0 -53
  82. package/src/sdk/instrumentedFetch.ts +0 -362
  83. package/src/sdk/invoke.ts +0 -298
  84. package/src/sdk/logger.ts +0 -304
  85. package/src/sdk/mergeCacheControl.ts +0 -150
  86. package/src/sdk/nonce.ts +0 -46
  87. package/src/sdk/normalizeUrls.ts +0 -91
  88. package/src/sdk/observability.ts +0 -116
  89. package/src/sdk/otel.ts +0 -1118
  90. package/src/sdk/otelAdapters/clickhouseCollector.ts +0 -65
  91. package/src/sdk/otelAdapters.ts +0 -144
  92. package/src/sdk/otelHttpLog.ts +0 -419
  93. package/src/sdk/otelHttpMeter.ts +0 -532
  94. package/src/sdk/otelHttpTracer.ts +0 -543
  95. package/src/sdk/redirects.ts +0 -336
  96. package/src/sdk/requestContext.ts +0 -281
  97. package/src/sdk/requestContextStorage.browser.ts +0 -43
  98. package/src/sdk/requestContextStorage.ts +0 -35
  99. package/src/sdk/responseCache.ts +0 -82
  100. package/src/sdk/retry.ts +0 -45
  101. package/src/sdk/serverTimings.ts +0 -68
  102. package/src/sdk/signal.ts +0 -42
  103. package/src/sdk/sitemap.ts +0 -160
  104. package/src/sdk/urlRedaction.ts +0 -82
  105. package/src/sdk/urlUtils.ts +0 -183
  106. package/src/sdk/useDevice.ts +0 -89
  107. package/src/sdk/useDeviceContext.tsx +0 -108
  108. package/src/sdk/useId.ts +0 -7
  109. package/src/sdk/useScript.ts +0 -210
  110. package/src/sdk/useSuggestions.ts +0 -188
  111. package/src/sdk/wrapCaughtErrors.ts +0 -107
  112. package/src/setup.ts +0 -120
  113. package/src/types/index.ts +0 -53
  114. package/src/types/widgets.ts +0 -14
@@ -1,65 +0,0 @@
1
- /**
2
- * Placeholder for a future OTel-collector based exporter that ships
3
- * logs / metrics / traces from this Worker to a co-deployed OTel Collector
4
- * gateway, which in turn batches into ClickHouse.
5
- *
6
- * **NOT IMPLEMENTED in 5.0.0.** The framework today defaults to CF-native
7
- * observability: logs and traces appear in the Cloudflare dashboard
8
- * (`observability.{logs,traces}` in `wrangler.jsonc`), and metrics go to
9
- * Workers Analytics Engine. When the platform-side ClickHouse + collector
10
- * deployment lands, this file gets the real OTLP/HTTP exporter wiring back
11
- * — mirror the implementation that lived in `src/sdk/otelAdapters.ts` and
12
- * `src/sdk/sampler.ts` at the pre-5.0.0 git tag.
13
- *
14
- * The expected shape (`endpoint`, optional `headers`, optional `resource`,
15
- * `exportIntervalMillis`) is recorded here so site-side bootstrapping code
16
- * can compile against the future surface today and we don't churn the
17
- * `OtelOptions` type when the implementation lands.
18
- *
19
- * Reference: https://clickhouse.com/docs/observability/integrating-opentelemetry
20
- *
21
- * Expected wiring once implemented:
22
- * - Worker → OTLP/HTTP → OTel Collector (gateway, per region)
23
- * - Collector pipelines:
24
- * receivers[otlp] → processors[batch, memory_limiter] → exporters[clickhouse]
25
- * - The Worker uses the collector URL as `endpoint`, NOT a direct
26
- * ClickHouse URL. The collector owns the credentials, retry policy,
27
- * and schema mapping.
28
- */
29
-
30
- export interface ClickhouseCollectorOptions {
31
- /**
32
- * OTel Collector OTLP/HTTP endpoint, e.g. `https://otel-collector.internal`.
33
- * The Worker never talks to ClickHouse directly — it talks to the collector.
34
- */
35
- endpoint: string;
36
- /** Optional bearer token / mTLS identity headers — collector-defined. */
37
- headers?: Record<string, string>;
38
- /**
39
- * Resource attributes to stamp on every emitted record. Keep narrow
40
- * (`service.name`, `deco.runtime.version`, `deployment.environment`) —
41
- * higher-cardinality attributes belong on individual spans / log records.
42
- */
43
- resource?: Record<string, string>;
44
- /** Periodic metric reader push interval in ms. Defaults to 60_000 once wired. */
45
- exportIntervalMillis?: number;
46
- }
47
-
48
- /**
49
- * Returns nothing today — throws to make accidental usage loud and obvious.
50
- *
51
- * The intended return shape is `{ logger: LoggerAdapter, meter: MeterAdapter,
52
- * tracer: TracerAdapter, flush: () => Promise<void> }` so the wiring inside
53
- * `instrumentWorker` can compose this with the AE meter (dual-emit) and the
54
- * default console logger. None of that ships in 5.0.0.
55
- */
56
- export function createClickhouseCollectorAdapter(_options: ClickhouseCollectorOptions): never {
57
- throw new Error(
58
- "createClickhouseCollectorAdapter is not implemented in @decocms/start@5.x. " +
59
- "ClickHouse + OTel-collector integration is on the roadmap. " +
60
- "Until then, use Cloudflare-native observability " +
61
- "(observability.{logs,traces} in wrangler.jsonc) plus the Workers " +
62
- "Analytics Engine meter wired by instrumentWorker(). Track progress " +
63
- "at https://github.com/decocms/blocks/issues.",
64
- );
65
- }
@@ -1,144 +0,0 @@
1
- /**
2
- * Metric adapters for `@decocms/start`.
3
- *
4
- * The OTLP logger and OTLP meter adapters that lived here through 4.x were
5
- * removed in 5.0.0. The framework now relies on:
6
- * - Cloudflare's platform-managed log capture (`console.*` → CF dashboard
7
- * via `observability.logs`) for log shipping
8
- * - Cloudflare's auto-instrumented + global-tracer-bridged trace export
9
- * (`observability.traces`) for trace shipping
10
- * - Workers Analytics Engine — implemented in this file — for metrics
11
- *
12
- * Forwarding to a co-deployed OTel collector (the path to ClickHouse)
13
- * lives in `./otelAdapters/clickhouseCollector.ts`. That file is a
14
- * documented stub today; the implementation will reintroduce a thin
15
- * OTLP/HTTP exporter aimed at the collector — never directly at a
16
- * destination — when the collector is deployed.
17
- *
18
- * The `setRuntimeEnv` / `getRuntimeEnv` helpers stay here because
19
- * `workerEntry.ts` and the AE adapter both depend on them.
20
- */
21
-
22
- import type { MeterAdapter } from "../middleware/observability";
23
- import { MetricNames } from "../middleware/observability";
24
- import { RequestContext } from "./requestContext";
25
-
26
- // ---------------------------------------------------------------------------
27
- // Env / binding access
28
- // ---------------------------------------------------------------------------
29
-
30
- /**
31
- * Per-request access to the Cloudflare Worker `env` bag. Stashed by
32
- * `workerEntry.ts` at the top of every request via
33
- * `RequestContext.setBag("__deco_env", env)`.
34
- *
35
- * Adapters use this to look up the AE binding (or any future binding-driven
36
- * destination) without forcing site code to thread `env` through every
37
- * call site.
38
- */
39
- const ENV_BAG_KEY = "__deco_env";
40
-
41
- export function setRuntimeEnv(env: Record<string, unknown>): void {
42
- RequestContext.setBag(ENV_BAG_KEY, env);
43
- }
44
-
45
- export function getRuntimeEnv(): Record<string, unknown> | undefined {
46
- return RequestContext.getBag<Record<string, unknown>>(ENV_BAG_KEY);
47
- }
48
-
49
- // ---------------------------------------------------------------------------
50
- // AnalyticsEngineMeterAdapter
51
- // ---------------------------------------------------------------------------
52
-
53
- /**
54
- * Workers Analytics Engine binding shape. Defined here so we don't need
55
- * `@cloudflare/workers-types` as a dep.
56
- */
57
- export interface AnalyticsEngineDataset {
58
- writeDataPoint(point: { indexes?: string[]; blobs?: string[]; doubles?: number[] }): void;
59
- }
60
-
61
- export interface AnalyticsEngineMeterAdapterOptions {
62
- /** Env var name holding the AE binding. Defaults to "DECO_METRICS". */
63
- bindingName?: string;
64
- /** Pre-resolved binding (mainly for tests). Bypasses RequestContext lookup. */
65
- binding?: AnalyticsEngineDataset;
66
- }
67
-
68
- /**
69
- * Writes one Analytics Engine data point per metric call.
70
- *
71
- * Schema (must stay stable — dashboards and SQL queries depend on it):
72
- * - `indexes[0]`: a low-cardinality dimension. For request metrics that's
73
- * the normalized URL path; for cache/resolve metrics that's the metric
74
- * name itself.
75
- * - `blobs[0]`: metric name (so a single dataset can hold many metrics).
76
- * - `blobs[1..]`: stringified label values, in stable label-name order.
77
- * - `doubles[0]`: the metric value (count, ms, gauge value).
78
- *
79
- * AE truncates / charges per data point, so this adapter is intentionally
80
- * coarse: one point per `counterInc` / `histogramRecord` / `gaugeSet`.
81
- */
82
- export function createAnalyticsEngineMeterAdapter(
83
- options: AnalyticsEngineMeterAdapterOptions = {},
84
- ): MeterAdapter {
85
- const bindingName = options.bindingName ?? "DECO_METRICS";
86
-
87
- function resolveBinding(): AnalyticsEngineDataset | null {
88
- if (options.binding) return options.binding;
89
- const env = getRuntimeEnv();
90
- const b = env?.[bindingName] as AnalyticsEngineDataset | undefined;
91
- return b ?? null;
92
- }
93
-
94
- function write(name: string, value: number, labels?: Record<string, unknown>): void {
95
- const binding = resolveBinding();
96
- if (!binding) return; // No-op when binding is missing — never throw.
97
-
98
- const indexValue = pickIndex(name, labels);
99
- const blobs: string[] = [name];
100
- if (labels) {
101
- // Stable order — sort keys so the same blob index always means the
102
- // same label across requests. AE has no schema; we enforce one here.
103
- const keys = Object.keys(labels).sort();
104
- for (const k of keys) {
105
- const v = labels[k];
106
- if (v !== undefined && v !== null) blobs.push(String(v));
107
- }
108
- }
109
-
110
- try {
111
- binding.writeDataPoint({
112
- indexes: indexValue ? [indexValue] : undefined,
113
- blobs,
114
- doubles: [value],
115
- });
116
- } catch {
117
- /* AE write failed — never fail the request */
118
- }
119
- }
120
-
121
- return {
122
- counterInc(name, value, labels) {
123
- write(name, value ?? 1, labels);
124
- },
125
- histogramRecord(name, value, labels) {
126
- write(name, value, labels);
127
- },
128
- gaugeSet(name, value, labels) {
129
- write(name, value, labels);
130
- },
131
- };
132
- }
133
-
134
- function pickIndex(metricName: string, labels?: Record<string, unknown>): string | undefined {
135
- if (!labels) return metricName;
136
- // For HTTP request metrics the natural index is the path (matches the
137
- // schema: `indexes[0]=path`). For other metrics, fall back to the metric
138
- // name so the dataset is always queryable by index.
139
- if (metricName === MetricNames.HTTP_SERVER_REQUEST_DURATION) {
140
- const p = labels.path;
141
- if (typeof p === "string" && p.length > 0) return p;
142
- }
143
- return metricName;
144
- }
@@ -1,419 +0,0 @@
1
- /**
2
- * Direct OTLP/HTTP JSON error-log exporter.
3
- *
4
- * Errors are too important to leave to head sampling. When CF Destinations'
5
- * `logs.head_sampling_rate` drops below 1.0 (the cost model in
6
- * `docs/observability.md` lowers it to `0.01` once this channel lands),
7
- * a 1%-sampled log pipe would lose 99 of every 100 errors emitted by
8
- * `logger.error(...)`. Sites lose the one signal they care about most.
9
- *
10
- * This adapter solves it by carrying `level: "error"` log records over a
11
- * separate, framework-controlled pipe direct to `deco-otel-ingest`
12
- * `/v1/logs`, the same endpoint CF Destinations targets. The two pipes
13
- * write to the same `otel_logs` table — the ingestor doesn't know or
14
- * care which transport the record arrived on, and dashboards / SQL
15
- * queries are unchanged.
16
- *
17
- * Design:
18
- *
19
- * - Filters out `debug`/`info`/`warn` upstream of the buffer — only
20
- * errors travel through this transport. Calls below "error" are a
21
- * no-op on this adapter; the composite logger fans them out to the
22
- * default console-JSON adapter which CF Destinations then samples
23
- * according to `logs.head_sampling_rate`.
24
- * - Per-isolate token-bucket rate limiter prevents log storms (a
25
- * pathological error loop blasting 10K errors/sec into the ingestor
26
- * would be a self-inflicted denial-of-service against stats-lake).
27
- * Default: 100 errors per minute, burst capacity of 20.
28
- * - Buffer + flush + cooldown identical to `otelHttpMeter.ts`. The
29
- * same `ctx.waitUntil(flush())` in `instrumentWorker` drains both.
30
- * - Wire format: OTLP/HTTP JSON `ResourceLogs` payload matching the
31
- * shape CF Destinations produces, so the existing ingestor handler
32
- * accepts both transports unchanged.
33
- *
34
- * Why a separate adapter and not a sampling-bypass flag on
35
- * `defaultLoggerAdapter`: the default writes via `console.error` which
36
- * is what CF Destinations samples. Bypassing CF Destinations means
37
- * NOT writing via `console.*` — a different transport, different
38
- * code path, different failure modes. Better as a dedicated adapter
39
- * composed onto the active logger via `createCompositeLogger`.
40
- */
41
-
42
- import { getActiveSpan } from "./observability";
43
- import type { LoggerAdapter, LogLevel } from "./logger";
44
-
45
- // ---------------------------------------------------------------------------
46
- // Types
47
- // ---------------------------------------------------------------------------
48
-
49
- export interface OtlpHttpLogOptions {
50
- /** Full OTLP/HTTP JSON logs endpoint, e.g. `https://.../v1/logs`. */
51
- endpoint: string;
52
- /** Resource attributes stamped on every payload (service.name etc.). */
53
- resourceAttributes: Record<string, string>;
54
- /** Scope name advertised in `scopeLogs[].scope.name`. */
55
- scopeName?: string;
56
- /** Scope version. */
57
- scopeVersion?: string;
58
- /** Hard cap on pending log records. Default: 500. */
59
- maxBufferRecords?: number;
60
- /** Cooldown between successful flushes (ms). Default: 5000. */
61
- minFlushIntervalMs?: number;
62
- /** Per-flush HTTP timeout (ms). Default: 5000. */
63
- flushTimeoutMs?: number;
64
- /**
65
- * Token-bucket parameters. The bucket holds up to `burstCapacity`
66
- * tokens and refills at `refillPerMinute` per minute. Each log record
67
- * costs one token. When the bucket is empty, records are dropped (with
68
- * `onError("rate-limit", ...)`) until refill resumes.
69
- * Defaults: 500 burst, 1000/min — sized for console monkey-patch, which
70
- * routes ALL console.* (including third-party libs at boot) through this
71
- * adapter. The old 20/100 defaults were sized for error-only traffic.
72
- */
73
- rateLimitBurstCapacity?: number;
74
- rateLimitRefillPerMinute?: number;
75
- /**
76
- * Minimum log level that this adapter forwards to OTLP. Levels below
77
- * this threshold are silently dropped at ingress. Defaults to `"error"`
78
- * — production-safe (only errors travel direct-POST; info/warn flow
79
- * through CF Destinations sampling). Set to `"info"` or `"debug"` in
80
- * local dev to capture lower-severity logs in the same channel.
81
- * Resolved from `DECO_OTEL_LOGS_MIN_LEVEL` in `bootObservability`.
82
- */
83
- minLevel?: LogLevel;
84
- /**
85
- * Extra HTTP headers merged into every OTLP POST (e.g. `Authorization: Bearer …`).
86
- * `Content-Type: application/json` is always set and cannot be overridden here.
87
- */
88
- headers?: Record<string, string>;
89
- /** Test seam — override fetch. */
90
- fetchImpl?: typeof fetch;
91
- /** Test seam — override Date.now(). */
92
- nowMs?: () => number;
93
- /**
94
- * Test seam — override getActiveSpan. Production code uses the
95
- * framework's AsyncLocalStorage-backed getActiveSpan automatically.
96
- */
97
- getActiveSpanFn?: () => { spanContext?: () => { traceId?: string; spanId?: string; traceFlags?: number } } | undefined | null;
98
- /** Optional sink for transport / rate-limit / overflow errors. */
99
- onError?: (kind: "flush" | "overflow" | "rate-limit", err: unknown) => void;
100
- /**
101
- * Called when an `error`-level log fires with an active trace context.
102
- * Used by the caller to promote unsampled traces so their spans are
103
- * exported regardless of head-sampling decisions.
104
- */
105
- promoteTrace?: (traceId: string) => void;
106
- }
107
-
108
- export interface OtlpHttpLog {
109
- adapter: LoggerAdapter;
110
- /** Flush, subject to the per-isolate cooldown unless `force` is true. */
111
- flush(force?: boolean): Promise<void>;
112
- /** Pending log record count. For tests + audit. */
113
- pendingRecordCount(): number;
114
- }
115
-
116
- // ---------------------------------------------------------------------------
117
- // Internal types
118
- // ---------------------------------------------------------------------------
119
-
120
- interface PendingRecord {
121
- timeUnixNano: string;
122
- observedTimeUnixNano: string;
123
- severityNumber: number;
124
- severityText: string;
125
- body: string;
126
- attributes: Record<string, unknown>;
127
- traceId: string;
128
- spanId: string;
129
- traceFlags: number;
130
- }
131
-
132
- // OTel SeverityNumber values, OTel spec. See
133
- // https://opentelemetry.io/docs/specs/otel/logs/data-model/#field-severitynumber
134
- const SEVERITY_NUMBER: Record<LogLevel, number> = {
135
- debug: 5,
136
- info: 9,
137
- warn: 13,
138
- error: 17,
139
- };
140
-
141
- // ---------------------------------------------------------------------------
142
- // Factory
143
- // ---------------------------------------------------------------------------
144
-
145
- export function createOtlpHttpLogAdapter(options: OtlpHttpLogOptions): OtlpHttpLog {
146
- const endpoint = options.endpoint;
147
- const resourceAttributes = options.resourceAttributes;
148
- const scopeName = options.scopeName ?? "@decocms/start";
149
- const scopeVersion = options.scopeVersion ?? "";
150
- const maxBuffer = options.maxBufferRecords ?? 500;
151
- const minFlushIntervalMs = options.minFlushIntervalMs ?? 5000;
152
- const flushTimeoutMs = options.flushTimeoutMs ?? 5000;
153
- const burstCapacity = options.rateLimitBurstCapacity ?? 500;
154
- const refillPerMinute = options.rateLimitRefillPerMinute ?? 1000;
155
- const minSeverity = SEVERITY_NUMBER[options.minLevel ?? "error"];
156
- const extraHeaders = options.headers ?? {};
157
- const fetchImpl = options.fetchImpl ?? fetch;
158
- const now = options.nowMs ?? (() => Date.now());
159
- const onError = options.onError;
160
- const promoteTrace = options.promoteTrace;
161
- const getSpan = options.getActiveSpanFn ?? getActiveSpan;
162
-
163
- const buffer: PendingRecord[] = [];
164
- // Token bucket — starts full.
165
- let tokens = burstCapacity;
166
- let tokensLastRefilledAt = now();
167
-
168
- let lastFlushAt = 0;
169
- let inflight: Promise<void> | null = null;
170
-
171
- function refillTokens(): void {
172
- const t = now();
173
- const elapsedMs = t - tokensLastRefilledAt;
174
- if (elapsedMs <= 0) return;
175
- // refillPerMinute tokens per 60_000ms.
176
- const refill = (elapsedMs / 60_000) * refillPerMinute;
177
- tokens = Math.min(burstCapacity, tokens + refill);
178
- tokensLastRefilledAt = t;
179
- }
180
-
181
- function tryConsumeToken(): boolean {
182
- refillTokens();
183
- if (tokens < 1) return false;
184
- tokens -= 1;
185
- return true;
186
- }
187
-
188
- const adapter: LoggerAdapter = {
189
- log(level, msg, attrs) {
190
- // Filter by severity threshold. Levels below minLevel are dropped.
191
- if (SEVERITY_NUMBER[level] < minSeverity) return;
192
-
193
- // No SDK-level sampling for info/debug. Volume is controlled by
194
- // minLevel (severity threshold) here, and by the probabilistic
195
- // sampler in the otel-ingest collector — keeping log and trace
196
- // sampling pipelines fully independent per OTel spec.
197
-
198
- if (!tryConsumeToken()) {
199
- onError?.(
200
- "rate-limit",
201
- new Error(`rate limit exceeded: ${refillPerMinute}/min, burst ${burstCapacity}`),
202
- );
203
- return;
204
- }
205
-
206
- if (buffer.length >= maxBuffer) {
207
- onError?.(
208
- "overflow",
209
- new Error(`error-log buffer at cap (${maxBuffer}) — dropping record`),
210
- );
211
- return;
212
- }
213
-
214
- const spanCtx = getSpan()?.spanContext?.();
215
- const t = msToNs(now());
216
- buffer.push({
217
- timeUnixNano: t,
218
- observedTimeUnixNano: t,
219
- severityNumber: SEVERITY_NUMBER[level],
220
- severityText: level,
221
- body: msg,
222
- attributes: attrs ? { ...attrs } : {},
223
- traceId: spanCtx?.traceId ?? "",
224
- spanId: spanCtx?.spanId ?? "",
225
- traceFlags: spanCtx?.traceFlags ?? 0,
226
- });
227
-
228
- if (level === "error" && spanCtx?.traceId) {
229
- promoteTrace?.(spanCtx.traceId);
230
- }
231
- },
232
- };
233
-
234
- async function doFlush(): Promise<void> {
235
- if (buffer.length === 0) return;
236
- // Snapshot + remove BEFORE the network call so concurrent records
237
- // landing during the POST don't get reset on success. On failure we
238
- // restore the snapshot to the FRONT of the buffer (preserving order
239
- // and prioritizing the originating-error context). The `maxBuffer`
240
- // cap on the `log()` path still bounds total memory if the endpoint
241
- // stays down — newest records are dropped via `onError("overflow")`.
242
- const snapshot = buffer.splice(0, buffer.length);
243
- const payload = serializeOtlp(snapshot, {
244
- resourceAttributes,
245
- scopeName,
246
- scopeVersion,
247
- });
248
-
249
- const restoreOnFailure = () => {
250
- // The snapshot was buffered BEFORE any records that landed during
251
- // the failed POST — its contents are older and more likely to be
252
- // the originating-error context operators care about. Restore
253
- // policy preserves the snapshot first, evicts the NEWEST records
254
- // when the cap is tight:
255
- //
256
- // 1. If snapshot + current buffer fit under the cap → unshift,
257
- // done.
258
- // 2. Otherwise, drop newest-tail records from the current buffer
259
- // (records that arrived during the failed POST). If that
260
- // suffices, unshift the full snapshot.
261
- // 3. Only if the snapshot ALONE exceeds the cap do we truncate
262
- // the snapshot — and even then, we keep its OLDEST end
263
- // (`snapshot.length = maxBuffer`) and drop the newest tail.
264
- //
265
- // This is the inverse of the original (buggy) restore which kept
266
- // newer records by truncating the older snapshot.
267
- const total = snapshot.length + buffer.length;
268
- if (total <= maxBuffer) {
269
- buffer.unshift(...snapshot);
270
- return;
271
- }
272
-
273
- let overflow = total - maxBuffer;
274
- let droppedFromBuffer = 0;
275
- if (buffer.length > 0 && overflow > 0) {
276
- droppedFromBuffer = Math.min(buffer.length, overflow);
277
- buffer.length -= droppedFromBuffer;
278
- overflow -= droppedFromBuffer;
279
- }
280
- let droppedFromSnapshot = 0;
281
- if (overflow > 0) {
282
- // Snapshot alone exceeds the cap. Keep the oldest `maxBuffer`
283
- // entries and drop the rest from the newest tail.
284
- droppedFromSnapshot = Math.min(snapshot.length, overflow);
285
- snapshot.length -= droppedFromSnapshot;
286
- }
287
- buffer.unshift(...snapshot);
288
-
289
- const parts = [`dropped ${droppedFromBuffer} newer records from buffer`];
290
- if (droppedFromSnapshot > 0) {
291
- parts.push(`${droppedFromSnapshot} newest-tail records from snapshot`);
292
- }
293
- onError?.(
294
- "overflow",
295
- new Error(
296
- `error-log buffer overflow on restore — ${parts.join(" and ")} (preserved ${snapshot.length} originating-error records)`,
297
- ),
298
- );
299
- };
300
-
301
- const controller = new AbortController();
302
- const timer = setTimeout(() => controller.abort(), flushTimeoutMs);
303
- try {
304
- const res = await fetchImpl(endpoint, {
305
- method: "POST",
306
- headers: { ...extraHeaders, "Content-Type": "application/json" },
307
- body: JSON.stringify(payload),
308
- signal: controller.signal,
309
- });
310
- if (!res.ok) {
311
- try {
312
- await res.text();
313
- } catch {
314
- /* swallow */
315
- }
316
- restoreOnFailure();
317
- onError?.("flush", new Error(`POST ${endpoint} → ${res.status}`));
318
- }
319
- } catch (err) {
320
- restoreOnFailure();
321
- onError?.("flush", err);
322
- } finally {
323
- clearTimeout(timer);
324
- }
325
- }
326
-
327
- async function flush(force = false): Promise<void> {
328
- if (inflight) return inflight;
329
-
330
- const elapsed = now() - lastFlushAt;
331
- const overCap = buffer.length >= maxBuffer;
332
- if (!force && !overCap && elapsed < minFlushIntervalMs) return;
333
-
334
- inflight = doFlush().finally(() => {
335
- lastFlushAt = now();
336
- inflight = null;
337
- });
338
- return inflight;
339
- }
340
-
341
- return {
342
- adapter,
343
- flush,
344
- pendingRecordCount: () => buffer.length,
345
- };
346
- }
347
-
348
- // ---------------------------------------------------------------------------
349
- // OTLP/HTTP JSON serialization
350
- // ---------------------------------------------------------------------------
351
-
352
- function msToNs(ms: number): string {
353
- return `${Math.floor(ms)}000000`;
354
- }
355
-
356
- function attrToOtlpValue(
357
- v: unknown,
358
- ):
359
- | { stringValue: string }
360
- | { intValue: string }
361
- | { doubleValue: number }
362
- | { boolValue: boolean } {
363
- if (typeof v === "string") return { stringValue: v };
364
- if (typeof v === "boolean") return { boolValue: v };
365
- if (typeof v === "number" && Number.isFinite(v)) {
366
- if (Number.isInteger(v)) return { intValue: String(v) };
367
- return { doubleValue: v };
368
- }
369
- // Fallback — JSON-stringify so structured attrs round-trip as strings.
370
- // `JSON.stringify` returns `undefined` for `undefined`, functions, and
371
- // symbols. OTLP requires `stringValue` to be a string, so coerce with
372
- // `String(v)` whenever serialization yields nothing parseable.
373
- try {
374
- const s = JSON.stringify(v);
375
- return { stringValue: typeof s === "string" ? s : String(v) };
376
- } catch {
377
- return { stringValue: String(v) };
378
- }
379
- }
380
-
381
- interface SerializeOpts {
382
- resourceAttributes: Record<string, string>;
383
- scopeName: string;
384
- scopeVersion: string;
385
- }
386
-
387
- function serializeOtlp(records: PendingRecord[], opts: SerializeOpts): { resourceLogs: unknown[] } {
388
- const otlpRecords = records.map((r) => ({
389
- timeUnixNano: r.timeUnixNano,
390
- observedTimeUnixNano: r.observedTimeUnixNano,
391
- severityNumber: r.severityNumber,
392
- severityText: r.severityText,
393
- body: { stringValue: r.body },
394
- attributes: Object.entries(r.attributes)
395
- .filter(([_, v]) => v !== undefined && v !== null)
396
- .map(([k, v]) => ({ key: k, value: attrToOtlpValue(v) })),
397
- ...(r.traceId ? { traceId: r.traceId } : {}),
398
- ...(r.spanId ? { spanId: r.spanId } : {}),
399
- ...(r.traceFlags ? { flags: r.traceFlags } : {}),
400
- }));
401
-
402
- const resourceAttrs = Object.entries(opts.resourceAttributes)
403
- .sort(([a], [b]) => a.localeCompare(b))
404
- .map(([k, v]) => ({ key: k, value: { stringValue: v } }));
405
-
406
- return {
407
- resourceLogs: [
408
- {
409
- resource: { attributes: resourceAttrs },
410
- scopeLogs: [
411
- {
412
- scope: { name: opts.scopeName, version: opts.scopeVersion },
413
- logRecords: otlpRecords,
414
- },
415
- ],
416
- },
417
- ],
418
- };
419
- }