@krak-stack/registry 0.1.49 → 0.1.51
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/CHANGELOG.md +23 -0
- package/README.md +16 -424
- package/dist/components/ui/data-table.js +25 -15
- package/dist/lib/docs.js +2 -2
- package/dist/services/agent/client/index.js +1 -1
- package/dist/services/events/browser.d.ts +26 -0
- package/dist/services/events/browser.js +423 -0
- package/dist/services/events/index.d.ts +82 -0
- package/dist/services/events/index.js +798 -0
- package/dist/services/events/migrations.d.ts +4 -0
- package/dist/services/events/migrations.js +42 -0
- package/dist/services/events/request-context.d.ts +6 -0
- package/dist/services/events/schema.d.ts +196 -0
- package/dist/services/opentelemetry/api.d.ts +3 -1
- package/dist/services/opentelemetry/api.js +133 -64
- package/dist/services/opentelemetry/browser.d.ts +17 -20
- package/dist/services/opentelemetry/browser.js +58 -162
- package/dist/services/opentelemetry/events.d.ts +3 -1
- package/dist/services/opentelemetry/{server.d.ts → index.d.ts} +8 -5
- package/dist/services/opentelemetry/{server.js → index.js} +66 -48
- package/dist/services/opentelemetry/processor.d.ts +19 -0
- package/dist/services/opentelemetry/schema.d.ts +8 -20
- package/dist/services/opentelemetry/web-vitals.d.ts +15 -0
- package/dist/services/opentelemetry/web-vitals.js +229 -0
- package/package.json +23 -5
|
@@ -1,15 +1,5 @@
|
|
|
1
1
|
// ../../src/services/opentelemetry/browser.ts
|
|
2
|
-
import {
|
|
3
|
-
Context as Context2,
|
|
4
|
-
Effect as Effect2,
|
|
5
|
-
FiberSet,
|
|
6
|
-
Layer as Layer2,
|
|
7
|
-
Predicate,
|
|
8
|
-
Ref,
|
|
9
|
-
References as References2,
|
|
10
|
-
Schema as Schema3,
|
|
11
|
-
Stream
|
|
12
|
-
} from "effect";
|
|
2
|
+
import { Effect as Effect2, FiberSet, Layer as Layer2, Predicate, Schema as Schema3, Stream } from "effect";
|
|
13
3
|
import { FetchHttpClient, HttpClientRequest } from "effect/http";
|
|
14
4
|
import {
|
|
15
5
|
OtlpExporter as OtlpExporter2,
|
|
@@ -38,6 +28,21 @@ import {
|
|
|
38
28
|
|
|
39
29
|
// ../../src/services/opentelemetry/schema.ts
|
|
40
30
|
import { Schema } from "effect";
|
|
31
|
+
var BrowserWebVital = Schema.Struct({
|
|
32
|
+
name: Schema.Literals(["LCP", "INP", "CLS", "FCP", "TTFB"]),
|
|
33
|
+
id: Schema.NonEmptyString.check(Schema.isMaxLength(64)),
|
|
34
|
+
value: Schema.Finite.check(Schema.isGreaterThanOrEqualTo(0)),
|
|
35
|
+
delta: Schema.Finite,
|
|
36
|
+
rating: Schema.Literals(["good", "needs-improvement", "poor"]),
|
|
37
|
+
navigationType: Schema.Literals([
|
|
38
|
+
"navigate",
|
|
39
|
+
"reload",
|
|
40
|
+
"back-forward",
|
|
41
|
+
"back-forward-cache",
|
|
42
|
+
"prerender",
|
|
43
|
+
"restore"
|
|
44
|
+
])
|
|
45
|
+
}).annotate({ identifier: "BrowserWebVital" });
|
|
41
46
|
var EventAttributeValue = Schema.Union([
|
|
42
47
|
Schema.String,
|
|
43
48
|
Schema.Finite,
|
|
@@ -60,17 +65,6 @@ var TelemetryEvent = Schema.Struct({
|
|
|
60
65
|
properties: Schema.optional(EventAttributesSchema),
|
|
61
66
|
options: Schema.optional(EventOptionsSchema)
|
|
62
67
|
}).annotate({ identifier: "TelemetryEvent" });
|
|
63
|
-
var PageEvent = Schema.Struct({
|
|
64
|
-
name: Schema.optional(Schema.NonEmptyString),
|
|
65
|
-
category: Schema.optional(Schema.NonEmptyString),
|
|
66
|
-
path: Schema.optional(Schema.NonEmptyString),
|
|
67
|
-
properties: Schema.optional(EventAttributesSchema),
|
|
68
|
-
options: Schema.optional(EventOptionsSchema)
|
|
69
|
-
}).annotate({ identifier: "PageEvent" });
|
|
70
|
-
var BrowserIdentity = Schema.Struct({
|
|
71
|
-
userId: Schema.optional(Schema.NonEmptyString),
|
|
72
|
-
organizationId: Schema.optional(Schema.NonEmptyString)
|
|
73
|
-
}).annotate({ identifier: "BrowserTelemetryIdentity" });
|
|
74
68
|
|
|
75
69
|
// ../../src/services/opentelemetry/events.ts
|
|
76
70
|
class OtlpEvents extends Context.Service()("krakstack/opentelemetry/Events") {
|
|
@@ -81,27 +75,36 @@ class OtlpEvents extends Context.Service()("krakstack/opentelemetry/Events") {
|
|
|
81
75
|
return yield* OtlpExporter.make({
|
|
82
76
|
...options,
|
|
83
77
|
label: "OtlpEvents",
|
|
84
|
-
body: (logRecords) =>
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
78
|
+
body: (logRecords) => {
|
|
79
|
+
const scopes = new Map;
|
|
80
|
+
for (const { scopeName, ...record } of logRecords) {
|
|
81
|
+
const name = scopeName ?? options.scopeName ?? "krakstack.events";
|
|
82
|
+
const records = scopes.get(name);
|
|
83
|
+
if (records)
|
|
84
|
+
records.push(record);
|
|
85
|
+
else
|
|
86
|
+
scopes.set(name, [record]);
|
|
87
|
+
}
|
|
88
|
+
return [
|
|
89
|
+
serialization.logs({
|
|
90
|
+
resourceLogs: [
|
|
91
|
+
{
|
|
92
|
+
resource,
|
|
93
|
+
scopeLogs: Array.from(scopes, ([name, records]) => ({
|
|
94
|
+
scope: { name },
|
|
95
|
+
logRecords: records
|
|
96
|
+
}))
|
|
97
|
+
}
|
|
98
|
+
]
|
|
99
|
+
}),
|
|
100
|
+
Effect.void
|
|
101
|
+
];
|
|
102
|
+
}
|
|
100
103
|
});
|
|
101
104
|
}));
|
|
102
105
|
}
|
|
103
|
-
var
|
|
104
|
-
const
|
|
106
|
+
var event = Effect.fnUntraced(function* (name, properties, options) {
|
|
107
|
+
const decoded = yield* Schema2.decodeUnknownEffect(TelemetryEvent)({
|
|
105
108
|
name,
|
|
106
109
|
properties,
|
|
107
110
|
options
|
|
@@ -109,20 +112,20 @@ var track = Effect.fnUntraced(function* (name, properties, options) {
|
|
|
109
112
|
const events = yield* OtlpEvents;
|
|
110
113
|
const clock = yield* Clock.Clock;
|
|
111
114
|
const observedTime = clock.currentTimeNanosUnsafe().toString();
|
|
112
|
-
const timestamp =
|
|
115
|
+
const timestamp = decoded.options?.timestamp ? (BigInt(DateTime.toEpochMillis(decoded.options.timestamp)) * 1000000n).toString() : observedTime;
|
|
113
116
|
const span = yield* Effect.currentSpan.pipe(Effect.option);
|
|
114
|
-
const attributes = { ...
|
|
115
|
-
if (
|
|
116
|
-
attributes["user.id"] =
|
|
117
|
-
if (
|
|
118
|
-
attributes["analytics.anonymous_id"] =
|
|
119
|
-
if (
|
|
120
|
-
attributes["session.id"] =
|
|
121
|
-
if (
|
|
122
|
-
attributes["log.record.uid"] =
|
|
117
|
+
const attributes = { ...decoded.options?.context, ...decoded.properties };
|
|
118
|
+
if (decoded.options?.userId)
|
|
119
|
+
attributes["user.id"] = decoded.options.userId;
|
|
120
|
+
if (decoded.options?.anonymousId)
|
|
121
|
+
attributes["analytics.anonymous_id"] = decoded.options.anonymousId;
|
|
122
|
+
if (decoded.options?.sessionId)
|
|
123
|
+
attributes["session.id"] = decoded.options.sessionId;
|
|
124
|
+
if (decoded.options?.messageId)
|
|
125
|
+
attributes["log.record.uid"] = decoded.options.messageId;
|
|
123
126
|
const annotations = yield* References.CurrentLogAnnotations;
|
|
124
127
|
events.push({
|
|
125
|
-
eventName:
|
|
128
|
+
eventName: decoded.name,
|
|
126
129
|
timeUnixNano: timestamp,
|
|
127
130
|
observedTimeUnixNano: observedTime,
|
|
128
131
|
severityNumber: 9,
|
|
@@ -137,10 +140,6 @@ var track = Effect.fnUntraced(function* (name, properties, options) {
|
|
|
137
140
|
});
|
|
138
141
|
|
|
139
142
|
// ../../src/services/opentelemetry/browser.ts
|
|
140
|
-
class BrowserIdentityState extends Context2.Service()("krakstack/opentelemetry/BrowserIdentity") {
|
|
141
|
-
static layer = Layer2.effect(this, Ref.make({}));
|
|
142
|
-
}
|
|
143
|
-
|
|
144
143
|
class BrowserReportedError extends Schema3.TaggedError()("BrowserReportedError", {
|
|
145
144
|
type: Schema3.Literals(["window.error", "unhandledrejection"]),
|
|
146
145
|
message: Schema3.String,
|
|
@@ -197,124 +196,21 @@ var BrowserLifecycleFlushingLive = Layer2.effectDiscard(Effect2.gen(function* ()
|
|
|
197
196
|
globalThis.removeEventListener("pagehide", flush);
|
|
198
197
|
}));
|
|
199
198
|
}));
|
|
200
|
-
var browserEffects = {
|
|
201
|
-
identify: Effect2.fnUntraced(function* (input) {
|
|
202
|
-
const identity = yield* Schema3.decodeUnknownEffect(BrowserIdentity)(input === null ? {} : input);
|
|
203
|
-
const state = yield* BrowserIdentityState;
|
|
204
|
-
yield* Ref.set(state, identity);
|
|
205
|
-
}),
|
|
206
|
-
track: Effect2.fnUntraced(function* (name, properties, options) {
|
|
207
|
-
const state = yield* BrowserIdentityState;
|
|
208
|
-
const identity = yield* Ref.get(state);
|
|
209
|
-
const annotations = {};
|
|
210
|
-
if (identity.userId)
|
|
211
|
-
annotations["user.id"] = identity.userId;
|
|
212
|
-
if (identity.organizationId)
|
|
213
|
-
annotations["organization.id"] = identity.organizationId;
|
|
214
|
-
const scoped = yield* References2.CurrentLogAnnotations;
|
|
215
|
-
yield* track(name, properties, options).pipe(Effect2.annotateLogs({ ...annotations, ...scoped }));
|
|
216
|
-
}),
|
|
217
|
-
page: Effect2.fnUntraced(function* (input = {}) {
|
|
218
|
-
const page = yield* Schema3.decodeUnknownEffect(PageEvent)(input);
|
|
219
|
-
const properties = { ...page.properties };
|
|
220
|
-
if (page.name)
|
|
221
|
-
properties["analytics.page.name"] = page.name;
|
|
222
|
-
if (page.category)
|
|
223
|
-
properties["analytics.page.category"] = page.category;
|
|
224
|
-
properties["url.path"] = (page.path ?? globalThis.location.pathname).split(/[?#]/, 1)[0] ?? "/";
|
|
225
|
-
yield* browserEffects.track("browser.page_view", properties, page.options);
|
|
226
|
-
})
|
|
227
|
-
};
|
|
228
|
-
var browserClients = [];
|
|
229
|
-
var runBrowser = (program) => Effect2.suspend(() => {
|
|
230
|
-
const client = browserClients.at(-1);
|
|
231
|
-
if (!client)
|
|
232
|
-
return Effect2.void;
|
|
233
|
-
return program.pipe(Effect2.provideService(BrowserIdentityState, client.identity), Effect2.provideService(OtlpEvents, client.events));
|
|
234
|
-
});
|
|
235
|
-
var BrowserClientLive = Layer2.effectDiscard(Effect2.gen(function* () {
|
|
236
|
-
const identity = yield* BrowserIdentityState;
|
|
237
|
-
const events = yield* OtlpEvents;
|
|
238
|
-
yield* Effect2.acquireRelease(Effect2.sync(() => {
|
|
239
|
-
const client = { identity, events };
|
|
240
|
-
browserClients.push(client);
|
|
241
|
-
return client;
|
|
242
|
-
}), (client) => Effect2.sync(() => {
|
|
243
|
-
const index = browserClients.indexOf(client);
|
|
244
|
-
if (index !== -1)
|
|
245
|
-
browserClients.splice(index, 1);
|
|
246
|
-
}));
|
|
247
|
-
}));
|
|
248
|
-
var pageViewListeners = new Set;
|
|
249
|
-
var stopPageViewTracking;
|
|
250
|
-
var subscribePageViews = (listener) => {
|
|
251
|
-
if (pageViewListeners.size === 0) {
|
|
252
|
-
const history = globalThis.history;
|
|
253
|
-
const pushState = history.pushState;
|
|
254
|
-
const replaceState = history.replaceState;
|
|
255
|
-
const notify = () => {
|
|
256
|
-
for (const onPage of pageViewListeners)
|
|
257
|
-
onPage();
|
|
258
|
-
};
|
|
259
|
-
const trackedPushState = (...args) => {
|
|
260
|
-
pushState.apply(history, args);
|
|
261
|
-
notify();
|
|
262
|
-
};
|
|
263
|
-
const trackedReplaceState = (...args) => {
|
|
264
|
-
replaceState.apply(history, args);
|
|
265
|
-
notify();
|
|
266
|
-
};
|
|
267
|
-
history.pushState = trackedPushState;
|
|
268
|
-
history.replaceState = trackedReplaceState;
|
|
269
|
-
globalThis.addEventListener("popstate", notify);
|
|
270
|
-
stopPageViewTracking = () => {
|
|
271
|
-
globalThis.removeEventListener("popstate", notify);
|
|
272
|
-
if (history.pushState === trackedPushState)
|
|
273
|
-
history.pushState = pushState;
|
|
274
|
-
if (history.replaceState === trackedReplaceState)
|
|
275
|
-
history.replaceState = replaceState;
|
|
276
|
-
};
|
|
277
|
-
}
|
|
278
|
-
pageViewListeners.add(listener);
|
|
279
|
-
return () => {
|
|
280
|
-
pageViewListeners.delete(listener);
|
|
281
|
-
if (pageViewListeners.size === 0) {
|
|
282
|
-
stopPageViewTracking?.();
|
|
283
|
-
stopPageViewTracking = undefined;
|
|
284
|
-
}
|
|
285
|
-
};
|
|
286
|
-
};
|
|
287
|
-
var BrowserPageViewsLive = Layer2.effectDiscard(Effect2.gen(function* () {
|
|
288
|
-
const fibers = yield* FiberSet.make();
|
|
289
|
-
const run = yield* FiberSet.runtime(fibers)();
|
|
290
|
-
let previousPath = globalThis.location.pathname;
|
|
291
|
-
yield* Effect2.acquireRelease(Effect2.sync(() => subscribePageViews(() => {
|
|
292
|
-
const path = globalThis.location.pathname;
|
|
293
|
-
if (path === previousPath)
|
|
294
|
-
return;
|
|
295
|
-
previousPath = path;
|
|
296
|
-
run(browserEffects.page({ path }).pipe(Effect2.ignore));
|
|
297
|
-
})), (unsubscribe) => Effect2.sync(unsubscribe));
|
|
298
|
-
yield* browserEffects.page({ path: previousPath }).pipe(Effect2.ignore);
|
|
299
|
-
}));
|
|
300
199
|
|
|
301
200
|
class BrowserOtlp {
|
|
302
|
-
static
|
|
303
|
-
static track = (name, properties, options) => runBrowser(browserEffects.track(name, properties, options));
|
|
304
|
-
static page = (options) => runBrowser(browserEffects.page(options));
|
|
305
|
-
static pageView = BrowserOtlp.page;
|
|
201
|
+
static event = event;
|
|
306
202
|
static layer = ({
|
|
307
203
|
attributes,
|
|
308
204
|
baseUrl = "/api/otel",
|
|
309
205
|
logs = false,
|
|
310
|
-
events =
|
|
206
|
+
events = {},
|
|
311
207
|
metrics = false,
|
|
312
208
|
serviceName,
|
|
313
209
|
serviceVersion,
|
|
314
210
|
traces = {},
|
|
315
211
|
headers
|
|
316
212
|
}) => {
|
|
317
|
-
const
|
|
213
|
+
const eventOptions = events === false ? undefined : events;
|
|
318
214
|
const resource = { attributes, serviceName, serviceVersion };
|
|
319
215
|
const request = HttpClientRequest.get(baseUrl);
|
|
320
216
|
const signalUrl = (signal) => HttpClientRequest.appendUrl(request, `/v1/${signal}`).url;
|
|
@@ -346,7 +242,7 @@ class BrowserOtlp {
|
|
|
346
242
|
resource,
|
|
347
243
|
headers
|
|
348
244
|
}) : Layer2.empty).pipe(Layer2.provideMerge(OtlpExporter2.layerFlusher), Layer2.provide(OtlpSerialization2.layerJson), Layer2.provide(FetchHttpClient.layer));
|
|
349
|
-
return Layer2.mergeAll(
|
|
245
|
+
return Layer2.mergeAll(BrowserLifecycleFlushingLive, traces !== false ? BrowserExceptionTrackingLive : Layer2.empty).pipe(Layer2.provideMerge(exporters));
|
|
350
246
|
};
|
|
351
247
|
}
|
|
352
248
|
export {
|
|
@@ -2,6 +2,7 @@ import { Context, DateTime, Effect, Layer, Schema } from "effect";
|
|
|
2
2
|
import { OtlpExporter, OtlpResource, OtlpSerialization } from "effect/observability";
|
|
3
3
|
type ExporterOptions = Pick<Parameters<typeof OtlpExporter.make>[0], "url" | "headers" | "exportInterval" | "maxBatchSize" | "shutdownTimeout"> & {
|
|
4
4
|
readonly resource?: Parameters<typeof OtlpResource.fromConfig>[0];
|
|
5
|
+
readonly scopeName?: string;
|
|
5
6
|
};
|
|
6
7
|
export type EventExportOptions = Partial<Pick<ExporterOptions, "exportInterval" | "maxBatchSize" | "shutdownTimeout">>;
|
|
7
8
|
declare const OtlpEvents_base: Context.ServiceClass<OtlpEvents, "krakstack/opentelemetry/Events", {
|
|
@@ -12,6 +13,7 @@ export declare class OtlpEvents extends OtlpEvents_base {
|
|
|
12
13
|
static readonly layer: (options: ExporterOptions) => Layer.Layer<OtlpEvents, never, OtlpExporter.Flusher | import("effect/http/HttpClient").HttpClient | OtlpSerialization.OtlpSerialization>;
|
|
13
14
|
}
|
|
14
15
|
interface EventRecord {
|
|
16
|
+
readonly scopeName?: string;
|
|
15
17
|
readonly eventName: string;
|
|
16
18
|
readonly timeUnixNano: string;
|
|
17
19
|
readonly observedTimeUnixNano: string;
|
|
@@ -22,7 +24,7 @@ interface EventRecord {
|
|
|
22
24
|
readonly spanId?: string;
|
|
23
25
|
}
|
|
24
26
|
/** Emits an INFO event without creating a tracing span or persisting identity. */
|
|
25
|
-
export declare const
|
|
27
|
+
export declare const event: (name: string, properties?: {
|
|
26
28
|
readonly [x: string]: string | number | boolean | readonly string[] | readonly number[] | readonly boolean[];
|
|
27
29
|
} | undefined, options?: {
|
|
28
30
|
readonly userId?: string | undefined;
|
|
@@ -1,10 +1,13 @@
|
|
|
1
|
-
import { Effect, Layer, Schema } from "effect";
|
|
2
|
-
import { OtlpExporter } from "effect/observability";
|
|
1
|
+
import { Context, Effect, Layer, Schema } from "effect";
|
|
2
|
+
import { OtlpExporter, OtlpResource } from "effect/observability";
|
|
3
3
|
import { OtlpEvents } from "./events.js";
|
|
4
4
|
export type { EventAttributes, EventOptions } from "./schema.js";
|
|
5
|
+
declare const Otlp_base: Context.ServiceClass<Otlp, "krakstack/opentelemetry/Otlp", {
|
|
6
|
+
readonly resource: Effect.Effect<OtlpResource.Resource>;
|
|
7
|
+
}>;
|
|
5
8
|
/** Runtime-neutral server telemetry for HTTP applications, workers, and CLIs. */
|
|
6
|
-
export declare class
|
|
7
|
-
static readonly
|
|
9
|
+
export declare class Otlp extends Otlp_base {
|
|
10
|
+
static readonly event: (name: string, properties?: {
|
|
8
11
|
readonly [x: string]: string | number | boolean | readonly string[] | readonly number[] | readonly boolean[];
|
|
9
12
|
} | undefined, options?: {
|
|
10
13
|
readonly userId?: string | undefined;
|
|
@@ -16,5 +19,5 @@ export declare class ServerOtlp {
|
|
|
16
19
|
readonly [x: string]: string | number | boolean | readonly string[] | readonly number[] | readonly boolean[];
|
|
17
20
|
} | undefined;
|
|
18
21
|
} | undefined) => Effect.Effect<void, Schema.SchemaError, OtlpEvents>;
|
|
19
|
-
static readonly layer: Layer.Layer<OtlpExporter.Flusher | OtlpEvents, never, never>;
|
|
22
|
+
static readonly layer: Layer.Layer<OtlpExporter.Flusher | Otlp | OtlpEvents, never, never>;
|
|
20
23
|
}
|
|
@@ -1,7 +1,12 @@
|
|
|
1
|
-
// ../../src/services/opentelemetry/
|
|
2
|
-
import { Config, Effect as Effect2, Layer as Layer2, Schema as Schema3 } from "effect";
|
|
1
|
+
// ../../src/services/opentelemetry/index.ts
|
|
2
|
+
import { Config, Context as Context2, Effect as Effect2, Layer as Layer2, Schema as Schema3 } from "effect";
|
|
3
3
|
import { FetchHttpClient } from "effect/http";
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
Otlp as EffectOtlp,
|
|
6
|
+
OtlpExporter as OtlpExporter2,
|
|
7
|
+
OtlpResource as OtlpResource2,
|
|
8
|
+
OtlpSerialization as OtlpSerialization2
|
|
9
|
+
} from "effect/observability";
|
|
5
10
|
|
|
6
11
|
// ../../src/services/opentelemetry/events.ts
|
|
7
12
|
import {
|
|
@@ -22,6 +27,21 @@ import {
|
|
|
22
27
|
|
|
23
28
|
// ../../src/services/opentelemetry/schema.ts
|
|
24
29
|
import { Schema } from "effect";
|
|
30
|
+
var BrowserWebVital = Schema.Struct({
|
|
31
|
+
name: Schema.Literals(["LCP", "INP", "CLS", "FCP", "TTFB"]),
|
|
32
|
+
id: Schema.NonEmptyString.check(Schema.isMaxLength(64)),
|
|
33
|
+
value: Schema.Finite.check(Schema.isGreaterThanOrEqualTo(0)),
|
|
34
|
+
delta: Schema.Finite,
|
|
35
|
+
rating: Schema.Literals(["good", "needs-improvement", "poor"]),
|
|
36
|
+
navigationType: Schema.Literals([
|
|
37
|
+
"navigate",
|
|
38
|
+
"reload",
|
|
39
|
+
"back-forward",
|
|
40
|
+
"back-forward-cache",
|
|
41
|
+
"prerender",
|
|
42
|
+
"restore"
|
|
43
|
+
])
|
|
44
|
+
}).annotate({ identifier: "BrowserWebVital" });
|
|
25
45
|
var EventAttributeValue = Schema.Union([
|
|
26
46
|
Schema.String,
|
|
27
47
|
Schema.Finite,
|
|
@@ -44,17 +64,6 @@ var TelemetryEvent = Schema.Struct({
|
|
|
44
64
|
properties: Schema.optional(EventAttributesSchema),
|
|
45
65
|
options: Schema.optional(EventOptionsSchema)
|
|
46
66
|
}).annotate({ identifier: "TelemetryEvent" });
|
|
47
|
-
var PageEvent = Schema.Struct({
|
|
48
|
-
name: Schema.optional(Schema.NonEmptyString),
|
|
49
|
-
category: Schema.optional(Schema.NonEmptyString),
|
|
50
|
-
path: Schema.optional(Schema.NonEmptyString),
|
|
51
|
-
properties: Schema.optional(EventAttributesSchema),
|
|
52
|
-
options: Schema.optional(EventOptionsSchema)
|
|
53
|
-
}).annotate({ identifier: "PageEvent" });
|
|
54
|
-
var BrowserIdentity = Schema.Struct({
|
|
55
|
-
userId: Schema.optional(Schema.NonEmptyString),
|
|
56
|
-
organizationId: Schema.optional(Schema.NonEmptyString)
|
|
57
|
-
}).annotate({ identifier: "BrowserTelemetryIdentity" });
|
|
58
67
|
|
|
59
68
|
// ../../src/services/opentelemetry/events.ts
|
|
60
69
|
class OtlpEvents extends Context.Service()("krakstack/opentelemetry/Events") {
|
|
@@ -65,27 +74,36 @@ class OtlpEvents extends Context.Service()("krakstack/opentelemetry/Events") {
|
|
|
65
74
|
return yield* OtlpExporter.make({
|
|
66
75
|
...options,
|
|
67
76
|
label: "OtlpEvents",
|
|
68
|
-
body: (logRecords) =>
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
77
|
+
body: (logRecords) => {
|
|
78
|
+
const scopes = new Map;
|
|
79
|
+
for (const { scopeName, ...record } of logRecords) {
|
|
80
|
+
const name = scopeName ?? options.scopeName ?? "krakstack.events";
|
|
81
|
+
const records = scopes.get(name);
|
|
82
|
+
if (records)
|
|
83
|
+
records.push(record);
|
|
84
|
+
else
|
|
85
|
+
scopes.set(name, [record]);
|
|
86
|
+
}
|
|
87
|
+
return [
|
|
88
|
+
serialization.logs({
|
|
89
|
+
resourceLogs: [
|
|
90
|
+
{
|
|
91
|
+
resource,
|
|
92
|
+
scopeLogs: Array.from(scopes, ([name, records]) => ({
|
|
93
|
+
scope: { name },
|
|
94
|
+
logRecords: records
|
|
95
|
+
}))
|
|
96
|
+
}
|
|
97
|
+
]
|
|
98
|
+
}),
|
|
99
|
+
Effect.void
|
|
100
|
+
];
|
|
101
|
+
}
|
|
84
102
|
});
|
|
85
103
|
}));
|
|
86
104
|
}
|
|
87
|
-
var
|
|
88
|
-
const
|
|
105
|
+
var event = Effect.fnUntraced(function* (name, properties, options) {
|
|
106
|
+
const decoded = yield* Schema2.decodeUnknownEffect(TelemetryEvent)({
|
|
89
107
|
name,
|
|
90
108
|
properties,
|
|
91
109
|
options
|
|
@@ -93,20 +111,20 @@ var track = Effect.fnUntraced(function* (name, properties, options) {
|
|
|
93
111
|
const events = yield* OtlpEvents;
|
|
94
112
|
const clock = yield* Clock.Clock;
|
|
95
113
|
const observedTime = clock.currentTimeNanosUnsafe().toString();
|
|
96
|
-
const timestamp =
|
|
114
|
+
const timestamp = decoded.options?.timestamp ? (BigInt(DateTime.toEpochMillis(decoded.options.timestamp)) * 1000000n).toString() : observedTime;
|
|
97
115
|
const span = yield* Effect.currentSpan.pipe(Effect.option);
|
|
98
|
-
const attributes = { ...
|
|
99
|
-
if (
|
|
100
|
-
attributes["user.id"] =
|
|
101
|
-
if (
|
|
102
|
-
attributes["analytics.anonymous_id"] =
|
|
103
|
-
if (
|
|
104
|
-
attributes["session.id"] =
|
|
105
|
-
if (
|
|
106
|
-
attributes["log.record.uid"] =
|
|
116
|
+
const attributes = { ...decoded.options?.context, ...decoded.properties };
|
|
117
|
+
if (decoded.options?.userId)
|
|
118
|
+
attributes["user.id"] = decoded.options.userId;
|
|
119
|
+
if (decoded.options?.anonymousId)
|
|
120
|
+
attributes["analytics.anonymous_id"] = decoded.options.anonymousId;
|
|
121
|
+
if (decoded.options?.sessionId)
|
|
122
|
+
attributes["session.id"] = decoded.options.sessionId;
|
|
123
|
+
if (decoded.options?.messageId)
|
|
124
|
+
attributes["log.record.uid"] = decoded.options.messageId;
|
|
107
125
|
const annotations = yield* References.CurrentLogAnnotations;
|
|
108
126
|
events.push({
|
|
109
|
-
eventName:
|
|
127
|
+
eventName: decoded.name,
|
|
110
128
|
timeUnixNano: timestamp,
|
|
111
129
|
observedTimeUnixNano: observedTime,
|
|
112
130
|
severityNumber: 9,
|
|
@@ -120,7 +138,7 @@ var track = Effect.fnUntraced(function* (name, properties, options) {
|
|
|
120
138
|
});
|
|
121
139
|
});
|
|
122
140
|
|
|
123
|
-
// ../../src/services/opentelemetry/
|
|
141
|
+
// ../../src/services/opentelemetry/index.ts
|
|
124
142
|
var ServerEventsLive = Layer2.unwrap(Effect2.gen(function* () {
|
|
125
143
|
const disabled = yield* Config.Boolean("OTEL_SDK_DISABLED").pipe(Config.withDefault(false));
|
|
126
144
|
const exporters = yield* Config.Array(Schema3.String, "OTEL_LOGS_EXPORTER").pipe(Config.withDefault([]));
|
|
@@ -145,10 +163,10 @@ var ServerEventsLive = Layer2.unwrap(Effect2.gen(function* () {
|
|
|
145
163
|
});
|
|
146
164
|
}).pipe(Effect2.orDie));
|
|
147
165
|
|
|
148
|
-
class
|
|
149
|
-
static
|
|
150
|
-
static layer = Layer2.mergeAll(
|
|
166
|
+
class Otlp extends Context2.Service()("krakstack/opentelemetry/Otlp") {
|
|
167
|
+
static event = event;
|
|
168
|
+
static layer = Layer2.mergeAll(Layer2.succeed(this, { resource: OtlpResource2.fromConfig() }), EffectOtlp.layerFromConfig(), ServerEventsLive).pipe(Layer2.provideMerge(OtlpExporter2.layerFlusher), Layer2.provide(OtlpSerialization2.layerJson), Layer2.provide(FetchHttpClient.layer));
|
|
151
169
|
}
|
|
152
170
|
export {
|
|
153
|
-
|
|
171
|
+
Otlp
|
|
154
172
|
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Context, Effect, Schema } from "effect";
|
|
2
|
+
import type { HttpServerRequest } from "effect/http";
|
|
3
|
+
export interface OtlpLogsResult {
|
|
4
|
+
readonly body: Uint8Array;
|
|
5
|
+
/** All records in this batch have committed to authoritative storage. */
|
|
6
|
+
readonly persisted: boolean;
|
|
7
|
+
}
|
|
8
|
+
declare const OtlpProcessingError_base: Schema.Class<OtlpProcessingError, Schema.TaggedStruct<"OtlpProcessingError", {
|
|
9
|
+
readonly status: Schema.Literals<readonly [400, 403, 409, 413, 503]>;
|
|
10
|
+
}>, import("effect/Cause").YieldableError>;
|
|
11
|
+
export declare class OtlpProcessingError extends OtlpProcessingError_base {
|
|
12
|
+
}
|
|
13
|
+
declare const OtlpLogsProcessor_base: Context.ServiceClass<OtlpLogsProcessor, "krakstack/opentelemetry/OtlpLogsProcessor", {
|
|
14
|
+
readonly process: (body: Uint8Array) => Effect.Effect<OtlpLogsResult, OtlpProcessingError, HttpServerRequest.HttpServerRequest>;
|
|
15
|
+
}>;
|
|
16
|
+
/** Optional processing at the existing OTLP/HTTP logs boundary. */
|
|
17
|
+
export declare class OtlpLogsProcessor extends OtlpLogsProcessor_base {
|
|
18
|
+
}
|
|
19
|
+
export {};
|
|
@@ -1,4 +1,12 @@
|
|
|
1
1
|
import { Schema } from "effect";
|
|
2
|
+
export declare const BrowserWebVital: Schema.Struct<{
|
|
3
|
+
readonly name: Schema.Literals<readonly ["LCP", "INP", "CLS", "FCP", "TTFB"]>;
|
|
4
|
+
readonly id: Schema.NonEmptyString;
|
|
5
|
+
readonly value: Schema.Finite;
|
|
6
|
+
readonly delta: Schema.Finite;
|
|
7
|
+
readonly rating: Schema.Literals<readonly ["good", "needs-improvement", "poor"]>;
|
|
8
|
+
readonly navigationType: Schema.Literals<readonly ["navigate", "reload", "back-forward", "back-forward-cache", "prerender", "restore"]>;
|
|
9
|
+
}>;
|
|
2
10
|
export declare const EventAttributesSchema: Schema.$Record<Schema.String, Schema.Union<readonly [Schema.String, Schema.Finite, Schema.Boolean, Schema.$Array<Schema.String>, Schema.$Array<Schema.Finite>, Schema.$Array<Schema.Boolean>]>>;
|
|
3
11
|
export declare const EventOptionsSchema: Schema.Struct<{
|
|
4
12
|
readonly userId: Schema.optional<Schema.NonEmptyString>;
|
|
@@ -20,25 +28,5 @@ export declare const TelemetryEvent: Schema.Struct<{
|
|
|
20
28
|
readonly context: Schema.optional<Schema.$Record<Schema.String, Schema.Union<readonly [Schema.String, Schema.Finite, Schema.Boolean, Schema.$Array<Schema.String>, Schema.$Array<Schema.Finite>, Schema.$Array<Schema.Boolean>]>>>;
|
|
21
29
|
}>>;
|
|
22
30
|
}>;
|
|
23
|
-
export declare const PageEvent: Schema.Struct<{
|
|
24
|
-
readonly name: Schema.optional<Schema.NonEmptyString>;
|
|
25
|
-
readonly category: Schema.optional<Schema.NonEmptyString>;
|
|
26
|
-
readonly path: Schema.optional<Schema.NonEmptyString>;
|
|
27
|
-
readonly properties: Schema.optional<Schema.$Record<Schema.String, Schema.Union<readonly [Schema.String, Schema.Finite, Schema.Boolean, Schema.$Array<Schema.String>, Schema.$Array<Schema.Finite>, Schema.$Array<Schema.Boolean>]>>>;
|
|
28
|
-
readonly options: Schema.optional<Schema.Struct<{
|
|
29
|
-
readonly userId: Schema.optional<Schema.NonEmptyString>;
|
|
30
|
-
readonly anonymousId: Schema.optional<Schema.NonEmptyString>;
|
|
31
|
-
readonly sessionId: Schema.optional<Schema.NonEmptyString>;
|
|
32
|
-
readonly messageId: Schema.optional<Schema.NonEmptyString>;
|
|
33
|
-
readonly timestamp: Schema.optional<Schema.DateTimeUtc>;
|
|
34
|
-
readonly context: Schema.optional<Schema.$Record<Schema.String, Schema.Union<readonly [Schema.String, Schema.Finite, Schema.Boolean, Schema.$Array<Schema.String>, Schema.$Array<Schema.Finite>, Schema.$Array<Schema.Boolean>]>>>;
|
|
35
|
-
}>>;
|
|
36
|
-
}>;
|
|
37
31
|
export type EventAttributes = typeof EventAttributesSchema.Type;
|
|
38
32
|
export type EventOptions = typeof EventOptionsSchema.Type;
|
|
39
|
-
export type PageOptions = typeof PageEvent.Type;
|
|
40
|
-
export declare const BrowserIdentity: Schema.Struct<{
|
|
41
|
-
readonly userId: Schema.optional<Schema.NonEmptyString>;
|
|
42
|
-
readonly organizationId: Schema.optional<Schema.NonEmptyString>;
|
|
43
|
-
}>;
|
|
44
|
-
export type BrowserIdentity = typeof BrowserIdentity.Type;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type Metric } from "web-vitals";
|
|
2
|
+
import { Context, Layer, Schema } from "effect";
|
|
3
|
+
import { OtlpEvents } from "./events.js";
|
|
4
|
+
/** web-vitals observers are document-scoped; only subscribers retain application state. */
|
|
5
|
+
export declare const subscribeWebVitals: (subscriber: (metric: Metric) => void) => () => void;
|
|
6
|
+
export declare const WebVitals: Context.Reference<{
|
|
7
|
+
readonly subscribe: typeof subscribeWebVitals;
|
|
8
|
+
}>;
|
|
9
|
+
export interface BrowserWebVitalsOptions {
|
|
10
|
+
readonly respectDoNotTrack?: boolean;
|
|
11
|
+
}
|
|
12
|
+
export declare class BrowserWebVitals {
|
|
13
|
+
/** Provide a browser telemetry layer with events enabled; no exporter is created here. */
|
|
14
|
+
static readonly layer: ({ respectDoNotTrack, }?: BrowserWebVitalsOptions) => Layer.Layer<never, Schema.SchemaError, OtlpEvents>;
|
|
15
|
+
}
|