@moostjs/otel 0.4.10 → 0.4.12

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 CHANGED
@@ -78,39 +78,51 @@ function useOtelPropagation() {
78
78
  };
79
79
  }
80
80
 
81
- const meter = api.metrics.getMeter('moost-meter');
82
- const moostMetrics = {
83
- httpRequestCount: meter.createCounter('http.server.requests', {
84
- description: 'The number of HTTP requests received',
85
- }),
86
- httpResponseCount: meter.createCounter('http.server.responses', {
87
- description: 'The number of HTTP responses sent',
88
- }),
89
- httpActiveRequests: meter.createUpDownCounter('http.server.active_requests', {
90
- description: 'The number of active HTTP requests',
91
- }),
92
- httpRequestSize: meter.createHistogram('http.server.request.size', {
93
- description: 'The size of HTTP request bodies',
94
- }),
95
- httpResponseSize: meter.createHistogram('http.server.response.size', {
96
- description: 'The size of HTTP response bodies',
97
- }),
98
- httpErrorCount: meter.createCounter('http.server.errors', {
99
- description: 'The number of HTTP requests that resulted in an error',
100
- }),
101
- moostEventCount: meter.createCounter('moost.events.count', {
102
- description: 'The number of HTTP requests received',
103
- }),
104
- moostActiveEvents: meter.createUpDownCounter('moost.events.active', {
105
- description: 'The number of active HTTP requests',
106
- }),
107
- moostErrorCount: meter.createCounter('moost.events.errors', {
108
- description: 'The number of HTTP requests that resulted in an error',
109
- }),
110
- };
81
+ let moostMetrics;
82
+ function getMoostMetrics() {
83
+ if (!moostMetrics) {
84
+ const meter = api.metrics.getMeter('moost-meter');
85
+ moostMetrics = {
86
+ httpRequestCount: meter.createCounter('http.server.requests', {
87
+ description: 'The number of HTTP requests received',
88
+ }),
89
+ httpResponseCount: meter.createCounter('http.server.responses', {
90
+ description: 'The number of HTTP responses sent',
91
+ }),
92
+ httpActiveRequests: meter.createUpDownCounter('http.server.active_requests', {
93
+ description: 'The number of active HTTP requests',
94
+ }),
95
+ httpRequestSize: meter.createHistogram('http.server.request.size', {
96
+ description: 'The size of HTTP request bodies',
97
+ unit: 'byte',
98
+ }),
99
+ httpResponseSize: meter.createHistogram('http.server.response.size', {
100
+ description: 'The size of HTTP response bodies',
101
+ unit: 'byte',
102
+ }),
103
+ httpErrorCount: meter.createCounter('http.server.errors', {
104
+ description: 'The number of HTTP requests that resulted in an error',
105
+ }),
106
+ moostEventCount: meter.createCounter('moost.events.count', {
107
+ description: 'The number of HTTP requests received',
108
+ }),
109
+ moostActiveEvents: meter.createUpDownCounter('moost.events.active', {
110
+ description: 'The number of active HTTP requests',
111
+ }),
112
+ moostErrorCount: meter.createCounter('moost.events.errors', {
113
+ description: 'The number of HTTP requests that resulted in an error',
114
+ }),
115
+ };
116
+ }
117
+ return moostMetrics;
118
+ }
111
119
 
112
120
  const tracer = api.trace.getTracer('moost-tracer');
