1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.SetInstrumentorLogger = void 0;
|
4 | let getTime = () => -1;
|
5 | function load(moduleName) {
|
6 |
|
7 | return eval('require')(moduleName);
|
8 | }
|
9 | let logger = () => { };
|
10 | const Instrumentor = function Instrumentor() {
|
11 | getTime = typeof window === 'undefined' ? load('perf_hooks').performance.now : performance.now;
|
12 | this.metricScopes = new Map([['default', 0]]);
|
13 | this.metricLabels = new Map([['default', 0]]);
|
14 | this.sessionName = '';
|
15 | this.sessionStart = Date.now();
|
16 | this.sessionStart = -1;
|
17 | return this;
|
18 | };
|
19 | exports.default = Instrumentor;
|
20 | Instrumentor.prototype.beginSession = function beginSession(name, options = {}) {
|
21 | logger('debug', `starting a new instrumentor session named: ${name}`);
|
22 | this.sessionName = name;
|
23 | this.sessionStart = getTime();
|
24 | if (!options.keepLabels) {
|
25 | this.metricLabels = new Map([['default', 0]]);
|
26 | }
|
27 | if (!options.keepScopes) {
|
28 | this.metricScopes = new Map([['default', 0]]);
|
29 | }
|
30 | if (!options.keepResults) {
|
31 | this.results = [];
|
32 | }
|
33 | };
|
34 | Instrumentor.prototype.endSession = function endSession() {
|
35 | logger('debug', `ending an active instrumentor session named: ${this.sessionName}`);
|
36 | const result = {
|
37 | metricLabels: strMapToStrArr(this.metricLabels),
|
38 | metricScopes: strMapToStrArr(this.metricScopes),
|
39 | sessionName: this.sessionName,
|
40 | sessionStart: this.sessionStart,
|
41 | sessionEnd: getTime(),
|
42 | results: Array.from(this.results),
|
43 | };
|
44 | return result;
|
45 | };
|
46 | Instrumentor.prototype.newLabel = function newLabel(name) {
|
47 | logger('debug', `added a new instrumentor label named: ${name}`);
|
48 | let value = this.metricLabels.get(name);
|
49 | if (value) {
|
50 | logger('warn', `tried to add a duplicate instrumentor label named: ${name}`);
|
51 | return value;
|
52 | }
|
53 | value = this.metricLabels.size;
|
54 | this.metricLabels.set(name, value);
|
55 | return value;
|
56 | };
|
57 | Instrumentor.prototype.newScope = function newScope(name) {
|
58 | logger('debug', `added a new instrumentor scope named: ${name}`);
|
59 | let value = this.metricScopes.get(name);
|
60 | if (value) {
|
61 | logger('warn', `tried to add a duplicate instrumentor scope named: ${name}`);
|
62 | return value;
|
63 | }
|
64 | value = this.metricScopes.size;
|
65 | this.metricScopes.set(name, value);
|
66 | return value;
|
67 | };
|
68 | Instrumentor.prototype.metric = function metric(options = {}) {
|
69 | const scopeName = options.scope || 'default';
|
70 | let scopeValue = this.metricScopes.get(scopeName);
|
71 | if (!scopeValue) {
|
72 | scopeValue = this.newScope(scopeName);
|
73 | }
|
74 | const labelName = options.label || 'default';
|
75 | let labelValue = this.metricLabels.get(labelName);
|
76 | if (!labelValue) {
|
77 | labelValue = this.newLabel(labelName);
|
78 | }
|
79 | const collector = new MetricCollector(scopeValue, labelValue, (result) => this.results.push(result));
|
80 | return collector;
|
81 | };
|
82 |
|
83 | const MetricCollector = function MetricCollector(scope, label, onSubmit) {
|
84 | this.labelValue = label;
|
85 | this.scopeValue = scope;
|
86 | this.onSubmit = onSubmit;
|
87 | return this;
|
88 | };
|
89 | MetricCollector.prototype.start = function start() {
|
90 | const start = getTime();
|
91 | return () => this.onSubmit({
|
92 | label_id: this.labelValue,
|
93 | scope_id: this.scopeValue,
|
94 | start,
|
95 | end: getTime(),
|
96 | });
|
97 | };
|
98 |
|
99 | function strMapToStrArr(map) {
|
100 | const strs = [];
|
101 | for (const [key, index] of map.entries()) {
|
102 | strs[index] = key;
|
103 | }
|
104 | return strs;
|
105 | }
|
106 |
|
107 | function SetInstrumentorLogger(newLogger) {
|
108 | logger('debug', 'replacing the instrumentor logger with a new one');
|
109 | if (typeof newLogger === 'function') {
|
110 | logger = newLogger;
|
111 | }
|
112 | }
|
113 | exports.SetInstrumentorLogger = SetInstrumentorLogger;
|