@moostjs/otel 0.4.11 → 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,41 +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
- unit: 'byte',
95
- }),
96
- httpResponseSize: meter.createHistogram('http.server.response.size', {
97
- description: 'The size of HTTP response bodies',
98
- unit: 'byte',
99
- }),
100
- httpErrorCount: meter.createCounter('http.server.errors', {
101
- description: 'The number of HTTP requests that resulted in an error',
102
- }),
103
- moostEventCount: meter.createCounter('moost.events.count', {
104
- description: 'The number of HTTP requests received',
105
- }),
106
- moostActiveEvents: meter.createUpDownCounter('moost.events.active', {
107
- description: 'The number of active HTTP requests',
108
- }),
109
- moostErrorCount: meter.createCounter('moost.events.errors', {
110
- description: 'The number of HTTP requests that resulted in an error',
111
- }),
112
- };
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
+ }
113
119
 
114
120
  const tracer = api.trace.getTracer('moost-tracer');
115
121
  class SpanInjector extends moost.ContextInjector {
122
+ constructor() {
123
+ super(...arguments);
124
+ this.metrics = getMoostMetrics();
125
+ }
116
126
  with(name, attributes, cb) {
117
127
  const fn = typeof attributes === 'function' ? attributes : cb;
118
128
  const attrs = typeof attributes === 'object' ? attributes : undefined;
@@ -206,6 +216,7 @@ class SpanInjector extends moost.ContextInjector {
206
216
  this.startEventMetrics(chm.attrs, route);
207
217
  }
208
218
  else if (name === 'Controller:registered') {
219
+ const _route = moost.useAsyncEventContext().store('otel').get('route');
209
220
  const chm = this.getControllerHandlerMeta();
210
221
  if (!chm.ignoreMeter) {
211
222
  this.startEventMetrics(chm.attrs, route);
@@ -215,10 +226,10 @@ class SpanInjector extends moost.ContextInjector {
215
226
  if (span) {
216
227
  span.setAttributes(chm.attrs);
217
228
  if (chm.attrs['moost.event_type'] === 'HTTP') {
218
- span.updateName(`${this.getRequest()?.method || ''} ${route || '<unresolved>'}`);
229
+ span.updateName(`${this.getRequest()?.method || ''} ${_route || '<unresolved>'}`);
219
230
  }
220
231
  else {
221
- span.updateName(`${chm.attrs['moost.event_type']} ${route || '<unresolved>'}`);
232
+ span.updateName(`${chm.attrs['moost.event_type']} ${_route || '<unresolved>'}`);
222
233
  }
223
234
  }
224
235
  }
@@ -278,15 +289,15 @@ class SpanInjector extends moost.ContextInjector {
278
289
  route,
279
290
  url: req?.url,
280
291
  };
281
- moostMetrics.httpRequestCount.add(1, attrs);
282
- moostMetrics.httpActiveRequests.add(1, attrs);
292
+ this.metrics.httpRequestCount.add(1, attrs);
293
+ this.metrics.httpActiveRequests.add(1, attrs);
283
294
  }
284
295
  const attrs = {
285
296
  ...a,
286
297
  route,
287
298
  };
288
- moostMetrics.moostEventCount.add(1, attrs);
289
- moostMetrics.moostActiveEvents.add(1, attrs);
299
+ this.metrics.moostEventCount.add(1, attrs);
300
+ this.metrics.moostActiveEvents.add(1, attrs);
290
301
  }
291
302
  endEventMetrics(a, error) {
292
303
  const route = moost.useAsyncEventContext().store('otel').get('route');
@@ -298,21 +309,21 @@ class SpanInjector extends moost.ContextInjector {
298
309
  route,
299
310
  url: req?.url,
300
311
  };
301
- moostMetrics.httpActiveRequests.add(-1, attrs);
312
+ this.metrics.httpActiveRequests.add(-1, attrs);
302
313
  if (error) {
303
- moostMetrics.httpErrorCount.add(1, attrs);
314
+ this.metrics.httpErrorCount.add(1, attrs);
304
315
  }
305
- moostMetrics.httpRequestSize.record(req?.socket.bytesRead || 0, attrs);
306
- moostMetrics.httpResponseSize.record(res?._contentLength || 0, attrs);
307
- 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 });
308
319
  }
309
320
  const attrs = {
310
321
  ...a,
311
322
  route,
312
323
  };