113
121
  class SpanInjector extends moost.ContextInjector {
122
+ constructor() {
123
+ super(...arguments);
124
+ this.metrics = getMoostMetrics();
125
+ }
114
126
  with(name, attributes, cb) {
115
127
  const fn = typeof attributes === 'function' ? attributes : cb;
116
128
  const attrs = typeof attributes === 'object' ? attributes : undefined;
@@ -199,22 +211,31 @@ class SpanInjector extends moost.ContextInjector {
199
211
  };
200
212
  }
201
213
  hook(name, route) {
202
- moost.useAsyncEventContext().store('otel').set('route', route);
203
- const chm = this.getControllerHandlerMeta();
204
- if (!chm.ignoreMeter) {
214
+ if (name === 'Handler:not_found') {
215
+ const chm = this.getControllerHandlerMeta();
205
216
  this.startEventMetrics(chm.attrs, route);
206
217
  }
207
- const { getSpan } = useOtelContext();
208
- const span = getSpan();
209
- if (span) {
210
- span.setAttributes(chm.attrs);
211
- if (chm.attrs['moost.event_type'] === 'HTTP') {
212
- span.updateName(`${this.getRequest()?.method || ''} ${route || '<unresolved>'}`);
218
+ else if (name === 'Controller:registered') {
219
+ const _route = moost.useAsyncEventContext().store('otel').get('route');
220
+ const chm = this.getControllerHandlerMeta();
221
+ if (!chm.ignoreMeter) {
222
+ this.startEventMetrics(chm.attrs, route);
213
223
  }
214
- else {
215
- span.updateName(`${chm.attrs['moost.event_type']} ${route || '<unresolved>'}`);
224
+ const { getSpan } = useOtelContext();
225
+ const span = getSpan();
226
+ if (span) {
227
+ span.setAttributes(chm.attrs);
228
+ if (chm.attrs['moost.event_type'] === 'HTTP') {
229
+ span.updateName(`${this.getRequest()?.method || ''} ${_route || '<unresolved>'}`);
230
+ }
231
+ else {
232
+ span.updateName(`${chm.attrs['moost.event_type']} ${_route || '<unresolved>'}`);
233
+ }
216
234
  }
217
235
  }
236
+ if (name !== 'Controller:registered') {
237
+ moost.useAsyncEventContext().store('otel').set('route', route);
238
+ }
218
239
  }
219
240
  withSpan(span, cb, opts) {
220
241
  let result;
@@ -268,15 +289,15 @@ class SpanInjector extends moost.ContextInjector {
268
289
  route,
269
290
  url: req?.url,
270
291
  };
271
- moostMetrics.httpRequestCount.add(1, attrs);
272
- moostMetrics.httpActiveRequests.add(1, attrs);
292
+ this.metrics.httpRequestCount.add(1, attrs);
293
+ this.metrics.httpActiveRequests.add(1, attrs);
273
294
  }
274
295
  const attrs = {
275
296
  ...a,
276
297
  route,
277
298
  };
278
- moostMetrics.moostEventCount.add(1, attrs);
279
- moostMetrics.moostActiveEvents.add(1, attrs);
299
+ this.metrics.moostEventCount.add(1, attrs);
300
+ this.metrics.moostActiveEvents.add(1, attrs);
280
301
  }
281
302
  endEventMetrics(a, error) {
282
303
  const route = moost.useAsyncEventContext().store('otel').get('route');
@@ -288,21 +309,21 @@ class SpanInjector extends moost.ContextInjector {
288
309
  route,
289
310
  url: req?.url,
290
311
  };
291
- moostMetrics.httpActiveRequests.add(-1, attrs);
312
+ this.metrics.httpActiveRequests.add(-1, attrs);
292
313
  if (error) {
293
- moostMetrics.httpErrorCount.add(1, attrs);
314
+ this.metrics.httpErrorCount.add(1, attrs);
294
315
  }
295
- moostMetrics.httpRequestSize.record(req?.socket.bytesRead || 0, attrs);
296
- moostMetrics.httpResponseSize.record(res?._contentLength || 0, attrs);
297
- moostMetrics.httpResponseCount.add(1, { ...attrs, status: res?._statusCode });
316
+ this.metrics.httpRequestSize.record(req?.socket.bytesRead || 0, attrs);
317
+ this.metrics.httpResponseSize.record(res?._contentLength || 0, attrs);
318
+ this.metrics.httpResponseCount.add(1, { ...attrs, status: res?._statusCode });
298
319
  }
299
320
  const attrs = {
300
321
  ...a,
301
322
  route,
302
323
  };
303
- moostMetrics.moostActiveEvents.add(-1, attrs);
324
+ this.metrics.moostActiveEvents.add(-1, attrs);
304
325
  if (error) {
305
- moostMetrics.moostErrorCount.add(1, attrs);
326
+ this.metrics.moostErrorCount.add(1, attrs);
306
327
  }
307
328
  }
308
329
  getRequest() {
package/dist/index.d.ts CHANGED
@@ -79,6 +79,7 @@ type TInterceptorOnError = (error: Error, reply: (response: TAny) => void) => vo
79
79
  interface TInterceptorFn {
80
80
  (before: (fn: TInterceptorBefore) => void, after: (fn: TInterceptorAfter) => void, onError: (fn: TInterceptorOnError) => void): unknown | Promise<unknown>;
81
81
  priority?: TInterceptorPriority;
82
+ _name?: string;
82
83
  }
83
84
  declare enum TInterceptorPriority {
84
85
  BEFORE_ALL = 0,
@@ -157,6 +158,7 @@ type TMoostHandler<T> = {
157
158
  interface TInterceptorData {
158
159
  handler: TCallableClassFunction<TInterceptorFn>;
159
160
  priority: TInterceptorPriority;
161
+ name: string;
160
162
  }
161
163
  interface TCommonMetaFields {
162
164
  id?: string;
@@ -195,6 +197,17 @@ declare function shouldSpanBeIgnored(span: ReadableSpan): boolean;
195
197
 
196
198
  type TAttributes = Record<string, string | number | boolean>;
197
199
  declare class SpanInjector extends ContextInjector<TContextInjectorHook> {
200
+ metrics: {
201
+ httpRequestCount: _opentelemetry_api.Counter<_opentelemetry_api.Attributes>;
202
+ httpResponseCount: _opentelemetry_api.Counter<_opentelemetry_api.Attributes>;
203
+ httpActiveRequests: _opentelemetry_api.Counter<_opentelemetry_api.Attributes>;
204
+ httpRequestSize: _opentelemetry_api.Histogram<_opentelemetry_api.Attributes>;
205
+ httpResponseSize: _opentelemetry_api.Histogram<_opentelemetry_api.Attributes>;
206
+ httpErrorCount: _opentelemetry_api.Counter<_opentelemetry_api.Attributes>;
207
+ moostEventCount: _opentelemetry_api.Counter<_opentelemetry_api.Attributes>;
208
+ moostActiveEvents: _opentelemetry_api.Counter<_opentelemetry_api.Attributes>;
209
+ moostErrorCount: _opentelemetry_api.Counter<_opentelemetry_api.Attributes>;
210
+ };
198
211
  with<T>(name: TContextInjectorHook, attributes: TAttributes, cb: () => T): T;
199
212
  with<T>(name: TContextInjectorHook, cb: () => T): T;
200
213
  patchRsponse(): void;
@@ -215,7 +228,7 @@ declare class SpanInjector extends ContextInjector<TContextInjectorHook> {
215
228
  'moost.event_type': string;
216
229
  };
217
230
  };
218
- hook(name: 'Handler:not_found' | 'Handler:routed' | 'C', route?: string): void;
231
+ hook(name: 'Handler:not_found' | 'Handler:routed' | 'Controller:registered', route?: string): void;
219
232
  withSpan<T>(span: Span, cb: () => T, opts: {
220
233
  rootSpan: boolean;
221
234
  endSpan: boolean;
package/dist/index.mjs CHANGED
@@ -76,39 +76,51 @@ function useOtelPropagation() {
76
76
  };
77
77
  }
78
78
 
79
- const meter = metrics.getMeter('moost-meter');
80
- const moostMetrics = {
81
- httpRequestCount: meter.createCounter('http.server.requests', {
82
- description: 'The number of HTTP requests received',
83
- }),
84
- httpResponseCount: meter.createCounter('http.server.responses', {
85
- description: 'The number of HTTP responses sent',
86
- }),
87
- httpActiveRequests: meter.createUpDownCounter('http.server.active_requests', {
88
- description: 'The number of active HTTP requests',
89
- }),
90
- httpRequestSize: meter.createHistogram('http.server.request.size', {
91
- description: 'The size of HTTP request bodies',
92
- }),
93
- httpResponseSize: meter.createHistogram('http.server.response.size', {
94
- description: 'The size of HTTP response bodies',
95
- }),
96
- httpErrorCount: meter.createCounter('http.server.errors', {
97
- description: 'The number of HTTP requests that resulted in an error',
98
- }),
99
- moostEventCount: meter.createCounter('moost.events.count', {
100
- description: 'The number of HTTP requests received',
101
- }),
102
- moostActiveEvents: meter.createUpDownCounter('moost.events.active', {
103
- description: 'The number of active HTTP requests',
104
- }),
105
- moostErrorCount: meter.createCounter('moost.events.errors', {
106
- description: 'The number of HTTP requests that resulted in an error',
107
- }),
108
- };
79
+ let moostMetrics;
80
+ function getMoostMetrics() {
81
+ if (!moostMetrics) {
82
+ const meter = metrics.getMeter('moost-meter');
83
+ moostMetrics = {
84
+ httpRequestCount: meter.createCounter('http.server.requests', {
85
+ description: 'The number of HTTP requests received',
86
+ }),
87
+ httpResponseCount: meter.createCounter('http.server.responses', {
88
+ description: 'The number of HTTP responses sent',
89
+ }),
90
+ httpActiveRequests: meter.createUpDownCounter('http.server.active_requests', {
91
+ description: 'The number of active HTTP requests',
92
+ }),
93
+ httpRequestSize: meter.createHistogram('http.server.request.size', {
94
+ description: 'The size of HTTP request bodies',
95
+ unit: 'byte',
96
+ }),
97
+ httpResponseSize: meter.createHistogram('http.server.response.size', {
98
+ description: 'The size of HTTP response bodies',
99
+ unit: 'byte',
100
+ }),
101
+ httpErrorCount: meter.createCounter('http.server.errors', {
102
+ description: 'The number of HTTP requests that resulted in an error',
103
+ }),
104
+ moostEventCount: meter.createCounter('moost.events.count', {
105
+ description: 'The number of HTTP requests received',
106
+ }),
107
+ moostActiveEvents: meter.createUpDownCounter('moost.events.active', {
108
+ description: 'The number of active HTTP requests',
109
+ }),
110
+ moostErrorCount: meter.createCounter('moost.events.errors', {
111
+ description: 'The number of HTTP requests that resulted in an error',
112
+ }),
113
+ };
114
+ }
115
+ return moostMetrics;
116
+ }
109
117
 
110
118
  const tracer = trace.getTracer('moost-tracer');
111
119
  class SpanInjector extends ContextInjector {
120
+ constructor() {
121
+ super(...arguments);
122
+ this.metrics = getMoostMetrics();
123
+ }
112
124
  with(name, attributes, cb) {
113
125
  const fn = typeof attributes === 'function' ? attributes : cb;
114
126
  const attrs = typeof attributes === 'object' ? attributes : undefined;
@@ -197,22 +209,31 @@ class SpanInjector extends ContextInjector {
197
209
  };
198
210
  }
199
211
  hook(name, route) {
200
- useAsyncEventContext$1().store('otel').set('route', route);
201
- const chm = this.getControllerHandlerMeta();
202
- if (!chm.ignoreMeter) {
212
+ if (name === 'Handler:not_found') {
213
+ const chm = this.getControllerHandlerMeta();
203
214
  this.startEventMetrics(chm.attrs, route);
204
215
  }
205
- const { getSpan } = useOtelContext();
206
- const span = getSpan();
207
- if (span) {
208
- span.setAttributes(chm.attrs);
209
- if (chm.attrs['moost.event_type'] === 'HTTP') {
210
- span.updateName(`${this.getRequest()?.method || ''} ${route || '<unresolved>'}`);
216
+ else if (name === 'Controller:registered') {
217
+ const _route = useAsyncEventContext$1().store('otel').get('route');
218
+ const chm = this.getControllerHandlerMeta();
219
+ if (!chm.ignoreMeter) {
220
+ this.startEventMetrics(chm.attrs, route);
211
221
  }
212
- else {
213
- span.updateName(`${chm.attrs['moost.event_type']} ${route || '<unresolved>'}`);
222
+ const { getSpan } = useOtelContext();
223
+ const span = getSpan();
224
+ if (span) {
225
+ span.setAttributes(chm.attrs);
226
+ if (chm.attrs['moost.event_type'] === 'HTTP') {
227
+ span.updateName(`${this.getRequest()?.method || ''} ${_route || '<unresolved>'}`);
228
+ }
229
+ else {
230
+ span.updateName(`${chm.attrs['moost.event_type']} ${_route || '<unresolved>'}`);
231
+ }
214
232
  }
215
233
  }
234
+ if (name !== 'Controller:registered') {
235
+ useAsyncEventContext$1().store('otel').set('route', route);
236
+ }
216
237
  }
217
238
  withSpan(span, cb, opts) {
218
239
  let result;
@@ -266,15 +287,15 @@ class SpanInjector extends ContextInjector {
266
287
  route,
267
288
  url: req?.url,
268
289
  };
269
- moostMetrics.httpRequestCount.add(1, attrs);
270
- moostMetrics.httpActiveRequests.add(1, attrs);
290
+ this.metrics.httpRequestCount.add(1, attrs);
291
+ this.metrics.httpActiveRequests.add(1, attrs);
271
292
  }
272
293
  const attrs = {
273
294
  ...a,
274
295
  route,
275
296
  };
276
- moostMetrics.moostEventCount.add(1, attrs);
277
- moostMetrics.moostActiveEvents.add(1, attrs);
297
+ this.metrics.moostEventCount.add(1, attrs);
298
+ this.metrics.moostActiveEvents.add(1, attrs);
278
299
  }
279
300
  endEventMetrics(a, error) {
280
301
  const route = useAsyncEventContext$1().store('otel').get('route');
@@ -286,21 +307,21 @@ class SpanInjector extends ContextInjector {
286
307
  route,
287
308
  url: req?.url,
288
309
  };
289
- moostMetrics.httpActiveRequests.add(-1, attrs);
310
+ this.metrics.httpActiveRequests.add(-1, attrs);
290
311
  if (error) {
291
- moostMetrics.httpErrorCount.add(1, attrs);
312
+ this.metrics.httpErrorCount.add(1, attrs);
292
313
  }
293
- moostMetrics.httpRequestSize.record(req?.socket.bytesRead || 0, attrs);
294
- moostMetrics.httpResponseSize.record(res?._contentLength || 0, attrs);
295
- moostMetrics.httpResponseCount.add(1, { ...attrs, status: res?._statusCode });
314
+ this.metrics.httpRequestSize.record(req?.socket.bytesRead || 0, attrs);
315
+ this.metrics.httpResponseSize.record(res?._contentLength || 0, attrs);
316
+ this.metrics.httpResponseCount.add(1, { ...attrs, status: res?._statusCode });
296
317
  }
297
318
  const attrs = {
298
319
  ...a,
299
320
  route,
300
321
  };
301
- moostMetrics.moostActiveEvents.add(-1, attrs);
322
+ this.metrics.moostActiveEvents.add(-1, attrs);
302
323
  if (error) {
303
- moostMetrics.moostErrorCount.add(1, attrs);
324
+ this.metrics.moostErrorCount.add(1, attrs);
304
325
  }
305
326
  }
306
327
  getRequest() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@moostjs/otel",
3
- "version": "0.4.10",
3
+ "version": "0.4.12",
4
4
  "description": "@moostjs/otel",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.mjs",
@@ -39,6 +39,6 @@
39
39
  "dependencies": {
40
40
  "@opentelemetry/api": "^1.9.0",
41
41
  "@opentelemetry/sdk-trace-base": "^1.25.1",
42
- "moost": "0.4.10"
42
+ "moost": "0.4.12"
43
43
  }
44
44
  }