rian 0.0.5 → 0.2.1

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/index.js CHANGED
@@ -1,132 +1,76 @@
1
- 'use strict';
2
-
3
- const package_json = require('rian/package.json');
1
+ // src/index.ts
2
+ const { name:rian_name, version:rian_version } = require('rian/package.json');
4
3
  const tctx = require('tctx');
5
-
6
- function _interopNamespace(e) {
7
- if (e && e.__esModule) return e;
8
- const n = Object.create(null);
9
- if (e) {
10
- for (const k in e) {
11
- if (k !== 'default') {
12
- const d = Object.getOwnPropertyDescriptor(e, k);
13
- Object.defineProperty(n, k, d.get ? d : {
14
- enumerable: true,
15
- get: function () { return e[k]; }
16
- });
17
- }
18
- }
19
- }
20
- n["default"] = e;
21
- return n;
22
- }
23
-
24
- const tctx__namespace = /*#__PURE__*/_interopNamespace(tctx);
25
-
26
- const set_error = (scope, error) => {
27
- scope.set_context({
28
- error,
29
- });
4
+ const { measureFn } = require('rian/utils');
5
+ var PROMISES = /* @__PURE__ */ new WeakMap();
6
+ var ADD_PROMISE = (scope, promise) => {
7
+ if (PROMISES.has(scope))
8
+ PROMISES.get(scope).push(promise);
9
+ else
10
+ PROMISES.set(scope, [promise]);
30
11
  };
31
- const measure = (fn, // TODO: fn doesnt see scope correctly
32
- scope, promises, ...args) => {
33
- try {
34
- var r = fn(...args, scope), is_promise = r instanceof Promise;
35
- if (is_promise)
36
- promises.push(r
37
- .catch((e) => void set_error(scope, e))
38
- .finally(() => scope.end()));
39
- return r;
40
- }
41
- catch (e) {
42
- set_error(scope, e);
43
- throw e;
44
- }
45
- finally {
46
- if (is_promise !== true)
47
- scope.end();
48
- }
12
+ var defaultSampler = (_name, parentId) => {
13
+ if (!parentId)
14
+ return true;
15
+ return tctx.is_sampled(parentId);
49
16
  };
50
-
51
- // ==> impl
52
- /**
53
- * The default sampler;
54
- *
55
- * If no parent
56
- * ~> sample
57
- * if parent was off
58
- * ~> never sample
59
- * if parent was on
60
- * ~> always sample
61
- */
62
- const defaultSampler = (_name, parentId) => {
63
- if (!parentId)
64
- return true;
65
- return tctx__namespace.is_sampled(parentId);
17
+ var sdk_object = {
18
+ "telemetry.sdk.name": rian_name,
19
+ "telemetry.sdk.version": rian_version
66
20
  };
67
- const sdk_object = {
68
- 'telemetry.sdk.name': package_json.name,
69
- 'telemetry.sdk.version': package_json.version,
70
- };
71
- const create = (name, options) => {
72
- const spans = new Set();
73
- const promises = [];
74
- const sampler = options.sampler || defaultSampler;
75
- const sampler_callable = typeof sampler !== 'boolean';
76
- const span = (name, parent) => {
77
- const should_sample = sampler_callable
78
- ? sampler(name, parent, options.context)
79
- : sampler;
80
- const id = parent
81
- ? parent.child(should_sample)
82
- : tctx__namespace.make(should_sample);
83
- const span_obj = {
84
- id,
85
- parent,
86
- start: Date.now(),
87
- name,
88
- events: [],
89
- context: {},
90
- };
91
- if (should_sample)
92
- spans.add(span_obj);
93
- const $ = (cb) => measure(cb, $, promises);
94
- $.traceparent = id;
95
- $.fork = (name) => span(name, id);
96
- $.measure = (name, cb, ...args) => measure(cb, span(name, id), promises, ...args);
97
- $.set_context = (ctx) => {
98
- if (typeof ctx === 'function')
99
- return void (span_obj.context = ctx(span_obj.context));
100
- Object.assign(span_obj.context, ctx);
101
- };
102
- $.add_event = (name, attributes) => {
103
- span_obj.events.push({
104
- name,
105
- timestamp: Date.now(),
106
- attributes: attributes || {},
107
- });
108
- };
109
- $.end = () => {
110
- if (span_obj.end)
111
- return void 0;
112
- span_obj.end = Date.now();
113
- };
114
- return $;
21
+ var create = (name, options) => {
22
+ const spans = /* @__PURE__ */ new Set();
23
+ const sampler = options.sampler || defaultSampler;
24
+ const sampler_callable = typeof sampler !== "boolean";
25
+ const span = (name2, parent) => {
26
+ const should_sample = sampler_callable ? sampler(name2, parent, options.context) : sampler;
27
+ const id = parent ? parent.child(should_sample) : tctx.make(should_sample);
28
+ const span_obj = {
29
+ id,
30
+ parent,
31
+ start: Date.now(),
32
+ name: name2,
33
+ events: [],
34
+ context: {}
35
+ };
36
+ if (should_sample)
37
+ spans.add(span_obj);
38
+ const $ = (cb) => measureFn($, cb);
39
+ $.traceparent = id;
40
+ $.fork = (name3) => span(name3, id);
41
+ $.set_context = (ctx) => {
42
+ if (typeof ctx === "function")
43
+ return void (span_obj.context = ctx(span_obj.context));
44
+ Object.assign(span_obj.context, ctx);
45
+ };
46
+ $.add_event = (name3, attributes) => {
47
+ span_obj.events.push({
48
+ name: name3,
49
+ timestamp: Date.now(),
50
+ attributes: attributes || {}
51
+ });
115
52
  };
116
- const root = span(name, typeof options.traceparent === 'string'
117
- ? tctx__namespace.parse(options.traceparent)
118
- : undefined);
119
- const endRoot = root.end.bind(root);
120
- root.end = async () => {
121
- endRoot();
122
- if (promises.length)
123
- await Promise.all(promises);
124
- return options.exporter(spans, {
125
- ...(options.context || {}),
126
- ...sdk_object,
127
- });
53
+ $.end = () => {
54
+ if (span_obj.end == null)
55
+ span_obj.end = Date.now();
128
56
  };
129
- return root;
57
+ return $;
58
+ };
59
+ const root = span(name, typeof options.traceparent === "string" ? tctx.parse(options.traceparent) : void 0);
60
+ const endRoot = root.end.bind(root);
61
+ root.end = async () => {
62
+ endRoot();
63
+ if (PROMISES.has(root))
64
+ await Promise.all(PROMISES.get(root));
65
+ return options.exporter(spans, {
66
+ ...options.context || {},
67
+ ...sdk_object
68
+ });
69
+ };
70
+ return root;
130
71
  };
131
72
 
132
- exports.create = create;
73
+
74
+ exports.ADD_PROMISE = ADD_PROMISE;
75
+ exports.PROMISES = PROMISES;
76
+ exports.create = create;
package/index.mjs CHANGED
@@ -1,110 +1,76 @@
1
- import { name, version } from 'rian/package.json';
2
- import * as tctx from 'tctx';
3
-
4
- const set_error = (scope, error) => {
5
- scope.set_context({
6
- error,
7
- });
8
- };
9
- const measure = (fn, // TODO: fn doesnt see scope correctly
10
- scope, promises, ...args) => {
11
- try {
12
- var r = fn(...args, scope), is_promise = r instanceof Promise;
13
- if (is_promise)
14
- promises.push(r
15
- .catch((e) => void set_error(scope, e))
16
- .finally(() => scope.end()));
17
- return r;
18
- }
19
- catch (e) {
20
- set_error(scope, e);
21
- throw e;
22
- }
23
- finally {
24
- if (is_promise !== true)
25
- scope.end();
26
- }
1
+ // src/index.ts
2
+ import { name as rian_name, version as rian_version } from "rian/package.json";
3
+ import * as tctx from "tctx";
4
+ import { measureFn } from "rian/utils";
5
+ var PROMISES = /* @__PURE__ */ new WeakMap();
6
+ var ADD_PROMISE = (scope, promise) => {
7
+ if (PROMISES.has(scope))
8
+ PROMISES.get(scope).push(promise);
9
+ else
10
+ PROMISES.set(scope, [promise]);
27
11
  };
28
-
29
- // ==> impl
30
- /**
31
- * The default sampler;
32
- *
33
- * If no parent
34
- * ~> sample
35
- * if parent was off
36
- * ~> never sample
37
- * if parent was on
38
- * ~> always sample
39
- */
40
- const defaultSampler = (_name, parentId) => {
41
- if (!parentId)
42
- return true;
43
- return tctx.is_sampled(parentId);
12
+ var defaultSampler = (_name, parentId) => {
13
+ if (!parentId)
14
+ return true;
15
+ return tctx.is_sampled(parentId);
44
16
  };
45
- const sdk_object = {
46
- 'telemetry.sdk.name': name,
47
- 'telemetry.sdk.version': version,
17
+ var sdk_object = {
18
+ "telemetry.sdk.name": rian_name,
19
+ "telemetry.sdk.version": rian_version
48
20
  };
49
- const create = (name, options) => {
50
- const spans = new Set();
51
- const promises = [];
52
- const sampler = options.sampler || defaultSampler;
53
- const sampler_callable = typeof sampler !== 'boolean';
54
- const span = (name, parent) => {
55
- const should_sample = sampler_callable
56
- ? sampler(name, parent, options.context)
57
- : sampler;
58
- const id = parent
59
- ? parent.child(should_sample)
60
- : tctx.make(should_sample);
61
- const span_obj = {
62
- id,
63
- parent,
64
- start: Date.now(),
65
- name,
66
- events: [],
67
- context: {},
68
- };
69
- if (should_sample)
70
- spans.add(span_obj);
71
- const $ = (cb) => measure(cb, $, promises);
72
- $.traceparent = id;
73
- $.fork = (name) => span(name, id);
74
- $.measure = (name, cb, ...args) => measure(cb, span(name, id), promises, ...args);
75
- $.set_context = (ctx) => {
76
- if (typeof ctx === 'function')
77
- return void (span_obj.context = ctx(span_obj.context));
78
- Object.assign(span_obj.context, ctx);
79
- };
80
- $.add_event = (name, attributes) => {
81
- span_obj.events.push({
82
- name,
83
- timestamp: Date.now(),
84
- attributes: attributes || {},
85
- });
86
- };
87
- $.end = () => {
88
- if (span_obj.end)
89
- return void 0;
90
- span_obj.end = Date.now();
91
- };
92
- return $;
21
+ var create = (name, options) => {
22
+ const spans = /* @__PURE__ */ new Set();
23
+ const sampler = options.sampler || defaultSampler;
24
+ const sampler_callable = typeof sampler !== "boolean";
25
+ const span = (name2, parent) => {
26
+ const should_sample = sampler_callable ? sampler(name2, parent, options.context) : sampler;
27
+ const id = parent ? parent.child(should_sample) : tctx.make(should_sample);
28
+ const span_obj = {
29
+ id,
30
+ parent,
31
+ start: Date.now(),
32
+ name: name2,
33
+ events: [],
34
+ context: {}
35
+ };
36
+ if (should_sample)
37
+ spans.add(span_obj);
38
+ const $ = (cb) => measureFn($, cb);
39
+ $.traceparent = id;
40
+ $.fork = (name3) => span(name3, id);
41
+ $.set_context = (ctx) => {
42
+ if (typeof ctx === "function")
43
+ return void (span_obj.context = ctx(span_obj.context));
44
+ Object.assign(span_obj.context, ctx);
93
45
  };
94
- const root = span(name, typeof options.traceparent === 'string'
95
- ? tctx.parse(options.traceparent)
96
- : undefined);
97
- const endRoot = root.end.bind(root);
98
- root.end = async () => {
99
- endRoot();
100
- if (promises.length)
101
- await Promise.all(promises);
102
- return options.exporter(spans, {
103
- ...(options.context || {}),
104
- ...sdk_object,
105
- });
46
+ $.add_event = (name3, attributes) => {
47
+ span_obj.events.push({
48
+ name: name3,
49
+ timestamp: Date.now(),
50
+ attributes: attributes || {}
51
+ });
106
52
  };
107
- return root;
53
+ $.end = () => {
54
+ if (span_obj.end == null)
55
+ span_obj.end = Date.now();
56
+ };
57
+ return $;
58
+ };
59
+ const root = span(name, typeof options.traceparent === "string" ? tctx.parse(options.traceparent) : void 0);
60
+ const endRoot = root.end.bind(root);
61
+ root.end = async () => {
62
+ endRoot();
63
+ if (PROMISES.has(root))
64
+ await Promise.all(PROMISES.get(root));
65
+ return options.exporter(spans, {
66
+ ...options.context || {},
67
+ ...sdk_object
68
+ });
69
+ };
70
+ return root;
71
+ };
72
+ export {
73
+ ADD_PROMISE,
74
+ PROMISES,
75
+ create
108
76
  };
109
-
110
- export { create };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rian",
3
- "version": "0.0.5",
3
+ "version": "0.2.1",
4
4
  "description": "Effective tracing for the edge and origins",
5
5
  "keywords": [
6
6
  "opentelemetry",
@@ -26,16 +26,24 @@
26
26
  "type": "module",
27
27
  "exports": {
28
28
  ".": {
29
+ "types": "./index.d.ts",
29
30
  "import": "./index.mjs",
30
31
  "require": "./index.js"
31
32
  },
32
33
  "./exporter.otel.http": {
33
- "import": "./exporter.otel.http.mjs",
34
- "require": "./exporter.otel.http.js"
34
+ "types": "./exporter.otel.http/index.d.ts",
35
+ "import": "./exporter.otel.http/index.mjs",
36
+ "require": "./exporter.otel.http/index.js"
35
37
  },
36
38
  "./exporter.zipkin": {
37
- "import": "./exporter.zipkin.mjs",
38
- "require": "./exporter.zipkin.js"
39
+ "types": "./exporter.zipkin/index.d.ts",
40
+ "import": "./exporter.zipkin/index.mjs",
41
+ "require": "./exporter.zipkin/index.js"
42
+ },
43
+ "./utils": {
44
+ "types": "./utils/index.d.ts",
45
+ "import": "./utils/index.mjs",
46
+ "require": "./utils/index.js"
39
47
  },
40
48
  "./package.json": "./package.json"
41
49
  },
@@ -45,10 +53,18 @@
45
53
  "files": [
46
54
  "*.mjs",
47
55
  "*.js",
48
- "*.d.ts"
56
+ "*.d.ts",
57
+ "exporter.*/*",
58
+ "utils/*"
49
59
  ],
60
+ "scripts": {
61
+ "build": "bundt"
62
+ },
50
63
  "dependencies": {
51
64
  "flattie": "^1.1.0",
52
65
  "tctx": "^0.0.10"
66
+ },
67
+ "devDependencies": {
68
+ "bundt": "2.0.0-next.5"
53
69
  }
54
70
  }
@@ -0,0 +1,68 @@
1
+ import type { Scope } from 'rian';
2
+
3
+ export type MeasureFn =
4
+ | ((...args: [...args: any[]]) => any)
5
+ | ((...args: [...args: any[], scope: Scope]) => any);
6
+
7
+ /**
8
+ * With a passed function — will start a span, and run the function, when the function finishes
9
+ * the span finishes.
10
+ *
11
+ * The measure method will return whatever the function is, so if it's a promise, it returns a
12
+ * promise and so on. Any error is caught and re thrown, and automatically tracked in the
13
+ * context under the `error` property.
14
+ *
15
+ * All promises are tracked, and awaited on a `tracer.end`.
16
+ *
17
+ * @example
18
+ *
19
+ * ```text
20
+ * const data = await measure(scope, 'name', get_data, 'user_id_123');
21
+ * ^ ^ ^ ^ ^
22
+ * | | | | |
23
+ * | | | | the first argument to get_data
24
+ * | | | function to be called
25
+ * | | the name of the sub scope
26
+ * | |
27
+ * | the parent scope
28
+ * return value from get_data
29
+ * ```
30
+ */
31
+ export const measure: <Fn extends MeasureFn>(
32
+ scope: Scope,
33
+ name: string,
34
+ fn: Fn, // TODO: fn doesnt see scope correctly
35
+ ...args: RealMeasureFnParams<Parameters<Fn>>
36
+ ) => ReturnType<Fn>;
37
+
38
+ /**
39
+ * Wraps any function with a measured scoped function. Useful for when defer function execution
40
+ * till a later time.
41
+ *
42
+ * @example
43
+ *
44
+ * ```js
45
+ * const wrapped = wrap(scope, "run something", my_function);
46
+ *
47
+ * // ... lots of things, where the access to `scope` is lost.
48
+ *
49
+ * wrapped();
50
+ * ```
51
+ */
52
+ export const wrap: <Fn extends MeasureFn>(
53
+ scope: Scope,
54
+ name: string,
55
+ fn: Fn, // TODO: fn doesnt see scope correctly
56
+ ) => Fn;
57
+
58
+ // ==> internals
59
+
60
+ /** @internal */
61
+ export type RealMeasureFnParams<T extends unknown[]> = T extends []
62
+ ? []
63
+ : T extends [...rest: infer U, scope: Scope]
64
+ ? U
65
+ : T;
66
+
67
+ /** @internal */
68
+ export const measureFn: (scope: Scope, fn: any, ...args: any[]) => any;
package/utils/index.js ADDED
@@ -0,0 +1,30 @@
1
+ // src/utils.ts
2
+ const { ADD_PROMISE, PROMISES } = require('rian');
3
+ var measureFn = (scope, fn, ...args) => {
4
+ try {
5
+ var r = fn(...args, scope), is_promise = r instanceof Promise;
6
+ if (is_promise && !PROMISES.has(scope))
7
+ ADD_PROMISE(scope, r.catch((e) => void scope.set_context({
8
+ error: e
9
+ })).finally(() => scope.end()));
10
+ return r;
11
+ } catch (e) {
12
+ if (e instanceof Error)
13
+ scope.set_context({
14
+ error: e
15
+ });
16
+ throw e;
17
+ } finally {
18
+ if (is_promise !== true)
19
+ scope.end();
20
+ }
21
+ };
22
+ var measure = (scope, name, fn, ...args) => measureFn(scope.fork(name), fn, ...args);
23
+ var wrap = (scope, name, fn) => function() {
24
+ return measureFn(scope.fork(name), fn, ...arguments);
25
+ };
26
+
27
+
28
+ exports.measure = measure;
29
+ exports.measureFn = measureFn;
30
+ exports.wrap = wrap;
@@ -0,0 +1,30 @@
1
+ // src/utils.ts
2
+ import { ADD_PROMISE, PROMISES } from "rian";
3
+ var measureFn = (scope, fn, ...args) => {
4
+ try {
5
+ var r = fn(...args, scope), is_promise = r instanceof Promise;
6
+ if (is_promise && !PROMISES.has(scope))
7
+ ADD_PROMISE(scope, r.catch((e) => void scope.set_context({
8
+ error: e
9
+ })).finally(() => scope.end()));
10
+ return r;
11
+ } catch (e) {
12
+ if (e instanceof Error)
13
+ scope.set_context({
14
+ error: e
15
+ });
16
+ throw e;
17
+ } finally {
18
+ if (is_promise !== true)
19
+ scope.end();
20
+ }
21
+ };
22
+ var measure = (scope, name, fn, ...args) => measureFn(scope.fork(name), fn, ...args);
23
+ var wrap = (scope, name, fn) => function() {
24
+ return measureFn(scope.fork(name), fn, ...arguments);
25
+ };
26
+ export {
27
+ measure,
28
+ measureFn,
29
+ wrap
30
+ };
@@ -1,5 +0,0 @@
1
- import * as rian from 'rian';
2
-
3
- declare const exporter: (request: (payload: any) => any) => rian.Exporter;
4
-
5
- export { exporter };