@krak-stack/registry 0.1.50 → 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.
@@ -0,0 +1,229 @@
1
+ // ../../src/services/opentelemetry/web-vitals.ts
2
+ import { onCLS, onFCP, onINP, onLCP, onTTFB } from "web-vitals";
3
+ import { Clock as Clock2, Context as Context2, Effect as Effect2, Layer as Layer2, Schema as Schema3 } from "effect";
4
+
5
+ // ../../src/services/opentelemetry/events.ts
6
+ import {
7
+ Clock,
8
+ Context,
9
+ DateTime,
10
+ Effect,
11
+ Layer,
12
+ Option,
13
+ References,
14
+ Schema as Schema2
15
+ } from "effect";
16
+ import {
17
+ OtlpExporter,
18
+ OtlpResource,
19
+ OtlpSerialization
20
+ } from "effect/observability";
21
+
22
+ // ../../src/services/opentelemetry/schema.ts
23
+ import { Schema } from "effect";
24
+ var BrowserWebVital = Schema.Struct({
25
+ name: Schema.Literals(["LCP", "INP", "CLS", "FCP", "TTFB"]),
26
+ id: Schema.NonEmptyString.check(Schema.isMaxLength(64)),
27
+ value: Schema.Finite.check(Schema.isGreaterThanOrEqualTo(0)),
28
+ delta: Schema.Finite,
29
+ rating: Schema.Literals(["good", "needs-improvement", "poor"]),
30
+ navigationType: Schema.Literals([
31
+ "navigate",
32
+ "reload",
33
+ "back-forward",
34
+ "back-forward-cache",
35
+ "prerender",
36
+ "restore"
37
+ ])
38
+ }).annotate({ identifier: "BrowserWebVital" });
39
+ var EventAttributeValue = Schema.Union([
40
+ Schema.String,
41
+ Schema.Finite,
42
+ Schema.Boolean,
43
+ Schema.Array(Schema.String),
44
+ Schema.Array(Schema.Finite),
45
+ Schema.Array(Schema.Boolean)
46
+ ]);
47
+ var EventAttributesSchema = Schema.Record(Schema.String, EventAttributeValue).annotate({ identifier: "EventAttributes" });
48
+ var EventOptionsSchema = Schema.Struct({
49
+ userId: Schema.optional(Schema.NonEmptyString),
50
+ anonymousId: Schema.optional(Schema.NonEmptyString),
51
+ sessionId: Schema.optional(Schema.NonEmptyString),
52
+ messageId: Schema.optional(Schema.NonEmptyString),
53
+ timestamp: Schema.optional(Schema.DateTimeUtc),
54
+ context: Schema.optional(EventAttributesSchema)
55
+ }).annotate({ identifier: "EventOptions" });
56
+ var TelemetryEvent = Schema.Struct({
57
+ name: Schema.NonEmptyString,
58
+ properties: Schema.optional(EventAttributesSchema),
59
+ options: Schema.optional(EventOptionsSchema)
60
+ }).annotate({ identifier: "TelemetryEvent" });
61
+
62
+ // ../../src/services/opentelemetry/events.ts
63
+ class OtlpEvents extends Context.Service()("krakstack/opentelemetry/Events") {
64
+ static disabledLayer = Layer.succeed(this, { push: () => {} });
65
+ static layer = (options) => Layer.effect(this, Effect.gen(function* () {
66
+ const serialization = yield* OtlpSerialization.OtlpSerialization;
67
+ const resource = yield* OtlpResource.fromConfig(options.resource);
68
+ return yield* OtlpExporter.make({
69
+ ...options,
70
+ label: "OtlpEvents",
71
+ body: (logRecords) => {
72
+ const scopes = new Map;
73
+ for (const { scopeName, ...record } of logRecords) {
74
+ const name = scopeName ?? options.scopeName ?? "krakstack.events";
75
+ const records = scopes.get(name);
76
+ if (records)
77
+ records.push(record);
78
+ else
79
+ scopes.set(name, [record]);
80
+ }
81
+ return [
82
+ serialization.logs({
83
+ resourceLogs: [
84
+ {
85
+ resource,
86
+ scopeLogs: Array.from(scopes, ([name, records]) => ({
87
+ scope: { name },
88
+ logRecords: records
89
+ }))
90
+ }
91
+ ]
92
+ }),
93
+ Effect.void
94
+ ];
95
+ }
96
+ });
97
+ }));
98
+ }
99
+ var event = Effect.fnUntraced(function* (name, properties, options) {
100
+ const decoded = yield* Schema2.decodeUnknownEffect(TelemetryEvent)({
101
+ name,
102
+ properties,
103
+ options
104
+ });
105
+ const events = yield* OtlpEvents;
106
+ const clock = yield* Clock.Clock;
107
+ const observedTime = clock.currentTimeNanosUnsafe().toString();
108
+ const timestamp = decoded.options?.timestamp ? (BigInt(DateTime.toEpochMillis(decoded.options.timestamp)) * 1000000n).toString() : observedTime;
109
+ const span = yield* Effect.currentSpan.pipe(Effect.option);
110
+ const attributes = { ...decoded.options?.context, ...decoded.properties };
111
+ if (decoded.options?.userId)
112
+ attributes["user.id"] = decoded.options.userId;
113
+ if (decoded.options?.anonymousId)
114
+ attributes["analytics.anonymous_id"] = decoded.options.anonymousId;
115
+ if (decoded.options?.sessionId)
116
+ attributes["session.id"] = decoded.options.sessionId;
117
+ if (decoded.options?.messageId)
118
+ attributes["log.record.uid"] = decoded.options.messageId;
119
+ const annotations = yield* References.CurrentLogAnnotations;
120
+ events.push({
121
+ eventName: decoded.name,
122
+ timeUnixNano: timestamp,
123
+ observedTimeUnixNano: observedTime,
124
+ severityNumber: 9,
125
+ attributes: OtlpResource.entriesToAttributes(Object.entries({
126
+ ...attributes,
127
+ ...annotations
128
+ })),
129
+ droppedAttributesCount: 0,
130
+ traceId: Option.isSome(span) ? span.value.traceId : undefined,
131
+ spanId: Option.isSome(span) ? span.value.spanId : undefined
132
+ });
133
+ });
134
+
135
+ // ../../src/services/opentelemetry/web-vitals.ts
136
+ var subscribers = new Set;
137
+ var started = false;
138
+ var subscribeWebVitals = (subscriber) => {
139
+ subscribers.add(subscriber);
140
+ if (!started) {
141
+ started = true;
142
+ const report = (metric) => {
143
+ for (const listener of subscribers)
144
+ listener(metric);
145
+ };
146
+ onCLS(report);
147
+ onFCP(report);
148
+ onINP(report);
149
+ onLCP(report);
150
+ onTTFB(report);
151
+ }
152
+ return () => {
153
+ subscribers.delete(subscriber);
154
+ };
155
+ };
156
+ var WebVitals = Context2.Reference("krakstack/opentelemetry/WebVitals", {
157
+ defaultValue: () => ({ subscribe: subscribeWebVitals })
158
+ });
159
+ var WebVitalsLive = Layer2.effectDiscard(Effect2.gen(function* () {
160
+ const vitals = yield* WebVitals;
161
+ const events = yield* OtlpEvents;
162
+ const clock = yield* Clock2.Clock;
163
+ const seen = new Map;
164
+ yield* Effect2.acquireRelease(Effect2.sync(() => vitals.subscribe((input) => {
165
+ const parsed = Schema3.decodeUnknownOption(BrowserWebVital)(input);
166
+ if (parsed._tag === "None")
167
+ return;
168
+ const metric = parsed.value;
169
+ const key = `${metric.name}:${metric.id}`;
170
+ if (seen.get(key) === metric.value)
171
+ return;
172
+ seen.set(key, metric.value);
173
+ const timestamp = clock.currentTimeNanosUnsafe().toString();
174
+ events.push({
175
+ eventName: "browser.web_vital",
176
+ timeUnixNano: timestamp,
177
+ observedTimeUnixNano: timestamp,
178
+ severityNumber: 9,
179
+ droppedAttributesCount: 0,
180
+ attributes: [
181
+ {
182
+ key: "browser.web_vital.name",
183
+ value: { stringValue: metric.name.toLowerCase() }
184
+ },
185
+ {
186
+ key: "browser.web_vital.id",
187
+ value: { stringValue: metric.id }
188
+ },
189
+ {
190
+ key: "browser.web_vital.value",
191
+ value: { doubleValue: metric.value }
192
+ },
193
+ {
194
+ key: "browser.web_vital.delta",
195
+ value: { doubleValue: metric.delta }
196
+ },
197
+ {
198
+ key: "browser.web_vital.rating",
199
+ value: { stringValue: metric.rating }
200
+ },
201
+ {
202
+ key: "browser.web_vital.navigation_type",
203
+ value: { stringValue: metric.navigationType }
204
+ },
205
+ {
206
+ key: "log.record.uid",
207
+ value: { stringValue: `${metric.id}:${metric.value}` }
208
+ }
209
+ ]
210
+ });
211
+ })), (unsubscribe) => Effect2.sync(unsubscribe));
212
+ }));
213
+
214
+ class BrowserWebVitals {
215
+ static layer = ({
216
+ respectDoNotTrack = true
217
+ } = {}) => Layer2.unwrap(Effect2.gen(function* () {
218
+ const privacy = yield* Schema3.decodeUnknownEffect(Schema3.Struct({
219
+ globalPrivacyControl: Schema3.optional(Schema3.Boolean)
220
+ }))(navigator);
221
+ const optedOut = respectDoNotTrack && (navigator.doNotTrack === "1" || navigator.doNotTrack === "yes" || privacy.globalPrivacyControl === true);
222
+ return optedOut ? Layer2.empty : WebVitalsLive;
223
+ }));
224
+ }
225
+ export {
226
+ BrowserWebVitals,
227
+ WebVitals,
228
+ subscribeWebVitals
229
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@krak-stack/registry",
3
- "version": "0.1.50",
3
+ "version": "0.1.51",
4
4
  "description": "Tree-shakable KrakStack components and Effect services.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -17,6 +17,18 @@
17
17
  "type": "module",
18
18
  "sideEffects": false,
19
19
  "exports": {
20
+ "./events": {
21
+ "types": "./dist/services/events/index.d.ts",
22
+ "import": "./dist/services/events/index.js"
23
+ },
24
+ "./events/browser": {
25
+ "types": "./dist/services/events/browser.d.ts",
26
+ "import": "./dist/services/events/browser.js"
27
+ },
28
+ "./events/migrations": {
29
+ "types": "./dist/services/events/migrations.d.ts",
30
+ "import": "./dist/services/events/migrations.js"
31
+ },
20
32
  "./agent": {
21
33
  "types": "./dist/services/agent/index.d.ts",
22
34
  "import": "./dist/services/agent/index.js"
@@ -49,9 +61,13 @@
49
61
  "types": "./dist/services/opentelemetry/browser.d.ts",
50
62
  "import": "./dist/services/opentelemetry/browser.js"
51
63
  },
52
- "./opentelemetry/server": {
53
- "types": "./dist/services/opentelemetry/server.d.ts",
54
- "import": "./dist/services/opentelemetry/server.js"
64
+ "./opentelemetry/web-vitals": {
65
+ "types": "./dist/services/opentelemetry/web-vitals.d.ts",
66
+ "import": "./dist/services/opentelemetry/web-vitals.js"
67
+ },
68
+ "./opentelemetry": {
69
+ "types": "./dist/services/opentelemetry/index.d.ts",
70
+ "import": "./dist/services/opentelemetry/index.js"
55
71
  },
56
72
  "./docs": {
57
73
  "types": "./dist/lib/docs.d.ts",
@@ -232,7 +248,9 @@
232
248
  "prepack": "bun run check:release"
233
249
  },
234
250
  "dependencies": {
235
- "@oxlint/plugins": "1.78.0"
251
+ "@oxlint/plugins": "1.78.0",
252
+ "bowser": "^2.14.1",
253
+ "web-vitals": "^5.1.0"
236
254
  },
237
255
  "peerDependencies": {
238
256
  "@aws-sdk/client-sesv2": "^3.1068.0",