Skip to content

Commit ce5b8d1

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

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
@@ -417,6 +417,14 @@ exports[`otel > otel 3`] = `
417417
"children": [
418418
{
419419
"_name": "client-session-sync-processor:materialize-event",
420+
"children": [
421+
{
422+
"_name": "livestore.in-memory-db:execute",
423+
"attributes": {
424+
"sql.query": "INSERT INTO todos (id, text, completed) VALUES ('t1', 'buy milk', 0)",
425+
},
426+
},
427+
],
420428
},
421429
],
422430
},
@@ -449,14 +457,6 @@ exports[`otel > otel 3`] = `
449457
],
450458
"livestore.eventsCount": 1,
451459
},
452-
"children": [
453-
{
454-
"_name": "livestore.in-memory-db:execute",
455-
"attributes": {
456-
"sql.query": "INSERT INTO todos (id, text, completed) VALUES ('t1', 'buy milk', 0)",
457-
},
458-
},
459-
],
460460
},
461461
],
462462
},
@@ -703,6 +703,14 @@ exports[`otel > with thunks 7`] = `
703703
"children": [
704704
{
705705
"_name": "client-session-sync-processor:materialize-event",
706+
"children": [
707+
{
708+
"_name": "livestore.in-memory-db:execute",
709+
"attributes": {
710+
"sql.query": "INSERT INTO todos (id, text, completed) VALUES ('t1', 'buy milk', 0)",
711+
},
712+
},
713+
],
706714
},
707715
],
708716
},
@@ -735,14 +743,6 @@ exports[`otel > with thunks 7`] = `
735743
],
736744
"livestore.eventsCount": 1,
737745
},
738-
"children": [
739-
{
740-
"_name": "livestore.in-memory-db:execute",
741-
"attributes": {
742-
"sql.query": "INSERT INTO todos (id, text, completed) VALUES ('t1', 'buy milk', 0)",
743-
},
744-
},
745-
],
746746
},
747747
],
748748
},
@@ -823,6 +823,14 @@ exports[`otel > with thunks with query builder and without labels 3`] = `
823823
"children": [
824824
{
825825
"_name": "client-session-sync-processor:materialize-event",
826+
"children": [
827+
{
828+
"_name": "livestore.in-memory-db:execute",
829+
"attributes": {
830+
"sql.query": "INSERT INTO todos (id, text, completed) VALUES ('t1', 'buy milk', 0)",
831+
},
832+
},
833+
],
826834
},
827835
],
828836
},
@@ -855,14 +863,6 @@ exports[`otel > with thunks with query builder and without labels 3`] = `
855863
],
856864
"livestore.eventsCount": 1,
857865
},
858-
"children": [
859-
{
860-
"_name": "livestore.in-memory-db:execute",
861-
"attributes": {
862-
"sql.query": "INSERT INTO todos (id, text, completed) VALUES ('t1', 'buy milk', 0)",
863-
},
864-
},
865-
],
866866
},
867867
],
868868
},

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

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

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

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

@@ -588,7 +590,7 @@ export class Store<TSchema extends LiveStoreSchema = LiveStoreSchema, TContext =
588590
const { writeTables } = (() => {
589591
try {
590592
const materializeEvents = () => {
591-
return Runtime.runSync(this.effectContext.runtime, this.syncProcessor.push(events, { otelContext }))
593+
return Runtime.runSync(this.effectContext.runtime, this.syncProcessor.push(events))
592594
}
593595

594596
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)