rian 0.0.1 → 0.0.2-alpha.1

Sign up to get free protection for your applications and to get access to all the features.
package/index.d.ts ADDED
@@ -0,0 +1,35 @@
1
+ import { Traceparent } from 'rian/tracecontext';
2
+
3
+ declare type Span = {
4
+ name: string;
5
+ id: Traceparent;
6
+ parent?: Traceparent;
7
+ start: number;
8
+ end: number;
9
+ attributes: Attributes;
10
+ };
11
+ declare type Collector = (spans: ReadonlySet<Span>) => any;
12
+ declare type Attributes = {
13
+ [property: string]: string | number | boolean | undefined | Error;
14
+ };
15
+ declare type Options = {
16
+ collector: Collector;
17
+ traceparent?: Traceparent;
18
+ };
19
+ declare type OmitScopeParam<T extends unknown[]> = T extends [] ? [] : T extends [infer H, ...infer R] ? H extends Scope ? OmitScopeParam<R> : [H, ...OmitScopeParam<R>] : T;
20
+ interface CallableScope extends Scope {
21
+ (cb: (scope: Omit<Scope, 'end'>) => void): ReturnType<typeof cb>;
22
+ }
23
+ interface Scope {
24
+ traceparent: Traceparent;
25
+ fork(name: string, traceparent?: Traceparent): CallableScope;
26
+ measure<Fn extends (...args: any[]) => any, Params extends Parameters<Fn>>(name: string, fn: Fn, ...args: OmitScopeParam<Params>): ReturnType<Fn>;
27
+ set_attributes(attributes: Attributes): void;
28
+ end(): void;
29
+ }
30
+ interface Tracer extends Scope {
31
+ end(): ReturnType<Collector>;
32
+ }
33
+ declare const create: (name: string, options: Options) => Tracer;
34
+
35
+ export { Attributes, Collector, Options, Scope, Span, Tracer, create };
package/index.js ADDED
@@ -0,0 +1,75 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.create = void 0;
5
+ const tracecontext_1 = require("rian/tracecontext");
6
+ const measure = (cb, scope, promises) => {
7
+ const set_error = (error) => {
8
+ scope.set_attributes({
9
+ error,
10
+ });
11
+ };
12
+ try {
13
+ const r = cb();
14
+ if (r instanceof Promise)
15
+ promises.push(r.catch(set_error).finally(() => scope.end()));
16
+ return r;
17
+ }
18
+ catch (e) {
19
+ set_error(e);
20
+ throw e;
21
+ }
22
+ finally {
23
+ scope.end();
24
+ }
25
+ };
26
+ const create = (name, options) => {
27
+ const spans = new Set();
28
+ const promises = [];
29
+ const scope = (name, parent) => {
30
+ const me = parent ? parent.child() : (0, tracecontext_1.make_traceparent)();
31
+ const attributes = {};
32
+ const start = performance.now();
33
+ let ended = false;
34
+ const $ = {
35
+ get traceparent() {
36
+ return me;
37
+ },
38
+ fork(name) {
39
+ return scope(name, me);
40
+ },
41
+ measure(name, cb, ...args) {
42
+ const scope = this.fork(name);
43
+ return measure(() => cb(...args, scope), scope, promises);
44
+ },
45
+ set_attributes(attr) {
46
+ Object.assign(attributes, attr);
47
+ },
48
+ end() {
49
+ if (ended)
50
+ return void 0;
51
+ spans.add({
52
+ id: me,
53
+ parent,
54
+ start,
55
+ end: performance.now(),
56
+ name,
57
+ attributes,
58
+ });
59
+ ended = true;
60
+ },
61
+ };
62
+ return Object.setPrototypeOf((cb) => measure(cb, $, promises), $);
63
+ };
64
+ const me = scope(name, typeof options.traceparent === 'string'
65
+ ? (0, tracecontext_1.parse_traceparent)(options.traceparent)
66
+ : options.traceparent);
67
+ const meEnd = me.end.bind(me);
68
+ me.end = async () => {
69
+ await Promise.all(promises);
70
+ meEnd();
71
+ return options.collector(spans);
72
+ };
73
+ return me;
74
+ };
75
+ exports.create = create;
package/index.mjs ADDED
@@ -0,0 +1,73 @@
1
+ Object.defineProperty(exports, "__esModule", { value: true });
2
+ exports.create = void 0;
3
+ const tracecontext_1 = require("rian/tracecontext");
4
+ const measure = (cb, scope, promises) => {
5
+ const set_error = (error) => {
6
+ scope.set_attributes({
7
+ error,
8
+ });
9
+ };
10
+ try {
11
+ const r = cb();
12
+ if (r instanceof Promise)
13
+ promises.push(r.catch(set_error).finally(() => scope.end()));
14
+ return r;
15
+ }
16
+ catch (e) {
17
+ set_error(e);
18
+ throw e;
19
+ }
20
+ finally {
21
+ scope.end();
22
+ }
23
+ };
24
+ const create = (name, options) => {
25
+ const spans = new Set();
26
+ const promises = [];
27
+ const scope = (name, parent) => {
28
+ const me = parent ? parent.child() : (0, tracecontext_1.make_traceparent)();
29
+ const attributes = {};
30
+ const start = performance.now();
31
+ let ended = false;
32
+ const $ = {
33
+ get traceparent() {
34
+ return me;
35
+ },
36
+ fork(name) {
37
+ return scope(name, me);
38
+ },
39
+ measure(name, cb, ...args) {
40
+ const scope = this.fork(name);
41
+ return measure(() => cb(...args, scope), scope, promises);
42
+ },
43
+ set_attributes(attr) {
44
+ Object.assign(attributes, attr);
45
+ },
46
+ end() {
47
+ if (ended)
48
+ return void 0;
49
+ spans.add({
50
+ id: me,
51
+ parent,
52
+ start,
53
+ end: performance.now(),
54
+ name,
55
+ attributes,
56
+ });
57
+ ended = true;
58
+ },
59
+ };
60
+ return Object.setPrototypeOf((cb) => measure(cb, $, promises), $);
61
+ };
62
+ const me = scope(name, typeof options.traceparent === 'string'
63
+ ? (0, tracecontext_1.parse_traceparent)(options.traceparent)
64
+ : options.traceparent);
65
+ const meEnd = me.end.bind(me);
66
+ me.end = async () => {
67
+ await Promise.all(promises);
68
+ meEnd();
69
+ return options.collector(spans);
70
+ };
71
+ return me;
72
+ };
73
+ exports.create = create;
package/package.json CHANGED
@@ -1,5 +1,39 @@
1
1
  {
2
- "name": "rian",
3
- "version": "0.0.1",
4
- "description": "(WIP) trace in Irish is Rian"
2
+ "name": "rian",
3
+ "version": "0.0.2-alpha.1",
4
+ "description": "A tracer for the edge",
5
+ "keywords": [
6
+ "TODO"
7
+ ],
8
+ "repository": "maraisr/rian",
9
+ "license": "MIT",
10
+ "author": {
11
+ "name": "Marais Rossouw",
12
+ "email": "me@marais.dev",
13
+ "url": "https://marais.io"
14
+ },
15
+ "sideEffects": false,
16
+ "type": "module",
17
+ "exports": {
18
+ ".": {
19
+ "import": "./index.mjs",
20
+ "require": "./index.js"
21
+ },
22
+ "./tracecontext": {
23
+ "import": "./tracecontext.mjs",
24
+ "require": "./tracecontext.js"
25
+ },
26
+ "./package.json": "./package.json"
27
+ },
28
+ "main": "./index.js",
29
+ "module": "./index.mjs",
30
+ "types": "index.d.ts",
31
+ "files": [
32
+ "*.mjs",
33
+ "*.js",
34
+ "*.d.ts"
35
+ ],
36
+ "dependencies": {
37
+ "tctx": "^0.0.6"
38
+ }
5
39
  }
