effect-start 0.26.0 → 0.27.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. package/package.json +4 -2
  2. package/src/Entity.ts +6 -6
  3. package/src/FileRouterCodegen.ts +4 -4
  4. package/src/FileSystem.ts +4 -8
  5. package/src/RouteHook.ts +1 -1
  6. package/src/RouteSse.ts +3 -3
  7. package/src/SqlIntrospect.ts +2 -2
  8. package/src/Start.ts +102 -2
  9. package/src/Values.ts +11 -0
  10. package/src/bun/BunRoute.ts +1 -1
  11. package/src/bun/BunRuntime.ts +5 -5
  12. package/src/hyper/HyperHtml.ts +11 -7
  13. package/src/hyper/jsx.d.ts +1 -1
  14. package/src/lint/plugin.js +174 -4
  15. package/src/sql/SqlClient.ts +355 -0
  16. package/src/sql/bun/index.ts +117 -50
  17. package/src/sql/index.ts +1 -1
  18. package/src/sql/libsql/index.ts +91 -77
  19. package/src/sql/libsql/libsql.d.ts +4 -1
  20. package/src/sql/mssql/index.ts +141 -108
  21. package/src/sql/mssql/mssql.d.ts +1 -0
  22. package/src/testing/TestLogger.ts +4 -4
  23. package/src/x/tailwind/compile.ts +6 -14
  24. package/src/console/Console.ts +0 -42
  25. package/src/console/ConsoleErrors.ts +0 -213
  26. package/src/console/ConsoleLogger.ts +0 -56
  27. package/src/console/ConsoleMetrics.ts +0 -72
  28. package/src/console/ConsoleProcess.ts +0 -59
  29. package/src/console/ConsoleStore.ts +0 -187
  30. package/src/console/ConsoleTracer.ts +0 -107
  31. package/src/console/Simulation.ts +0 -814
  32. package/src/console/console.html +0 -340
  33. package/src/console/index.ts +0 -3
  34. package/src/console/routes/errors/route.tsx +0 -97
  35. package/src/console/routes/fiberDetail.tsx +0 -54
  36. package/src/console/routes/fibers/route.tsx +0 -45
  37. package/src/console/routes/git/route.tsx +0 -64
  38. package/src/console/routes/layout.tsx +0 -4
  39. package/src/console/routes/logs/route.tsx +0 -77
  40. package/src/console/routes/metrics/route.tsx +0 -36
  41. package/src/console/routes/route.tsx +0 -8
  42. package/src/console/routes/routes/route.tsx +0 -30
  43. package/src/console/routes/services/route.tsx +0 -21
  44. package/src/console/routes/system/route.tsx +0 -43
  45. package/src/console/routes/traceDetail.tsx +0 -22
  46. package/src/console/routes/traces/route.tsx +0 -81
  47. package/src/console/routes/tree.ts +0 -30
  48. package/src/console/ui/Errors.tsx +0 -76
  49. package/src/console/ui/Fibers.tsx +0 -321
  50. package/src/console/ui/Git.tsx +0 -182
  51. package/src/console/ui/Logs.tsx +0 -46
  52. package/src/console/ui/Metrics.tsx +0 -78
  53. package/src/console/ui/Routes.tsx +0 -125
  54. package/src/console/ui/Services.tsx +0 -273
  55. package/src/console/ui/Shell.tsx +0 -62
  56. package/src/console/ui/System.tsx +0 -131
  57. package/src/console/ui/Traces.tsx +0 -426
  58. package/src/sql/Sql.ts +0 -51
