Skip to content

Commit daeb6f0

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

File tree

4 files changed

+112
-120
lines changed

4 files changed

+112
-120
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: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ exports[`otel > QueryBuilder subscription - basic functionality 1`] = `
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 (?, ?, ?)",
36+
},
37+
},
38+
],
3139
},
3240
],
3341
},
@@ -60,14 +68,6 @@ exports[`otel > QueryBuilder subscription - basic functionality 1`] = `
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 (?, ?, ?)",
68-
},
69-
},
70-
],
7171
},
7272
],
7373
},
@@ -152,6 +152,14 @@ exports[`otel > QueryBuilder subscription - direct table subscription 1`] = `
152152
"children": [
153153
{
154154
"_name": "client-session-sync-processor:materialize-event",
155+
"children": [
156+
{
157+
"_name": "livestore.in-memory-db:execute",
158+
"attributes": {
159+
"sql.query": "INSERT INTO 'todos' (id, text, completed) VALUES (?, ?, ?)",
160+
},
161+
},
162+
],
155163
},
156164
],
157165
},
@@ -184,14 +192,6 @@ exports[`otel > QueryBuilder subscription - direct table subscription 1`] = `
184192
],
185193
"livestore.eventsCount": 1,
186194
},
187-
"children": [
188-
{
189-
"_name": "livestore.in-memory-db:execute",
190-
"attributes": {
191-
"sql.query": "INSERT INTO 'todos' (id, text, completed) VALUES (?, ?, ?)",
192-
},
193-
},
194-
],
195195
},
196196
],
197197
},
@@ -276,6 +276,14 @@ exports[`otel > QueryBuilder subscription - unsubscribe functionality 1`] = `
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": "INSERT INTO 'todos' (id, text, completed) VALUES (?, ?, ?)",
284+
},
285+
},
286+
],
279287
},
280288
],
281289
},
@@ -287,6 +295,14 @@ exports[`otel > QueryBuilder subscription - unsubscribe functionality 1`] = `
287295
"children": [
288296
{
289297
"_name": "client-session-sync-processor:materialize-event",
298+
"children": [
299+
{
300+
"_name": "livestore.in-memory-db:execute",
301+
"attributes": {
302+
"sql.query": "INSERT INTO 'todos' (id, text, completed) VALUES (?, ?, ?)",
303+
},
304+
},
305+
],
290306
},
291307
],
292308
},
@@ -326,14 +342,6 @@ exports[`otel > QueryBuilder subscription - unsubscribe functionality 1`] = `
326342
],
327343
"livestore.eventsCount": 1,
328344
},
329-
"children": [
330-
{
331-
"_name": "livestore.in-memory-db:execute",
332-
"attributes": {
333-
"sql.query": "INSERT INTO 'todos' (id, text, completed) VALUES (?, ?, ?)",
334-
},
335-
},
336-
],
337345
},
338346
{
339347
"_name": "LiveStore:commit",
@@ -343,14 +351,6 @@ exports[`otel > QueryBuilder subscription - unsubscribe functionality 1`] = `
343351
],
344352
"livestore.eventsCount": 1,
345353
},
346-
"children": [
347-
{
348-
"_name": "livestore.in-memory-db:execute",
349-
"attributes": {
350-
"sql.query": "INSERT INTO 'todos' (id, text, completed) VALUES (?, ?, ?)",
351-
},
352-
},
353-
],
354354
},
355355
],
356356
},
@@ -461,6 +461,14 @@ exports[`otel > otel 3`] = `
461461
"children": [
462462
{
463463
"_name": "client-session-sync-processor:materialize-event",
464+
"children": [
465+
{
466+
"_name": "livestore.in-memory-db:execute",
467+
"attributes": {
468+
"sql.query": "INSERT INTO 'todos' (id, text, completed) VALUES (?, ?, ?)",
469+
},
470+
},
471+
],
464472
},
465473
],
466474
},
@@ -493,14 +501,6 @@ exports[`otel > otel 3`] = `
493501
],
494502
"livestore.eventsCount": 1,
495503
},
496-
"children": [
497-
{
498-
"_name": "livestore.in-memory-db:execute",
499-
"attributes": {
500-
"sql.query": "INSERT INTO 'todos' (id, text, completed) VALUES (?, ?, ?)",
501-
},
502-
},
503-
],
504504
},
505505
],
506506
},
@@ -747,6 +747,14 @@ exports[`otel > with thunks 7`] = `
747747
"children": [
748748
{
749749
"_name": "client-session-sync-processor:materialize-event",
750+
"children": [
751+
{
752+
"_name": "livestore.in-memory-db:execute",
753+
"attributes": {
754+
"sql.query": "INSERT INTO 'todos' (id, text, completed) VALUES (?, ?, ?)",
755+
},
756+
},
757+
],
750758
},
751759
],
752760
},
@@ -779,14 +787,6 @@ exports[`otel > with thunks 7`] = `
779787
],
780788
"livestore.eventsCount": 1,
781789
},
782-
"children": [
783-
{
784-
"_name": "livestore.in-memory-db:execute",
785-
"attributes": {
786-
"sql.query": "INSERT INTO 'todos' (id, text, completed) VALUES (?, ?, ?)",
787-
},
788-
},
789-
],
790790
},
791791
],
792792
},
@@ -867,6 +867,14 @@ exports[`otel > with thunks with query builder and without labels 3`] = `
867867
"children": [
868868
{
869869
"_name": "client-session-sync-processor:materialize-event",
870+
"children": [
871+
{
872+
"_name": "livestore.in-memory-db:execute",
873+
"attributes": {
874+
"sql.query": "INSERT INTO 'todos' (id, text, completed) VALUES (?, ?, ?)",
875+
},
876+
},
877+
],
870878
},
871879
],
872880
},
@@ -899,14 +907,6 @@ exports[`otel > with thunks with query builder and without labels 3`] = `
899907
],
900908
"livestore.eventsCount": 1,
901909
},
902-
"children": [
903-
{
904-
"_name": "livestore.in-memory-db:execute",
905-
"attributes": {
906-
"sql.query": "INSERT INTO 'todos' (id, text, completed) VALUES (?, ?, ?)",
907-
},
908-
},
909-
],
910910
},
911911
],
912912
},

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

0 commit comments

Comments
 (0)