@moostjs/otel 0.5.29 → 0.5.30
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 +44 -27
- package/dist/index.d.ts +28 -7
- package/dist/index.mjs +31 -13
- package/package.json +6 -4
package/dist/index.cjs
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
//#region rolldown:runtime
|
|
3
2
|
var __create = Object.create;
|
|
4
3
|
var __defProp = Object.defineProperty;
|
|
@@ -112,9 +111,20 @@ function getMoostMetrics() {
|
|
|
112
111
|
|
|
113
112
|
//#endregion
|
|
114
113
|
//#region packages/otel/src/utils.ts
|
|
115
|
-
|
|
114
|
+
/**
|
|
115
|
+
* Starts or continues a span, executes the callback within the span's context,
|
|
116
|
+
* and handles span completion and error recording. Supports both synchronous and asynchronous callbacks.
|
|
117
|
+
* An optional post-processing callback can be used to enrich the span before it ends.
|
|
118
|
+
*
|
|
119
|
+
* @template T
|
|
120
|
+
* @param {TSpanInput | Span} span - The span input containing name and options, or an existing span.
|
|
121
|
+
* @param {() => T} cb - The callback function to execute within the span's context.
|
|
122
|
+
* @param {TPostSpanProcessFn<T>=} postProcess - An optional post-processing callback to enrich the span before it ends. **CAUTION: When used, you must end the span yourself `span.end()`.**
|
|
123
|
+
* @returns {T} The result of the callback function.
|
|
124
|
+
* @throws {Error} Will throw an error if the callback function throws.
|
|
125
|
+
*/ function withSpan(span, cb, postProcess) {
|
|
116
126
|
const _span = typeof span.name === "string" && !span.spanContext ? __opentelemetry_api.trace.getTracer("default").startSpan(span.name, span.options) : span;
|
|
117
|
-
let result =
|
|
127
|
+
let result = void 0;
|
|
118
128
|
const finalizeSpan = (e, r) => {
|
|
119
129
|
if (e) {
|
|
120
130
|
_span.recordException(e);
|
|
@@ -130,16 +140,16 @@ function withSpan(span, cb, postProcess) {
|
|
|
130
140
|
try {
|
|
131
141
|
result = cb();
|
|
132
142
|
} catch (error) {
|
|
133
|
-
finalizeSpan(error,
|
|
143
|
+
finalizeSpan(error, void 0);
|
|
134
144
|
throw error;
|
|
135
145
|
}
|
|
136
146
|
if (result instanceof Promise) result.then((r) => {
|
|
137
|
-
finalizeSpan(
|
|
147
|
+
finalizeSpan(void 0, r);
|
|
138
148
|
return r;
|
|
139
149
|
}).catch((error) => {
|
|
140
|
-
finalizeSpan(error,
|
|
150
|
+
finalizeSpan(error, void 0);
|
|
141
151
|
});
|
|
142
|
-
else finalizeSpan(
|
|
152
|
+
else finalizeSpan(void 0, result);
|
|
143
153
|
});
|
|
144
154
|
return result;
|
|
145
155
|
}
|
|
@@ -160,7 +170,7 @@ const tracer = __opentelemetry_api.trace.getTracer("moost-tracer");
|
|
|
160
170
|
var SpanInjector = class extends moost.ContextInjector {
|
|
161
171
|
with(name, attributes, cb) {
|
|
162
172
|
const fn = typeof attributes === "function" ? attributes : cb;
|
|
163
|
-
const attrs = typeof attributes === "object" ? attributes :
|
|
173
|
+
const attrs = typeof attributes === "object" ? attributes : void 0;
|
|
164
174
|
if (name === "Event:start" && attrs?.eventType) return this.startEvent(attrs.eventType, fn);
|
|
165
175
|
else if (name !== "Event:start") if (this.getIgnoreSpan()) return fn();
|
|
166
176
|
else {
|
|
@@ -181,7 +191,7 @@ var SpanInjector = class extends moost.ContextInjector {
|
|
|
181
191
|
const originalWriteHead = res.writeHead;
|
|
182
192
|
Object.assign(res, { writeHead: (arg0, arg1, arg2) => {
|
|
183
193
|
res._statusCode = arg0;
|
|
184
|
-
const headers = typeof arg2 === "object" ? arg2 : typeof arg1 === "object" ? arg1 :
|
|
194
|
+
const headers = typeof arg2 === "object" ? arg2 : typeof arg1 === "object" ? arg1 : void 0;
|
|
185
195
|
res._contentLength = headers?.["content-length"] ? Number(headers["content-length"]) : 0;
|
|
186
196
|
return originalWriteHead.apply(res, [
|
|
187
197
|
arg0,
|
|
@@ -219,13 +229,13 @@ var SpanInjector = class extends moost.ContextInjector {
|
|
|
219
229
|
const { getMethod, getMethodMeta, getController, getControllerMeta, getRoute } = (0, moost.useControllerContext)();
|
|
220
230
|
const methodName = getMethod();
|
|
221
231
|
const controller = getController();
|
|
222
|
-
const cMeta = controller ? getControllerMeta() :
|
|
223
|
-
const mMeta = controller ? getMethodMeta() :
|
|
232
|
+
const cMeta = controller ? getControllerMeta() : void 0;
|
|
233
|
+
const mMeta = controller ? getMethodMeta() : void 0;
|
|
224
234
|
return {
|
|
225
235
|
ignoreMeter: cMeta?.otelIgnoreMeter || mMeta?.otelIgnoreMeter,
|
|
226
236
|
ignoreSpan: cMeta?.otelIgnoreSpan || mMeta?.otelIgnoreSpan,
|
|
227
237
|
attrs: {
|
|
228
|
-
"moost.controller": controller ? (0, moost.getConstructor)(controller).name :
|
|
238
|
+
"moost.controller": controller ? (0, moost.getConstructor)(controller).name : void 0,
|
|
229
239
|
"moost.handler": methodName,
|
|
230
240
|
"moost.handler_description": mMeta?.description,
|
|
231
241
|
"moost.handler_label": mMeta?.label,
|
|
@@ -325,8 +335,15 @@ function getOtelMate() {
|
|
|
325
335
|
//#endregion
|
|
326
336
|
//#region packages/otel/src/otel.decorators.ts
|
|
327
337
|
const mate = getOtelMate();
|
|
328
|
-
|
|
329
|
-
|
|
338
|
+
/**
|
|
339
|
+
* Annotate controller and/or handler to filter
|
|
340
|
+
* out the corresponding spans from transporting
|
|
341
|
+
*
|
|
342
|
+
* Requires use of MoostBatchSpanProcessor or MoostSimpleSpanProcessor
|
|
343
|
+
*/ const OtelIgnoreSpan = () => mate.decorate("otelIgnoreSpan", true);
|
|
344
|
+
/**
|
|
345
|
+
* Annotate controller and/or handler to suppress metrics
|
|
346
|
+
*/ const OtelIgnoreMeter = () => mate.decorate("otelIgnoreMeter", true);
|
|
330
347
|
|
|
331
348
|
//#endregion
|
|
332
349
|
//#region packages/otel/src/processors/span-filter.ts
|
|
@@ -353,16 +370,16 @@ var MoostSimpleSpanProcessor = class extends __opentelemetry_sdk_trace_base.Simp
|
|
|
353
370
|
};
|
|
354
371
|
|
|
355
372
|
//#endregion
|
|
356
|
-
exports.MoostBatchSpanProcessor = MoostBatchSpanProcessor
|
|
357
|
-
exports.MoostSimpleSpanProcessor = MoostSimpleSpanProcessor
|
|
358
|
-
exports.OtelIgnoreMeter = OtelIgnoreMeter
|
|
359
|
-
exports.OtelIgnoreSpan = OtelIgnoreSpan
|
|
360
|
-
exports.SpanInjector = SpanInjector
|
|
361
|
-
exports.enableOtelForMoost = enableOtelForMoost
|
|
362
|
-
exports.getOtelMate = getOtelMate
|
|
363
|
-
exports.shouldSpanBeIgnored = shouldSpanBeIgnored
|
|
364
|
-
exports.useOtelContext = useOtelContext
|
|
365
|
-
exports.useOtelPropagation = useOtelPropagation
|
|
366
|
-
exports.useSpan = useSpan
|
|
367
|
-
exports.useTrace = useTrace
|
|
368
|
-
exports.withSpan = withSpan
|
|
373
|
+
exports.MoostBatchSpanProcessor = MoostBatchSpanProcessor;
|
|
374
|
+
exports.MoostSimpleSpanProcessor = MoostSimpleSpanProcessor;
|
|
375
|
+
exports.OtelIgnoreMeter = OtelIgnoreMeter;
|
|
376
|
+
exports.OtelIgnoreSpan = OtelIgnoreSpan;
|
|
377
|
+
exports.SpanInjector = SpanInjector;
|
|
378
|
+
exports.enableOtelForMoost = enableOtelForMoost;
|
|
379
|
+
exports.getOtelMate = getOtelMate;
|
|
380
|
+
exports.shouldSpanBeIgnored = shouldSpanBeIgnored;
|
|
381
|
+
exports.useOtelContext = useOtelContext;
|
|
382
|
+
exports.useOtelPropagation = useOtelPropagation;
|
|
383
|
+
exports.useSpan = useSpan;
|
|
384
|
+
exports.useTrace = useTrace;
|
|
385
|
+
exports.withSpan = withSpan;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
1
|
import * as _opentelemetry_api from '@opentelemetry/api';
|
|
3
2
|
import { Span, SpanOptions } from '@opentelemetry/api';
|
|
4
3
|
import { Mate, TMoostMetadata, TMateParamMeta, ContextInjector, TContextInjectorHook } from 'moost';
|
|
@@ -43,14 +42,23 @@ declare function useOtelPropagation(): {
|
|
|
43
42
|
};
|
|
44
43
|
traceId?: string | undefined;
|
|
45
44
|
spanId?: string | undefined;
|
|
46
|
-
isRemote?: boolean
|
|
45
|
+
isRemote?: boolean;
|
|
47
46
|
traceFlags?: number | undefined;
|
|
48
|
-
traceState?: _opentelemetry_api.TraceState
|
|
47
|
+
traceState?: _opentelemetry_api.TraceState;
|
|
49
48
|
};
|
|
50
49
|
|
|
51
50
|
declare function enableOtelForMoost(): void;
|
|
52
51
|
|
|
52
|
+
/**
|
|
53
|
+
* Annotate controller and/or handler to filter
|
|
54
|
+
* out the corresponding spans from transporting
|
|
55
|
+
*
|
|
56
|
+
* Requires use of MoostBatchSpanProcessor or MoostSimpleSpanProcessor
|
|
57
|
+
*/
|
|
53
58
|
declare const OtelIgnoreSpan: () => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
|
|
59
|
+
/**
|
|
60
|
+
* Annotate controller and/or handler to suppress metrics
|
|
61
|
+
*/
|
|
54
62
|
declare const OtelIgnoreMeter: () => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
|
|
55
63
|
|
|
56
64
|
interface TOtelMate {
|
|
@@ -76,7 +84,7 @@ declare function shouldSpanBeIgnored(span: ReadableSpan): boolean;
|
|
|
76
84
|
type TAttributes = Record<string, string | number | boolean>;
|
|
77
85
|
declare class SpanInjector extends ContextInjector<TContextInjectorHook> {
|
|
78
86
|
metrics: {
|
|
79
|
-
moostEventDuration: _opentelemetry_api.Histogram
|
|
87
|
+
moostEventDuration: _opentelemetry_api.Histogram;
|
|
80
88
|
};
|
|
81
89
|
with<T>(name: TContextInjectorHook, attributes: TAttributes, cb: () => T): T;
|
|
82
90
|
with<T>(name: TContextInjectorHook, cb: () => T): T;
|
|
@@ -107,8 +115,8 @@ declare class SpanInjector extends ContextInjector<TContextInjectorHook> {
|
|
|
107
115
|
endEventMetrics(a: Record<string, string | number | boolean | undefined>, error?: Error): void;
|
|
108
116
|
getRequest(): IncomingMessage | undefined;
|
|
109
117
|
getResponse(): (ServerResponse<IncomingMessage> & {
|
|
110
|
-
_statusCode?: number
|
|
111
|
-
_contentLength?: number
|
|
118
|
+
_statusCode?: number;
|
|
119
|
+
_contentLength?: number;
|
|
112
120
|
}) | undefined;
|
|
113
121
|
}
|
|
114
122
|
|
|
@@ -117,6 +125,19 @@ interface TSpanInput {
|
|
|
117
125
|
name: string;
|
|
118
126
|
options?: SpanOptions;
|
|
119
127
|
}
|
|
128
|
+
/**
|
|
129
|
+
* Starts or continues a span, executes the callback within the span's context,
|
|
130
|
+
* and handles span completion and error recording. Supports both synchronous and asynchronous callbacks.
|
|
131
|
+
* An optional post-processing callback can be used to enrich the span before it ends.
|
|
132
|
+
*
|
|
133
|
+
* @template T
|
|
134
|
+
* @param {TSpanInput | Span} span - The span input containing name and options, or an existing span.
|
|
135
|
+
* @param {() => T} cb - The callback function to execute within the span's context.
|
|
136
|
+
* @param {TPostSpanProcessFn<T>=} postProcess - An optional post-processing callback to enrich the span before it ends. **CAUTION: When used, you must end the span yourself `span.end()`.**
|
|
137
|
+
* @returns {T} The result of the callback function.
|
|
138
|
+
* @throws {Error} Will throw an error if the callback function throws.
|
|
139
|
+
*/
|
|
120
140
|
declare function withSpan<T>(span: TSpanInput | Span, cb: () => T, postProcess?: TPostSpanProcessFn<T>): T;
|
|
121
141
|
|
|
122
|
-
export { MoostBatchSpanProcessor, MoostSimpleSpanProcessor, OtelIgnoreMeter, OtelIgnoreSpan, SpanInjector,
|
|
142
|
+
export { MoostBatchSpanProcessor, MoostSimpleSpanProcessor, OtelIgnoreMeter, OtelIgnoreSpan, SpanInjector, enableOtelForMoost, getOtelMate, shouldSpanBeIgnored, useOtelContext, useOtelPropagation, useSpan, useTrace, withSpan };
|
|
143
|
+
export type { TOtelContext, TOtelMate, TPostSpanProcessFn, TSpanInput };
|
package/dist/index.mjs
CHANGED
|
@@ -88,9 +88,20 @@ function getMoostMetrics() {
|
|
|
88
88
|
|
|
89
89
|
//#endregion
|
|
90
90
|
//#region packages/otel/src/utils.ts
|
|
91
|
-
|
|
91
|
+
/**
|
|
92
|
+
* Starts or continues a span, executes the callback within the span's context,
|
|
93
|
+
* and handles span completion and error recording. Supports both synchronous and asynchronous callbacks.
|
|
94
|
+
* An optional post-processing callback can be used to enrich the span before it ends.
|
|
95
|
+
*
|
|
96
|
+
* @template T
|
|
97
|
+
* @param {TSpanInput | Span} span - The span input containing name and options, or an existing span.
|
|
98
|
+
* @param {() => T} cb - The callback function to execute within the span's context.
|
|
99
|
+
* @param {TPostSpanProcessFn<T>=} postProcess - An optional post-processing callback to enrich the span before it ends. **CAUTION: When used, you must end the span yourself `span.end()`.**
|
|
100
|
+
* @returns {T} The result of the callback function.
|
|
101
|
+
* @throws {Error} Will throw an error if the callback function throws.
|
|
102
|
+
*/ function withSpan(span, cb, postProcess) {
|
|
92
103
|
const _span = typeof span.name === "string" && !span.spanContext ? trace.getTracer("default").startSpan(span.name, span.options) : span;
|
|
93
|
-
let result =
|
|
104
|
+
let result = void 0;
|
|
94
105
|
const finalizeSpan = (e, r) => {
|
|
95
106
|
if (e) {
|
|
96
107
|
_span.recordException(e);
|
|
@@ -106,16 +117,16 @@ function withSpan(span, cb, postProcess) {
|
|
|
106
117
|
try {
|
|
107
118
|
result = cb();
|
|
108
119
|
} catch (error) {
|
|
109
|
-
finalizeSpan(error,
|
|
120
|
+
finalizeSpan(error, void 0);
|
|
110
121
|
throw error;
|
|
111
122
|
}
|
|
112
123
|
if (result instanceof Promise) result.then((r) => {
|
|
113
|
-
finalizeSpan(
|
|
124
|
+
finalizeSpan(void 0, r);
|
|
114
125
|
return r;
|
|
115
126
|
}).catch((error) => {
|
|
116
|
-
finalizeSpan(error,
|
|
127
|
+
finalizeSpan(error, void 0);
|
|
117
128
|
});
|
|
118
|
-
else finalizeSpan(
|
|
129
|
+
else finalizeSpan(void 0, result);
|
|
119
130
|
});
|
|
120
131
|
return result;
|
|
121
132
|
}
|
|
@@ -136,7 +147,7 @@ const tracer = trace.getTracer("moost-tracer");
|
|
|
136
147
|
var SpanInjector = class extends ContextInjector {
|
|
137
148
|
with(name, attributes, cb) {
|
|
138
149
|
const fn = typeof attributes === "function" ? attributes : cb;
|
|
139
|
-
const attrs = typeof attributes === "object" ? attributes :
|
|
150
|
+
const attrs = typeof attributes === "object" ? attributes : void 0;
|
|
140
151
|
if (name === "Event:start" && attrs?.eventType) return this.startEvent(attrs.eventType, fn);
|
|
141
152
|
else if (name !== "Event:start") if (this.getIgnoreSpan()) return fn();
|
|
142
153
|
else {
|
|
@@ -157,7 +168,7 @@ var SpanInjector = class extends ContextInjector {
|
|
|
157
168
|
const originalWriteHead = res.writeHead;
|
|
158
169
|
Object.assign(res, { writeHead: (arg0, arg1, arg2) => {
|
|
159
170
|
res._statusCode = arg0;
|
|
160
|
-
const headers = typeof arg2 === "object" ? arg2 : typeof arg1 === "object" ? arg1 :
|
|
171
|
+
const headers = typeof arg2 === "object" ? arg2 : typeof arg1 === "object" ? arg1 : void 0;
|
|
161
172
|
res._contentLength = headers?.["content-length"] ? Number(headers["content-length"]) : 0;
|
|
162
173
|
return originalWriteHead.apply(res, [
|
|
163
174
|
arg0,
|
|
@@ -195,13 +206,13 @@ var SpanInjector = class extends ContextInjector {
|
|
|
195
206
|
const { getMethod, getMethodMeta, getController, getControllerMeta, getRoute } = useControllerContext();
|
|
196
207
|
const methodName = getMethod();
|
|
197
208
|
const controller = getController();
|
|
198
|
-
const cMeta = controller ? getControllerMeta() :
|
|
199
|
-
const mMeta = controller ? getMethodMeta() :
|
|
209
|
+
const cMeta = controller ? getControllerMeta() : void 0;
|
|
210
|
+
const mMeta = controller ? getMethodMeta() : void 0;
|
|
200
211
|
return {
|
|
201
212
|
ignoreMeter: cMeta?.otelIgnoreMeter || mMeta?.otelIgnoreMeter,
|
|
202
213
|
ignoreSpan: cMeta?.otelIgnoreSpan || mMeta?.otelIgnoreSpan,
|
|
203
214
|
attrs: {
|
|
204
|
-
"moost.controller": controller ? getConstructor(controller).name :
|
|
215
|
+
"moost.controller": controller ? getConstructor(controller).name : void 0,
|
|
205
216
|
"moost.handler": methodName,
|
|
206
217
|
"moost.handler_description": mMeta?.description,
|
|
207
218
|
"moost.handler_label": mMeta?.label,
|
|
@@ -301,8 +312,15 @@ function getOtelMate() {
|
|
|
301
312
|
//#endregion
|
|
302
313
|
//#region packages/otel/src/otel.decorators.ts
|
|
303
314
|
const mate = getOtelMate();
|
|
304
|
-
|
|
305
|
-
|
|
315
|
+
/**
|
|
316
|
+
* Annotate controller and/or handler to filter
|
|
317
|
+
* out the corresponding spans from transporting
|
|
318
|
+
*
|
|
319
|
+
* Requires use of MoostBatchSpanProcessor or MoostSimpleSpanProcessor
|
|
320
|
+
*/ const OtelIgnoreSpan = () => mate.decorate("otelIgnoreSpan", true);
|
|
321
|
+
/**
|
|
322
|
+
* Annotate controller and/or handler to suppress metrics
|
|
323
|
+
*/ const OtelIgnoreMeter = () => mate.decorate("otelIgnoreMeter", true);
|
|
306
324
|
|
|
307
325
|
//#endregion
|
|
308
326
|
//#region packages/otel/src/processors/span-filter.ts
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@moostjs/otel",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.30",
|
|
4
4
|
"description": "@moostjs/otel",
|
|
5
5
|
"main": "dist/index.cjs",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -36,13 +36,15 @@
|
|
|
36
36
|
"url": "https://github.com/moostjs/moostjs/issues"
|
|
37
37
|
},
|
|
38
38
|
"homepage": "https://github.com/moostjs/moostjs/tree/main/packages/otel#readme",
|
|
39
|
+
"peerDependencies": {
|
|
40
|
+
"moost": "^0.5.30"
|
|
41
|
+
},
|
|
39
42
|
"dependencies": {
|
|
40
43
|
"@opentelemetry/api": "^1.9.0",
|
|
41
|
-
"@opentelemetry/sdk-trace-base": "^
|
|
42
|
-
"moost": "^0.5.29"
|
|
44
|
+
"@opentelemetry/sdk-trace-base": "^2.0.1"
|
|
43
45
|
},
|
|
44
46
|
"devDependencies": {
|
|
45
|
-
"vitest": "
|
|
47
|
+
"vitest": "3.2.4"
|
|
46
48
|
},
|
|
47
49
|
"scripts": {
|
|
48
50
|
"pub": "pnpm publish --access public",
|