@@ -1,213 +0,0 @@
1
- import * as Cause from "effect/Cause"
2
- import * as Chunk from "effect/Chunk"
3
- import * as Effect from "effect/Effect"
4
- import * as Exit from "effect/Exit"
5
- import * as FiberId from "effect/FiberId"
6
- import * as FiberRef from "effect/FiberRef"
7
- import * as HashMap from "effect/HashMap"
8
- import * as Layer from "effect/Layer"
9
- import * as Option from "effect/Option"
10
- import * as PubSub from "effect/PubSub"
11
- import type * as Context from "effect/Context"
12
- import type * as Fiber from "effect/Fiber"
13
- import * as Supervisor from "effect/Supervisor"
14
- import * as ConsoleStore from "./ConsoleStore.ts"
15
-
16
- let errorId = 0
17
-
18
- function safeSerialize(value: unknown, depth = 0): unknown {
19
- if (depth > 4) return "<deep>"
20
- if (value === null || value === undefined) return value
21
- if (typeof value === "bigint") return `${value}n`
22
- if (typeof value === "function") return undefined
23
- if (typeof value === "symbol") return value.toString()
24
- if (typeof value !== "object") return value
25
- if (value instanceof Date) return value.toISOString()
26
- if (value instanceof Error) return value.message
27
- if (Array.isArray(value)) return value.slice(0, 20).map((v) => safeSerialize(v, depth + 1))
28
- const proto = Object.getPrototypeOf(value)
29
- if (proto !== null && proto !== Object.prototype) {
30
- if (typeof (value as any)._tag === "string") {
31
- return serializeTaggedObject(value as Record<string, unknown>, depth)
32
- }
33
- return `<${proto.constructor?.name ?? "object"}>`
34
- }
35
- return serializePlainObject(value as Record<string, unknown>, depth)
36
- }
37
-
38
- function serializePlainObject(
39
- obj: Record<string, unknown>,
40
- depth: number,
41
- ): Record<string, unknown> {
42
- const out: Record<string, unknown> = {}
43
- let count = 0
44
- for (const [k, v] of Object.entries(obj)) {
45
- if (count >= 20) break
46
- if (typeof v === "function") continue
47
- const serialized = safeSerialize(v, depth + 1)
48
- if (serialized !== undefined) {
49
- out[k] = serialized
50
- count++
51
- }
52
- }
53
- return out
54
- }
55
-
56
- function serializeTaggedObject(
57
- obj: Record<string, unknown>,
58
- depth: number,
59
- ): Record<string, unknown> {
60
- const out: Record<string, unknown> = { _tag: obj._tag }
61
- let count = 0
62
- for (const [k, v] of Object.entries(obj)) {
63
- if (count >= 20) break
64
- if (k === "_tag") continue
65
- if (typeof v === "function") continue
66
- if (k === "stack" || k === "name") continue
67
- const serialized = safeSerialize(v, depth + 1)
68
- if (serialized !== undefined) {
69
- out[k] = serialized
70
- count++
71
- }
72
- }
73
- return out
74
- }
75
-
76
- function extractTag(error: unknown): string | undefined {
77
- if (
78
- error !== null &&
79
- typeof error === "object" &&
80
- "_tag" in error &&
81
- typeof (error as any)._tag === "string"
82
- ) {
83
- return (error as any)._tag
84
- }
85
- return undefined
86
- }
87
-
88
- function extractMessage(error: unknown): string {
89
- if (error instanceof Error) return error.message
90
- if (typeof error === "string") return error
91
- const tag = extractTag(error)
92
- if (tag) return tag
93
- try {
94
- return String(error)
95
- } catch {
96
- return "<unknown>"
97
- }
98
- }
99
-
100
- function extractProperties(error: unknown): Record<string, unknown> {
101
- if (error === null || error === undefined || typeof error !== "object") return {}
102
- const out: Record<string, unknown> = {}
103
- let count = 0
104
- for (const [k, v] of Object.entries(error)) {
105
- if (count >= 20) break
106
- if (k === "_tag" || k === "stack" || k === "name") continue
107
- if (typeof v === "function") continue
108
- const serialized = safeSerialize(v, 0)
109
- if (serialized !== undefined) {
110
- out[k] = serialized
111
- count++
112
- }
113
- }
114
- return out
115
- }
116
-
117
- const spanSymbol = Symbol.for("effect/SpanAnnotation")
118
-
119
- function extractSpanName(error: unknown): string | undefined {
120
- if (error !== null && typeof error === "object" && spanSymbol in error) {
121
- const span = (error as any)[spanSymbol]
122
- return typeof span?.name === "string" ? span.name : undefined
123
- }
124
- return undefined
125
- }
126
-
127
- function extractDetails(cause: Cause.Cause<unknown>): Array<ConsoleStore.ConsoleErrorDetail> {
128
- const details: Array<ConsoleStore.ConsoleErrorDetail> = []
129
-
130
- const failures = Chunk.toArray(Cause.failures(cause))
131
- for (const error of failures) {
132
- details.push({
133
- kind: "fail",
134
- tag: extractTag(error),
135
- message: extractMessage(error),
136
- properties: extractProperties(error),
137
- span: extractSpanName(error),
138
- })
139
- }
140
-
141
- const defects = Chunk.toArray(Cause.defects(cause))
142
- for (const defect of defects) {
143
- details.push({
144
- kind: "die",
145
- tag: extractTag(defect),
146
- message: extractMessage(defect),
147
- properties: extractProperties(defect),
148
- span: extractSpanName(defect),
149
- })
150
- }
151
-
152
- return details
153
- }
154
-
155
- function make(store: ConsoleStore.ConsoleStoreShape): Supervisor.Supervisor<void> {
156
- return new (class extends Supervisor.AbstractSupervisor<void> {
157
- value = Effect.void
158
-
159
- onStart<A, E, R>(
160
- _context: Context.Context<R>,
161
- _effect: Effect.Effect<A, E, R>,
162
- parent: Option.Option<Fiber.RuntimeFiber<any, any>>,
163
- fiber: Fiber.RuntimeFiber<A, E>,
164
- ) {
165
- const childId = FiberId.threadName(fiber.id())
166
- if (Option.isSome(parent)) {
167
- const parentId = FiberId.threadName(parent.value.id())
168
- if (childId !== parentId) {
169
- store.fiberParents.set(childId, parentId)
170
- }
171
- }
172
-
173
- const span = fiber.currentSpan
174
- const annotations: Record<string, unknown> = {}
175
- const spanAnnotations = fiber.getFiberRef(FiberRef.currentTracerSpanAnnotations)
176
- HashMap.forEach(spanAnnotations, (value, key) => {
177
- annotations[key] = value
178
- })
179
- const logAnnotations = fiber.getFiberRef(FiberRef.currentLogAnnotations)
180
- HashMap.forEach(logAnnotations, (value, key) => {
181
- annotations[key] = value
182
- })
183
-
184
- store.fiberContexts.set(childId, {
185
- spanName: span?._tag === "Span" ? span.name : undefined,
186
- traceId: span ? span.traceId : undefined,
187
- annotations,
188
- })
189
- }
190
-
191
- onEnd<A, E>(exit: Exit.Exit<A, E>, fiber: Fiber.RuntimeFiber<A, E>) {
192
- if (Exit.isFailure(exit) && !Cause.isInterruptedOnly(exit.cause)) {
193
- const error: ConsoleStore.ConsoleError = {
194
- id: String(++errorId),
195
- date: new Date(),
196
- fiberId: FiberId.threadName(fiber.id()),
197
- interrupted: Cause.isInterrupted(exit.cause),
198
- prettyPrint: Cause.pretty(exit.cause, { renderErrorCause: true }),
199
- details: extractDetails(exit.cause),
200
- }
201
- store.errors.push(error)
202
- Effect.runSync(PubSub.publish(store.events, { _tag: "Error", error }))
203
- }
204
- }
205
- })()
206
- }
207
-
208
- export const layer: Layer.Layer<never, never, ConsoleStore.ConsoleStore> = Layer.unwrapEffect(
209
- Effect.gen(function* () {
210
- const store = yield* ConsoleStore.ConsoleStore
211
- return Supervisor.addSupervisor(make(store))
212
- }),
213
- )
@@ -1,56 +0,0 @@
1
- import * as Cause from "effect/Cause"
2
- import * as Effect from "effect/Effect"
3
- import * as FiberId from "effect/FiberId"
4
- import * as FiberRef from "effect/FiberRef"
5
- import * as HashMap from "effect/HashMap"
6
- import * as HashSet from "effect/HashSet"
7
- import * as Layer from "effect/Layer"
8
- import * as List from "effect/List"
9
- import * as Logger from "effect/Logger"
10
- import * as PubSub from "effect/PubSub"
11
- import * as ConsoleStore from "./ConsoleStore.ts"
12
-
13
- let logId = 0
14
-
15
- export const layer: Layer.Layer<never, never, ConsoleStore.ConsoleStore> = Layer.effectDiscard(
16
- Effect.gen(function* () {
17
- const store = yield* ConsoleStore.ConsoleStore
18
-
19
- const logger = Logger.make(
20
- ({ message, logLevel, cause, fiberId, spans, annotations, date }) => {
21
- const levelMap: Record<string, ConsoleStore.ConsoleLog["level"]> = {
22
- Debug: "DEBUG",
23
- Info: "INFO",
24
- Warning: "WARNING",
25
- Error: "ERROR",
26
- Fatal: "FATAL",
27
- }
28
- const level = levelMap[logLevel._tag] ?? "INFO"
29
- const causeStr = !Cause.isEmpty(cause)
30
- ? Cause.pretty(cause, { renderErrorCause: true })
31
- : undefined
32
- const spanNames: Array<string> = []
33
- List.forEach(spans, (s) => spanNames.push(s.label))
34
- const ann: Record<string, unknown> = {}
35
- HashMap.forEach(annotations, (v, k) => {
36
- ann[k] = v
37
- })
38
-
39
- const log: ConsoleStore.ConsoleLog = {
40
- id: String(++logId),
41
- date,
42
- level,
43
- message: String(message),
44
- fiberId: FiberId.threadName(fiberId),
45
- cause: causeStr,
46
- spans: spanNames,
47
- annotations: ann,
48
- }
49
- store.logs.push(log)
50
- Effect.runSync(PubSub.publish(store.events, { _tag: "Log", log }))
51
- },
52
- )
53
-
54
- yield* FiberRef.update(FiberRef.currentLoggers, (loggers) => HashSet.add(loggers, logger))
55
- }),
56
- )
@@ -1,72 +0,0 @@
1
- import * as Effect from "effect/Effect"
2
- import * as Layer from "effect/Layer"
3
- import * as Metric from "effect/Metric"
4
- import * as MetricKeyType from "effect/MetricKeyType"
5
- import * as PubSub from "effect/PubSub"
6
- import * as Schedule from "effect/Schedule"
7
- import * as ConsoleStore from "./ConsoleStore.ts"
8
-
9
- export const layer: Layer.Layer<never, never, ConsoleStore.ConsoleStore> = Layer.scopedDiscard(
10
- Effect.gen(function* () {
11
- const store = yield* ConsoleStore.ConsoleStore
12
-
13
- yield* Effect.forkScoped(
14
- Effect.schedule(
15
- Effect.sync(() => {
16
- const pairs = Metric.unsafeSnapshot()
17
- const snapshots: Array<ConsoleStore.ConsoleMetricSnapshot> = []
18
-
19
- for (const pair of pairs) {
20
- const key = pair.metricKey
21
- const state = pair.metricState as any
22
- let type: ConsoleStore.ConsoleMetricSnapshot["type"] = "counter"
23
- let value: unknown = 0
24
-
25
- if (MetricKeyType.CounterKeyTypeTypeId in key.keyType) {
26
- type = "counter"
27
- value = state.count
28
- } else if (MetricKeyType.GaugeKeyTypeTypeId in key.keyType) {
29
- type = "gauge"
30
- value = state.value
31
- } else if (MetricKeyType.HistogramKeyTypeTypeId in key.keyType) {
32
- type = "histogram"
33
- value = {
34
- buckets: state.buckets,
35
- count: state.count,
36
- sum: state.sum,
37
- min: state.min,
38
- max: state.max,
39
- }
40
- } else if (MetricKeyType.FrequencyKeyTypeTypeId in key.keyType) {
41
- type = "frequency"
42
- value = Object.fromEntries(state.occurrences)
43
- } else if (MetricKeyType.SummaryKeyTypeTypeId in key.keyType) {
44
- type = "summary"
45
- value = {
46
- quantiles: state.quantiles,
47
- count: state.count,
48
- sum: state.sum,
49
- min: state.min,
50
- max: state.max,
51
- }
52
- }
53
-
54
- snapshots.push({
55
- name: key.name,
56
- type,
57
- value,
58
- tags: key.tags.map((t: any) => ({ key: t.key, value: t.value })),
59
- timestamp: Date.now(),
60
- })
61
- }
62
-
63
- store.metrics = snapshots
64
- Effect.runSync(
65
- PubSub.publish(store.events, { _tag: "MetricsSnapshot", metrics: snapshots }),
66
- )
67
- }),
68
- Schedule.spaced("2 seconds"),
69
- ),
70
- )
71
- }),
72
- )
@@ -1,59 +0,0 @@
1
- import * as NOS from "node:os"
2
- import * as Effect from "effect/Effect"
3
- import * as Layer from "effect/Layer"
4
- import * as PubSub from "effect/PubSub"
5
- import * as Schedule from "effect/Schedule"
6
- import * as ConsoleStore from "./ConsoleStore.ts"
7
-
8
- function snapshot(): ConsoleStore.ProcessStats {
9
- const mem = process.memoryUsage()
10
- const cpu = process.cpuUsage()
11
- const res = process.resourceUsage()
12
- const loadavg = NOS.loadavg() as [number, number, number]
13
- return {
14
- pid: process.pid,
15
- uptime: process.uptime(),
16
- memory: {
17
- rss: mem.rss,
18
- heapUsed: mem.heapUsed,
19
- heapTotal: mem.heapTotal,
20
- external: mem.external,
21
- arrayBuffers: mem.arrayBuffers,
22
- },
23
- cpu: { user: cpu.user, system: cpu.system },
24
- resourceUsage: {
25
- maxRSS: res.maxRSS,
26
- minorPageFault: res.minorPageFault,
27
- majorPageFault: res.majorPageFault,
28
- fsRead: res.fsRead,
29
- fsWrite: res.fsWrite,
30
- voluntaryContextSwitches: res.voluntaryContextSwitches,
31
- involuntaryContextSwitches: res.involuntaryContextSwitches,
32
- },
33
- system: {
34
- loadavg,
35
- freemem: NOS.freemem(),
36
- totalmem: NOS.totalmem(),
37
- cpuCount: NOS.cpus().length,
38
- platform: NOS.platform(),
39
- arch: NOS.arch(),
40
- },
41
- }
42
- }
43
-
44
- export const layer: Layer.Layer<never, never, ConsoleStore.ConsoleStore> = Layer.scopedDiscard(
45
- Effect.gen(function* () {
46
- const store = yield* ConsoleStore.ConsoleStore
47
-
48
- yield* Effect.forkScoped(
49
- Effect.schedule(
50
- Effect.sync(() => {
51
- const stats = snapshot()
52
- store.process = stats
53
- Effect.runSync(PubSub.publish(store.events, { _tag: "ProcessSnapshot", stats }))
54
- }),
55
- Schedule.spaced("2 seconds"),
56
- ),
57
- )
58
- }),
59
- )
@@ -1,187 +0,0 @@
1
- import * as Context from "effect/Context"
2
- import * as Effect from "effect/Effect"
3
- import * as GlobalValue from "effect/GlobalValue"
4
- import * as Layer from "effect/Layer"
5
- import * as MutableRef from "effect/MutableRef"
6
- import * as PubSub from "effect/PubSub"
7
-
8
- export const store: ConsoleStoreShape = GlobalValue.globalValue(
9
- Symbol.for("effect-start/ConsoleStore"),
10
- () => ({
11
- prefix: "/console",
12
- spans: new CircularBuffer<ConsoleSpan>(1000),
13
- logs: new CircularBuffer<ConsoleLog>(5000),
14
- errors: new CircularBuffer<ConsoleError>(1000),
15
- metrics: [] as Array<ConsoleMetricSnapshot>,
16
- process: undefined as ProcessStats | undefined,
17
- events: Effect.runSync(PubSub.unbounded<ConsoleEvent>()),
18
- fiberParents: new Map<string, string>(),
19
- fiberContexts: new Map<string, FiberContext>(),
20
- }),
21
- )
22
-
23
- export interface ConsoleSpan {
24
- readonly spanId: string
25
- readonly traceId: string
26
- readonly name: string
27
- readonly kind: string
28
- readonly parentSpanId: string | undefined
29
- startTime: bigint
30
- endTime: bigint | undefined
31
- durationMs: number | undefined
32
- status: "started" | "ok" | "error"
33
- readonly attributes: Record<string, unknown>
34
- readonly events: Array<{ name: string; startTime: bigint; attributes?: Record<string, unknown> }>
35
- }
36
-
37
- export interface ConsoleLog {
38
- readonly id: string
39
- readonly date: Date
40
- readonly level: "DEBUG" | "INFO" | "WARNING" | "ERROR" | "FATAL"
41
- readonly message: string
42
- readonly fiberId: string
43
- readonly cause: string | undefined
44
- readonly spans: Array<string>
45
- readonly annotations: Record<string, unknown>
46
- }
47
-
48
- export interface ProcessStats {
49
- readonly pid: number
50
- readonly uptime: number
51
- readonly memory: {
52
- readonly rss: number
53
- readonly heapUsed: number
54
- readonly heapTotal: number
55
- readonly external: number
56
- readonly arrayBuffers: number
57
- }
58
- readonly cpu: { readonly user: number; readonly system: number }
59
- readonly resourceUsage: {
60
- readonly maxRSS: number
61
- readonly minorPageFault: number
62
- readonly majorPageFault: number
63
- readonly fsRead: number
64
- readonly fsWrite: number
65
- readonly voluntaryContextSwitches: number
66
- readonly involuntaryContextSwitches: number
67
- }
68
- readonly system: {
69
- readonly loadavg: readonly [number, number, number]
70
- readonly freemem: number
71
- readonly totalmem: number
72
- readonly cpuCount: number
73
- readonly platform: string
74
- readonly arch: string
75
- }
76
- }
77
-
78
- export interface ConsoleErrorDetail {
79
- readonly kind: "fail" | "die"
80
- readonly tag: string | undefined
81
- readonly message: string
82
- readonly properties: Record<string, unknown>
83
- readonly span: string | undefined
84
- }
85
-
86
- export interface ConsoleError {
87
- readonly id: string
88
- readonly date: Date
89
- readonly fiberId: string
90
- readonly interrupted: boolean
91
- readonly prettyPrint: string
92
- readonly details: Array<ConsoleErrorDetail>
93
- }
94
-
95
- export interface ConsoleMetricSnapshot {
96
- readonly name: string
97
- readonly type: "counter" | "gauge" | "histogram" | "summary" | "frequency"
98
- readonly value: unknown
99
- readonly tags: ReadonlyArray<{ key: string; value: string }>
100
- readonly timestamp: number
101
- }
102
-
103
- export type ConsoleEvent =
104
- | { readonly _tag: "SpanStart"; readonly span: ConsoleSpan }
105
- | { readonly _tag: "SpanEnd"; readonly span: ConsoleSpan }
106
- | { readonly _tag: "Log"; readonly log: ConsoleLog }
107
- | { readonly _tag: "Error"; readonly error: ConsoleError }
108
- | { readonly _tag: "MetricsSnapshot"; readonly metrics: Array<ConsoleMetricSnapshot> }
109
- | { readonly _tag: "ProcessSnapshot"; readonly stats: ProcessStats }
110
-
111
- export class CircularBuffer<T> {
112
- private buffer: Array<T | undefined>
113
- private head = 0
114
- private count = 0
115
- capacity: number
116
-
117
- constructor(capacity: number) {
118
- this.capacity = capacity
119
- this.buffer = Array.from({ length: capacity })
120
- }
121
-
122
- push(item: T): void {
123
- this.buffer[this.head] = item
124
- this.head = (this.head + 1) % this.capacity
125
- if (this.count < this.capacity) this.count++
126
- }
127
-
128
- toArray(): Array<T> {
129
- if (this.count === 0) return []
130
- const result: Array<T> = []
131
- const start = this.count < this.capacity ? 0 : this.head
132
- for (let i = 0; i < this.count; i++) {
133
- result.push(this.buffer[(start + i) % this.capacity]!)
134
- }
135
- return result
136
- }
137
-
138
- get size(): number {
139
- return this.count
140
- }
141
- }
142
-
143
- export interface FiberContext {
144
- readonly spanName: string | undefined
145
- readonly traceId: string | undefined
146
- readonly annotations: Record<string, unknown>
147
- }
148
-
149
- export interface ConsoleStoreShape {
150
- prefix: string
151
- readonly spans: CircularBuffer<ConsoleSpan>
152
- readonly logs: CircularBuffer<ConsoleLog>
153
- readonly errors: CircularBuffer<ConsoleError>
154
- metrics: Array<ConsoleMetricSnapshot>
155
- process: ProcessStats | undefined
156
- readonly events: PubSub.PubSub<ConsoleEvent>
157
- readonly fiberParents: Map<string, string>
158
- readonly fiberContexts: Map<string, FiberContext>
159
- }
160
-
161
- export function fiberIdCounter(): number {
162
- const counter = GlobalValue.globalValue(Symbol.for("effect/Fiber/Id/_fiberCounter"), () =>
163
- MutableRef.make(0),
164
- )
165
- return MutableRef.get(counter)
166
- }
167
-
168
- export class ConsoleStore extends Context.Tag("effect-start/ConsoleStore")<
169
- ConsoleStore,
170
- ConsoleStoreShape
171
- >() {}
172
-
173
- export interface ConsoleStoreOptions {
174
- readonly spanCapacity?: number
175
- readonly logCapacity?: number
176
- readonly errorCapacity?: number
177
- }
178
-
179
- export function layer(options?: ConsoleStoreOptions): Layer.Layer<ConsoleStore> {
180
- if (options?.spanCapacity)
181
- (store as any).spans = new CircularBuffer<ConsoleSpan>(options.spanCapacity)
182
- if (options?.logCapacity)
183
- (store as any).logs = new CircularBuffer<ConsoleLog>(options.logCapacity)
184
- if (options?.errorCapacity)
185
- (store as any).errors = new CircularBuffer<ConsoleError>(options.errorCapacity)
186
- return Layer.sync(ConsoleStore, () => store)
187
- }
@@ -1,107 +0,0 @@
1
- import * as Effect from "effect/Effect"
2
- import * as Exit from "effect/Exit"
3
- import * as Fiber from "effect/Fiber"
4
- import * as FiberId from "effect/FiberId"
5
- import * as Layer from "effect/Layer"
6
- import * as Option from "effect/Option"
7
- import * as PubSub from "effect/PubSub"
8
- import * as Tracer from "effect/Tracer"
9
- import * as ConsoleStore from "./ConsoleStore.ts"
10
-
11
- const publish = (store: ConsoleStore.ConsoleStoreShape, event: ConsoleStore.ConsoleEvent) =>
12
- Effect.runSync(PubSub.publish(store.events, event))
13
-
14
- const make = (store: ConsoleStore.ConsoleStoreShape): Tracer.Tracer =>
15
- Tracer.make({
16
- span(name, parent, context, links, startTime, kind, options) {
17
- const parentSpanId =
18
- Option.isSome(parent) && parent.value._tag === "Span" ? parent.value.spanId : undefined
19
- const traceId = Option.isSome(parent)
20
- ? parent.value.traceId
21
- : Math.random().toString(36).slice(2) + Math.random().toString(36).slice(2)
22
- const spanId = Math.random().toString(36).slice(2)
23
-
24
- const attributes: Record<string, unknown> = {}
25
- const currentFiber = Fiber.getCurrentFiber()
26
- if (Option.isSome(currentFiber)) {
27
- attributes["fiber.id"] = FiberId.threadName(currentFiber.value.id())
28
- }
29
- if (typeof options?.captureStackTrace === "function") {
30
- const stacktrace = options.captureStackTrace()
31
- if (stacktrace) {
32
- attributes["code.stacktrace"] = stacktrace
33
- }
34
- }
35
-
36
- const consoleSpan: ConsoleStore.ConsoleSpan = {
37
- spanId,
38
- traceId,
39
- name,
40
- kind,
41
- parentSpanId,
42
- startTime,
43
- endTime: undefined,
44
- durationMs: undefined,
45
- status: "started",
46
- attributes,
47
- events: [],
48
- }
49
-
50
- store.spans.push(consoleSpan)
51
- publish(store, { _tag: "SpanStart", span: consoleSpan })
52
-
53
- const attrs = new Map<string, unknown>(Object.entries(attributes))
54
- const spanLinks = [...links]
55
-
56
- const span: Tracer.Span = {
57
- _tag: "Span",
58
- name,
59
- spanId,
60
- traceId,
61
- parent,
62
- context,
63
- get status(): Tracer.SpanStatus {
64
- if (consoleSpan.endTime != null) {
65
- return {
66
- _tag: "Ended",
67
- startTime: consoleSpan.startTime,
68
- endTime: consoleSpan.endTime,
69
- exit: Exit.void,
70
- }
71
- }
72
- return { _tag: "Started", startTime: consoleSpan.startTime }
73
- },
74
- attributes: attrs,
75
- links: spanLinks,
76
- sampled: true,
77
- kind,
78
- end(endTime, exit) {
79
- consoleSpan.endTime = endTime
80
- consoleSpan.durationMs = Number(endTime - consoleSpan.startTime) / 1_000_000
81
- consoleSpan.status = Exit.isSuccess(exit) ? "ok" : "error"
82
- publish(store, { _tag: "SpanEnd", span: consoleSpan })
83
- },
84
- attribute(key, value) {
85
- attrs.set(key, value)
86
- ;(consoleSpan.attributes as Record<string, unknown>)[key] = value
87
- },
88
- event(name, startTime, attributes) {
89
- consoleSpan.events.push({ name, startTime, attributes })
90
- },
91
- addLinks(newLinks) {
92
- spanLinks.push(...newLinks)
93
- },
94
- }
95
- return span
96
- },
97
- context(f) {
98
- return f()
99
- },
100
- })
101
-
102
- export const layer: Layer.Layer<never, never, ConsoleStore.ConsoleStore> = Layer.unwrapEffect(
103
- Effect.gen(function* () {
104
- const store = yield* ConsoleStore.ConsoleStore
105
- return Layer.setTracer(make(store))
106
- }),
107
- )