@@ -0,0 +1 @@
1
+ export { Traceparent, make as make_traceparent, parse as parse_traceparent } from 'tctx';
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.parse_traceparent = exports.make_traceparent = void 0;
5
+ var tctx_1 = require("tctx");
6
+ Object.defineProperty(exports, "make_traceparent", { enumerable: true, get: function () { return tctx_1.make; } });
7
+ Object.defineProperty(exports, "parse_traceparent", { enumerable: true, get: function () { return tctx_1.parse; } });
@@ -0,0 +1,5 @@
1
+ Object.defineProperty(exports, "__esModule", { value: true });
2
+ exports.parse_traceparent = exports.make_traceparent = void 0;
3
+ var tctx_1 = require("tctx");
4
+ Object.defineProperty(exports, "make_traceparent", { enumerable: true, get: function () { return tctx_1.make; } });
5
+ Object.defineProperty(exports, "parse_traceparent", { enumerable: true, get: function () { return tctx_1.parse; } });
package/.pnpm-debug.log DELETED
@@ -1,35 +0,0 @@
1
- {
2
- "0 debug pnpm:scope": {
3
- "selected": 1
4
- },
5
- "1 info pnpm": {
6
- "err": {
7
- "name": "Error",
8
- "message": "Command failed with ENOENT: pub\nspawn pub ENOENT",
9
- "code": "ENOENT",
10
- "stack": "Error: Command failed with ENOENT: pub\nspawn pub ENOENT\n at Process.ChildProcess._handle.onexit (internal/child_process.js:269:19)\n at onErrorNT (internal/child_process.js:467:16)\n at processTicksAndRejections (internal/process/task_queues.js:82:21)"
11
- }
12
- },
13
- "2 error pnpm": {
14
- "errno": -2,
15
- "code": "ERR_PNPM_RECURSIVE_EXEC_FIRST_FAIL",
16
- "syscall": "spawn pub",
17
- "path": "pub",
18
- "spawnargs": [],
19
- "originalMessage": "spawn pub ENOENT",
20
- "shortMessage": "Command failed with ENOENT: pub\nspawn pub ENOENT",
21
- "command": "pub",
22
- "escapedCommand": "pub",
23
- "failed": true,
24
- "timedOut": false,
25
- "isCanceled": false,
26
- "killed": false,
27
- "prefix": "/Users/marais.rossouw/dev/playground/package-parker",
28
- "err": {
29
- "name": "pnpm",
30
- "message": "Command failed with ENOENT: pub\nspawn pub ENOENT",
31
- "code": "ERR_PNPM_RECURSIVE_EXEC_FIRST_FAIL",
32
- "stack": "Error: Command failed with ENOENT: pub\nspawn pub ENOENT\n at Process.ChildProcess._handle.onexit (internal/child_process.js:269:19)\n at onErrorNT (internal/child_process.js:467:16)\n at processTicksAndRejections (internal/process/task_queues.js:82:21)"
33
- }
34
- }
35
- }