313
- moostMetrics.moostActiveEvents.add(-1, attrs);
324
+ this.metrics.moostActiveEvents.add(-1, attrs);
314
325
  if (error) {
315
- moostMetrics.moostErrorCount.add(1, attrs);
326
+ this.metrics.moostErrorCount.add(1, attrs);
316
327
  }
317
328
  }
318
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;
package/dist/index.mjs CHANGED
@@ -76,41 +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
- unit: 'byte',
93
- }),
94
- httpResponseSize: meter.createHistogram('http.server.response.size', {
95
- description: 'The size of HTTP response bodies',
96
- unit: 'byte',
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
- };
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
+ }
111
117
 
112
118
  const tracer = trace.getTracer('moost-tracer');
113
119
  class SpanInjector extends ContextInjector {
120
+ constructor() {
121
+ super(...arguments);
122
+ this.metrics = getMoostMetrics();
123
+ }
114
124
  with(name, attributes, cb) {
115
125
  const fn = typeof attributes === 'function' ? attributes : cb;
116
126
  const attrs = typeof attributes === 'object' ? attributes : undefined;
@@ -204,6 +214,7 @@ class SpanInjector extends ContextInjector {
204
214
  this.startEventMetrics(chm.attrs, route);
205
215
  }
206
216
  else if (name === 'Controller:registered') {
217
+ const _route = useAsyncEventContext$1().store('otel').get('route');
207
218
  const chm = this.getControllerHandlerMeta();
208
219
  if (!chm.ignoreMeter) {
209
220
  this.startEventMetrics(chm.attrs, route);
@@ -213,10 +224,10 @@ class SpanInjector extends ContextInjector {
213
224
  if (span) {
214
225
  span.setAttributes(chm.attrs);
215
226
  if (chm.attrs['moost.event_type'] === 'HTTP') {
216
- span.updateName(`${this.getRequest()?.method || ''} ${route || '<unresolved>'}`);
227
+ span.updateName(`${this.getRequest()?.method || ''} ${_route || '<unresolved>'}`);
217
228
  }
218
229
  else {
219
- span.updateName(`${chm.attrs['moost.event_type']} ${route || '<unresolved>'}`);
230
+ span.updateName(`${chm.attrs['moost.event_type']} ${_route || '<unresolved>'}`);
220
231
  }
221
232
  }
222
233
  }
@@ -276,15 +287,15 @@ class SpanInjector extends ContextInjector {
276
287
  route,
277
288
  url: req?.url,
278
289
  };
279
- moostMetrics.httpRequestCount.add(1, attrs);
280
- moostMetrics.httpActiveRequests.add(1, attrs);
290
+ this.metrics.httpRequestCount.add(1, attrs);
291
+ this.metrics.httpActiveRequests.add(1, attrs);
281
292
  }
282
293
  const attrs = {
283
294
  ...a,
284
295
  route,
285
296
  };
286
- moostMetrics.moostEventCount.add(1, attrs);
287
- moostMetrics.moostActiveEvents.add(1, attrs);
297
+ this.metrics.moostEventCount.add(1, attrs);
298
+ this.metrics.moostActiveEvents.add(1, attrs);
288
299
  }
289
300
  endEventMetrics(a, error) {
290
301
  const route = useAsyncEventContext$1().store('otel').get('route');
@@ -296,21 +307,21 @@ class SpanInjector extends ContextInjector {
296
307
  route,
297
308
  url: req?.url,
298
309
  };
299
- moostMetrics.httpActiveRequests.add(-1, attrs);
310
+ this.metrics.httpActiveRequests.add(-1, attrs);
300
311
  if (error) {
301
- moostMetrics.httpErrorCount.add(1, attrs);
312
+ this.metrics.httpErrorCount.add(1, attrs);
302
313
  }
303
- moostMetrics.httpRequestSize.record(req?.socket.bytesRead || 0, attrs);
304
- moostMetrics.httpResponseSize.record(res?._contentLength || 0, attrs);
305
- 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 });
306
317
  }
307
318
  const attrs = {
308
319
  ...a,
309
320
  route,
310
321
  };
311
- moostMetrics.moostActiveEvents.add(-1, attrs);
322
+ this.metrics.moostActiveEvents.add(-1, attrs);
312
323
  if (error) {
313
- moostMetrics.moostErrorCount.add(1, attrs);
324
+ this.metrics.moostErrorCount.add(1, attrs);
314
325
  }
315
326
  }
316
327
  getRequest() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@moostjs/otel",
3
- "version": "0.4.11",
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.11"
42
+ "moost": "0.4.12"
43
43
  }
44
44
  }