@contello/opentelemetry 1.0.3 → 2.1.0
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 +122 -113
- package/dist/index.d.cts +31 -16
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.ts +31 -16
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +122 -93
- package/dist/index.js.map +1 -1
- package/package.json +9 -6
package/dist/index.cjs
CHANGED
|
@@ -1,131 +1,140 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
var __copyProps = (to, from, except, desc) => {
|
|
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
|
-
});
|
|
2
|
+
let node_module = require("node:module");
|
|
3
|
+
//#region src/api.ts
|
|
4
|
+
function loadApi() {
|
|
5
|
+
try {
|
|
6
|
+
return (0, node_module.createRequire)(require("url").pathToFileURL(__filename).href)("@opentelemetry/api");
|
|
7
|
+
} catch {
|
|
8
|
+
return;
|
|
16
9
|
}
|
|
17
|
-
|
|
10
|
+
}
|
|
11
|
+
const api = loadApi();
|
|
12
|
+
//#endregion
|
|
13
|
+
//#region src/env.ts
|
|
14
|
+
function envFlag(name, defaultValue) {
|
|
15
|
+
const raw = process.env[name];
|
|
16
|
+
if (raw === void 0) return defaultValue;
|
|
17
|
+
const value = raw.trim().toLowerCase();
|
|
18
|
+
if (value === "") return defaultValue;
|
|
19
|
+
return ![
|
|
20
|
+
"false",
|
|
21
|
+
"0",
|
|
22
|
+
"no",
|
|
23
|
+
"off"
|
|
24
|
+
].includes(value);
|
|
25
|
+
}
|
|
26
|
+
const otelEnv = {
|
|
27
|
+
enabled: envFlag("OTEL_CONTELLO_ENABLED", true),
|
|
28
|
+
captureQuery: envFlag("OTEL_CONTELLO_CAPTURE_QUERY", false),
|
|
29
|
+
captureVariables: envFlag("OTEL_CONTELLO_CAPTURE_VARIABLES", false)
|
|
18
30
|
};
|
|
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
31
|
//#endregion
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
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
|
-
function parseMetricAttrs(name, extra) {
|
|
32
|
+
//#region src/index.ts
|
|
33
|
+
const otel = otelEnv.enabled ? api : void 0;
|
|
34
|
+
function parseOperationAttributes(name) {
|
|
42
35
|
const colonIndex = name.indexOf(":");
|
|
43
36
|
if (colonIndex === -1) return {
|
|
44
37
|
category: "unknown",
|
|
45
|
-
operation: name
|
|
46
|
-
...extra
|
|
38
|
+
operation: name
|
|
47
39
|
};
|
|
48
40
|
return {
|
|
49
41
|
category: name.slice(0, colonIndex),
|
|
50
|
-
operation: name.slice(colonIndex + 1)
|
|
51
|
-
...extra
|
|
42
|
+
operation: name.slice(colonIndex + 1)
|
|
52
43
|
};
|
|
53
44
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
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);
|
|
45
|
+
function createOperationTelemetry(scope) {
|
|
46
|
+
if (!otel) return { wrap: (_name, fn) => fn() };
|
|
47
|
+
const { SpanStatusCode } = otel;
|
|
48
|
+
const tracer = otel.trace.getTracer(scope);
|
|
49
|
+
const meter = otel.metrics.getMeter(scope);
|
|
50
|
+
const duration = meter.createHistogram("contello.operation.duration", {
|
|
51
|
+
description: "duration of contello operations",
|
|
52
|
+
unit: "ms"
|
|
53
|
+
});
|
|
54
|
+
const errors = meter.createCounter("contello.operation.error", { description: "number of failed contello operations" });
|
|
55
|
+
const wrap = (name, fn, attributes) => {
|
|
56
|
+
const attrs = {
|
|
57
|
+
...parseOperationAttributes(name),
|
|
58
|
+
...attributes
|
|
59
|
+
};
|
|
60
|
+
return tracer.startActiveSpan(name, { attributes: attrs }, (span) => {
|
|
61
|
+
const start = performance.now();
|
|
62
|
+
const finish = (cached) => {
|
|
63
|
+
span.setAttribute("cached", cached);
|
|
64
|
+
duration.record(performance.now() - start, {
|
|
65
|
+
...attrs,
|
|
66
|
+
cached
|
|
67
|
+
});
|
|
88
68
|
span.end();
|
|
89
69
|
};
|
|
90
|
-
const
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
span.
|
|
98
|
-
|
|
70
|
+
const fail = (error) => {
|
|
71
|
+
duration.record(performance.now() - start, {
|
|
72
|
+
...attrs,
|
|
73
|
+
cached: false
|
|
74
|
+
});
|
|
75
|
+
errors.add(1, attrs);
|
|
76
|
+
span.recordException(error);
|
|
77
|
+
span.setStatus({
|
|
78
|
+
code: SpanStatusCode.ERROR,
|
|
79
|
+
message: error instanceof Error ? error.message : String(error)
|
|
80
|
+
});
|
|
99
81
|
span.end();
|
|
100
82
|
};
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
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
|
|
83
|
+
let result;
|
|
84
|
+
try {
|
|
85
|
+
result = fn();
|
|
86
|
+
} catch (error) {
|
|
87
|
+
fail(error);
|
|
88
|
+
throw error;
|
|
89
|
+
}
|
|
90
|
+
if (result instanceof Promise) {
|
|
91
|
+
result.then(() => finish(false), (error) => fail(error));
|
|
92
|
+
return result;
|
|
93
|
+
}
|
|
94
|
+
finish(true);
|
|
95
|
+
return result;
|
|
123
96
|
});
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
97
|
+
};
|
|
98
|
+
return { wrap };
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Renames the active span and/or merges attributes into it. No-op when telemetry is
|
|
102
|
+
* disabled, `@opentelemetry/api` is absent, or no recording span is active. Used to
|
|
103
|
+
* enrich an outer span (e.g. the platform HTTP server span) with context only the
|
|
104
|
+
* contello layer knows.
|
|
105
|
+
*/
|
|
106
|
+
function updateActiveSpan(update) {
|
|
107
|
+
if (!otel) return;
|
|
108
|
+
const span = otel.trace.getActiveSpan();
|
|
109
|
+
if (!span?.isRecording()) return;
|
|
110
|
+
if (update.attributes) span.setAttributes(update.attributes);
|
|
111
|
+
if (update.name) span.updateName(update.name);
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Attributes for GraphQL operation spans, gated by `OTEL_CONTELLO_CAPTURE_QUERY` and
|
|
115
|
+
* `OTEL_CONTELLO_CAPTURE_VARIABLES` (both off by default).
|
|
116
|
+
*/
|
|
117
|
+
function graphqlOperationAttributes(document, variables) {
|
|
118
|
+
if (!otel || !otelEnv.captureQuery && !otelEnv.captureVariables) return;
|
|
119
|
+
const attrs = {};
|
|
120
|
+
if (otelEnv.captureQuery) attrs["graphql.document"] = document;
|
|
121
|
+
if (otelEnv.captureVariables && variables !== void 0) attrs["graphql.variables"] = JSON.stringify(variables);
|
|
122
|
+
return attrs;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Injects W3C trace context (`traceparent` / `tracestate`) into an outgoing GraphQL
|
|
126
|
+
* WebSocket message. Returns the message unchanged when telemetry is disabled.
|
|
127
|
+
*/
|
|
128
|
+
function injectTraceContext(message) {
|
|
129
|
+
if (!otel) return message;
|
|
130
|
+
const carrier = {};
|
|
131
|
+
otel.propagation.inject(otel.context.active(), carrier);
|
|
132
|
+
if (carrier["traceparent"]) message.traceparent = carrier["traceparent"];
|
|
133
|
+
if (carrier["tracestate"]) message.tracestate = carrier["tracestate"];
|
|
134
|
+
return message;
|
|
135
|
+
}
|
|
130
136
|
//#endregion
|
|
131
|
-
exports.
|
|
137
|
+
exports.createOperationTelemetry = createOperationTelemetry;
|
|
138
|
+
exports.graphqlOperationAttributes = graphqlOperationAttributes;
|
|
139
|
+
exports.injectTraceContext = injectTraceContext;
|
|
140
|
+
exports.updateActiveSpan = updateActiveSpan;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,19 +1,34 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
//#region src/index.d.ts
|
|
2
|
+
type OperationAttributes = Record<string, string | number | boolean>;
|
|
3
|
+
type OperationTelemetry = {
|
|
4
|
+
/**
|
|
5
|
+
* Runs `fn` inside an active span named `name` (convention: `category:operation`) and records
|
|
6
|
+
* duration/error metrics. Sync results are attributed `cached: true`, promises `cached: false`.
|
|
7
|
+
* Zero-overhead pass-through when `@opentelemetry/api` is absent or `OTEL_CONTELLO_ENABLED=false`.
|
|
8
|
+
*/
|
|
9
|
+
wrap: <T>(name: string, fn: () => T, attributes?: OperationAttributes | undefined) => T;
|
|
7
10
|
};
|
|
11
|
+
declare function createOperationTelemetry(scope: string): OperationTelemetry;
|
|
12
|
+
/**
|
|
13
|
+
* Renames the active span and/or merges attributes into it. No-op when telemetry is
|
|
14
|
+
* disabled, `@opentelemetry/api` is absent, or no recording span is active. Used to
|
|
15
|
+
* enrich an outer span (e.g. the platform HTTP server span) with context only the
|
|
16
|
+
* contello layer knows.
|
|
17
|
+
*/
|
|
18
|
+
declare function updateActiveSpan(update: {
|
|
19
|
+
name?: string | undefined;
|
|
20
|
+
attributes?: OperationAttributes | undefined;
|
|
21
|
+
}): void;
|
|
22
|
+
/**
|
|
23
|
+
* Attributes for GraphQL operation spans, gated by `OTEL_CONTELLO_CAPTURE_QUERY` and
|
|
24
|
+
* `OTEL_CONTELLO_CAPTURE_VARIABLES` (both off by default).
|
|
25
|
+
*/
|
|
26
|
+
declare function graphqlOperationAttributes(document: string, variables?: Record<string, unknown> | undefined): OperationAttributes | undefined;
|
|
27
|
+
/**
|
|
28
|
+
* Injects W3C trace context (`traceparent` / `tracestate`) into an outgoing GraphQL
|
|
29
|
+
* WebSocket message. Returns the message unchanged when telemetry is disabled.
|
|
30
|
+
*/
|
|
31
|
+
declare function injectTraceContext(message: any): any;
|
|
8
32
|
//#endregion
|
|
9
|
-
|
|
10
|
-
declare class ContelloInstrumentation extends InstrumentationBase<ContelloInstrumentationConfig> {
|
|
11
|
-
private _subscriptions;
|
|
12
|
-
constructor(config?: ContelloInstrumentationConfig);
|
|
13
|
-
init(): never[];
|
|
14
|
-
enable(): void;
|
|
15
|
-
disable(): void;
|
|
16
|
-
}
|
|
17
|
-
//#endregion
|
|
18
|
-
export { ContelloInstrumentation, type ContelloInstrumentationConfig };
|
|
33
|
+
export { OperationAttributes, OperationTelemetry, createOperationTelemetry, graphqlOperationAttributes, injectTraceContext, updateActiveSpan };
|
|
19
34
|
//# sourceMappingURL=index.d.cts.map
|
package/dist/index.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.cts","names":[],"sources":["../src/
|
|
1
|
+
{"version":3,"file":"index.d.cts","names":[],"sources":["../src/index.ts"],"mappings":";KAKY,mBAAA,GAAsB,MAAM;AAAA,KAE5B,kBAAA;EAFmB;;;AAAS;AAExC;EAME,IAAA,MAAU,IAAA,UAAc,EAAA,QAAU,CAAA,EAAG,UAAA,GAAa,mBAAA,iBAAoC,CAAA;AAAA;AAAA,iBAaxE,wBAAA,CAAyB,KAAA,WAAgB,kBAAkB;;;;;;;iBA8E3D,gBAAA,CAAiB,MAAA;EAC/B,IAAA;EACA,UAAA,GAAa,mBAAmB;AAAA;;;;AA7FuD;iBAsHzE,0BAAA,CACd,QAAA,UACA,SAAA,GAAY,MAAA,gCACX,mBAAmB;;;;AA5GqD;iBAkI3D,kBAAA,CAAmB,OAAY"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,19 +1,34 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
//#region src/index.d.ts
|
|
2
|
+
type OperationAttributes = Record<string, string | number | boolean>;
|
|
3
|
+
type OperationTelemetry = {
|
|
4
|
+
/**
|
|
5
|
+
* Runs `fn` inside an active span named `name` (convention: `category:operation`) and records
|
|
6
|
+
* duration/error metrics. Sync results are attributed `cached: true`, promises `cached: false`.
|
|
7
|
+
* Zero-overhead pass-through when `@opentelemetry/api` is absent or `OTEL_CONTELLO_ENABLED=false`.
|
|
8
|
+
*/
|
|
9
|
+
wrap: <T>(name: string, fn: () => T, attributes?: OperationAttributes | undefined) => T;
|
|
7
10
|
};
|
|
11
|
+
declare function createOperationTelemetry(scope: string): OperationTelemetry;
|
|
12
|
+
/**
|
|
13
|
+
* Renames the active span and/or merges attributes into it. No-op when telemetry is
|
|
14
|
+
* disabled, `@opentelemetry/api` is absent, or no recording span is active. Used to
|
|
15
|
+
* enrich an outer span (e.g. the platform HTTP server span) with context only the
|
|
16
|
+
* contello layer knows.
|
|
17
|
+
*/
|
|
18
|
+
declare function updateActiveSpan(update: {
|
|
19
|
+
name?: string | undefined;
|
|
20
|
+
attributes?: OperationAttributes | undefined;
|
|
21
|
+
}): void;
|
|
22
|
+
/**
|
|
23
|
+
* Attributes for GraphQL operation spans, gated by `OTEL_CONTELLO_CAPTURE_QUERY` and
|
|
24
|
+
* `OTEL_CONTELLO_CAPTURE_VARIABLES` (both off by default).
|
|
25
|
+
*/
|
|
26
|
+
declare function graphqlOperationAttributes(document: string, variables?: Record<string, unknown> | undefined): OperationAttributes | undefined;
|
|
27
|
+
/**
|
|
28
|
+
* Injects W3C trace context (`traceparent` / `tracestate`) into an outgoing GraphQL
|
|
29
|
+
* WebSocket message. Returns the message unchanged when telemetry is disabled.
|
|
30
|
+
*/
|
|
31
|
+
declare function injectTraceContext(message: any): any;
|
|
8
32
|
//#endregion
|
|
9
|
-
|
|
10
|
-
declare class ContelloInstrumentation extends InstrumentationBase<ContelloInstrumentationConfig> {
|
|
11
|
-
private _subscriptions;
|
|
12
|
-
constructor(config?: ContelloInstrumentationConfig);
|
|
13
|
-
init(): never[];
|
|
14
|
-
enable(): void;
|
|
15
|
-
disable(): void;
|
|
16
|
-
}
|
|
17
|
-
//#endregion
|
|
18
|
-
export { ContelloInstrumentation, type ContelloInstrumentationConfig };
|
|
33
|
+
export { OperationAttributes, OperationTelemetry, createOperationTelemetry, graphqlOperationAttributes, injectTraceContext, updateActiveSpan };
|
|
19
34
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/index.ts"],"mappings":";KAKY,mBAAA,GAAsB,MAAM;AAAA,KAE5B,kBAAA;EAFmB;;;AAAS;AAExC;EAME,IAAA,MAAU,IAAA,UAAc,EAAA,QAAU,CAAA,EAAG,UAAA,GAAa,mBAAA,iBAAoC,CAAA;AAAA;AAAA,iBAaxE,wBAAA,CAAyB,KAAA,WAAgB,kBAAkB;;;;;;;iBA8E3D,gBAAA,CAAiB,MAAA;EAC/B,IAAA;EACA,UAAA,GAAa,mBAAmB;AAAA;;;;AA7FuD;iBAsHzE,0BAAA,CACd,QAAA,UACA,SAAA,GAAY,MAAA,gCACX,mBAAmB;;;;AA5GqD;iBAkI3D,kBAAA,CAAmB,OAAY"}
|
package/dist/index.js
CHANGED
|
@@ -1,109 +1,138 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
//#region src/api.ts
|
|
3
|
+
function loadApi() {
|
|
4
|
+
try {
|
|
5
|
+
return createRequire(import.meta.url)("@opentelemetry/api");
|
|
6
|
+
} catch {
|
|
7
|
+
return;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
const api = loadApi();
|
|
11
|
+
//#endregion
|
|
12
|
+
//#region src/env.ts
|
|
13
|
+
function envFlag(name, defaultValue) {
|
|
14
|
+
const raw = process.env[name];
|
|
15
|
+
if (raw === void 0) return defaultValue;
|
|
16
|
+
const value = raw.trim().toLowerCase();
|
|
17
|
+
if (value === "") return defaultValue;
|
|
18
|
+
return ![
|
|
19
|
+
"false",
|
|
20
|
+
"0",
|
|
21
|
+
"no",
|
|
22
|
+
"off"
|
|
23
|
+
].includes(value);
|
|
24
|
+
}
|
|
25
|
+
const otelEnv = {
|
|
26
|
+
enabled: envFlag("OTEL_CONTELLO_ENABLED", true),
|
|
27
|
+
captureQuery: envFlag("OTEL_CONTELLO_CAPTURE_QUERY", false),
|
|
28
|
+
captureVariables: envFlag("OTEL_CONTELLO_CAPTURE_VARIABLES", false)
|
|
29
|
+
};
|
|
30
|
+
//#endregion
|
|
31
|
+
//#region src/index.ts
|
|
32
|
+
const otel = otelEnv.enabled ? api : void 0;
|
|
33
|
+
function parseOperationAttributes(name) {
|
|
18
34
|
const colonIndex = name.indexOf(":");
|
|
19
35
|
if (colonIndex === -1) return {
|
|
20
36
|
category: "unknown",
|
|
21
|
-
operation: name
|
|
22
|
-
...extra
|
|
37
|
+
operation: name
|
|
23
38
|
};
|
|
24
39
|
return {
|
|
25
40
|
category: name.slice(0, colonIndex),
|
|
26
|
-
operation: name.slice(colonIndex + 1)
|
|
27
|
-
...extra
|
|
41
|
+
operation: name.slice(colonIndex + 1)
|
|
28
42
|
};
|
|
29
43
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
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);
|
|
44
|
+
function createOperationTelemetry(scope) {
|
|
45
|
+
if (!otel) return { wrap: (_name, fn) => fn() };
|
|
46
|
+
const { SpanStatusCode } = otel;
|
|
47
|
+
const tracer = otel.trace.getTracer(scope);
|
|
48
|
+
const meter = otel.metrics.getMeter(scope);
|
|
49
|
+
const duration = meter.createHistogram("contello.operation.duration", {
|
|
50
|
+
description: "duration of contello operations",
|
|
51
|
+
unit: "ms"
|
|
52
|
+
});
|
|
53
|
+
const errors = meter.createCounter("contello.operation.error", { description: "number of failed contello operations" });
|
|
54
|
+
const wrap = (name, fn, attributes) => {
|
|
55
|
+
const attrs = {
|
|
56
|
+
...parseOperationAttributes(name),
|
|
57
|
+
...attributes
|
|
58
|
+
};
|
|
59
|
+
return tracer.startActiveSpan(name, { attributes: attrs }, (span) => {
|
|
60
|
+
const start = performance.now();
|
|
61
|
+
const finish = (cached) => {
|
|
62
|
+
span.setAttribute("cached", cached);
|
|
63
|
+
duration.record(performance.now() - start, {
|
|
64
|
+
...attrs,
|
|
65
|
+
cached
|
|
66
|
+
});
|
|
64
67
|
span.end();
|
|
65
68
|
};
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
span.
|
|
74
|
-
|
|
69
|
+
const fail = (error) => {
|
|
70
|
+
duration.record(performance.now() - start, {
|
|
71
|
+
...attrs,
|
|
72
|
+
cached: false
|
|
73
|
+
});
|
|
74
|
+
errors.add(1, attrs);
|
|
75
|
+
span.recordException(error);
|
|
76
|
+
span.setStatus({
|
|
77
|
+
code: SpanStatusCode.ERROR,
|
|
78
|
+
message: error instanceof Error ? error.message : String(error)
|
|
79
|
+
});
|
|
75
80
|
span.end();
|
|
76
81
|
};
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
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
|
|
82
|
+
let result;
|
|
83
|
+
try {
|
|
84
|
+
result = fn();
|
|
85
|
+
} catch (error) {
|
|
86
|
+
fail(error);
|
|
87
|
+
throw error;
|
|
88
|
+
}
|
|
89
|
+
if (result instanceof Promise) {
|
|
90
|
+
result.then(() => finish(false), (error) => fail(error));
|
|
91
|
+
return result;
|
|
92
|
+
}
|
|
93
|
+
finish(true);
|
|
94
|
+
return result;
|
|
99
95
|
});
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
96
|
+
};
|
|
97
|
+
return { wrap };
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Renames the active span and/or merges attributes into it. No-op when telemetry is
|
|
101
|
+
* disabled, `@opentelemetry/api` is absent, or no recording span is active. Used to
|
|
102
|
+
* enrich an outer span (e.g. the platform HTTP server span) with context only the
|
|
103
|
+
* contello layer knows.
|
|
104
|
+
*/
|
|
105
|
+
function updateActiveSpan(update) {
|
|
106
|
+
if (!otel) return;
|
|
107
|
+
const span = otel.trace.getActiveSpan();
|
|
108
|
+
if (!span?.isRecording()) return;
|
|
109
|
+
if (update.attributes) span.setAttributes(update.attributes);
|
|
110
|
+
if (update.name) span.updateName(update.name);
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Attributes for GraphQL operation spans, gated by `OTEL_CONTELLO_CAPTURE_QUERY` and
|
|
114
|
+
* `OTEL_CONTELLO_CAPTURE_VARIABLES` (both off by default).
|
|
115
|
+
*/
|
|
116
|
+
function graphqlOperationAttributes(document, variables) {
|
|
117
|
+
if (!otel || !otelEnv.captureQuery && !otelEnv.captureVariables) return;
|
|
118
|
+
const attrs = {};
|
|
119
|
+
if (otelEnv.captureQuery) attrs["graphql.document"] = document;
|
|
120
|
+
if (otelEnv.captureVariables && variables !== void 0) attrs["graphql.variables"] = JSON.stringify(variables);
|
|
121
|
+
return attrs;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Injects W3C trace context (`traceparent` / `tracestate`) into an outgoing GraphQL
|
|
125
|
+
* WebSocket message. Returns the message unchanged when telemetry is disabled.
|
|
126
|
+
*/
|
|
127
|
+
function injectTraceContext(message) {
|
|
128
|
+
if (!otel) return message;
|
|
129
|
+
const carrier = {};
|
|
130
|
+
otel.propagation.inject(otel.context.active(), carrier);
|
|
131
|
+
if (carrier["traceparent"]) message.traceparent = carrier["traceparent"];
|
|
132
|
+
if (carrier["tracestate"]) message.tracestate = carrier["tracestate"];
|
|
133
|
+
return message;
|
|
134
|
+
}
|
|
106
135
|
//#endregion
|
|
107
|
-
export {
|
|
136
|
+
export { createOperationTelemetry, graphqlOperationAttributes, injectTraceContext, updateActiveSpan };
|
|
108
137
|
|
|
109
138
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../src/
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/api.ts","../src/env.ts","../src/index.ts"],"sourcesContent":["import { createRequire } from 'node:module';\nimport type * as otelApi from '@opentelemetry/api';\n\nexport type OtelApi = typeof otelApi;\n\nfunction loadApi(): OtelApi | undefined {\n try {\n return createRequire(import.meta.url)('@opentelemetry/api') as OtelApi;\n } catch {\n // @opentelemetry/api is not installed — telemetry stays disabled\n return undefined;\n }\n}\n\nexport const api = loadApi();\n","function envFlag(name: string, defaultValue: boolean): boolean {\n const raw = process.env[name];\n\n if (raw === undefined) {\n return defaultValue;\n }\n\n const value = raw.trim().toLowerCase();\n\n if (value === '') {\n return defaultValue;\n }\n\n return !['false', '0', 'no', 'off'].includes(value);\n}\n\nexport const otelEnv = {\n enabled: envFlag('OTEL_CONTELLO_ENABLED', true),\n captureQuery: envFlag('OTEL_CONTELLO_CAPTURE_QUERY', false),\n captureVariables: envFlag('OTEL_CONTELLO_CAPTURE_VARIABLES', false),\n} as const;\n","import { api } from './api';\nimport { otelEnv } from './env';\n\nconst otel = otelEnv.enabled ? api : undefined;\n\nexport type OperationAttributes = Record<string, string | number | boolean>;\n\nexport type OperationTelemetry = {\n /**\n * Runs `fn` inside an active span named `name` (convention: `category:operation`) and records\n * duration/error metrics. Sync results are attributed `cached: true`, promises `cached: false`.\n * Zero-overhead pass-through when `@opentelemetry/api` is absent or `OTEL_CONTELLO_ENABLED=false`.\n */\n wrap: <T>(name: string, fn: () => T, attributes?: OperationAttributes | undefined) => T;\n};\n\nfunction parseOperationAttributes(name: string): OperationAttributes {\n const colonIndex = name.indexOf(':');\n\n if (colonIndex === -1) {\n return { category: 'unknown', operation: name };\n }\n\n return { category: name.slice(0, colonIndex), operation: name.slice(colonIndex + 1) };\n}\n\nexport function createOperationTelemetry(scope: string): OperationTelemetry {\n if (!otel) {\n return { wrap: (_name, fn) => fn() };\n }\n\n const { SpanStatusCode } = otel;\n const tracer = otel.trace.getTracer(scope);\n const meter = otel.metrics.getMeter(scope);\n\n const duration = meter.createHistogram('contello.operation.duration', {\n description: 'duration of contello operations',\n unit: 'ms',\n });\n\n const errors = meter.createCounter('contello.operation.error', {\n description: 'number of failed contello operations',\n });\n\n const wrap = <T>(name: string, fn: () => T, attributes?: OperationAttributes | undefined): T => {\n const attrs = { ...parseOperationAttributes(name), ...attributes };\n\n return tracer.startActiveSpan(name, { attributes: attrs }, (span) => {\n const start = performance.now();\n\n const finish = (cached: boolean) => {\n span.setAttribute('cached', cached);\n duration.record(performance.now() - start, { ...attrs, cached });\n span.end();\n };\n\n const fail = (error: unknown) => {\n duration.record(performance.now() - start, { ...attrs, cached: false });\n errors.add(1, attrs);\n span.recordException(error as Error);\n span.setStatus({\n code: SpanStatusCode.ERROR,\n message: error instanceof Error ? error.message : String(error),\n });\n span.end();\n };\n\n let result: T;\n\n try {\n result = fn();\n } catch (error) {\n fail(error);\n\n throw error;\n }\n\n if (result instanceof Promise) {\n // passive observer: the caller keeps the original promise (no extra hop);\n // registered before returning, so finish/fail run ahead of the caller's continuation\n // eslint-disable-next-line unicorn/prefer-await\n result.then(\n () => finish(false),\n (error: unknown) => fail(error),\n );\n\n return result;\n }\n\n finish(true);\n\n return result;\n });\n };\n\n return { wrap };\n}\n\n/**\n * Renames the active span and/or merges attributes into it. No-op when telemetry is\n * disabled, `@opentelemetry/api` is absent, or no recording span is active. Used to\n * enrich an outer span (e.g. the platform HTTP server span) with context only the\n * contello layer knows.\n */\nexport function updateActiveSpan(update: {\n name?: string | undefined;\n attributes?: OperationAttributes | undefined;\n}): void {\n if (!otel) {\n return;\n }\n\n const span = otel.trace.getActiveSpan();\n\n if (!span?.isRecording()) {\n return;\n }\n\n if (update.attributes) {\n span.setAttributes(update.attributes);\n }\n\n if (update.name) {\n span.updateName(update.name);\n }\n}\n\n/**\n * Attributes for GraphQL operation spans, gated by `OTEL_CONTELLO_CAPTURE_QUERY` and\n * `OTEL_CONTELLO_CAPTURE_VARIABLES` (both off by default).\n */\nexport function graphqlOperationAttributes(\n document: string,\n variables?: Record<string, unknown> | undefined,\n): OperationAttributes | undefined {\n if (!otel || (!otelEnv.captureQuery && !otelEnv.captureVariables)) {\n return undefined;\n }\n\n const attrs: OperationAttributes = {};\n\n if (otelEnv.captureQuery) {\n attrs['graphql.document'] = document;\n }\n\n if (otelEnv.captureVariables && variables !== undefined) {\n attrs['graphql.variables'] = JSON.stringify(variables);\n }\n\n return attrs;\n}\n\n/**\n * Injects W3C trace context (`traceparent` / `tracestate`) into an outgoing GraphQL\n * WebSocket message. Returns the message unchanged when telemetry is disabled.\n */\nexport function injectTraceContext(message: any): any {\n if (!otel) {\n return message;\n }\n\n const carrier: Record<string, string> = {};\n\n otel.propagation.inject(otel.context.active(), carrier);\n\n if (carrier['traceparent']) {\n message.traceparent = carrier['traceparent'];\n }\n\n if (carrier['tracestate']) {\n message.tracestate = carrier['tracestate'];\n }\n\n return message;\n}\n"],"mappings":";;AAKA,SAAS,UAA+B;CACtC,IAAI;EACF,OAAO,cAAc,OAAO,KAAK,GAAG,CAAC,CAAC,oBAAoB;CAC5D,QAAQ;EAEN;CACF;AACF;AAEA,MAAa,MAAM,QAAQ;;;ACd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ACH,OAAO;CAGT,MAAM,UAAkC,CAAC;CAEzC,KAAK,YAAY,OAAO,KAAK,QAAQ,OAAO,GAAG,OAAO;CAEtD,IAAI,QAAQ,gBACV,QAAQ,cAAc,QAAQ;CAGhC,IAAI,QAAQ,eACV,QAAQ,aAAa,QAAQ;CAG/B,OAAO;AACT"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contello/opentelemetry",
|
|
3
|
-
"version": "1.0
|
|
4
|
-
"description": "OpenTelemetry
|
|
3
|
+
"version": "2.1.0",
|
|
4
|
+
"description": "Shared OpenTelemetry telemetry core for @contello packages",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"author": "admin@entwico.com",
|
|
@@ -11,12 +11,15 @@
|
|
|
11
11
|
"directory": "packages/opentelemetry"
|
|
12
12
|
},
|
|
13
13
|
"peerDependencies": {
|
|
14
|
-
"@opentelemetry/api": ">=1.
|
|
15
|
-
|
|
14
|
+
"@opentelemetry/api": ">=1.9.0"
|
|
15
|
+
},
|
|
16
|
+
"peerDependenciesMeta": {
|
|
17
|
+
"@opentelemetry/api": {
|
|
18
|
+
"optional": true
|
|
19
|
+
}
|
|
16
20
|
},
|
|
17
21
|
"devDependencies": {
|
|
18
|
-
"@opentelemetry/api": "^1.9.1"
|
|
19
|
-
"@opentelemetry/instrumentation": "^0.215.0"
|
|
22
|
+
"@opentelemetry/api": "^1.9.1"
|
|
20
23
|
},
|
|
21
24
|
"files": [
|
|
22
25
|
"dist"
|