Skip to content

Commit 2a6b1ad

Browse files
committed
refactor: link "livestore.in-memory-db:execute" to "client-session-sync-processor:materialize-event" span
1 parent c562bc6 commit 2a6b1ad

File tree

4 files changed

+79
-87
lines changed

4 files changed

+79
-87
lines changed

packages/@livestore/common/src/sync/ClientSessionSyncProcessor.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export const makeClientSessionSyncProcessor = ({
3838
runtime: Runtime.Runtime<Scope.Scope>
3939
materializeEvent: (
4040
eventDecoded: LiveStoreEvent.PartialAnyDecoded,
41-
options: { otelContext: otel.Context; withChangeset: boolean; materializerHashLeader: Option.Option<number> },
41+
options: { withChangeset: boolean; materializerHashLeader: Option.Option<number> },
4242
) => Effect.Effect<{
4343
writeTables: Set<string>
4444
sessionChangeset: { _tag: 'sessionChangeset'; data: Uint8Array; debug: any } | { _tag: 'no-op' } | { _tag: 'unset' }
@@ -76,10 +76,7 @@ export const makeClientSessionSyncProcessor = ({
7676
/** We're queuing push requests to reduce the number of messages sent to the leader by batching them */
7777
const leaderPushQueue = BucketQueue.make<LiveStoreEvent.EncodedWithMeta>().pipe(Effect.runSync)
7878

79-
const push: ClientSessionSyncProcessor['push'] = Effect.fn('client-session-sync-processor:push')(function* (
80-
batch,
81-
{ otelContext },
82-
) {
79+
const push: ClientSessionSyncProcessor['push'] = Effect.fn('client-session-sync-processor:push')(function* (batch) {
8380
// TODO validate batch
8481

8582
let baseEventSequenceNumber = syncStateRef.current.localHead
@@ -131,7 +128,6 @@ export const makeClientSessionSyncProcessor = ({
131128
sessionChangeset,
132129
materializerHash,
133130
} = yield* materializeEvent(decodedEventDef, {
134-
otelContext,
135131
withChangeset: true,
136132
materializerHashLeader: Option.none(),
137133
})
@@ -157,8 +153,6 @@ export const makeClientSessionSyncProcessor = ({
157153
rejectCount: 0,
158154
}
159155

160-
const otelContext = otel.trace.setSpan(otel.context.active(), span)
161-
162156
const boot: ClientSessionSyncProcessor['boot'] = Effect.gen(function* () {
163157
// eslint-disable-next-line unicorn/prefer-global-this
164158
if (confirmUnsavedChanges && typeof window !== 'undefined' && typeof window.addEventListener === 'function') {
@@ -285,7 +279,6 @@ export const makeClientSessionSyncProcessor = ({
285279
sessionChangeset,
286280
materializerHash,
287281
} = yield* materializeEvent(decodedEventDef, {
288-
otelContext,
289282
withChangeset: true,
290283
materializerHashLeader: event.meta.materializerHashLeader,
291284
})
@@ -342,10 +335,7 @@ export const makeClientSessionSyncProcessor = ({
342335
}
343336

344337
export interface ClientSessionSyncProcessor {
345-
push: (
346-
batch: ReadonlyArray<LiveStoreEvent.PartialAnyDecoded>,
347-
options: { otelContext: otel.Context },
348-
) => Effect.Effect<
338+
push: (batch: ReadonlyArray<LiveStoreEvent.PartialAnyDecoded>) => Effect.Effect<
349339
{
350340
writeTables: Set<string>
351341
},

packages/@livestore/livestore/src/live-queries/__snapshots__/db-query.test.ts.snap

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ exports[`otel > otel 3`] = `
2828
"children": [
2929
{
3030
"_name": "client-session-sync-processor:materialize-event",
31+
"children": [
32+
{
33+
"_name": "livestore.in-memory-db:execute",
34+
"attributes": {
35+
"sql.query": "INSERT INTO todos (id, text, completed) VALUES ('t1', 'buy milk', 0)",
36+
},
37+
},
38+
],
3139
},
3240
],
3341
},
@@ -60,14 +68,6 @@ exports[`otel > otel 3`] = `
6068
],
6169
"livestore.eventsCount": 1,
6270
},
63-
"children": [
64-
{
65-
"_name": "livestore.in-memory-db:execute",
66-
"attributes": {
67-
"sql.query": "INSERT INTO todos (id, text, completed) VALUES ('t1', 'buy milk', 0)",
68-
},
69-
},
70-
],
7171
},
7272
],
7373
},
@@ -314,6 +314,14 @@ exports[`otel > with thunks 7`] = `
314314
"children": [
315315
{
316316
"_name": "client-session-sync-processor:materialize-event",
317+
"children": [
318+
{
319+
"_name": "livestore.in-memory-db:execute",
320+
"attributes": {
321+
"sql.query": "INSERT INTO todos (id, text, completed) VALUES ('t1', 'buy milk', 0)",
322+
},
323+
},
324+
],
317325
},
318326
],
319327
},
@@ -346,14 +354,6 @@ exports[`otel > with thunks 7`] = `
346354
],
347355
"livestore.eventsCount": 1,
348356
},
349-
"children": [
350-
{
351-
"_name": "livestore.in-memory-db:execute",
352-
"attributes": {
353-
"sql.query": "INSERT INTO todos (id, text, completed) VALUES ('t1', 'buy milk', 0)",
354-
},
355-
},
356-
],
357357
},
358358
],
359359
},
@@ -434,6 +434,14 @@ exports[`otel > with thunks with query builder and without labels 3`] = `
434434
"children": [
435435
{
436436
"_name": "client-session-sync-processor:materialize-event",
437+
"children": [
438+
{
439+
"_name": "livestore.in-memory-db:execute",
440+
"attributes": {
441+
"sql.query": "INSERT INTO todos (id, text, completed) VALUES ('t1', 'buy milk', 0)",
442+
},
443+
},
444+
],
437445
},
438446
],
439447
},
@@ -466,14 +474,6 @@ exports[`otel > with thunks with query builder and without labels 3`] = `
466474
],
467475
"livestore.eventsCount": 1,
468476
},
469-
"children": [
470-
{
471-
"_name": "livestore.in-memory-db:execute",
472-
"attributes": {
473-
"sql.query": "INSERT INTO todos (id, text, completed) VALUES ('t1', 'buy milk', 0)",
474-
},
475-
},
476-
],
477477
},
478478
],
479479
},

packages/@livestore/livestore/src/store/store.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ export class Store<TSchema extends LiveStoreSchema = LiveStoreSchema, TContext =
116116
clientSession,
117117
runtime: effectContext.runtime,
118118
materializeEvent: Effect.fn('client-session-sync-processor:materialize-event')(
119-
(eventDecoded, { otelContext, withChangeset, materializerHashLeader }) =>
119+
(eventDecoded, { withChangeset, materializerHashLeader }) =>
120120
Effect.gen(this, function* () {
121121
const { eventDef, materializer } = getEventDef(schema, eventDecoded.name)
122122

@@ -146,6 +146,8 @@ export class Store<TSchema extends LiveStoreSchema = LiveStoreSchema, TContext =
146146
// TODO: we should probably handle this more gracefully using Effect’s error channel
147147
}
148148

149+
const span = yield* OtelTracer.currentOtelSpan.pipe(Effect.orDie)
150+
const otelContext = otel.trace.setSpan(otel.context.active(), span)
149151
return yield* Effect.sync(() => {
150152
const writeTablesForEvent = new Set<string>()
151153

@@ -586,7 +588,7 @@ export class Store<TSchema extends LiveStoreSchema = LiveStoreSchema, TContext =
586588
const { writeTables } = (() => {
587589
try {
588590
const materializeEvents = () => {
589-
return Runtime.runSync(this.effectContext.runtime, this.syncProcessor.push(events, { otelContext }))
591+
return Runtime.runSync(this.effectContext.runtime, this.syncProcessor.push(events))
590592
}
591593

592594
if (events.length > 1) {

packages/@livestore/react/src/__snapshots__/useClientDocument.test.tsx.snap

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@ exports[`useClientDocument > otel > should update the data based on component ke
2828
"children": [
2929
{
3030
"_name": "client-session-sync-processor:materialize-event",
31+
"children": [
32+
{
33+
"_name": "livestore.in-memory-db:execute",
34+
"attributes": {
35+
"sql.query": "
36+
INSERT INTO 'UserInfo' (id, value)
37+
VALUES (?, ?)
38+
ON CONFLICT (id) DO UPDATE SET value = json_set(json_set(value, ?, json(?)), ?, json(?))
39+
",
40+
},
41+
},
42+
],
3143
},
3244
],
3345
},
@@ -39,6 +51,18 @@ exports[`useClientDocument > otel > should update the data based on component ke
3951
"children": [
4052
{
4153
"_name": "client-session-sync-processor:materialize-event",
54+
"children": [
55+
{
56+
"_name": "livestore.in-memory-db:execute",
57+
"attributes": {
58+
"sql.query": "
59+
INSERT INTO 'UserInfo' (id, value)
60+
VALUES (?, ?)
61+
ON CONFLICT (id) DO UPDATE SET value = json_set(value, ?, json(?))
62+
",
63+
},
64+
},
65+
],
4266
},
4367
],
4468
},
@@ -78,18 +102,6 @@ exports[`useClientDocument > otel > should update the data based on component ke
78102
],
79103
"livestore.eventsCount": 1,
80104
},
81-
"children": [
82-
{
83-
"_name": "livestore.in-memory-db:execute",
84-
"attributes": {
85-
"sql.query": "
86-
INSERT INTO 'UserInfo' (id, value)
87-
VALUES (?, ?)
88-
ON CONFLICT (id) DO UPDATE SET value = json_set(value, ?, json(?))
89-
",
90-
},
91-
},
92-
],
93105
},
94106
],
95107
},
@@ -120,18 +132,6 @@ exports[`useClientDocument > otel > should update the data based on component ke
120132
],
121133
"livestore.eventsCount": 1,
122134
},
123-
"children": [
124-
{
125-
"_name": "livestore.in-memory-db:execute",
126-
"attributes": {
127-
"sql.query": "
128-
INSERT INTO 'UserInfo' (id, value)
129-
VALUES (?, ?)
130-
ON CONFLICT (id) DO UPDATE SET value = json_set(json_set(value, ?, json(?)), ?, json(?))
131-
",
132-
},
133-
},
134-
],
135135
},
136136
{
137137
"_name": "db:SELECT * FROM 'UserInfo' WHERE id = ?",
@@ -276,6 +276,18 @@ exports[`useClientDocument > otel > should update the data based on component ke
276276
"children": [
277277
{
278278
"_name": "client-session-sync-processor:materialize-event",
279+
"children": [
280+
{
281+
"_name": "livestore.in-memory-db:execute",
282+
"attributes": {
283+
"sql.query": "
284+
INSERT INTO 'UserInfo' (id, value)
285+
VALUES (?, ?)
286+
ON CONFLICT (id) DO UPDATE SET value = json_set(json_set(value, ?, json(?)), ?, json(?))
287+
",
288+
},
289+
},
290+
],
279291
},
280292
],
281293
},
@@ -287,6 +299,18 @@ exports[`useClientDocument > otel > should update the data based on component ke
287299
"children": [
288300
{
289301
"_name": "client-session-sync-processor:materialize-event",
302+
"children": [
303+
{
304+
"_name": "livestore.in-memory-db:execute",
305+
"attributes": {
306+
"sql.query": "
307+
INSERT INTO 'UserInfo' (id, value)
308+
VALUES (?, ?)
309+
ON CONFLICT (id) DO UPDATE SET value = json_set(value, ?, json(?))
310+
",
311+
},
312+
},
313+
],
290314
},
291315
],
292316
},
@@ -326,18 +350,6 @@ exports[`useClientDocument > otel > should update the data based on component ke
326350
],
327351
"livestore.eventsCount": 1,
328352
},
329-
"children": [
330-
{
331-
"_name": "livestore.in-memory-db:execute",
332-
"attributes": {
333-
"sql.query": "
334-
INSERT INTO 'UserInfo' (id, value)
335-
VALUES (?, ?)
336-
ON CONFLICT (id) DO UPDATE SET value = json_set(value, ?, json(?))
337-
",
338-
},
339-
},
340-
],
341353
},
342354
],
343355
},
@@ -368,18 +380,6 @@ exports[`useClientDocument > otel > should update the data based on component ke
368380
],
369381
"livestore.eventsCount": 1,
370382
},
371-
"children": [
372-
{
373-
"_name": "livestore.in-memory-db:execute",
374-
"attributes": {
375-
"sql.query": "
376-
INSERT INTO 'UserInfo' (id, value)
377-
VALUES (?, ?)
378-
ON CONFLICT (id) DO UPDATE SET value = json_set(json_set(value, ?, json(?)), ?, json(?))
379-
",
380-
},
381-
},
382-
],
383383
},
384384
{
385385
"_name": "db:SELECT * FROM 'UserInfo' WHERE id = ?",

0 commit comments

Comments
 (0)