taist 0.2.17 → 0.2.18
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/instrument.js +39 -0
- package/package.json +1 -1
- package/types/instrument.d.ts +22 -0
package/instrument.js
CHANGED
|
@@ -92,6 +92,45 @@ export async function flushTraces() {
|
|
|
92
92
|
}
|
|
93
93
|
}
|
|
94
94
|
|
|
95
|
+
/**
|
|
96
|
+
* Gracefully shutdown taist - call this before process exit.
|
|
97
|
+
* Waits for pending socket writes to complete and flushes remaining traces.
|
|
98
|
+
*
|
|
99
|
+
* This is the recommended way to ensure all traces are captured when integrating
|
|
100
|
+
* with applications that have their own shutdown hooks (e.g., Directus onShutdown).
|
|
101
|
+
*
|
|
102
|
+
* @param {number} timeoutMs - Maximum time to wait for pending writes (default: 2000ms)
|
|
103
|
+
* @returns {Promise<void>}
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* // Directus integration
|
|
107
|
+
* import { gracefulShutdown } from 'taist/instrument';
|
|
108
|
+
*
|
|
109
|
+
* export default defineHook(({ onShutdown }) => {
|
|
110
|
+
* onShutdown(async () => {
|
|
111
|
+
* await gracefulShutdown();
|
|
112
|
+
* });
|
|
113
|
+
* });
|
|
114
|
+
*/
|
|
115
|
+
export async function gracefulShutdown(timeoutMs = 2000) {
|
|
116
|
+
if (!reporter) return;
|
|
117
|
+
|
|
118
|
+
// Prevent SIGTERM handler from doing redundant work
|
|
119
|
+
if (reporter.shuttingDown || reporter.closed) return;
|
|
120
|
+
reporter.shuttingDown = true;
|
|
121
|
+
|
|
122
|
+
// 1. Wait for pending writes to complete
|
|
123
|
+
if (reporter._waitForPendingWrites) {
|
|
124
|
+
await reporter._waitForPendingWrites(timeoutMs);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// 2. Final flush of any buffered traces
|
|
128
|
+
await reporter.flush();
|
|
129
|
+
|
|
130
|
+
// 3. Close socket cleanly
|
|
131
|
+
reporter.close();
|
|
132
|
+
}
|
|
133
|
+
|
|
95
134
|
// Log initialization
|
|
96
135
|
if (tracer.options.enabled) {
|
|
97
136
|
logger.log('Instrumentation enabled');
|
package/package.json
CHANGED
package/types/instrument.d.ts
CHANGED
|
@@ -111,6 +111,28 @@ export declare const reporter: TraceReporter;
|
|
|
111
111
|
*/
|
|
112
112
|
export declare function flushTraces(): Promise<void>;
|
|
113
113
|
|
|
114
|
+
/**
|
|
115
|
+
* Gracefully shutdown taist - call this before process exit.
|
|
116
|
+
* Waits for pending socket writes to complete and flushes remaining traces.
|
|
117
|
+
*
|
|
118
|
+
* This is the recommended way to ensure all traces are captured when integrating
|
|
119
|
+
* with applications that have their own shutdown hooks (e.g., Directus onShutdown).
|
|
120
|
+
*
|
|
121
|
+
* @param timeoutMs Maximum time to wait for pending writes (default: 2000ms)
|
|
122
|
+
* @returns Promise that resolves when shutdown is complete
|
|
123
|
+
*
|
|
124
|
+
* @example
|
|
125
|
+
* // Directus integration
|
|
126
|
+
* import { gracefulShutdown } from 'taist/instrument';
|
|
127
|
+
*
|
|
128
|
+
* export default defineHook(({ onShutdown }) => {
|
|
129
|
+
* onShutdown(async () => {
|
|
130
|
+
* await gracefulShutdown();
|
|
131
|
+
* });
|
|
132
|
+
* });
|
|
133
|
+
*/
|
|
134
|
+
export declare function gracefulShutdown(timeoutMs?: number): Promise<void>;
|
|
135
|
+
|
|
114
136
|
/**
|
|
115
137
|
* Auto-instrument a module's exports
|
|
116
138
|
* @param moduleExports Module exports object
|