@moostjs/otel 0.4.18 → 0.4.20

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
@@ -8,7 +8,12 @@ var sdkTraceBase = require('@opentelemetry/sdk-trace-base');
8
8
 
9
9
  const spanStack = [];
10
10
  function useOtelContext() {
11
- const store = moost.useAsyncEventContext().store('otel');
11
+ const eventContext = moost.useAsyncEventContext();
12
+ const store = eventContext.store('otel');
13
+ const csa = eventContext.store('customSpanAttrs');
14
+ const cma = eventContext.store('customMetricAttrs');
15
+ const customSpanAttr = (name, value) => csa.set(name, value);
16
+ const customMetricAttr = (name, value) => cma.set(name, value);
12
17
  const getSpan = () => store.get('span');
13
18
  const getSpanContext = () => {
14
19
  const span = getSpan();
@@ -62,6 +67,8 @@ function useOtelContext() {
62
67
  getPropagationHeaders,
63
68
  registerSpan: (span) => store.set('span', span),
64
69
  pushSpan,
70
+ customSpanAttr,
71
+ customMetricAttr,
65
72
  };
66
73
  }
67
74
  function useTrace() {
@@ -83,34 +90,9 @@ function getMoostMetrics() {
83
90
  if (!moostMetrics) {
84
91
  const meter = api.metrics.getMeter('moost-meter');
85
92
  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',
93
+ moostEventDuration: meter.createHistogram('moost.event.duration', {
94
+ description: 'Moost Event Duration Histogram',
95
+ unit: 'ms',
114
96
  }),
115
97
  };
116
98
  }
@@ -311,52 +293,36 @@ class SpanInjector extends moost.ContextInjector {
311
293
  }
312
294
  }
313
295
  if (opts.endSpan) {
296
+ const customAttrs = moost.useAsyncEventContext().store('customSpanAttrs').value;
297
+ if (customAttrs) {
298
+ _span.setAttributes(customAttrs);
299
+ }
314
300
  _span.end();
315
301
  }
316
302
  });
317
303
  }
318
304
  startEventMetrics(a, route) {
319
- if (a['moost.event_type'] === 'HTTP') {
320
- const req = this.getRequest();
321
- const attrs = {
322
- 'route': route || req?.url,
323
- 'moost.event_type': a['moost.event_type'],
324
- };
325
- this.metrics.httpRequestCount.add(1, attrs);
326
- this.metrics.httpActiveRequests.add(1, attrs);
327
- }
328
- const attrs = {
329
- route,
330
- 'moost.event_type': a['moost.event_type'],
331
- };
332
- this.metrics.moostEventCount.add(1, attrs);
333
- this.metrics.moostActiveEvents.add(1, attrs);
305
+ moost.useAsyncEventContext().store('otel').set('startTime', Date.now());
334
306
  }
