rian 0.1.1 → 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.
@@ -1,111 +0,0 @@
1
- import { name, version } from 'rian/package.json';
2
-
3
- const SpanStatusCode_UNSET = 0;
4
- const SpanStatusCode_ERROR = 2;
5
- const convert_value_to_anyvalue = (value) => {
6
- let type = typeof value, any_value = {};
7
- if (type === 'string')
8
- any_value.stringValue = value;
9
- else if (type === 'number')
10
- if (Number.isInteger(value))
11
- any_value.intValue = value;
12
- else
13
- any_value.doubleValue = value;
14
- else if (type === 'boolean')
15
- any_value.boolValue = value;
16
- else if (Array.isArray(value))
17
- any_value.arrayValue = {
18
- values: value.map((i) => convert_value_to_anyvalue(i)),
19
- };
20
- else if (value)
21
- any_value.kvlistValue = { values: convert_object_to_kv(value) };
22
- return any_value;
23
- };
24
- const convert_object_to_kv = (input) => {
25
- const value = [];
26
- for (let key of Object.keys(input)) {
27
- value.push({
28
- key,
29
- value: convert_value_to_anyvalue(input[key]),
30
- });
31
- }
32
- return value;
33
- };
34
- // https://github.com/open-telemetry/opentelemetry-proto/blob/b43e9b18b76abf3ee040164b55b9c355217151f3/opentelemetry/proto/trace/v1/trace.proto#L127-L155
35
- const map_kind = (kind) => {
36
- switch (kind) {
37
- default:
38
- case 'INTERNAL': {
39
- return 1;
40
- }
41
- case 'SERVER': {
42
- return 2;
43
- }
44
- case 'CLIENT': {
45
- return 3;
46
- }
47
- case 'PRODUCER': {
48
- return 4;
49
- }
50
- case 'CONSUMER': {
51
- return 5;
52
- }
53
- }
54
- };
55
- const exporter = (request) => (spans, context) => {
56
- const otel_spans = [];
57
- for (let span of spans) {
58
- const { kind, error, ...span_ctx } = span.context;
59
- let status;
60
- if (error) {
61
- status = {
62
- code: SpanStatusCode_ERROR,
63
- };
64
- if ('message' in error) {
65
- status.message = error.message;
66
- }
67
- }
68
- otel_spans.push({
69
- traceId: span.id.trace_id,
70
- spanId: span.id.parent_id,
71
- parentSpanId: span.parent?.parent_id,
72
- name: span.name,
73
- kind: map_kind(kind || 'INTERNAL'),
74
- startTimeUnixNano: span.start * 1000000,
75
- endTimeUnixNano: span.end ? span.end * 1000000 : undefined,
76
- droppedAttributesCount: 0,
77
- droppedEventsCount: 0,
78
- droppedLinksCount: 0,
79
- attributes: convert_object_to_kv(span_ctx),
80
- // @ts-expect-error TS2454
81
- status: status || { code: SpanStatusCode_UNSET },
82
- events: span.events.map((i) => ({
83
- name: i.name,
84
- attributes: convert_object_to_kv(i.attributes),
85
- droppedAttributesCount: 0,
86
- timeUnixNano: i.timestamp * 1000000,
87
- })),
88
- });
89
- }
90
- return request({
91
- resourceSpans: [
92
- {
93
- resource: {
94
- attributes: convert_object_to_kv(context),
95
- droppedAttributesCount: 0,
96
- },
97
- instrumentationLibrarySpans: [
98
- {
99
- instrumentationLibrary: {
100
- name: name,
101
- version: version,
102
- },
103
- spans: otel_spans,
104
- },
105
- ],
106
- },
107
- ],
108
- });
109
- };
110
-
111
- export { exporter };
@@ -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 };
@@ -1,45 +0,0 @@
1
- 'use strict';
2
-
3
- const flattie = require('flattie');
4
-
5
- const exporter = (request) => (spans, context) => {
6
- const zipkin = [];
7
- for (let span of spans) {
8
- const { kind, error, ...span_ctx } = span.context;
9
- if (error) {
10
- if ('message' in error) {
11
- span_ctx.error = {
12
- name: error.name,
13
- message: error.message,
14
- stack: error.stack,
15
- };
16
- }
17
- else {
18
- span_ctx.error = true;
19
- }
20
- }
21
- zipkin.push({
22
- id: span.id.parent_id,
23
- traceId: span.id.trace_id,
24
- parentId: span.parent ? span.parent.parent_id : undefined,
25
- name: span.name,
26
- kind: kind === 'INTERNAL' ? undefined : kind,
27
- timestamp: span.start * 1000,
28
- duration: span.end ? (span.end - span.start) * 1000 : undefined,
29
- localEndpoint: context.localEndpoint || {
30
- serviceName: span_ctx['service.name'],
31
- },
32
- tags: flattie.flattie({
33
- ...context,
34
- ...span_ctx,
35
- }, '.', true),
36
- annotations: span.events.map((i) => ({
37
- value: `${i.name} :: ${JSON.stringify(i.attributes)}`,
38
- timestamp: i.timestamp * 1000,
39
- })),
40
- });
41
- }
42
- return request(zipkin);
43
- };
44
-
45
- exports.exporter = exporter;
@@ -1,43 +0,0 @@
1
- import { flattie } from 'flattie';
2
-
3
- const exporter = (request) => (spans, context) => {
4
- const zipkin = [];
5
- for (let span of spans) {
6
- const { kind, error, ...span_ctx } = span.context;
7
- if (error) {
8
- if ('message' in error) {
9
- span_ctx.error = {
10
- name: error.name,
11
- message: error.message,
12
- stack: error.stack,
13
- };
14
- }
15
- else {
16
- span_ctx.error = true;
17
- }
18
- }
19
- zipkin.push({
20
- id: span.id.parent_id,
21
- traceId: span.id.trace_id,
22
- parentId: span.parent ? span.parent.parent_id : undefined,
23
- name: span.name,
24
- kind: kind === 'INTERNAL' ? undefined : kind,
25
- timestamp: span.start * 1000,
26
- duration: span.end ? (span.end - span.start) * 1000 : undefined,
27
- localEndpoint: context.localEndpoint || {
28
- serviceName: span_ctx['service.name'],
29
- },
30
- tags: flattie({
31
- ...context,
32
- ...span_ctx,
33
- }, '.', true),
34
- annotations: span.events.map((i) => ({
35
- value: `${i.name} :: ${JSON.stringify(i.attributes)}`,
36
- timestamp: i.timestamp * 1000,
37
- })),
38
- });
39
- }
40
- return request(zipkin);
41
- };
42
-
43
- export { exporter };
package/utils.js DELETED
@@ -1,79 +0,0 @@
1
- 'use strict';
2
-
3
- const rian = require('rian');
4
-
5
- const set_error = (scope, error) => {
6
- scope.set_context({
7
- error,
8
- });
9
- };
10
- /**
11
- * @internal
12
- */
13
- const measureFn = (scope, fn, ...args) => {
14
- try {
15
- var r = fn(...args, scope), is_promise = r instanceof Promise;
16
- if (is_promise && rian.PROMISES.has(scope))
17
- rian.ADD_PROMISE(scope, r
18
- .catch((e) => void set_error(scope, e))
19
- .finally(() => scope.end()));
20
- return r;
21
- }
22
- catch (e) {
23
- if (e instanceof Error)
24
- set_error(scope, e);
25
- throw e;
26
- }
27
- finally {
28
- // @ts-expect-error TS2454
29
- if (is_promise !== true)
30
- scope.end();
31
- }
32
- };
33
- /**
34
- * With a passed function — will start a span, and run the function, when the function finishes
35
- * the span finishes.
36
- *
37
- * The measure method will return whatever the function is, so if it's a promise, it returns a
38
- * promise and so on. Any error is caught and re thrown, and automatically tracked in the
39
- * context under the `error` property.
40
- *
41
- * All promises are tracked, and awaited on a `tracer.end`.
42
- *
43
- * @example
44
- *
45
- * ```text
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
55
- * ```
56
- */
57
- const measure = (scope, name, fn, // TODO: fn doesnt see scope correctly
58
- ...args) => measureFn(scope.fork(name), fn, ...args);
59
- /**
60
- * Wraps any function with a measured scoped function. Useful for when defer function execution
61
- * till a later time.
62
- *
63
- * @example
64
- *
65
- * ```js
66
- * const wrapped = wrap(scope, "run something", my_function);
67
- *
68
- * // ... lots of things, where the access to `scope` is lost.
69
- *
70
- * wrapped();
71
- * ```
72
- */
73
- const wrap = (scope, name, fn) => function () {
74
- return measureFn(scope.fork(name), fn, ...arguments);
75
- };
76
-
77
- exports.measure = measure;
78
- exports.measureFn = measureFn;
79
- exports.wrap = wrap;
package/utils.mjs DELETED
@@ -1,75 +0,0 @@
1
- import { PROMISES, ADD_PROMISE } from 'rian';
2
-
3
- const set_error = (scope, error) => {
4
- scope.set_context({
5
- error,
6
- });
7
- };
8
- /**
9
- * @internal
10
- */
11
- const measureFn = (scope, fn, ...args) => {
12
- try {
13
- var r = fn(...args, scope), is_promise = r instanceof Promise;
14
- if (is_promise && PROMISES.has(scope))
15
- ADD_PROMISE(scope, r
16
- .catch((e) => void set_error(scope, e))
17
- .finally(() => scope.end()));
18
- return r;
19
- }
20
- catch (e) {
21
- if (e instanceof Error)
22
- set_error(scope, e);
23
- throw e;
24
- }
25
- finally {
26
- // @ts-expect-error TS2454
27
- if (is_promise !== true)
28
- scope.end();
29
- }
30
- };
31
- /**
32
- * With a passed function — will start a span, and run the function, when the function finishes
33
- * the span finishes.
34
- *
35
- * The measure method will return whatever the function is, so if it's a promise, it returns a
36
- * promise and so on. Any error is caught and re thrown, and automatically tracked in the
37
- * context under the `error` property.
38
- *
39
- * All promises are tracked, and awaited on a `tracer.end`.
40
- *
41
- * @example
42
- *
43
- * ```text
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
53
- * ```
54
- */
55
- const measure = (scope, name, fn, // TODO: fn doesnt see scope correctly
56
- ...args) => measureFn(scope.fork(name), fn, ...args);
57
- /**
58
- * Wraps any function with a measured scoped function. Useful for when defer function execution
59
- * till a later time.
60
- *
61
- * @example
62
- *
63
- * ```js
64
- * const wrapped = wrap(scope, "run something", my_function);
65
- *
66
- * // ... lots of things, where the access to `scope` is lost.
67
- *
68
- * wrapped();
69
- * ```
70
- */
71
- const wrap = (scope, name, fn) => function () {
72
- return measureFn(scope.fork(name), fn, ...arguments);
73
- };
74
-
75
- export { measure, measureFn, wrap };