Skip to content

Commit 29d458c

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

File tree

4 files changed

+80
-64
lines changed

4 files changed

+80
-64
lines changed

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

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
Stream,
1414
Subscribable,
1515
} from '@livestore/utils/effect'
16-
import * as otel from '@opentelemetry/api'
16+
import type * as otel from '@opentelemetry/api'
1717

1818
import { type ClientSession, SyncError, type UnexpectedError } from '../adapter-types.ts'
1919
import * as EventSequenceNumber from '../schema/EventSequenceNumber.ts'
@@ -52,7 +52,7 @@ export const makeClientSessionSyncProcessor = ({
5252
runtime: Runtime.Runtime<Scope.Scope>
5353
materializeEvent: (
5454
eventDecoded: LiveStoreEvent.AnyDecoded,
55-
options: { otelContext: otel.Context; withChangeset: boolean; materializerHashLeader: Option.Option<number> },
55+
options: { withChangeset: boolean; materializerHashLeader: Option.Option<number> },
5656
) => Effect.Effect<{
5757
writeTables: Set<string>
5858
sessionChangeset: { _tag: 'sessionChangeset'; data: Uint8Array; debug: any } | { _tag: 'no-op' } | { _tag: 'unset' }
@@ -96,10 +96,7 @@ export const makeClientSessionSyncProcessor = ({
9696
/** We're queuing push requests to reduce the number of messages sent to the leader by batching them */
9797
const leaderPushQueue = BucketQueue.make<LiveStoreEvent.EncodedWithMeta>().pipe(Effect.runSync)
9898

99-
const push: ClientSessionSyncProcessor['push'] = Effect.fn('client-session-sync-processor:push')(function* (
100-
batch,
101-
{ otelContext },
102-
) {
99+
const push: ClientSessionSyncProcessor['push'] = Effect.fn('client-session-sync-processor:push')(function* (batch) {
103100
// TODO validate batch
104101

105102
let baseEventSequenceNumber = syncStateRef.current.localHead
@@ -154,7 +151,6 @@ export const makeClientSessionSyncProcessor = ({
154151
sessionChangeset,
155152
materializerHash,
156153
} = yield* materializeEvent(decodedEventDef, {
157-
otelContext,
158154
withChangeset: true,
159155
materializerHashLeader: Option.none(),
160156
})
@@ -180,8 +176,6 @@ export const makeClientSessionSyncProcessor = ({
180176
rejectCount: 0,
181177
}
182178

183-
const otelContext = otel.trace.setSpan(otel.context.active(), span)
184-
185179
const boot: ClientSessionSyncProcessor['boot'] = Effect.gen(function* () {
186180
if (confirmUnsavedChanges && typeof window !== 'undefined' && typeof window.addEventListener === 'function') {
187181
const onBeforeUnload = (event: BeforeUnloadEvent) => {
@@ -306,7 +300,6 @@ export const makeClientSessionSyncProcessor = ({
306300
sessionChangeset,
307301
materializerHash,
308302
} = yield* materializeEvent(decodedEventDef, {
309-
otelContext,
310303
withChangeset: true,
311304
materializerHashLeader: event.meta.materializerHashLeader,
312305
})
@@ -363,10 +356,7 @@ export const makeClientSessionSyncProcessor = ({
363356
}
364357

365358
export interface ClientSessionSyncProcessor {
366-
push: (
367-
batch: ReadonlyArray<LiveStoreEvent.PartialAnyDecoded>,
368-
options: { otelContext: otel.Context },
369-
) => Effect.Effect<
359+
push: (batch: ReadonlyArray<LiveStoreEvent.PartialAnyDecoded>) => Effect.Effect<
370360
{
371361
writeTables: Set<string>
372362
},

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

Lines changed: 24 additions & 0 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
},
@@ -727,6 +735,14 @@ exports[`otel > with thunks 7`] = `
727735
"children": [
728736
{
729737
"_name": "client-session-sync-processor:materialize-event",
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+
],
730746
},
731747
],
732748
},
@@ -847,6 +863,14 @@ exports[`otel > with thunks with query builder and without labels 3`] = `
847863
"children": [
848864
{
849865
"_name": "client-session-sync-processor:materialize-event",
866+
"children": [
867+
{
868+
"_name": "livestore.in-memory-db:execute",
869+
"attributes": {
870+
"sql.query": "INSERT INTO todos (id, text, completed) VALUES ('t1', 'buy milk', 0)",
871+
},
872+
},
873+
],
850874
},
851875
],
852876
},

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ export class Store<TSchema extends LiveStoreSchema = LiveStoreSchema.Any, TConte
129129
clientSession,
130130
runtime: effectContext.runtime,
131131
materializeEvent: Effect.fn('client-session-sync-processor:materialize-event')(
132-
(eventDecoded, { otelContext, withChangeset, materializerHashLeader }) =>
132+
(eventDecoded, { withChangeset, materializerHashLeader }) =>
133133
Effect.gen(this, function* () {
134134
const { eventDef, materializer } = getEventDef(schema, eventDecoded.name)
135135

@@ -161,6 +161,8 @@ export class Store<TSchema extends LiveStoreSchema = LiveStoreSchema.Any, TConte
161161
)
162162
}
163163

164+
const span = yield* OtelTracer.currentOtelSpan.pipe(Effect.orDie)
165+
const otelContext = otel.trace.setSpan(otel.context.active(), span)
164166
return yield* Effect.sync(() => {
165167
const writeTablesForEvent = new Set<string>()
166168

@@ -641,7 +643,7 @@ export class Store<TSchema extends LiveStoreSchema = LiveStoreSchema.Any, TConte
641643
const { writeTables } = (() => {
642644
try {
643645
const materializeEvents = () => {
644-
return Runtime.runSync(this.effectContext.runtime, this.syncProcessor.push(events, { otelContext }))
646+
return Runtime.runSync(this.effectContext.runtime, this.syncProcessor.push(events))
645647
}
646648

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