@dxos/effect 0.8.4-main.84f28bd → 0.8.4-main.ae835ea
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.
- package/dist/lib/browser/index.mjs +142 -51
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +142 -51
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/ast.d.ts +2 -1
- package/dist/types/src/ast.d.ts.map +1 -1
- package/dist/types/src/context.d.ts +2 -1
- package/dist/types/src/context.d.ts.map +1 -1
- package/dist/types/src/errors.d.ts +31 -1
- package/dist/types/src/errors.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +2 -0
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/interrupt.test.d.ts +2 -0
- package/dist/types/src/interrupt.test.d.ts.map +1 -0
- package/dist/types/src/jsonPath.d.ts +1 -1
- package/dist/types/src/jsonPath.d.ts.map +1 -1
- package/dist/types/src/layers.test.d.ts +2 -0
- package/dist/types/src/layers.test.d.ts.map +1 -0
- package/dist/types/src/otel.d.ts +17 -0
- package/dist/types/src/otel.d.ts.map +1 -0
- package/dist/types/src/otel.test.d.ts +2 -0
- package/dist/types/src/otel.test.d.ts.map +1 -0
- package/dist/types/src/resource.d.ts +4 -0
- package/dist/types/src/resource.d.ts.map +1 -0
- package/dist/types/src/resource.test.d.ts +2 -0
- package/dist/types/src/resource.test.d.ts.map +1 -0
- package/dist/types/src/testing.d.ts +67 -0
- package/dist/types/src/testing.d.ts.map +1 -0
- package/dist/types/src/url.d.ts +3 -1
- package/dist/types/src/url.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +16 -7
- package/src/ast.test.ts +4 -3
- package/src/ast.ts +29 -16
- package/src/context.ts +2 -1
- package/src/errors.test.ts +1 -1
- package/src/errors.ts +87 -25
- package/src/index.ts +2 -0
- package/src/interrupt.test.ts +33 -0
- package/src/jsonPath.test.ts +1 -1
- package/src/jsonPath.ts +2 -1
- package/src/layers.test.ts +110 -0
- package/src/otel.test.ts +125 -0
- package/src/otel.ts +45 -0
- package/src/resource.test.ts +32 -0
- package/src/resource.ts +25 -0
- package/src/sanity.test.ts +6 -5
- package/src/testing.ts +110 -0
- package/src/url.test.ts +1 -1
- package/src/url.ts +5 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/effect",
|
|
3
|
-
"version": "0.8.4-main.
|
|
3
|
+
"version": "0.8.4-main.ae835ea",
|
|
4
4
|
"description": "Effect utils.",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
"type": "module",
|
|
10
10
|
"exports": {
|
|
11
11
|
".": {
|
|
12
|
+
"source": "./src/index.ts",
|
|
12
13
|
"types": "./dist/types/src/index.d.ts",
|
|
13
14
|
"browser": "./dist/lib/browser/index.mjs",
|
|
14
15
|
"node": "./dist/lib/node-esm/index.mjs"
|
|
@@ -23,15 +24,23 @@
|
|
|
23
24
|
"src"
|
|
24
25
|
],
|
|
25
26
|
"dependencies": {
|
|
27
|
+
"@effect/opentelemetry": "^0.58.0",
|
|
28
|
+
"@opentelemetry/api": "^1.9.0",
|
|
26
29
|
"jsonpath-plus": "10.2.0",
|
|
27
|
-
"@dxos/context": "0.8.4-main.
|
|
28
|
-
"@dxos/invariant": "0.8.4-main.
|
|
29
|
-
"@dxos/node-std": "0.8.4-main.
|
|
30
|
-
"@dxos/util": "0.8.4-main.
|
|
30
|
+
"@dxos/context": "0.8.4-main.ae835ea",
|
|
31
|
+
"@dxos/invariant": "0.8.4-main.ae835ea",
|
|
32
|
+
"@dxos/node-std": "0.8.4-main.ae835ea",
|
|
33
|
+
"@dxos/util": "0.8.4-main.ae835ea"
|
|
31
34
|
},
|
|
32
35
|
"devDependencies": {
|
|
33
|
-
"
|
|
34
|
-
"@
|
|
36
|
+
"@opentelemetry/api-logs": "^0.203.0",
|
|
37
|
+
"@opentelemetry/resources": "^2.1.0",
|
|
38
|
+
"@opentelemetry/sdk-logs": "^0.203.0",
|
|
39
|
+
"@opentelemetry/sdk-node": "^0.203.0",
|
|
40
|
+
"@opentelemetry/sdk-trace-node": "^2.1.0",
|
|
41
|
+
"@opentelemetry/semantic-conventions": "^1.37.0",
|
|
42
|
+
"effect": "3.18.3",
|
|
43
|
+
"@dxos/log": "0.8.4-main.ae835ea"
|
|
35
44
|
},
|
|
36
45
|
"peerDependencies": {
|
|
37
46
|
"effect": "^3.13.3"
|
package/src/ast.test.ts
CHANGED
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import
|
|
5
|
+
import * as Schema from 'effect/Schema';
|
|
6
|
+
import * as SchemaAST from 'effect/SchemaAST';
|
|
6
7
|
import { describe, test } from 'vitest';
|
|
7
8
|
|
|
8
9
|
import { invariant } from '@dxos/invariant';
|
|
@@ -12,13 +13,13 @@ import {
|
|
|
12
13
|
findNode,
|
|
13
14
|
findProperty,
|
|
14
15
|
getAnnotation,
|
|
15
|
-
getDiscriminatingProps,
|
|
16
16
|
getDiscriminatedType,
|
|
17
|
+
getDiscriminatingProps,
|
|
17
18
|
getSimpleType,
|
|
19
|
+
isArrayType,
|
|
18
20
|
isOption,
|
|
19
21
|
isSimpleType,
|
|
20
22
|
visit,
|
|
21
|
-
isArrayType,
|
|
22
23
|
} from './ast';
|
|
23
24
|
import { type JsonPath, type JsonProp } from './jsonPath';
|
|
24
25
|
|
package/src/ast.ts
CHANGED
|
@@ -2,8 +2,10 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import
|
|
6
|
-
import
|
|
5
|
+
import * as Function from 'effect/Function';
|
|
6
|
+
import * as Option from 'effect/Option';
|
|
7
|
+
import * as Schema from 'effect/Schema';
|
|
8
|
+
import * as SchemaAST from 'effect/SchemaAST';
|
|
7
9
|
|
|
8
10
|
import { invariant } from '@dxos/invariant';
|
|
9
11
|
import { isNonNullable } from '@dxos/util';
|
|
@@ -17,8 +19,14 @@ import { type JsonPath, type JsonProp } from './jsonPath';
|
|
|
17
19
|
// https://effect-ts.github.io/effect/schema/SchemaAST.ts.html
|
|
18
20
|
//
|
|
19
21
|
|
|
22
|
+
// TODO(wittjosiah): What is a "simple type"?
|
|
20
23
|
export type SimpleType = 'object' | 'string' | 'number' | 'boolean' | 'enum' | 'literal';
|
|
21
24
|
|
|
25
|
+
const isTupleType = (node: SchemaAST.AST): boolean => {
|
|
26
|
+
// NOTE: Arrays are represented as tuples with no elements and a rest part.
|
|
27
|
+
return SchemaAST.isTupleType(node) && node.elements.length > 0;
|
|
28
|
+
};
|
|
29
|
+
|
|
22
30
|
/**
|
|
23
31
|
* Get the base type; e.g., traverse through refinements.
|
|
24
32
|
*/
|
|
@@ -27,6 +35,8 @@ export const getSimpleType = (node: SchemaAST.AST): SimpleType | undefined => {
|
|
|
27
35
|
SchemaAST.isDeclaration(node) ||
|
|
28
36
|
SchemaAST.isObjectKeyword(node) ||
|
|
29
37
|
SchemaAST.isTypeLiteral(node) ||
|
|
38
|
+
// TODO(wittjosiah): Tuples are actually arrays.
|
|
39
|
+
isTupleType(node) ||
|
|
30
40
|
isDiscriminatedUnion(node)
|
|
31
41
|
) {
|
|
32
42
|
return 'object';
|
|
@@ -127,15 +137,15 @@ const visitNode = (
|
|
|
127
137
|
path: Path = [],
|
|
128
138
|
depth = 0,
|
|
129
139
|
): VisitResult | undefined => {
|
|
130
|
-
const
|
|
140
|
+
const $result = test?.(node, path, depth);
|
|
131
141
|
const result: VisitResult =
|
|
132
|
-
|
|
142
|
+
$result === undefined
|
|
133
143
|
? VisitResult.CONTINUE
|
|
134
|
-
: typeof
|
|
135
|
-
?
|
|
144
|
+
: typeof $result === 'boolean'
|
|
145
|
+
? $result
|
|
136
146
|
? VisitResult.CONTINUE
|
|
137
147
|
: VisitResult.SKIP
|
|
138
|
-
:
|
|
148
|
+
: $result;
|
|
139
149
|
|
|
140
150
|
if (result === VisitResult.EXIT) {
|
|
141
151
|
return result;
|
|
@@ -218,12 +228,14 @@ export const findNode = (node: SchemaAST.AST, test: (node: SchemaAST.AST) => boo
|
|
|
218
228
|
|
|
219
229
|
// Branching union (e.g., optional, discriminated unions).
|
|
220
230
|
else if (SchemaAST.isUnion(node)) {
|
|
221
|
-
if (
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
231
|
+
if (isLiteralUnion(node)) {
|
|
232
|
+
return undefined;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
for (const type of node.types) {
|
|
236
|
+
const child = findNode(type, test);
|
|
237
|
+
if (child) {
|
|
238
|
+
return child;
|
|
227
239
|
}
|
|
228
240
|
}
|
|
229
241
|
}
|
|
@@ -279,8 +291,8 @@ export const getAnnotation =
|
|
|
279
291
|
<T>(annotationId: symbol, noDefault = true) =>
|
|
280
292
|
(node: SchemaAST.AST): T | undefined => {
|
|
281
293
|
// Title fallback seems to be the identifier.
|
|
282
|
-
const id = pipe(SchemaAST.getIdentifierAnnotation(node), Option.getOrUndefined);
|
|
283
|
-
const value = pipe(SchemaAST.getAnnotation<T>(annotationId)(node), Option.getOrUndefined);
|
|
294
|
+
const id = Function.pipe(SchemaAST.getIdentifierAnnotation(node), Option.getOrUndefined);
|
|
295
|
+
const value = Function.pipe(SchemaAST.getAnnotation<T>(annotationId)(node), Option.getOrUndefined);
|
|
284
296
|
if (noDefault && (value === defaultAnnotations[node._tag]?.annotations[annotationId] || value === id)) {
|
|
285
297
|
return undefined;
|
|
286
298
|
}
|
|
@@ -432,6 +444,7 @@ export const mapAst = (
|
|
|
432
444
|
),
|
|
433
445
|
),
|
|
434
446
|
ast.indexSignatures,
|
|
447
|
+
ast.annotations,
|
|
435
448
|
);
|
|
436
449
|
}
|
|
437
450
|
case 'Union': {
|
|
@@ -464,7 +477,7 @@ export const isArrayType = (node: SchemaAST.AST): boolean => {
|
|
|
464
477
|
SchemaAST.isTupleType(node) ||
|
|
465
478
|
(SchemaAST.isUnion(node) &&
|
|
466
479
|
node.types.some(isArrayType) &&
|
|
467
|
-
node.types.some(isUndefinedKeyword) &&
|
|
480
|
+
node.types.some(SchemaAST.isUndefinedKeyword) &&
|
|
468
481
|
node.types.length === 2)
|
|
469
482
|
);
|
|
470
483
|
};
|
package/src/context.ts
CHANGED
package/src/errors.test.ts
CHANGED
package/src/errors.ts
CHANGED
|
@@ -2,18 +2,25 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import
|
|
6
|
-
import
|
|
5
|
+
import * as Cause from 'effect/Cause';
|
|
6
|
+
import * as Chunk from 'effect/Chunk';
|
|
7
|
+
import * as Effect from 'effect/Effect';
|
|
8
|
+
import * as Exit from 'effect/Exit';
|
|
9
|
+
import * as GlobalValue from 'effect/GlobalValue';
|
|
10
|
+
import * as Option from 'effect/Option';
|
|
11
|
+
import type * as Tracer from 'effect/Tracer';
|
|
7
12
|
|
|
8
13
|
const spanSymbol = Symbol.for('effect/SpanAnnotation');
|
|
14
|
+
const originalSymbol = Symbol.for('effect/OriginalAnnotation');
|
|
9
15
|
const spanToTrace = GlobalValue.globalValue('effect/Tracer/spanToTrace', () => new WeakMap());
|
|
10
16
|
const locationRegex = /\((.*)\)/g;
|
|
11
17
|
|
|
12
18
|
/**
|
|
13
19
|
* Adds effect spans.
|
|
14
20
|
* Removes effect internal functions.
|
|
21
|
+
* Unwraps error proxy.
|
|
15
22
|
*/
|
|
16
|
-
const prettyErrorStack = (error: any, appendStacks: string[] = []):
|
|
23
|
+
const prettyErrorStack = (error: any, appendStacks: string[] = []): any => {
|
|
17
24
|
const span = error[spanSymbol];
|
|
18
25
|
|
|
19
26
|
const lines = typeof error.stack === 'string' ? error.stack.split('\n') : [];
|
|
@@ -46,7 +53,7 @@ const prettyErrorStack = (error: any, appendStacks: string[] = []): void => {
|
|
|
46
53
|
}
|
|
47
54
|
|
|
48
55
|
if (span) {
|
|
49
|
-
let current: Span | AnySpan | undefined = span;
|
|
56
|
+
let current: Tracer.Span | Tracer.AnySpan | undefined = span;
|
|
50
57
|
let i = 0;
|
|
51
58
|
while (current && current._tag === 'Span' && i < 10) {
|
|
52
59
|
const stackFn = spanToTrace.get(current);
|
|
@@ -75,16 +82,25 @@ const prettyErrorStack = (error: any, appendStacks: string[] = []): void => {
|
|
|
75
82
|
|
|
76
83
|
out.push(...appendStacks);
|
|
77
84
|
|
|
85
|
+
if (error[originalSymbol]) {
|
|
86
|
+
error = error[originalSymbol];
|
|
87
|
+
}
|
|
88
|
+
if (error.cause) {
|
|
89
|
+
error.cause = prettyErrorStack(error.cause);
|
|
90
|
+
}
|
|
91
|
+
|
|
78
92
|
Object.defineProperty(error, 'stack', {
|
|
79
93
|
value: out.join('\n'),
|
|
80
94
|
writable: true,
|
|
81
95
|
enumerable: false,
|
|
82
96
|
configurable: true,
|
|
83
97
|
});
|
|
98
|
+
|
|
99
|
+
return error;
|
|
84
100
|
};
|
|
85
101
|
|
|
86
102
|
/**
|
|
87
|
-
*
|
|
103
|
+
* Converts a cause to an error.
|
|
88
104
|
* Inserts effect spans as stack frames.
|
|
89
105
|
* The error will have stack frames of where the effect was run (if stack trace limit allows).
|
|
90
106
|
* Removes effect runtime internal stack frames.
|
|
@@ -93,21 +109,13 @@ const prettyErrorStack = (error: any, appendStacks: string[] = []): void => {
|
|
|
93
109
|
*
|
|
94
110
|
* @throws AggregateError if there are multiple errors.
|
|
95
111
|
*/
|
|
96
|
-
export const
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
if (Exit.isSuccess(exit)) {
|
|
102
|
-
return exit.value;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
if (Cause.isEmpty(exit.cause)) {
|
|
106
|
-
throw new Error('Fiber failed without a cause');
|
|
107
|
-
} else if (Cause.isInterrupted(exit.cause)) {
|
|
108
|
-
throw new Error('Fiber was interrupted');
|
|
112
|
+
export const causeToError = (cause: Cause.Cause<any>): Error => {
|
|
113
|
+
if (Cause.isEmpty(cause)) {
|
|
114
|
+
return new Error('Fiber failed without a cause');
|
|
115
|
+
} else if (Cause.isInterruptedOnly(cause)) {
|
|
116
|
+
return new Error('Fiber was interrupted');
|
|
109
117
|
} else {
|
|
110
|
-
const errors = [...Chunk.toArray(Cause.failures(
|
|
118
|
+
const errors = [...Chunk.toArray(Cause.failures(cause)), ...Chunk.toArray(Cause.defects(cause))];
|
|
111
119
|
|
|
112
120
|
const getStackFrames = (): string[] => {
|
|
113
121
|
const o: { stack: string } = {} as any;
|
|
@@ -116,14 +124,68 @@ export const runAndForwardErrors = async <A, E>(
|
|
|
116
124
|
};
|
|
117
125
|
|
|
118
126
|
const stackFrames = getStackFrames();
|
|
119
|
-
|
|
120
|
-
prettyErrorStack(error, stackFrames);
|
|
121
|
-
}
|
|
127
|
+
const newErrors = errors.map((error) => prettyErrorStack(error, stackFrames));
|
|
122
128
|
|
|
123
|
-
if (
|
|
124
|
-
|
|
129
|
+
if (newErrors.length === 1) {
|
|
130
|
+
return newErrors[0];
|
|
125
131
|
} else {
|
|
126
|
-
|
|
132
|
+
return new AggregateError(newErrors);
|
|
127
133
|
}
|
|
128
134
|
}
|
|
129
135
|
};
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Throws an error based on the cause.
|
|
139
|
+
* Inserts effect spans as stack frames.
|
|
140
|
+
* The error will have stack frames of where the effect was run (if stack trace limit allows).
|
|
141
|
+
* Removes effect runtime internal stack frames.
|
|
142
|
+
*
|
|
143
|
+
* To be used in place of `Effect.runPromise`.
|
|
144
|
+
*
|
|
145
|
+
* @throws AggregateError if there are multiple errors.
|
|
146
|
+
*/
|
|
147
|
+
export const throwCause = (cause: Cause.Cause<any>): never => {
|
|
148
|
+
throw causeToError(cause);
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
export const unwrapExit = <A>(exit: Exit.Exit<A, any>): A => {
|
|
152
|
+
if (Exit.isSuccess(exit)) {
|
|
153
|
+
return exit.value;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
return throwCause(exit.cause);
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Runs the embedded effect asynchronously and throws any failures and defects as errors.
|
|
161
|
+
* Inserts effect spans as stack frames.
|
|
162
|
+
* The error will have stack frames of where the effect was run (if stack trace limit allows).
|
|
163
|
+
* Removes effect runtime internal stack frames.
|
|
164
|
+
*
|
|
165
|
+
* To be used in place of `Effect.runPromise`.
|
|
166
|
+
*
|
|
167
|
+
* @throws AggregateError if there are multiple errors.
|
|
168
|
+
*/
|
|
169
|
+
export const runAndForwardErrors = async <A, E>(
|
|
170
|
+
effect: Effect.Effect<A, E, never>,
|
|
171
|
+
options?: { signal?: AbortSignal },
|
|
172
|
+
): Promise<A> => {
|
|
173
|
+
const exit = await Effect.runPromiseExit(effect, options);
|
|
174
|
+
return unwrapExit(exit);
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Like `Effect.promise` but also caputes spans for defects.
|
|
179
|
+
* Workaround for: https://github.com/Effect-TS/effect/issues/5436
|
|
180
|
+
*/
|
|
181
|
+
export const promiseWithCauseCapture: <A>(evaluate: (signal: AbortSignal) => PromiseLike<A>) => Effect.Effect<A> = (
|
|
182
|
+
evaluate,
|
|
183
|
+
) =>
|
|
184
|
+
Effect.promise(async (signal) => {
|
|
185
|
+
try {
|
|
186
|
+
const result = await evaluate(signal);
|
|
187
|
+
return Effect.succeed(result);
|
|
188
|
+
} catch (err) {
|
|
189
|
+
return Effect.die(err);
|
|
190
|
+
}
|
|
191
|
+
}).pipe(Effect.flatten);
|
package/src/index.ts
CHANGED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { it } from '@effect/vitest';
|
|
6
|
+
import * as Cause from 'effect/Cause';
|
|
7
|
+
import * as Effect from 'effect/Effect';
|
|
8
|
+
import * as Fiber from 'effect/Fiber';
|
|
9
|
+
|
|
10
|
+
const doWork = Effect.fn('doWork')(function* () {
|
|
11
|
+
yield* Effect.sleep('1 minute');
|
|
12
|
+
return 'work done';
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it.effect.skip(
|
|
16
|
+
'call a function to generate a research report',
|
|
17
|
+
Effect.fnUntraced(
|
|
18
|
+
function* (_) {
|
|
19
|
+
const resultFiber = yield* doWork().pipe(Effect.fork);
|
|
20
|
+
setTimeout(() => {
|
|
21
|
+
void Effect.runPromise(Fiber.interrupt(resultFiber));
|
|
22
|
+
}, 2_000);
|
|
23
|
+
|
|
24
|
+
const result = yield* resultFiber;
|
|
25
|
+
console.log({ result });
|
|
26
|
+
},
|
|
27
|
+
Effect.catchAllCause((cause) => {
|
|
28
|
+
// console.log(inspect(cause, { depth: null, colors: true }));
|
|
29
|
+
console.log(Cause.pretty(cause));
|
|
30
|
+
return Effect.failCause(cause);
|
|
31
|
+
}),
|
|
32
|
+
),
|
|
33
|
+
);
|
package/src/jsonPath.test.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
import { describe, expect, test } from 'vitest';
|
|
6
6
|
|
|
7
|
-
import { createJsonPath, getField, isJsonPath,
|
|
7
|
+
import { type JsonPath, createJsonPath, getField, isJsonPath, splitJsonPath } from './jsonPath';
|
|
8
8
|
|
|
9
9
|
describe('createJsonPath', () => {
|
|
10
10
|
test('supported path subset', () => {
|
package/src/jsonPath.ts
CHANGED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { it } from '@effect/vitest';
|
|
6
|
+
import * as Context from 'effect/Context';
|
|
7
|
+
import * as Duration from 'effect/Duration';
|
|
8
|
+
import * as Effect from 'effect/Effect';
|
|
9
|
+
import * as Layer from 'effect/Layer';
|
|
10
|
+
import * as ManagedRuntime from 'effect/ManagedRuntime';
|
|
11
|
+
import { test } from 'vitest';
|
|
12
|
+
|
|
13
|
+
class ClientConfig extends Context.Tag('ClientConfig')<ClientConfig, { endpoint: string }>() {}
|
|
14
|
+
|
|
15
|
+
class Client extends Context.Tag('Client')<Client, { call: () => Effect.Effect<void> }>() {
|
|
16
|
+
static layer = Layer.effect(
|
|
17
|
+
Client,
|
|
18
|
+
Effect.gen(function* () {
|
|
19
|
+
const config = yield* ClientConfig;
|
|
20
|
+
return {
|
|
21
|
+
call: () => {
|
|
22
|
+
console.log('called', config.endpoint);
|
|
23
|
+
return Effect.void;
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
}),
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const ServerLive = Layer.scoped(
|
|
31
|
+
ClientConfig,
|
|
32
|
+
Effect.gen(function* () {
|
|
33
|
+
console.log('start server');
|
|
34
|
+
|
|
35
|
+
yield* Effect.sleep(Duration.millis(100));
|
|
36
|
+
|
|
37
|
+
yield* Effect.addFinalizer(
|
|
38
|
+
Effect.fn(function* () {
|
|
39
|
+
yield* Effect.sleep(Duration.millis(100));
|
|
40
|
+
console.log('stop server');
|
|
41
|
+
}),
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
return {
|
|
45
|
+
endpoint: 'http://localhost:8080',
|
|
46
|
+
};
|
|
47
|
+
}),
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
it.effect.skip(
|
|
51
|
+
'test',
|
|
52
|
+
Effect.fn(
|
|
53
|
+
function* (_) {
|
|
54
|
+
const client = yield* Client;
|
|
55
|
+
yield* client.call();
|
|
56
|
+
},
|
|
57
|
+
Effect.provide(Layer.provide(Client.layer, ServerLive)),
|
|
58
|
+
),
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
class ServerPlugin {
|
|
62
|
+
#runtime = ManagedRuntime.make(ServerLive);
|
|
63
|
+
|
|
64
|
+
readonly clientConfigLayer = Layer.effectContext(
|
|
65
|
+
this.#runtime.runtimeEffect.pipe(Effect.map((rt) => rt.context.pipe(Context.pick(ClientConfig)))),
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
async dispose() {
|
|
69
|
+
await this.#runtime.dispose();
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
class ClientPlugin {
|
|
74
|
+
constructor(private readonly _serverPlugin: ServerPlugin) {}
|
|
75
|
+
|
|
76
|
+
async run() {
|
|
77
|
+
const layer = Layer.provide(Client.layer, this._serverPlugin.clientConfigLayer);
|
|
78
|
+
|
|
79
|
+
await Effect.runPromise(
|
|
80
|
+
Effect.gen(function* () {
|
|
81
|
+
const client = yield* Client;
|
|
82
|
+
yield* client.call();
|
|
83
|
+
}).pipe(Effect.provide(layer)),
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
test.skip('plugins', async () => {
|
|
89
|
+
const serverPlugin = new ServerPlugin();
|
|
90
|
+
console.log('ServerPlugin created');
|
|
91
|
+
|
|
92
|
+
await Effect.runPromise(Effect.sleep(Duration.millis(500)));
|
|
93
|
+
console.log('wake up');
|
|
94
|
+
|
|
95
|
+
{
|
|
96
|
+
const clientPlugin1 = new ClientPlugin(serverPlugin);
|
|
97
|
+
console.log('ClientPlugin1 created');
|
|
98
|
+
await clientPlugin1.run();
|
|
99
|
+
console.log('client1 run');
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
{
|
|
103
|
+
const clientPlugin2 = new ClientPlugin(serverPlugin);
|
|
104
|
+
console.log('ClientPlugin2 created');
|
|
105
|
+
await clientPlugin2.run();
|
|
106
|
+
console.log('client2 run');
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
await serverPlugin.dispose();
|
|
110
|
+
});
|
package/src/otel.test.ts
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { afterAll, beforeAll, it } from '@effect/vitest';
|
|
6
|
+
import { SpanStatusCode, trace } from '@opentelemetry/api';
|
|
7
|
+
import { type Logger, SeverityNumber, logs } from '@opentelemetry/api-logs';
|
|
8
|
+
import { resourceFromAttributes } from '@opentelemetry/resources';
|
|
9
|
+
import { ConsoleLogRecordExporter, LoggerProvider, SimpleLogRecordProcessor } from '@opentelemetry/sdk-logs';
|
|
10
|
+
import { NodeSDK } from '@opentelemetry/sdk-node';
|
|
11
|
+
import { ConsoleSpanExporter } from '@opentelemetry/sdk-trace-node';
|
|
12
|
+
import {
|
|
13
|
+
ATTR_CODE_FILE_PATH,
|
|
14
|
+
ATTR_CODE_LINE_NUMBER,
|
|
15
|
+
ATTR_CODE_STACKTRACE,
|
|
16
|
+
ATTR_SERVICE_NAME,
|
|
17
|
+
} from '@opentelemetry/semantic-conventions';
|
|
18
|
+
import * as Duration from 'effect/Duration';
|
|
19
|
+
import * as Effect from 'effect/Effect';
|
|
20
|
+
import { beforeEach } from 'vitest';
|
|
21
|
+
|
|
22
|
+
import { LogLevel, type LogProcessor, log } from '@dxos/log';
|
|
23
|
+
|
|
24
|
+
import { layerOtel } from './otel';
|
|
25
|
+
|
|
26
|
+
const resource = resourceFromAttributes({
|
|
27
|
+
[ATTR_SERVICE_NAME]: 'test',
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const sdk = new NodeSDK({
|
|
31
|
+
traceExporter: new ConsoleSpanExporter(),
|
|
32
|
+
resource,
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
// and add a processor to export log record
|
|
36
|
+
const loggerProvider = new LoggerProvider({
|
|
37
|
+
processors: [new SimpleLogRecordProcessor(new ConsoleLogRecordExporter())],
|
|
38
|
+
resource,
|
|
39
|
+
});
|
|
40
|
+
logs.setGlobalLoggerProvider(loggerProvider);
|
|
41
|
+
|
|
42
|
+
// You can also use global singleton
|
|
43
|
+
const logger = logs.getLogger('test');
|
|
44
|
+
|
|
45
|
+
const makeOtelLogProcessor = (logger: Logger): LogProcessor => {
|
|
46
|
+
return (config, entry) => {
|
|
47
|
+
let severity: SeverityNumber = SeverityNumber.UNSPECIFIED;
|
|
48
|
+
switch (entry.level) {
|
|
49
|
+
case LogLevel.DEBUG:
|
|
50
|
+
severity = SeverityNumber.DEBUG;
|
|
51
|
+
break;
|
|
52
|
+
case LogLevel.INFO:
|
|
53
|
+
severity = SeverityNumber.INFO;
|
|
54
|
+
break;
|
|
55
|
+
case LogLevel.WARN:
|
|
56
|
+
severity = SeverityNumber.WARN;
|
|
57
|
+
break;
|
|
58
|
+
case LogLevel.ERROR:
|
|
59
|
+
severity = SeverityNumber.ERROR;
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
logger.emit({
|
|
64
|
+
body: entry.error ? ('stack' in entry.error ? entry.error.stack : String(entry.error)) : entry.message,
|
|
65
|
+
severityNumber: severity,
|
|
66
|
+
attributes: {
|
|
67
|
+
[ATTR_CODE_FILE_PATH]: entry.meta?.F,
|
|
68
|
+
[ATTR_CODE_LINE_NUMBER]: entry.meta?.L,
|
|
69
|
+
[ATTR_CODE_STACKTRACE]: entry.error?.stack,
|
|
70
|
+
...(typeof entry.context === 'object'
|
|
71
|
+
? entry.context
|
|
72
|
+
: {
|
|
73
|
+
ctx: entry.context,
|
|
74
|
+
}),
|
|
75
|
+
},
|
|
76
|
+
});
|
|
77
|
+
};
|
|
78
|
+
};
|
|
79
|
+
log.addProcessor(makeOtelLogProcessor(logger));
|
|
80
|
+
|
|
81
|
+
beforeAll(() => {
|
|
82
|
+
sdk.start();
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
afterAll(async () => {
|
|
86
|
+
await loggerProvider.shutdown();
|
|
87
|
+
await sdk.shutdown();
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
beforeEach((ctx) => {
|
|
91
|
+
const span = trace.getTracer('testing-framework').startSpan(ctx.task.name);
|
|
92
|
+
ctx.onTestFailed((ctx) => {
|
|
93
|
+
// TODO(dmaretskyi): Record result.
|
|
94
|
+
span.setStatus({ code: SpanStatusCode.ERROR });
|
|
95
|
+
span.end();
|
|
96
|
+
});
|
|
97
|
+
ctx.onTestFinished((ctx) => {
|
|
98
|
+
span.setStatus({ code: SpanStatusCode.OK });
|
|
99
|
+
span.end();
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
const foo = Effect.fn('foo')(function* () {
|
|
104
|
+
yield* Effect.sleep(Duration.millis(100));
|
|
105
|
+
|
|
106
|
+
log('log inside foo', { detail: 123 });
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
const bar = Effect.fn('bar')(function* () {
|
|
110
|
+
yield* foo();
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
const baz = Effect.fn('baz')(function* () {
|
|
114
|
+
yield* bar();
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it.live(
|
|
118
|
+
'Test Suite One',
|
|
119
|
+
Effect.fnUntraced(
|
|
120
|
+
function* () {
|
|
121
|
+
yield* baz();
|
|
122
|
+
},
|
|
123
|
+
Effect.provide(layerOtel(Effect.succeed({}))),
|
|
124
|
+
),
|
|
125
|
+
);
|