Skip to content

Commit 73a7d84

Browse files
authored
fix: exception flushing issue during sdk shutdown (#157)
* fix: sdk exception flushing issue * style: remove some benchmarks * docs: type * docs: add changeset
1 parent 1147218 commit 73a7d84

4 files changed

Lines changed: 23 additions & 14 deletions

File tree

.changeset/proud-nails-worry.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@hyperdx/instrumentation-exception': patch
3+
'@hyperdx/node-opentelemetry': patch
4+
---
5+
6+
fix: sdk shutdown exception flushing issue

packages/instrumentation-exception/src/instrumentation.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,15 @@ export class ExceptionInstrumentation extends InstrumentationBase {
6262

6363
override enable() {
6464
const config = this.getConfig();
65+
const ff = config._internalForceFlush ?? this.forceFlush;
6566
onUncaughtExceptionIntegration({
6667
exitEvenIfOtherHandlersAreRegistered:
6768
config.exitEvenIfOtherHandlersAreRegistered,
68-
forceFlush: () => this.forceFlush(),
69+
forceFlush: () => ff(),
6970
}).setup({} as any);
7071
onUnhandledRejectionIntegration({
7172
mode: config.unhandledRejectionMode,
72-
forceFlush: () => this.forceFlush(),
73+
forceFlush: () => ff(),
7374
}).setup({} as any);
7475
}
7576

packages/instrumentation-exception/src/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ export interface EventCustomAttributeFunction {
1212
* Options available for the Exception Instrumentation (see [documentation]())
1313
*/
1414
export interface ExceptionInstrumentationConfig extends InstrumentationConfig {
15+
// INTERNAL USE ONLY
16+
_internalForceFlush?: () => Promise<void>;
17+
1518
/**
1619
* Controls if the SDK should register a handler to exit the process on uncaught errors:
1720
* - `true`: The SDK will exit the process on all uncaught errors.

packages/node-opentelemetry/src/otel.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -228,14 +228,12 @@ export const initSDK = (config: SDKConfig) => {
228228
//--------------------------------------------------
229229
// ------------------- LOGGER ----------------------
230230
//--------------------------------------------------
231-
let _t = process.hrtime();
232231
ui.text = 'Initializing OpenTelemetry Logger...';
233232
const _logger = new OtelLogger({
234233
detectResources: defaultDetectResources,
235234
service: defaultServiceName,
236235
});
237-
const t0 = process.hrtime(_t);
238-
ui.succeed(`Initialized OpenTelemetry Logger in ${hrtimeToMs(t0)} ms`);
236+
ui.succeed('Initialized OpenTelemetry Logger');
239237
//--------------------------------------------------
240238

241239
// Health check
@@ -318,7 +316,13 @@ export const initSDK = (config: SDKConfig) => {
318316
...(defaultSentryIntegrationEnabled
319317
? [new SentryNodeInstrumentation()]
320318
: []),
321-
...(defaultExceptionCapture ? [new ExceptionInstrumentation()] : []),
319+
...(defaultExceptionCapture
320+
? [
321+
new ExceptionInstrumentation({
322+
_internalForceFlush: shutdown,
323+
}),
324+
]
325+
: []),
322326
...(config.additionalInstrumentations ?? []),
323327
];
324328

@@ -350,8 +354,7 @@ export const initSDK = (config: SDKConfig) => {
350354
contextManager: contextManager,
351355
});
352356

353-
const t1 = process.hrtime(_t);
354-
ui.succeed(`Initialized instrumentations packages in ${hrtimeToMs(t1)} ms`);
357+
ui.succeed('Initialized instrumentations packages');
355358

356359
if (defaultEnableInternalProfiling) {
357360
ui.text = 'Enabling internal profiling...';
@@ -412,14 +415,11 @@ export const initSDK = (config: SDKConfig) => {
412415
}
413416
}
414417

415-
_t = process.hrtime();
416418
ui.text = 'Starting OpenTelemetry Node SDK...';
417419
sdk.start();
418-
const t2 = process.hrtime(_t);
419-
ui.succeed(`Started OpenTelemetry Node SDK in ${hrtimeToMs(t2)} ms`);
420+
ui.succeed('Started OpenTelemetry Node SDK');
420421

421422
if (config.programmaticImports) {
422-
_t = process.hrtime();
423423
ui.text = 'Repatching instrumentation packages...';
424424
for (const instrumentation of allInstrumentations) {
425425
const modules = (instrumentation as any)
@@ -526,8 +526,7 @@ export const initSDK = (config: SDKConfig) => {
526526
}
527527
}
528528
}
529-
const t3 = process.hrtime(_t);
530-
ui.succeed(`Repatched instrumentation packages in ${hrtimeToMs(t3)} ms`);
529+
ui.succeed('Repatched instrumentation packages');
531530
}
532531

533532
function handleTerminationSignal(signal: string) {

0 commit comments

Comments
 (0)