335
307
  endEventMetrics(a, error) {
336
- const route = moost.useAsyncEventContext().store('otel').get('route');
337
- if (a['moost.event_type'] === 'HTTP') {
338
- const req = this.getRequest();
339
- const res = this.getResponse();
340
- const attrs = {
341
- 'route': route || req?.url,
342
- 'moost.event_type': a['moost.event_type'],
343
- };
344
- this.metrics.httpActiveRequests.add(-1, attrs);
345
- if (error) {
346
- this.metrics.httpErrorCount.add(1, attrs);
347
- }
348
- this.metrics.httpRequestSize.record(req?.socket.bytesRead || 0, attrs);
349
- this.metrics.httpResponseSize.record(res?._contentLength || 0, attrs);
350
- this.metrics.httpResponseCount.add(1, { ...attrs, status: res?._statusCode });
351
- }
308
+ const otelStore = moost.useAsyncEventContext().store('otel');
309
+ const route = otelStore.get('route');
310
+ const duration = Date.now() - (otelStore.get('startTime') || Date.now() - 1);
311
+ const customAttrs = moost.useAsyncEventContext().store('customMetricAttrs').value || {};
352
312
  const attrs = {
313
+ ...customAttrs,
353
314
  route,
354
315
  'moost.event_type': a['moost.event_type'],
316
+ 'moost.is_error': error ? 1 : 0,
355
317
  };
356
- this.metrics.moostActiveEvents.add(-1, attrs);
357
- if (error) {
358
- this.metrics.moostErrorCount.add(1, attrs);
318
+ if (a['moost.event_type'] === 'HTTP') {
319
+ if (!attrs.route) {
320
+ attrs.route = this.getRequest()?.url || '';
321
+ }
322
+ attrs['http.status_code'] = this.getResponse()?._statusCode || 0;
323
+ attrs['moost.is_error'] = attrs['moost.is_error'] || attrs['http.status_code'] > 399 ? 1 : 0;
359
324
  }
325
+ this.metrics.moostEventDuration.record(duration, attrs);
360
326
  }
361
327
  getRequest() {
362
328
  return moost.useAsyncEventContext().store('event').get('req');
package/dist/index.d.ts CHANGED
@@ -11,7 +11,10 @@ interface TOtelContext {
11
11
  otel?: {
12
12
  span?: Span;
13
13
  route?: string;
14
+ startTime?: number;
14
15
  };
16
+ customSpanAttrs?: Record<string, string>;
17
+ customMetricAttrs?: Record<string, string>;
15
18
  }
16
19
  declare function useOtelContext(): {
17
20
  trace: _opentelemetry_api.TraceAPI;
@@ -27,6 +30,8 @@ declare function useOtelContext(): {
27
30
  };
28
31
  registerSpan: (span: Span) => unknown;
29
32
  pushSpan: (span: Span) => void;
33
+ customSpanAttr: (name: string, value: string | number) => unknown;
34
+ customMetricAttr: (name: string, value: string | number) => unknown;
30
35
  };
31
36
  declare function useTrace(): _opentelemetry_api.TraceAPI;
32
37
  declare function useSpan(): Span | undefined;
@@ -198,15 +203,7 @@ declare function shouldSpanBeIgnored(span: ReadableSpan): boolean;
198
203
  type TAttributes = Record<string, string | number | boolean>;
199
204
  declare class SpanInjector extends ContextInjector<TContextInjectorHook> {
200
205
  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>;
206
+ moostEventDuration: _opentelemetry_api.Histogram<_opentelemetry_api.Attributes>;
210
207
  };
211
208
  with<T>(name: TContextInjectorHook, attributes: TAttributes, cb: () => T): T;
212
209
  with<T>(name: TContextInjectorHook, cb: () => T): T;
package/dist/index.mjs CHANGED
@@ -6,7 +6,12 @@ import { BatchSpanProcessor, SimpleSpanProcessor } from '@opentelemetry/sdk-trac
6
6
 
7
7
  const spanStack = [];
8
8
  function useOtelContext() {
9
- const store = useAsyncEventContext$1().store('otel');
9
+ const eventContext = useAsyncEventContext$1();
10
+ const store = eventContext.store('otel');
11
+ const csa = eventContext.store('customSpanAttrs');
12
+ const cma = eventContext.store('customMetricAttrs');
13
+ const customSpanAttr = (name, value) => csa.set(name, value);
14
+ const customMetricAttr = (name, value) => cma.set(name, value);
10
15
  const getSpan = () => store.get('span');
11
16
  const getSpanContext = () => {
12
17
  const span = getSpan();
@@ -60,6 +65,8 @@ function useOtelContext() {
60
65
  getPropagationHeaders,
61
66
  registerSpan: (span) => store.set('span', span),
62
67
  pushSpan,
68
+ customSpanAttr,
69
+ customMetricAttr,
63
70
  };
64
71
  }
65
72
  function useTrace() {
@@ -81,34 +88,9 @@ function getMoostMetrics() {
81
88
  if (!moostMetrics) {
82
89
  const meter = metrics.getMeter('moost-meter');
83
90
  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',
91
+ moostEventDuration: meter.createHistogram('moost.event.duration', {
92
+ description: 'Moost Event Duration Histogram',
93
+ unit: 'ms',
112
94
  }),
113
95
  };
114
96
  }
@@ -309,52 +291,36 @@ class SpanInjector extends ContextInjector {
309
291
  }
310
292
  }
311
293
  if (opts.endSpan) {
294
+ const customAttrs = useAsyncEventContext$1().store('customSpanAttrs').value;
295
+ if (customAttrs) {
296
+ _span.setAttributes(customAttrs);
297
+ }
312
298
  _span.end();
313
299
  }
314
300
  });
315
301
  }
