@contello/opentelemetry 1.0.2 → 1.0.3
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/dist/index.cjs +120 -118
- package/dist/index.d.cts +13 -10
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.ts +13 -10
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +103 -86
- package/dist/index.js.map +1 -0
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -1,129 +1,131 @@
|
|
|
1
|
-
"
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
//#region \0rolldown/runtime.js
|
|
2
3
|
var __create = Object.create;
|
|
3
4
|
var __defProp = Object.defineProperty;
|
|
4
5
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
6
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
7
|
var __getProtoOf = Object.getPrototypeOf;
|
|
7
8
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __export = (target, all) => {
|
|
9
|
-
for (var name in all)
|
|
10
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
-
};
|
|
12
9
|
var __copyProps = (to, from, except, desc) => {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
11
|
+
key = keys[i];
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
13
|
+
get: ((k) => from[k]).bind(null, key),
|
|
14
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
19
18
|
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
)
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
20
|
+
value: mod,
|
|
21
|
+
enumerable: true
|
|
22
|
+
}) : target, mod));
|
|
23
|
+
//#endregion
|
|
24
|
+
let node_diagnostics_channel = require("node:diagnostics_channel");
|
|
25
|
+
node_diagnostics_channel = __toESM(node_diagnostics_channel, 1);
|
|
26
|
+
let _opentelemetry_api = require("@opentelemetry/api");
|
|
27
|
+
let _opentelemetry_instrumentation = require("@opentelemetry/instrumentation");
|
|
28
|
+
//#region src/wrap.ts
|
|
29
|
+
/**
|
|
30
|
+
* Parses the operation name into metric attributes.
|
|
31
|
+
*
|
|
32
|
+
* All callers use the convention `category:operation`, e.g.:
|
|
33
|
+
* - `singleton:routes` -> { category: 'singleton', operation: 'routes' }
|
|
34
|
+
* - `collection:assets` -> { category: 'collection', operation: 'assets' }
|
|
35
|
+
* - `sdk:execute` -> { category: 'sdk', operation: 'execute' }
|
|
36
|
+
* - `store:init` -> { category: 'store', operation: 'init' }
|
|
37
|
+
*
|
|
38
|
+
* If the name doesn't contain `:`, the whole name is used as the operation
|
|
39
|
+
* with category 'unknown'.
|
|
40
|
+
*/
|
|
41
41
|
function parseMetricAttrs(name, extra) {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
42
|
+
const colonIndex = name.indexOf(":");
|
|
43
|
+
if (colonIndex === -1) return {
|
|
44
|
+
category: "unknown",
|
|
45
|
+
operation: name,
|
|
46
|
+
...extra
|
|
47
|
+
};
|
|
48
|
+
return {
|
|
49
|
+
category: name.slice(0, colonIndex),
|
|
50
|
+
operation: name.slice(colonIndex + 1),
|
|
51
|
+
...extra
|
|
52
|
+
};
|
|
51
53
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
54
|
+
const CHANNEL_PREFIXES = [
|
|
55
|
+
"@contello/store",
|
|
56
|
+
"@contello/client",
|
|
57
|
+
"@contello/astro"
|
|
58
|
+
];
|
|
59
|
+
var ContelloInstrumentation = class extends _opentelemetry_instrumentation.InstrumentationBase {
|
|
60
|
+
_subscriptions = [];
|
|
61
|
+
constructor(config = {}) {
|
|
62
|
+
super("@contello/opentelemetry", "1.0.3", config);
|
|
63
|
+
}
|
|
64
|
+
init() {
|
|
65
|
+
return [];
|
|
66
|
+
}
|
|
67
|
+
enable() {
|
|
68
|
+
this._subscriptions ??= [];
|
|
69
|
+
if (this._subscriptions.length > 0) return;
|
|
70
|
+
const tracer = this.tracer;
|
|
71
|
+
const meter = this.meter;
|
|
72
|
+
const durationHistogram = meter.createHistogram("contello.operation.duration", {
|
|
73
|
+
description: "Duration of contello operations",
|
|
74
|
+
unit: "ms"
|
|
75
|
+
});
|
|
76
|
+
const errorCounter = meter.createCounter("contello.operation.error", { description: "Number of failed contello operations" });
|
|
77
|
+
const userConfig = this.getConfig();
|
|
78
|
+
for (const prefix of CHANNEL_PREFIXES) {
|
|
79
|
+
const endChannel = node_diagnostics_channel.default.channel(`${prefix}:end`);
|
|
80
|
+
const errorChannel = node_diagnostics_channel.default.channel(`${prefix}:error`);
|
|
81
|
+
const onEnd = (message) => {
|
|
82
|
+
const msg = message;
|
|
83
|
+
const attrs = parseMetricAttrs(msg.name, { cached: msg.cached ?? false });
|
|
84
|
+
durationHistogram.record(msg.durationMs, attrs);
|
|
85
|
+
userConfig?.onComplete?.(msg.name, msg.durationMs);
|
|
86
|
+
const span = tracer.startSpan(msg.name);
|
|
87
|
+
span.setAttributes(attrs);
|
|
88
|
+
span.end();
|
|
89
|
+
};
|
|
90
|
+
const onError = (message) => {
|
|
91
|
+
const msg = message;
|
|
92
|
+
const attrs = parseMetricAttrs(msg.name, { cached: false });
|
|
93
|
+
durationHistogram.record(msg.durationMs, attrs);
|
|
94
|
+
errorCounter.add(1, attrs);
|
|
95
|
+
userConfig?.onError?.(msg.name, msg.error);
|
|
96
|
+
const span = tracer.startSpan(msg.name);
|
|
97
|
+
span.setAttributes(attrs);
|
|
98
|
+
span.recordException(msg.error);
|
|
99
|
+
span.end();
|
|
100
|
+
};
|
|
101
|
+
endChannel.subscribe(onEnd);
|
|
102
|
+
errorChannel.subscribe(onError);
|
|
103
|
+
this._subscriptions.push({
|
|
104
|
+
channel: endChannel,
|
|
105
|
+
handler: onEnd
|
|
106
|
+
}, {
|
|
107
|
+
channel: errorChannel,
|
|
108
|
+
handler: onError
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
const messageChannel = node_diagnostics_channel.default.channel("@contello/client:message");
|
|
112
|
+
const onMessage = (message) => {
|
|
113
|
+
const msg = message;
|
|
114
|
+
const output = {};
|
|
115
|
+
_opentelemetry_api.propagation.inject(_opentelemetry_api.context.active(), output);
|
|
116
|
+
if (output["traceparent"]) msg.message.traceparent = output["traceparent"];
|
|
117
|
+
if (output["tracestate"]) msg.message.tracestate = output["tracestate"];
|
|
118
|
+
};
|
|
119
|
+
messageChannel.subscribe(onMessage);
|
|
120
|
+
this._subscriptions.push({
|
|
121
|
+
channel: messageChannel,
|
|
122
|
+
handler: onMessage
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
disable() {
|
|
126
|
+
for (const { channel, handler } of this._subscriptions) channel.unsubscribe(handler);
|
|
127
|
+
this._subscriptions = [];
|
|
128
|
+
}
|
|
125
129
|
};
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
ContelloInstrumentation
|
|
129
|
-
});
|
|
130
|
+
//#endregion
|
|
131
|
+
exports.ContelloInstrumentation = ContelloInstrumentation;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,16 +1,19 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { InstrumentationBase, InstrumentationConfig } from "@opentelemetry/instrumentation";
|
|
2
2
|
|
|
3
|
+
//#region src/types.d.ts
|
|
3
4
|
type ContelloInstrumentationConfig = InstrumentationConfig & {
|
|
4
|
-
|
|
5
|
-
|
|
5
|
+
onComplete?: ((name: string, durationMs: number) => void) | undefined;
|
|
6
|
+
onError?: ((name: string, error: unknown) => void) | undefined;
|
|
6
7
|
};
|
|
7
|
-
|
|
8
|
+
//#endregion
|
|
9
|
+
//#region src/wrap.d.ts
|
|
8
10
|
declare class ContelloInstrumentation extends InstrumentationBase<ContelloInstrumentationConfig> {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
private _subscriptions;
|
|
12
|
+
constructor(config?: ContelloInstrumentationConfig);
|
|
13
|
+
init(): never[];
|
|
14
|
+
enable(): void;
|
|
15
|
+
disable(): void;
|
|
14
16
|
}
|
|
15
|
-
|
|
17
|
+
//#endregion
|
|
16
18
|
export { ContelloInstrumentation, type ContelloInstrumentationConfig };
|
|
19
|
+
//# sourceMappingURL=index.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.cts","names":[],"sources":["../src/types.ts","../src/wrap.ts"],"mappings":";;;KAEY,6BAAA,GAAgC,qBAAqB;EAC/D,UAAA,KAAe,IAAA,UAAc,UAAA;EAC7B,OAAA,KAAY,IAAA,UAAc,KAAA;AAAA;;;cCuCf,uBAAA,SAAgC,mBAAA,CAAoB,6BAAA;EAAA,QACvD,cAAA;cAEI,MAAA,GAAQ,6BAAA;EAIX,IAAA;EAIA,MAAA;EAkFA,OAAA;AAAA"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,16 +1,19 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { InstrumentationBase, InstrumentationConfig } from "@opentelemetry/instrumentation";
|
|
2
2
|
|
|
3
|
+
//#region src/types.d.ts
|
|
3
4
|
type ContelloInstrumentationConfig = InstrumentationConfig & {
|
|
4
|
-
|
|
5
|
-
|
|
5
|
+
onComplete?: ((name: string, durationMs: number) => void) | undefined;
|
|
6
|
+
onError?: ((name: string, error: unknown) => void) | undefined;
|
|
6
7
|
};
|
|
7
|
-
|
|
8
|
+
//#endregion
|
|
9
|
+
//#region src/wrap.d.ts
|
|
8
10
|
declare class ContelloInstrumentation extends InstrumentationBase<ContelloInstrumentationConfig> {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
private _subscriptions;
|
|
12
|
+
constructor(config?: ContelloInstrumentationConfig);
|
|
13
|
+
init(): never[];
|
|
14
|
+
enable(): void;
|
|
15
|
+
disable(): void;
|
|
14
16
|
}
|
|
15
|
-
|
|
17
|
+
//#endregion
|
|
16
18
|
export { ContelloInstrumentation, type ContelloInstrumentationConfig };
|
|
19
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/types.ts","../src/wrap.ts"],"mappings":";;;KAEY,6BAAA,GAAgC,qBAAqB;EAC/D,UAAA,KAAe,IAAA,UAAc,UAAA;EAC7B,OAAA,KAAY,IAAA,UAAc,KAAA;AAAA;;;cCuCf,uBAAA,SAAgC,mBAAA,CAAoB,6BAAA;EAAA,QACvD,cAAA;cAEI,MAAA,GAAQ,6BAAA;EAIX,IAAA;EAIA,MAAA;EAkFA,OAAA;AAAA"}
|
package/dist/index.js
CHANGED
|
@@ -1,92 +1,109 @@
|
|
|
1
|
-
|
|
2
|
-
import dc from "diagnostics_channel";
|
|
1
|
+
import dc from "node:diagnostics_channel";
|
|
3
2
|
import { context, propagation } from "@opentelemetry/api";
|
|
4
3
|
import { InstrumentationBase } from "@opentelemetry/instrumentation";
|
|
4
|
+
//#region src/wrap.ts
|
|
5
|
+
/**
|
|
6
|
+
* Parses the operation name into metric attributes.
|
|
7
|
+
*
|
|
8
|
+
* All callers use the convention `category:operation`, e.g.:
|
|
9
|
+
* - `singleton:routes` -> { category: 'singleton', operation: 'routes' }
|
|
10
|
+
* - `collection:assets` -> { category: 'collection', operation: 'assets' }
|
|
11
|
+
* - `sdk:execute` -> { category: 'sdk', operation: 'execute' }
|
|
12
|
+
* - `store:init` -> { category: 'store', operation: 'init' }
|
|
13
|
+
*
|
|
14
|
+
* If the name doesn't contain `:`, the whole name is used as the operation
|
|
15
|
+
* with category 'unknown'.
|
|
16
|
+
*/
|
|
5
17
|
function parseMetricAttrs(name, extra) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
18
|
+
const colonIndex = name.indexOf(":");
|
|
19
|
+
if (colonIndex === -1) return {
|
|
20
|
+
category: "unknown",
|
|
21
|
+
operation: name,
|
|
22
|
+
...extra
|
|
23
|
+
};
|
|
24
|
+
return {
|
|
25
|
+
category: name.slice(0, colonIndex),
|
|
26
|
+
operation: name.slice(colonIndex + 1),
|
|
27
|
+
...extra
|
|
28
|
+
};
|
|
15
29
|
}
|
|
16
|
-
|
|
30
|
+
const CHANNEL_PREFIXES = [
|
|
31
|
+
"@contello/store",
|
|
32
|
+
"@contello/client",
|
|
33
|
+
"@contello/astro"
|
|
34
|
+
];
|
|
17
35
|
var ContelloInstrumentation = class extends InstrumentationBase {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
this._subscriptions = [];
|
|
88
|
-
}
|
|
89
|
-
};
|
|
90
|
-
export {
|
|
91
|
-
ContelloInstrumentation
|
|
36
|
+
_subscriptions = [];
|
|
37
|
+
constructor(config = {}) {
|
|
38
|
+
super("@contello/opentelemetry", "1.0.3", config);
|
|
39
|
+
}
|
|
40
|
+
init() {
|
|
41
|
+
return [];
|
|
42
|
+
}
|
|
43
|
+
enable() {
|
|
44
|
+
this._subscriptions ??= [];
|
|
45
|
+
if (this._subscriptions.length > 0) return;
|
|
46
|
+
const tracer = this.tracer;
|
|
47
|
+
const meter = this.meter;
|
|
48
|
+
const durationHistogram = meter.createHistogram("contello.operation.duration", {
|
|
49
|
+
description: "Duration of contello operations",
|
|
50
|
+
unit: "ms"
|
|
51
|
+
});
|
|
52
|
+
const errorCounter = meter.createCounter("contello.operation.error", { description: "Number of failed contello operations" });
|
|
53
|
+
const userConfig = this.getConfig();
|
|
54
|
+
for (const prefix of CHANNEL_PREFIXES) {
|
|
55
|
+
const endChannel = dc.channel(`${prefix}:end`);
|
|
56
|
+
const errorChannel = dc.channel(`${prefix}:error`);
|
|
57
|
+
const onEnd = (message) => {
|
|
58
|
+
const msg = message;
|
|
59
|
+
const attrs = parseMetricAttrs(msg.name, { cached: msg.cached ?? false });
|
|
60
|
+
durationHistogram.record(msg.durationMs, attrs);
|
|
61
|
+
userConfig?.onComplete?.(msg.name, msg.durationMs);
|
|
62
|
+
const span = tracer.startSpan(msg.name);
|
|
63
|
+
span.setAttributes(attrs);
|
|
64
|
+
span.end();
|
|
65
|
+
};
|
|
66
|
+
const onError = (message) => {
|
|
67
|
+
const msg = message;
|
|
68
|
+
const attrs = parseMetricAttrs(msg.name, { cached: false });
|
|
69
|
+
durationHistogram.record(msg.durationMs, attrs);
|
|
70
|
+
errorCounter.add(1, attrs);
|
|
71
|
+
userConfig?.onError?.(msg.name, msg.error);
|
|
72
|
+
const span = tracer.startSpan(msg.name);
|
|
73
|
+
span.setAttributes(attrs);
|
|
74
|
+
span.recordException(msg.error);
|
|
75
|
+
span.end();
|
|
76
|
+
};
|
|
77
|
+
endChannel.subscribe(onEnd);
|
|
78
|
+
errorChannel.subscribe(onError);
|
|
79
|
+
this._subscriptions.push({
|
|
80
|
+
channel: endChannel,
|
|
81
|
+
handler: onEnd
|
|
82
|
+
}, {
|
|
83
|
+
channel: errorChannel,
|
|
84
|
+
handler: onError
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
const messageChannel = dc.channel("@contello/client:message");
|
|
88
|
+
const onMessage = (message) => {
|
|
89
|
+
const msg = message;
|
|
90
|
+
const output = {};
|
|
91
|
+
propagation.inject(context.active(), output);
|
|
92
|
+
if (output["traceparent"]) msg.message.traceparent = output["traceparent"];
|
|
93
|
+
if (output["tracestate"]) msg.message.tracestate = output["tracestate"];
|
|
94
|
+
};
|
|
95
|
+
messageChannel.subscribe(onMessage);
|
|
96
|
+
this._subscriptions.push({
|
|
97
|
+
channel: messageChannel,
|
|
98
|
+
handler: onMessage
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
disable() {
|
|
102
|
+
for (const { channel, handler } of this._subscriptions) channel.unsubscribe(handler);
|
|
103
|
+
this._subscriptions = [];
|
|
104
|
+
}
|
|
92
105
|
};
|
|
106
|
+
//#endregion
|
|
107
|
+
export { ContelloInstrumentation };
|
|
108
|
+
|
|
109
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/wrap.ts"],"sourcesContent":["import dc from 'node:diagnostics_channel';\nimport { type Attributes, context, propagation } from '@opentelemetry/api';\nimport { InstrumentationBase } from '@opentelemetry/instrumentation';\n\nimport type { ContelloInstrumentationConfig } from './types';\n\ndeclare const PACKAGE_VERSION: string;\n\n/**\n * Parses the operation name into metric attributes.\n *\n * All callers use the convention `category:operation`, e.g.:\n * - `singleton:routes` -> { category: 'singleton', operation: 'routes' }\n * - `collection:assets` -> { category: 'collection', operation: 'assets' }\n * - `sdk:execute` -> { category: 'sdk', operation: 'execute' }\n * - `store:init` -> { category: 'store', operation: 'init' }\n *\n * If the name doesn't contain `:`, the whole name is used as the operation\n * with category 'unknown'.\n */\nfunction parseMetricAttrs(name: string, extra?: Record<string, unknown>): Attributes {\n const colonIndex = name.indexOf(':');\n\n if (colonIndex === -1) {\n return { category: 'unknown', operation: name, ...extra };\n }\n\n return {\n category: name.slice(0, colonIndex),\n operation: name.slice(colonIndex + 1),\n ...extra,\n };\n}\n\nconst CHANNEL_PREFIXES = ['@contello/store', '@contello/client', '@contello/astro'] as const;\n\ntype ChannelHandler = (message: unknown) => void;\n\ntype Subscription = {\n channel: ReturnType<typeof dc.channel>;\n handler: ChannelHandler;\n};\n\nexport class ContelloInstrumentation extends InstrumentationBase<ContelloInstrumentationConfig> {\n private _subscriptions: Subscription[] = [];\n\n constructor(config: ContelloInstrumentationConfig = {}) {\n super('@contello/opentelemetry', PACKAGE_VERSION, config);\n }\n\n override init() {\n return [];\n }\n\n override enable() {\n this._subscriptions ??= [];\n\n if (this._subscriptions.length > 0) {\n return;\n }\n\n const tracer = this.tracer;\n const meter = this.meter;\n\n const durationHistogram = meter.createHistogram('contello.operation.duration', {\n description: 'Duration of contello operations',\n unit: 'ms',\n });\n\n const errorCounter = meter.createCounter('contello.operation.error', {\n description: 'Number of failed contello operations',\n });\n\n const userConfig = this.getConfig() as ContelloInstrumentationConfig;\n\n for (const prefix of CHANNEL_PREFIXES) {\n const endChannel = dc.channel(`${prefix}:end`);\n const errorChannel = dc.channel(`${prefix}:error`);\n\n const onEnd: ChannelHandler = (message) => {\n const msg = message as { name: string; durationMs: number; cached?: boolean };\n const attrs = parseMetricAttrs(msg.name, { cached: msg.cached ?? false });\n\n durationHistogram.record(msg.durationMs, attrs);\n userConfig?.onComplete?.(msg.name, msg.durationMs);\n\n const span = tracer.startSpan(msg.name);\n\n span.setAttributes(attrs);\n span.end();\n };\n\n const onError: ChannelHandler = (message) => {\n const msg = message as { name: string; error: unknown; durationMs: number };\n const attrs = parseMetricAttrs(msg.name, { cached: false });\n\n durationHistogram.record(msg.durationMs, attrs);\n errorCounter.add(1, attrs);\n userConfig?.onError?.(msg.name, msg.error);\n\n const span = tracer.startSpan(msg.name);\n\n span.setAttributes(attrs);\n span.recordException(msg.error as Error);\n span.end();\n };\n\n endChannel.subscribe(onEnd);\n errorChannel.subscribe(onError);\n\n this._subscriptions.push({ channel: endChannel, handler: onEnd }, { channel: errorChannel, handler: onError });\n }\n\n // message decorator for trace context propagation on WebSocket messages\n const messageChannel = dc.channel('@contello/client:message');\n\n const onMessage: ChannelHandler = (message) => {\n const msg = message as { message: any };\n const output = {} as Record<string, string>;\n\n propagation.inject(context.active(), output);\n\n if (output['traceparent']) {\n msg.message.traceparent = output['traceparent'];\n }\n\n if (output['tracestate']) {\n msg.message.tracestate = output['tracestate'];\n }\n };\n\n messageChannel.subscribe(onMessage);\n\n this._subscriptions.push({ channel: messageChannel, handler: onMessage });\n }\n\n override disable() {\n for (const { channel, handler } of this._subscriptions) {\n channel.unsubscribe(handler);\n }\n\n this._subscriptions = [];\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAoBA,SAAS,iBAAiB,MAAc,OAA6C;CACnF,MAAM,aAAa,KAAK,QAAQ,GAAG;CAEnC,IAAI,eAAe,IACjB,OAAO;EAAE,UAAU;EAAW,WAAW;EAAM,GAAG;CAAM;CAG1D,OAAO;EACL,UAAU,KAAK,MAAM,GAAG,UAAU;EAClC,WAAW,KAAK,MAAM,aAAa,CAAC;EACpC,GAAG;CACL;AACF;AAEA,MAAM,mBAAmB;CAAC;CAAmB;CAAoB;AAAiB;AASlF,IAAa,0BAAb,cAA6C,oBAAmD;CAC9F,iBAAyC,CAAC;CAE1C,YAAY,SAAwC,CAAC,GAAG;EACtD,MAAM,2BAAA,SAA4C,MAAM;CAC1D;CAEA,OAAgB;EACd,OAAO,CAAC;CACV;CAEA,SAAkB;EAChB,KAAK,mBAAmB,CAAC;EAEzB,IAAI,KAAK,eAAe,SAAS,GAC/B;EAGF,MAAM,SAAS,KAAK;EACpB,MAAM,QAAQ,KAAK;EAEnB,MAAM,oBAAoB,MAAM,gBAAgB,+BAA+B;GAC7E,aAAa;GACb,MAAM;EACR,CAAC;EAED,MAAM,eAAe,MAAM,cAAc,4BAA4B,EACnE,aAAa,uCACf,CAAC;EAED,MAAM,aAAa,KAAK,UAAU;EAElC,KAAK,MAAM,UAAU,kBAAkB;GACrC,MAAM,aAAa,GAAG,QAAQ,GAAG,OAAO,KAAK;GAC7C,MAAM,eAAe,GAAG,QAAQ,GAAG,OAAO,OAAO;GAEjD,MAAM,SAAyB,YAAY;IACzC,MAAM,MAAM;IACZ,MAAM,QAAQ,iBAAiB,IAAI,MAAM,EAAE,QAAQ,IAAI,UAAU,MAAM,CAAC;IAExE,kBAAkB,OAAO,IAAI,YAAY,KAAK;IAC9C,YAAY,aAAa,IAAI,MAAM,IAAI,UAAU;IAEjD,MAAM,OAAO,OAAO,UAAU,IAAI,IAAI;IAEtC,KAAK,cAAc,KAAK;IACxB,KAAK,IAAI;GACX;GAEA,MAAM,WAA2B,YAAY;IAC3C,MAAM,MAAM;IACZ,MAAM,QAAQ,iBAAiB,IAAI,MAAM,EAAE,QAAQ,MAAM,CAAC;IAE1D,kBAAkB,OAAO,IAAI,YAAY,KAAK;IAC9C,aAAa,IAAI,GAAG,KAAK;IACzB,YAAY,UAAU,IAAI,MAAM,IAAI,KAAK;IAEzC,MAAM,OAAO,OAAO,UAAU,IAAI,IAAI;IAEtC,KAAK,cAAc,KAAK;IACxB,KAAK,gBAAgB,IAAI,KAAc;IACvC,KAAK,IAAI;GACX;GAEA,WAAW,UAAU,KAAK;GAC1B,aAAa,UAAU,OAAO;GAE9B,KAAK,eAAe,KAAK;IAAE,SAAS;IAAY,SAAS;GAAM,GAAG;IAAE,SAAS;IAAc,SAAS;GAAQ,CAAC;EAC/G;EAGA,MAAM,iBAAiB,GAAG,QAAQ,0BAA0B;EAE5D,MAAM,aAA6B,YAAY;GAC7C,MAAM,MAAM;GACZ,MAAM,SAAS,CAAC;GAEhB,YAAY,OAAO,QAAQ,OAAO,GAAG,MAAM;GAE3C,IAAI,OAAO,gBACT,IAAI,QAAQ,cAAc,OAAO;GAGnC,IAAI,OAAO,eACT,IAAI,QAAQ,aAAa,OAAO;EAEpC;EAEA,eAAe,UAAU,SAAS;EAElC,KAAK,eAAe,KAAK;GAAE,SAAS;GAAgB,SAAS;EAAU,CAAC;CAC1E;CAEA,UAAmB;EACjB,KAAK,MAAM,EAAE,SAAS,aAAa,KAAK,gBACtC,QAAQ,YAAY,OAAO;EAG7B,KAAK,iBAAiB,CAAC;CACzB;AACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contello/opentelemetry",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "OpenTelemetry instrumentation for Contello packages",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -33,8 +33,8 @@
|
|
|
33
33
|
"registry": "https://registry.npmjs.org"
|
|
34
34
|
},
|
|
35
35
|
"scripts": {
|
|
36
|
-
"build": "
|
|
37
|
-
"lint": "eslint .",
|
|
36
|
+
"build": "tsdown",
|
|
37
|
+
"lint": "eslint . --cache",
|
|
38
38
|
"lint:fix": "eslint . --fix",
|
|
39
39
|
"typecheck": "tsc --noEmit"
|
|
40
40
|
}
|