Skip to content

Commit 000cf5b

Browse files
committed
refactor: link "livestore.in-memory-db:execute" to "client-session-sync-processor:materialize-event" span
1 parent 59110d7 commit 000cf5b

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
@@ -441,6 +441,14 @@ exports[`otel > otel 3`] = `
441441
"children": [
442442
{
443443
"_name": "client-session-sync-processor:materialize-event",
444+
"children": [
445+
{
446+
"_name": "livestore.in-memory-db:execute",
447+
"attributes": {
448+
"sql.query": "INSERT INTO todos (id, text, completed) VALUES ('t1', 'buy milk', 0)",
449+
},
450+
},
451+
],
444452
},
445453
],
446454
},
@@ -473,14 +481,6 @@ exports[`otel > otel 3`] = `
473481
],
474482
"livestore.eventsCount": 1,
475483
},
476-
"children": [
477-
{
478-
"_name": "livestore.in-memory-db:execute",
479-
"attributes": {
480-
"sql.query": "INSERT INTO todos (id, text, completed) VALUES ('t1', 'buy milk', 0)",
481-
},
482-
},
483-
],
484484
},
485485
],
486486
},
@@ -727,6 +727,14 @@ exports[`otel > with thunks 7`] = `
727727
"children": [
728728
{
729729
"_name": "client-session-sync-processor:materialize-event",
730+
"children": [
731+
{
732+
"_name": "livestore.in-memory-db:execute",
733+
"attributes": {
734+
"sql.query": "INSERT INTO todos (id, text, completed) VALUES ('t1', 'buy milk', 0)",
735+
},
736+
},
737+
],
730738
},
731739
],
732740
},
@@ -759,14 +767,6 @@ exports[`otel > with thunks 7`] = `
759767
],
760768
"livestore.eventsCount": 1,
761769
},
762-
"children": [
763-
{
764-
"_name": "livestore.in-memory-db:execute",
765-
"attributes": {
766-
"sql.query": "INSERT INTO todos (id, text, completed) VALUES ('t1', 'buy milk', 0)",
767-
},
768-
},
769-
],
770770
},
771771
],
772772
},
@@ -847,6 +847,14 @@ exports[`otel > with thunks with query builder and without labels 3`] = `
847847
"children": [
848848
{
849849
"_name": "client-session-sync-processor:materialize-event",
850+
"children": [
851+
{
852+
"_name": "livestore.in-memory-db:execute",
853+
"attributes": {
854+
"sql.query": "INSERT INTO todos (id, text, completed) VALUES ('t1', 'buy milk', 0)",
855+
},
856+
},
857+
],
850858
},
851859
],
852860
},
@@ -879,14 +887,6 @@ exports[`otel > with thunks with query builder and without labels 3`] = `
879887
],
880888
"livestore.eventsCount": 1,
881889
},
882-
"children": [
883-
{
884-
"_name": "livestore.in-memory-db:execute",
885-
"attributes": {
886-
"sql.query": "INSERT INTO todos (id, text, completed) VALUES ('t1', 'buy milk', 0)",
887-
},
888-
},
889-
],
890890
},
891891
],
892892
},

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)