316
302
  startEventMetrics(a, route) {
317
- if (a['moost.event_type'] === 'HTTP') {
318
- const req = this.getRequest();
319
- const attrs = {
320
- 'route': route || req?.url,
321
- 'moost.event_type': a['moost.event_type'],
322
- };
323
- this.metrics.httpRequestCount.add(1, attrs);
324
- this.metrics.httpActiveRequests.add(1, attrs);
325
- }
326
- const attrs = {
327
- route,
328
- 'moost.event_type': a['moost.event_type'],
329
- };
330
- this.metrics.moostEventCount.add(1, attrs);
331
- this.metrics.moostActiveEvents.add(1, attrs);
303
+ useAsyncEventContext$1().store('otel').set('startTime', Date.now());
332
304
  }
333
305
  endEventMetrics(a, error) {
334
- const route = useAsyncEventContext$1().store('otel').get('route');
335
- if (a['moost.event_type'] === 'HTTP') {
336
- const req = this.getRequest();
337
- const res = this.getResponse();
338
- const attrs = {
339
- 'route': route || req?.url,
340
- 'moost.event_type': a['moost.event_type'],
341
- };
342
- this.metrics.httpActiveRequests.add(-1, attrs);
343
- if (error) {
344
- this.metrics.httpErrorCount.add(1, attrs);
345
- }
346
- this.metrics.httpRequestSize.record(req?.socket.bytesRead || 0, attrs);
347
- this.metrics.httpResponseSize.record(res?._contentLength || 0, attrs);
348
- this.metrics.httpResponseCount.add(1, { ...attrs, status: res?._statusCode });
349
- }
306
+ const otelStore = useAsyncEventContext$1().store('otel');
307
+ const route = otelStore.get('route');
308
+ const duration = Date.now() - (otelStore.get('startTime') || Date.now() - 1);
309
+ const customAttrs = useAsyncEventContext$1().store('customMetricAttrs').value || {};
350
310
  const attrs = {
311
+ ...customAttrs,
351
312
  route,
352
313
  'moost.event_type': a['moost.event_type'],
314
+ 'moost.is_error': error ? 1 : 0,
353
315
  };
354
- this.metrics.moostActiveEvents.add(-1, attrs);
355
- if (error) {
356
- this.metrics.moostErrorCount.add(1, attrs);
316
+ if (a['moost.event_type'] === 'HTTP') {
317
+ if (!attrs.route) {
318
+ attrs.route = this.getRequest()?.url || '';
319
+ }
320
+ attrs['http.status_code'] = this.getResponse()?._statusCode || 0;
321
+ attrs['moost.is_error'] = attrs['moost.is_error'] || attrs['http.status_code'] > 399 ? 1 : 0;
357
322
  }
323
+ this.metrics.moostEventDuration.record(duration, attrs);
358
324
  }
359
325
  getRequest() {
360
326
  return useAsyncEventContext$1().store('event').get('req');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@moostjs/otel",
3
- "version": "0.4.18",
3
+ "version": "0.4.20",
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.18"
42
+ "moost": "0.4.20"
43
43
  }
44
44
  }