rian 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- package/exporter.otel.http.js +1 -0
- package/exporter.otel.http.mjs +1 -0
- package/index.js +5 -52
- package/index.mjs +5 -52
- package/package.json +1 -1
- package/utils.d.ts +12 -9
- package/utils.js +21 -12
- package/utils.mjs +21 -13
package/exporter.otel.http.js
CHANGED
@@ -79,6 +79,7 @@ const exporter = (request) => (spans, context) => {
|
|
79
79
|
droppedEventsCount: 0,
|
80
80
|
droppedLinksCount: 0,
|
81
81
|
attributes: convert_object_to_kv(span_ctx),
|
82
|
+
// @ts-expect-error TS2454
|
82
83
|
status: status || { code: SpanStatusCode_UNSET },
|
83
84
|
events: span.events.map((i) => ({
|
84
85
|
name: i.name,
|
package/exporter.otel.http.mjs
CHANGED
@@ -77,6 +77,7 @@ const exporter = (request) => (spans, context) => {
|
|
77
77
|
droppedEventsCount: 0,
|
78
78
|
droppedLinksCount: 0,
|
79
79
|
attributes: convert_object_to_kv(span_ctx),
|
80
|
+
// @ts-expect-error TS2454
|
80
81
|
status: status || { code: SpanStatusCode_UNSET },
|
81
82
|
events: span.events.map((i) => ({
|
82
83
|
name: i.name,
|
package/index.js
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
const package_json = require('rian/package.json');
|
4
4
|
const tctx = require('tctx');
|
5
|
-
const
|
5
|
+
const utils = require('rian/utils');
|
6
6
|
|
7
7
|
function _interopNamespace(e) {
|
8
8
|
if (e && e.__esModule) return e;
|
@@ -24,53 +24,6 @@ function _interopNamespace(e) {
|
|
24
24
|
|
25
25
|
const tctx__namespace = /*#__PURE__*/_interopNamespace(tctx);
|
26
26
|
|
27
|
-
const set_error = (scope, error) => {
|
28
|
-
scope.set_context({
|
29
|
-
error,
|
30
|
-
});
|
31
|
-
};
|
32
|
-
const measureFn = (scope, fn, ...args) => {
|
33
|
-
try {
|
34
|
-
var r = fn(...args, scope), is_promise = r instanceof Promise;
|
35
|
-
if (is_promise && rian.PROMISES.has(scope))
|
36
|
-
rian.ADD_PROMISE(scope, 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
|
-
}
|
49
|
-
};
|
50
|
-
/**
|
51
|
-
* With a passed function — will start a span, and run the function, when the function finishes
|
52
|
-
* the span finishes.
|
53
|
-
*
|
54
|
-
* The measure method will return whatever the function is, so if it's a promise, it returns a
|
55
|
-
* promise and so on. Any error is caught and re thrown, and automatically tracked in the
|
56
|
-
* context under the `error` property.
|
57
|
-
*
|
58
|
-
* All promises are tracked, and awaited on a `tracer.end`.
|
59
|
-
*
|
60
|
-
* @example
|
61
|
-
*
|
62
|
-
* ```text
|
63
|
-
* const data = await measure(scope, get_data, 'user_id_123');
|
64
|
-
* ^ ^ ^
|
65
|
-
* | | |
|
66
|
-
* | | the first argument to get_data
|
67
|
-
* | function to be called
|
68
|
-
* return value from get_data
|
69
|
-
* ```
|
70
|
-
*/
|
71
|
-
const measure = (scope, fn, // TODO: fn doesnt see scope correctly
|
72
|
-
...args) => measureFn(scope, fn, ...args);
|
73
|
-
|
74
27
|
// ==> impl
|
75
28
|
/**
|
76
29
|
* @internal
|
@@ -125,9 +78,10 @@ const create = (name, options) => {
|
|
125
78
|
};
|
126
79
|
if (should_sample)
|
127
80
|
spans.add(span_obj);
|
128
|
-
const $ = (cb) =>
|
81
|
+
const $ = (cb) => utils.measureFn($, cb);
|
129
82
|
$.traceparent = id;
|
130
83
|
$.fork = (name) => span(name, id);
|
84
|
+
// @ts-expect-error TS7030 its always undefined ts :eye-roll:
|
131
85
|
$.set_context = (ctx) => {
|
132
86
|
if (typeof ctx === 'function')
|
133
87
|
return void (span_obj.context = ctx(span_obj.context));
|
@@ -141,9 +95,8 @@ const create = (name, options) => {
|
|
141
95
|
});
|
142
96
|
};
|
143
97
|
$.end = () => {
|
144
|
-
if (span_obj.end)
|
145
|
-
|
146
|
-
span_obj.end = Date.now();
|
98
|
+
if (span_obj.end == null)
|
99
|
+
span_obj.end = Date.now();
|
147
100
|
};
|
148
101
|
return $;
|
149
102
|
};
|
package/index.mjs
CHANGED
@@ -1,53 +1,6 @@
|
|
1
1
|
import { name, version } from 'rian/package.json';
|
2
2
|
import * as tctx from 'tctx';
|
3
|
-
import {
|
4
|
-
|
5
|
-
const set_error = (scope, error) => {
|
6
|
-
scope.set_context({
|
7
|
-
error,
|
8
|
-
});
|
9
|
-
};
|
10
|
-
const measureFn = (scope, fn, ...args) => {
|
11
|
-
try {
|
12
|
-
var r = fn(...args, scope), is_promise = r instanceof Promise;
|
13
|
-
if (is_promise && PROMISES$1.has(scope))
|
14
|
-
ADD_PROMISE$1(scope, 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
|
-
}
|
27
|
-
};
|
28
|
-
/**
|
29
|
-
* With a passed function — will start a span, and run the function, when the function finishes
|
30
|
-
* the span finishes.
|
31
|
-
*
|
32
|
-
* The measure method will return whatever the function is, so if it's a promise, it returns a
|
33
|
-
* promise and so on. Any error is caught and re thrown, and automatically tracked in the
|
34
|
-
* context under the `error` property.
|
35
|
-
*
|
36
|
-
* All promises are tracked, and awaited on a `tracer.end`.
|
37
|
-
*
|
38
|
-
* @example
|
39
|
-
*
|
40
|
-
* ```text
|
41
|
-
* const data = await measure(scope, get_data, 'user_id_123');
|
42
|
-
* ^ ^ ^
|
43
|
-
* | | |
|
44
|
-
* | | the first argument to get_data
|
45
|
-
* | function to be called
|
46
|
-
* return value from get_data
|
47
|
-
* ```
|
48
|
-
*/
|
49
|
-
const measure = (scope, fn, // TODO: fn doesnt see scope correctly
|
50
|
-
...args) => measureFn(scope, fn, ...args);
|
3
|
+
import { measureFn } from 'rian/utils';
|
51
4
|
|
52
5
|
// ==> impl
|
53
6
|
/**
|
@@ -103,9 +56,10 @@ const create = (name, options) => {
|
|
103
56
|
};
|
104
57
|
if (should_sample)
|
105
58
|
spans.add(span_obj);
|
106
|
-
const $ = (cb) =>
|
59
|
+
const $ = (cb) => measureFn($, cb);
|
107
60
|
$.traceparent = id;
|
108
61
|
$.fork = (name) => span(name, id);
|
62
|
+
// @ts-expect-error TS7030 its always undefined ts :eye-roll:
|
109
63
|
$.set_context = (ctx) => {
|
110
64
|
if (typeof ctx === 'function')
|
111
65
|
return void (span_obj.context = ctx(span_obj.context));
|
@@ -119,9 +73,8 @@ const create = (name, options) => {
|
|
119
73
|
});
|
120
74
|
};
|
121
75
|
$.end = () => {
|
122
|
-
if (span_obj.end)
|
123
|
-
|
124
|
-
span_obj.end = Date.now();
|
76
|
+
if (span_obj.end == null)
|
77
|
+
span_obj.end = Date.now();
|
125
78
|
};
|
126
79
|
return $;
|
127
80
|
};
|
package/package.json
CHANGED
package/utils.d.ts
CHANGED
@@ -15,15 +15,18 @@ declare type RealMeasureFnParams<T extends unknown[]> = T extends [] ? [] : T ex
|
|
15
15
|
* @example
|
16
16
|
*
|
17
17
|
* ```text
|
18
|
-
* const data = await measure(scope, get_data, 'user_id_123');
|
19
|
-
* ^
|
20
|
-
* |
|
21
|
-
* |
|
22
|
-
* |
|
23
|
-
*
|
18
|
+
* const data = await measure(scope, 'name', get_data, 'user_id_123');
|
19
|
+
* ^ ^ ^ ^ ^
|
20
|
+
* | | | | |
|
21
|
+
* | | | | the first argument to get_data
|
22
|
+
* | | | function to be called
|
23
|
+
* | | the name of the sub scope
|
24
|
+
* | |
|
25
|
+
* | the parent scope
|
26
|
+
* return value from get_data
|
24
27
|
* ```
|
25
28
|
*/
|
26
|
-
declare const measure: <Fn extends MeasureFn>(scope: Scope, fn: Fn, ...args: RealMeasureFnParams<Parameters<Fn>>) => ReturnType<Fn>;
|
29
|
+
declare const measure: <Fn extends MeasureFn>(scope: Scope, name: string, fn: Fn, ...args: RealMeasureFnParams<Parameters<Fn>>) => ReturnType<Fn>;
|
27
30
|
/**
|
28
31
|
* Wraps any function with a measured scoped function. Useful for when defer function execution
|
29
32
|
* till a later time.
|
@@ -31,13 +34,13 @@ declare const measure: <Fn extends MeasureFn>(scope: Scope, fn: Fn, ...args: Rea
|
|
31
34
|
* @example
|
32
35
|
*
|
33
36
|
* ```js
|
34
|
-
* const wrapped = wrap(scope, my_function);
|
37
|
+
* const wrapped = wrap(scope, "run something", my_function);
|
35
38
|
*
|
36
39
|
* // ... lots of things, where the access to `scope` is lost.
|
37
40
|
*
|
38
41
|
* wrapped();
|
39
42
|
* ```
|
40
43
|
*/
|
41
|
-
declare const wrap: <Fn extends MeasureFn>(scope: Scope, fn: Fn) => Fn;
|
44
|
+
declare const wrap: <Fn extends MeasureFn>(scope: Scope, name: string, fn: Fn) => Fn;
|
42
45
|
|
43
46
|
export { MeasureFn, measure, wrap };
|
package/utils.js
CHANGED
@@ -7,6 +7,9 @@ const set_error = (scope, error) => {
|
|
7
7
|
error,
|
8
8
|
});
|
9
9
|
};
|
10
|
+
/**
|
11
|
+
* @internal
|
12
|
+
*/
|
10
13
|
const measureFn = (scope, fn, ...args) => {
|
11
14
|
try {
|
12
15
|
var r = fn(...args, scope), is_promise = r instanceof Promise;
|
@@ -17,10 +20,12 @@ const measureFn = (scope, fn, ...args) => {
|
|
17
20
|
return r;
|
18
21
|
}
|
19
22
|
catch (e) {
|
20
|
-
|
23
|
+
if (e instanceof Error)
|
24
|
+
set_error(scope, e);
|
21
25
|
throw e;
|
22
26
|
}
|
23
27
|
finally {
|
28
|
+
// @ts-expect-error TS2454
|
24
29
|
if (is_promise !== true)
|
25
30
|
scope.end();
|
26
31
|
}
|
@@ -38,16 +43,19 @@ const measureFn = (scope, fn, ...args) => {
|
|
38
43
|
* @example
|
39
44
|
*
|
40
45
|
* ```text
|
41
|
-
* const data = await measure(scope, get_data, 'user_id_123');
|
42
|
-
* ^
|
43
|
-
* |
|
44
|
-
* |
|
45
|
-
* |
|
46
|
-
*
|
46
|
+
* const data = await measure(scope, 'name', get_data, 'user_id_123');
|
47
|
+
* ^ ^ ^ ^ ^
|
48
|
+
* | | | | |
|
49
|
+
* | | | | the first argument to get_data
|
50
|
+
* | | | function to be called
|
51
|
+
* | | the name of the sub scope
|
52
|
+
* | |
|
53
|
+
* | the parent scope
|
54
|
+
* return value from get_data
|
47
55
|
* ```
|
48
56
|
*/
|
49
|
-
const measure = (scope, fn, // TODO: fn doesnt see scope correctly
|
50
|
-
...args) => measureFn(scope, fn, ...args);
|
57
|
+
const measure = (scope, name, fn, // TODO: fn doesnt see scope correctly
|
58
|
+
...args) => measureFn(scope.fork(name), fn, ...args);
|
51
59
|
/**
|
52
60
|
* Wraps any function with a measured scoped function. Useful for when defer function execution
|
53
61
|
* till a later time.
|
@@ -55,16 +63,17 @@ const measure = (scope, fn, // TODO: fn doesnt see scope correctly
|
|
55
63
|
* @example
|
56
64
|
*
|
57
65
|
* ```js
|
58
|
-
* const wrapped = wrap(scope, my_function);
|
66
|
+
* const wrapped = wrap(scope, "run something", my_function);
|
59
67
|
*
|
60
68
|
* // ... lots of things, where the access to `scope` is lost.
|
61
69
|
*
|
62
70
|
* wrapped();
|
63
71
|
* ```
|
64
72
|
*/
|
65
|
-
const wrap = (scope, fn) => function () {
|
66
|
-
return measureFn(scope, fn, ...arguments);
|
73
|
+
const wrap = (scope, name, fn) => function () {
|
74
|
+
return measureFn(scope.fork(name), fn, ...arguments);
|
67
75
|
};
|
68
76
|
|
69
77
|
exports.measure = measure;
|
78
|
+
exports.measureFn = measureFn;
|
70
79
|
exports.wrap = wrap;
|
package/utils.mjs
CHANGED
@@ -5,6 +5,9 @@ const set_error = (scope, error) => {
|
|
5
5
|
error,
|
6
6
|
});
|
7
7
|
};
|
8
|
+
/**
|
9
|
+
* @internal
|
10
|
+
*/
|
8
11
|
const measureFn = (scope, fn, ...args) => {
|
9
12
|
try {
|
10
13
|
var r = fn(...args, scope), is_promise = r instanceof Promise;
|
@@ -15,10 +18,12 @@ const measureFn = (scope, fn, ...args) => {
|
|
15
18
|
return r;
|
16
19
|
}
|
17
20
|
catch (e) {
|
18
|
-
|
21
|
+
if (e instanceof Error)
|
22
|
+
set_error(scope, e);
|
19
23
|
throw e;
|
20
24
|
}
|
21
25
|
finally {
|
26
|
+
// @ts-expect-error TS2454
|
22
27
|
if (is_promise !== true)
|
23
28
|
scope.end();
|
24
29
|
}
|
@@ -36,16 +41,19 @@ const measureFn = (scope, fn, ...args) => {
|
|
36
41
|
* @example
|
37
42
|
*
|
38
43
|
* ```text
|
39
|
-
* const data = await measure(scope, get_data, 'user_id_123');
|
40
|
-
* ^
|
41
|
-
* |
|
42
|
-
* |
|
43
|
-
* |
|
44
|
-
*
|
44
|
+
* const data = await measure(scope, 'name', get_data, 'user_id_123');
|
45
|
+
* ^ ^ ^ ^ ^
|
46
|
+
* | | | | |
|
47
|
+
* | | | | the first argument to get_data
|
48
|
+
* | | | function to be called
|
49
|
+
* | | the name of the sub scope
|
50
|
+
* | |
|
51
|
+
* | the parent scope
|
52
|
+
* return value from get_data
|
45
53
|
* ```
|
46
54
|
*/
|
47
|
-
const measure = (scope, fn, // TODO: fn doesnt see scope correctly
|
48
|
-
...args) => measureFn(scope, fn, ...args);
|
55
|
+
const measure = (scope, name, fn, // TODO: fn doesnt see scope correctly
|
56
|
+
...args) => measureFn(scope.fork(name), fn, ...args);
|
49
57
|
/**
|
50
58
|
* Wraps any function with a measured scoped function. Useful for when defer function execution
|
51
59
|
* till a later time.
|
@@ -53,15 +61,15 @@ const measure = (scope, fn, // TODO: fn doesnt see scope correctly
|
|
53
61
|
* @example
|
54
62
|
*
|
55
63
|
* ```js
|
56
|
-
* const wrapped = wrap(scope, my_function);
|
64
|
+
* const wrapped = wrap(scope, "run something", my_function);
|
57
65
|
*
|
58
66
|
* // ... lots of things, where the access to `scope` is lost.
|
59
67
|
*
|
60
68
|
* wrapped();
|
61
69
|
* ```
|
62
70
|
*/
|
63
|
-
const wrap = (scope, fn) => function () {
|
64
|
-
return measureFn(scope, fn, ...arguments);
|
71
|
+
const wrap = (scope, name, fn) => function () {
|
72
|
+
return measureFn(scope.fork(name), fn, ...arguments);
|
65
73
|
};
|
66
74
|
|
67
|
-
export { measure, wrap };
|
75
|
+
export { measure, measureFn, wrap };
|