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.
package/index.js CHANGED
@@ -1,121 +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
- const utils = require('rian/utils');
6
-
7
- function _interopNamespace(e) {
8
- if (e && e.__esModule) return e;
9
- const n = Object.create(null);
10
- if (e) {
11
- for (const k in e) {
12
- if (k !== 'default') {
13
- const d = Object.getOwnPropertyDescriptor(e, k);
14
- Object.defineProperty(n, k, d.get ? d : {
15
- enumerable: true,
16
- get: function () { return e[k]; }
17
- });
18
- }
19
- }
20
- }
21
- n["default"] = e;
22
- return n;
23
- }
24
-
25
- const tctx__namespace = /*#__PURE__*/_interopNamespace(tctx);
26
-
27
- // ==> impl
28
- /**
29
- * @internal
30
- */
31
- const PROMISES = new WeakMap();
32
- /**
33
- * @internal
34
- */
35
- const ADD_PROMISE = (scope, promise) => {
36
- if (PROMISES.has(scope))
37
- PROMISES.get(scope).push(promise);
38
- else
39
- PROMISES.set(scope, [promise]);
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]);
40
11
  };
41
- /**
42
- * The default sampler;
43
- *
44
- * If no parent
45
- * ~> sample
46
- * if parent was off
47
- * ~> never sample
48
- * if parent was on
49
- * ~> always sample
50
- */
51
- const defaultSampler = (_name, parentId) => {
52
- if (!parentId)
53
- return true;
54
- return tctx__namespace.is_sampled(parentId);
12
+ var defaultSampler = (_name, parentId) => {
13
+ if (!parentId)
14
+ return true;
15
+ return tctx.is_sampled(parentId);
55
16
  };
56
- const sdk_object = {
57
- 'telemetry.sdk.name': package_json.name,
58
- 'telemetry.sdk.version': package_json.version,
17
+ var sdk_object = {
18
+ "telemetry.sdk.name": rian_name,
19
+ "telemetry.sdk.version": rian_version
59
20
  };
60
- const create = (name, options) => {
61
- const spans = new Set();
62
- const sampler = options.sampler || defaultSampler;
63
- const sampler_callable = typeof sampler !== 'boolean';
64
- const span = (name, parent) => {
65
- const should_sample = sampler_callable
66
- ? sampler(name, parent, options.context)
67
- : sampler;
68
- const id = parent
69
- ? parent.child(should_sample)
70
- : tctx__namespace.make(should_sample);
71
- const span_obj = {
72
- id,
73
- parent,
74
- start: Date.now(),
75
- name,
76
- events: [],
77
- context: {},
78
- };
79
- if (should_sample)
80
- spans.add(span_obj);
81
- const $ = (cb) => utils.measureFn($, cb);
82
- $.traceparent = id;
83
- $.fork = (name) => span(name, id);
84
- // @ts-expect-error TS7030 its always undefined ts :eye-roll:
85
- $.set_context = (ctx) => {
86
- if (typeof ctx === 'function')
87
- return void (span_obj.context = ctx(span_obj.context));
88
- Object.assign(span_obj.context, ctx);
89
- };
90
- $.add_event = (name, attributes) => {
91
- span_obj.events.push({
92
- name,
93
- timestamp: Date.now(),
94
- attributes: attributes || {},
95
- });
96
- };
97
- $.end = () => {
98
- if (span_obj.end == null)
99
- span_obj.end = Date.now();
100
- };
101
- 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: {}
102
35
  };
103
- const root = span(name, typeof options.traceparent === 'string'
104
- ? tctx__namespace.parse(options.traceparent)
105
- : undefined);
106
- const endRoot = root.end.bind(root);
107
- root.end = async () => {
108
- endRoot();
109
- if (PROMISES.has(root))
110
- await Promise.all(PROMISES.get(root));
111
- return options.exporter(spans, {
112
- ...(options.context || {}),
113
- ...sdk_object,
114
- });
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);
115
45
  };
116
- return root;
46
+ $.add_event = (name3, attributes) => {
47
+ span_obj.events.push({
48
+ name: name3,
49
+ timestamp: Date.now(),
50
+ attributes: attributes || {}
51
+ });
52
+ };
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;
117
71
  };
118
72
 
73
+
119
74
  exports.ADD_PROMISE = ADD_PROMISE;
120
75
  exports.PROMISES = PROMISES;
121
- exports.create = create;
76
+ exports.create = create;
package/index.mjs CHANGED
@@ -1,97 +1,76 @@
1
- import { name, version } from 'rian/package.json';
2
- import * as tctx from 'tctx';
3
- import { measureFn } from 'rian/utils';
4
-
5
- // ==> impl
6
- /**
7
- * @internal
8
- */
9
- const PROMISES = new WeakMap();
10
- /**
11
- * @internal
12
- */
13
- const ADD_PROMISE = (scope, promise) => {
14
- if (PROMISES.has(scope))
15
- PROMISES.get(scope).push(promise);
16
- else
17
- PROMISES.set(scope, [promise]);
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]);
18
11
  };
