|
| 1 | +import type { Observable } from '@opentelemetry/api'; |
1 | 2 | import { type EventLoopUtilization, type IntervalHistogram, monitorEventLoopDelay, performance } from 'node:perf_hooks'; |
2 | 3 |
|
3 | 4 | import * as Attributes from './attributes.js'; |
@@ -142,17 +143,26 @@ export class NodejsMetricsMonitor { |
142 | 143 | // - https://youtu.be/WetXnEPraYM |
143 | 144 | obs.observe(this.eventLoopUilization, delta.utilization); |
144 | 145 |
|
145 | | - this.eventLoopTime.add(Math.floor(delta.idle), { [Attributes.NODEJS_EVENT_LOOP_STATE]: 'idle' }); |
146 | | - this.eventLoopTime.add(Math.floor(delta.active), { [Attributes.NODEJS_EVENT_LOOP_STATE]: 'active' }); |
| 146 | + this.eventLoopTime.add(Math.trunc(delta.idle), { [Attributes.NODEJS_EVENT_LOOP_STATE]: 'idle' }); |
| 147 | + this.eventLoopTime.add(Math.trunc(delta.active), { [Attributes.NODEJS_EVENT_LOOP_STATE]: 'active' }); |
147 | 148 |
|
148 | | - obs.observe(this.eventLoopDelayGauges.min, Math.floor(this.eventLoopDelay.min)); |
149 | | - obs.observe(this.eventLoopDelayGauges.mean, Math.floor(this.eventLoopDelay.mean)); |
150 | | - obs.observe(this.eventLoopDelayGauges.max, Math.floor(this.eventLoopDelay.max)); |
151 | | - obs.observe(this.eventLoopDelayGauges.stddev, Math.floor(this.eventLoopDelay.stddev)); |
152 | | - obs.observe(this.eventLoopDelayGauges.p50, Math.floor(this.eventLoopDelay.percentile(50))); |
153 | | - obs.observe(this.eventLoopDelayGauges.p90, Math.floor(this.eventLoopDelay.percentile(90))); |
154 | | - obs.observe(this.eventLoopDelayGauges.p99, Math.floor(this.eventLoopDelay.percentile(99))); |
| 149 | + safeObserveInt(obs, this.eventLoopDelayGauges.min, this.eventLoopDelay.min); |
| 150 | + safeObserveInt(obs, this.eventLoopDelayGauges.mean, this.eventLoopDelay.mean); |
| 151 | + safeObserveInt(obs, this.eventLoopDelayGauges.max, this.eventLoopDelay.max); |
| 152 | + safeObserveInt(obs, this.eventLoopDelayGauges.stddev, this.eventLoopDelay.stddev); |
| 153 | + safeObserveInt(obs, this.eventLoopDelayGauges.p50, this.eventLoopDelay.percentile(50)); |
| 154 | + safeObserveInt(obs, this.eventLoopDelayGauges.p90, this.eventLoopDelay.percentile(90)); |
| 155 | + safeObserveInt(obs, this.eventLoopDelayGauges.p99, this.eventLoopDelay.percentile(99)); |
155 | 156 |
|
156 | 157 | this.eventLoopDelay.reset(); |
157 | 158 | }; |
158 | 159 | } |
| 160 | + |
| 161 | +function safeObserveInt(observer: BatchObservableResult, metric: Observable, value: number, attrs?: object) { |
| 162 | + // discard NaN, Infinity, -Infinity |
| 163 | + if (!Number.isFinite(value)) { |
| 164 | + return; |
| 165 | + } |
| 166 | + |
| 167 | + observer.observe(metric, Math.trunc(value), attrs); |
| 168 | +} |
0 commit comments