rian 0.0.1 → 0.0.2-alpha.4

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]: any;
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
+ const tracecontext = require('rian/tracecontext');
4
+
5
+ const measure = (cb, scope, promises) => {
6
+ const set_error = (error) => {
7
+ scope.set_attributes({
8
+ error,
9
+ });
10
+ };
11
+ try {
12
+ const r = cb();
13
+ if (r instanceof Promise)
14
+ promises.push(r.catch(set_error).finally(() => scope.end()));
15
+ return r;
16
+ }
17
+ catch (e) {
18
+ set_error(e);
19
+ throw e;
20
+ }
21
+ finally {
22
+ scope.end();
23
+ }
24
+ };
25
+ const create = (name, options) => {
26
+ const spans = new Set();
27
+ const promises = [];
28
+ const scope = (name, parent) => {
29
+ const me = parent ? parent.child() : tracecontext.make_traceparent();
30
+ const attributes = {};
31
+ const start = Date.now();
32
+ let ended = false;
33
+ const $ = {
34
+ get traceparent() {
35
+ return me;
36
+ },
37
+ fork(name) {
38
+ return scope(name, me);
39
+ },
40
+ measure(name, cb, ...args) {
41
+ const scope = this.fork(name);
42
+ return measure(() => cb(...args, scope), scope, promises);
43
+ },
44
+ set_attributes(attr) {
45
+ Object.assign(attributes, attr);
46
+ },
47
+ end() {
48
+ if (ended)
49
+ return void 0;
50
+ spans.add({
51
+ id: me,
52
+ parent,
53
+ start,
54
+ end: Date.now(),
55
+ name,
56
+ attributes,
57
+ });
58
+ ended = true;
59
+ },
60
+ };
61
+ return Object.setPrototypeOf((cb) => measure(cb, $, promises), $);
62
+ };
63
+ const me = scope(name, typeof options.traceparent === 'string'
64
+ ? tracecontext.parse_traceparent(options.traceparent)
65
+ : options.traceparent);
66
+ const meEnd = me.end.bind(me);
67
+ me.end = async () => {
68
+ await Promise.all(promises);
69
+ meEnd();
70
+ return options.collector(spans);
71
+ };
72
+ return me;
73
+ };
74
+
75
+ exports.create = create;
package/index.mjs ADDED
@@ -0,0 +1,73 @@
1
+ import { parse_traceparent, make_traceparent } from 'rian/tracecontext';
2
+
3
+ const measure = (cb, scope, promises) => {
4
+ const set_error = (error) => {
5
+ scope.set_attributes({
6
+ error,
7
+ });
8
+ };
9
+ try {
10
+ const r = cb();
11
+ if (r instanceof Promise)
12
+ promises.push(r.catch(set_error).finally(() => scope.end()));
13
+ return r;
14
+ }
15
+ catch (e) {
16
+ set_error(e);
17
+ throw e;
18
+ }
19
+ finally {
20
+ scope.end();
21
+ }
22
+ };
23
+ const create = (name, options) => {
24
+ const spans = new Set();
25
+ const promises = [];
26
+ const scope = (name, parent) => {
27
+ const me = parent ? parent.child() : make_traceparent();
28
+ const attributes = {};
29
+ const start = Date.now();
30
+ let ended = false;
31
+ const $ = {
32
+ get traceparent() {
33
+ return me;
34
+ },
35
+ fork(name) {
36
+ return scope(name, me);
37
+ },
38
+ measure(name, cb, ...args) {
39
+ const scope = this.fork(name);
40
+ return measure(() => cb(...args, scope), scope, promises);
41
+ },
42
+ set_attributes(attr) {
43
+ Object.assign(attributes, attr);
44
+ },
45
+ end() {
46
+ if (ended)
47
+ return void 0;
48
+ spans.add({
49
+ id: me,
50
+ parent,
51
+ start,
52
+ end: Date.now(),
53
+ name,
54
+ attributes,
55
+ });
56
+ ended = true;
57
+ },
58
+ };
59
+ return Object.setPrototypeOf((cb) => measure(cb, $, promises), $);
60
+ };
61
+ const me = scope(name, typeof options.traceparent === 'string'
62
+ ? parse_traceparent(options.traceparent)
63
+ : options.traceparent);
64
+ const meEnd = me.end.bind(me);
65
+ me.end = async () => {
66
+ await Promise.all(promises);
67
+ meEnd();
68
+ return options.collector(spans);
69
+ };
70
+ return me;
71
+ };
72
+
73
+ export { 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.4",
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,14 @@
1
+ 'use strict';
2
+
3
+ const tctx = require('tctx');
4
+
5
+
6
+
7
+ Object.defineProperty(exports, 'make_traceparent', {
8
+ enumerable: true,
9
+ get: function () { return tctx.make; }
10
+ });
11
+ Object.defineProperty(exports, 'parse_traceparent', {
12
+ enumerable: true,
13
+ get: function () { return tctx.parse; }
14
+ });
@@ -0,0 +1 @@
1
+ export { make as make_traceparent, parse as parse_traceparent } from 'tctx';
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
- }