19
- /**
20
- * The default sampler;
21
- *
22
- * If no parent
23
- * ~> sample
24
- * if parent was off
25
- * ~> never sample
26
- * if parent was on
27
- * ~> always sample
28
- */
29
- const defaultSampler = (_name, parentId) => {
30
- if (!parentId)
31
- return true;
32
- return tctx.is_sampled(parentId);
12
+ var defaultSampler = (_name, parentId) => {
13
+ if (!parentId)
14
+ return true;
15
+ return tctx.is_sampled(parentId);
33
16
  };
34
- const sdk_object = {
35
- 'telemetry.sdk.name': name,
36
- 'telemetry.sdk.version': version,
17
+ var sdk_object = {
18
+ "telemetry.sdk.name": rian_name,
19
+ "telemetry.sdk.version": rian_version
37
20
  };
38
- const create = (name, options) => {
39
- const spans = new Set();
40
- const sampler = options.sampler || defaultSampler;
41
- const sampler_callable = typeof sampler !== 'boolean';
42
- const span = (name, parent) => {
43
- const should_sample = sampler_callable
44
- ? sampler(name, parent, options.context)
45
- : sampler;
46
- const id = parent
47
- ? parent.child(should_sample)
48
- : tctx.make(should_sample);
49
- const span_obj = {
50
- id,
51
- parent,
52
- start: Date.now(),
53
- name,
54
- events: [],
55
- context: {},
56
- };
57
- if (should_sample)
58
- spans.add(span_obj);
59
- const $ = (cb) => measureFn($, cb);
60
- $.traceparent = id;
61
- $.fork = (name) => span(name, id);
62
- // @ts-expect-error TS7030 its always undefined ts :eye-roll:
63
- $.set_context = (ctx) => {
64
- if (typeof ctx === 'function')
65
- return void (span_obj.context = ctx(span_obj.context));
66
- Object.assign(span_obj.context, ctx);
67
- };
68
- $.add_event = (name, attributes) => {
69
- span_obj.events.push({
70
- name,
71
- timestamp: Date.now(),
72
- attributes: attributes || {},
73
- });
74
- };
75
- $.end = () => {
76
- if (span_obj.end == null)
77
- span_obj.end = Date.now();
78
- };
79
- 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: {}
80
35
  };
81
- const root = span(name, typeof options.traceparent === 'string'
82
- ? tctx.parse(options.traceparent)
83
- : undefined);
84
- const endRoot = root.end.bind(root);
85
- root.end = async () => {
86
- endRoot();
87
- if (PROMISES.has(root))
88
- await Promise.all(PROMISES.get(root));
89
- return options.exporter(spans, {
90
- ...(options.context || {}),
91
- ...sdk_object,
92
- });
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
- return root;
46
+ $.add_event = (name3, attributes) => {
47
+ span_obj.events.push({
48
+ name: name3,
49
+ timestamp: Date.now(),
50
+ attributes: attributes || {}
51
+ });
52
+ };
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
95
76
  };
96
-
97
- export { ADD_PROMISE, PROMISES, create };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rian",
3
- "version": "0.1.1",
3
+ "version": "0.2.1",
4
4
  "description": "Effective tracing for the edge and origins",
5
5
  "keywords": [
6
6
  "opentelemetry",
@@ -26,20 +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"
39
42
  },
40
43
  "./utils": {
41
- "import": "./utils.mjs",
42
- "require": "./utils.js"
44
+ "types": "./utils/index.d.ts",
45
+ "import": "./utils/index.mjs",
46
+ "require": "./utils/index.js"
43
47
  },
44
48
  "./package.json": "./package.json"
45
49
  },
@@ -49,10 +53,18 @@
49
53
  "files": [
50
54
  "*.mjs",
51
55
  "*.js",
52
- "*.d.ts"
56
+ "*.d.ts",
57
+ "exporter.*/*",
58
+ "utils/*"
53
59
  ],
60
+ "scripts": {
61
+ "build": "bundt"
62
+ },
54
63
  "dependencies": {
55
64
  "flattie": "^1.1.0",
56
65
  "tctx": "^0.0.10"
66
+ },
67
+ "devDependencies": {
68
+ "bundt": "2.0.0-next.5"
57
69
  }
58
70
  }
@@ -1,7 +1,9 @@
1
- import { Scope } from 'rian';
1
+ import type { Scope } from 'rian';
2
+
3
+ export type MeasureFn =
4
+ | ((...args: [...args: any[]]) => any)
5
+ | ((...args: [...args: any[], scope: Scope]) => any);
2
6
 
3
- declare type MeasureFn = ((...args: [...args: any[]]) => any) | ((...args: [...args: any[], scope: Scope]) => any);
4
- declare type RealMeasureFnParams<T extends unknown[]> = T extends [] ? [] : T extends [...rest: infer U, scope: Scope] ? U : T;
5
7
  /**
6
8
  * With a passed function — will start a span, and run the function, when the function finishes
7
9
  * the span finishes.
@@ -26,7 +28,13 @@ declare type RealMeasureFnParams<T extends unknown[]> = T extends [] ? [] : T ex
26
28
  * return value from get_data
27
29
  * ```
28
30
  */
29
- declare const measure: <Fn extends MeasureFn>(scope: Scope, name: string, fn: Fn, ...args: RealMeasureFnParams<Parameters<Fn>>) => ReturnType<Fn>;
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
+
30
38
  /**
31
39
  * Wraps any function with a measured scoped function. Useful for when defer function execution
32
40
  * till a later time.
@@ -41,6 +49,20 @@ declare const measure: <Fn extends MeasureFn>(scope: Scope, name: string, fn: Fn
41
49
  * wrapped();
42
50
  * ```
43
51
  */
44
- declare const wrap: <Fn extends MeasureFn>(scope: Scope, name: string, fn: Fn) => Fn;
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;
45
66
 
46
- export { MeasureFn, measure, wrap };
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 };
@@ -1,113 +0,0 @@
1
- 'use strict';
2
-
3
- const package_json = require('rian/package.json');
4
-
5
- const SpanStatusCode_UNSET = 0;
6
- const SpanStatusCode_ERROR = 2;
7
- const convert_value_to_anyvalue = (value) => {
8
- let type = typeof value, any_value = {};
9
- if (type === 'string')
10
- any_value.stringValue = value;
11
- else if (type === 'number')
12
- if (Number.isInteger(value))
13
- any_value.intValue = value;
14
- else
15
- any_value.doubleValue = value;
16
- else if (type === 'boolean')
17
- any_value.boolValue = value;
18
- else if (Array.isArray(value))
19
- any_value.arrayValue = {
20
- values: value.map((i) => convert_value_to_anyvalue(i)),
21
- };
22
- else if (value)
23
- any_value.kvlistValue = { values: convert_object_to_kv(value) };
24
- return any_value;
25
- };
26
- const convert_object_to_kv = (input) => {
27
- const value = [];
28
- for (let key of Object.keys(input)) {
29
- value.push({
30
- key,
31
- value: convert_value_to_anyvalue(input[key]),
32
- });
33
- }
34
- return value;
35
- };
36
- // https://github.com/open-telemetry/opentelemetry-proto/blob/b43e9b18b76abf3ee040164b55b9c355217151f3/opentelemetry/proto/trace/v1/trace.proto#L127-L155
37
- const map_kind = (kind) => {
38
- switch (kind) {
39
- default:
40
- case 'INTERNAL': {
41
- return 1;
42
- }
43
- case 'SERVER': {
44
- return 2;
45
- }
46
- case 'CLIENT': {
47
- return 3;
48
- }
49
- case 'PRODUCER': {
50
- return 4;
51
- }
52
- case 'CONSUMER': {
53
- return 5;
54
- }
55
- }
56
- };
57
- const exporter = (request) => (spans, context) => {
58
- const otel_spans = [];
59
- for (let span of spans) {
60
- const { kind, error, ...span_ctx } = span.context;
61
- let status;
62
- if (error) {
63
- status = {
64
- code: SpanStatusCode_ERROR,
65
- };
66
- if ('message' in error) {
67
- status.message = error.message;
68
- }
69
- }
70
- otel_spans.push({
71
- traceId: span.id.trace_id,
72
- spanId: span.id.parent_id,
73
- parentSpanId: span.parent?.parent_id,
74
- name: span.name,
75
- kind: map_kind(kind || 'INTERNAL'),
76
- startTimeUnixNano: span.start * 1000000,
77
- endTimeUnixNano: span.end ? span.end * 1000000 : undefined,
78
- droppedAttributesCount: 0,
79
- droppedEventsCount: 0,
80
- droppedLinksCount: 0,
81
- attributes: convert_object_to_kv(span_ctx),
82
- // @ts-expect-error TS2454
83
- status: status || { code: SpanStatusCode_UNSET },
84
- events: span.events.map((i) => ({
85
- name: i.name,
86
- attributes: convert_object_to_kv(i.attributes),
87
- droppedAttributesCount: 0,
88
- timeUnixNano: i.timestamp * 1000000,
89
- })),
90
- });
91
- }
92
- return request({
93
- resourceSpans: [
94
- {
95
- resource: {
96
- attributes: convert_object_to_kv(context),
97
- droppedAttributesCount: 0,
98
- },
99
- instrumentationLibrarySpans: [
100
- {
101
- instrumentationLibrary: {
102
- name: package_json.name,
103
- version: package_json.version,
104
- },
105
- spans: otel_spans,
106
- },
107
- ],
108
- },
109
- ],
110
- });
111
- };
112
-
113
- exports.exporter = exporter;