UNPKG

@sentry/node

Version:

Sentry Node SDK using OpenTelemetry for performance instrumentation

101 lines (100 loc) • 4.18 kB
import { DEBUG_BUILD } from "../debug-build.js"; import { SERVER_ADDRESS } from "@sentry/conventions/attributes"; import { SDK_VERSION, _INTERNAL_clearAiProviderSkips, _INTERNAL_setDeferSegmentSpanCapture, applySdkMetadata, debug } from "@sentry/core"; import { ServerRuntimeClient } from "@sentry/core/server"; import { trace } from "@opentelemetry/api"; import { setAsyncLocalStorageAsyncContextStrategy } from "@sentry/server-utils"; import * as os from "node:os"; import { isMainThread, threadId } from "worker_threads"; import { registerPrepareSpanScope, setOpenTelemetryContextAsyncContextStrategy } from "@sentry/opentelemetry"; import { registerDiagnosticsChannelInjection } from "@sentry/server-runtime-injection/register"; // src/sdk/client.ts const DEFAULT_CLIENT_REPORT_FLUSH_INTERVAL_MS = 6e4; /** A client for using Sentry with Node & OpenTelemetry. */ var NodeClient = class extends ServerRuntimeClient { constructor(options) { const serverName = options.includeServerName === false ? void 0 : options.serverName || global.process.env.SENTRY_NAME || os.hostname(); const clientOptions = { ...options, platform: "node", runtime: options.runtime || { name: "node", version: global.process.version }, serverName }; applySdkMetadata(clientOptions, "node"); debug.log(`Initializing Sentry: process: ${process.pid}, thread: ${isMainThread ? "main" : `worker-${threadId}`}.`); super(clientOptions); this._onExitFlushListener = () => { this.emit("flush"); }; if (serverName) this.on("beforeCaptureLog", (log) => { log.attributes = { ...log.attributes, [SERVER_ADDRESS]: serverName }; }); process.on("beforeExit", this._onExitFlushListener); _INTERNAL_setDeferSegmentSpanCapture(this); registerPrepareSpanScope(this); if ((typeof __SENTRY_CHANNEL_INJECTION__ === "undefined" || __SENTRY_CHANNEL_INJECTION__) && options.enableRuntimeChannelInjection !== false) registerDiagnosticsChannelInjection(); } /** @inheritDoc */ init() { if (this.getOptions().enableOpenTelemetrySetup) this.asyncLocalStorageLookup = setOpenTelemetryContextAsyncContextStrategy(); else this.asyncLocalStorageLookup = { asyncLocalStorage: setAsyncLocalStorageAsyncContextStrategy() }; super.init(); } /** Get the OTEL tracer. */ get tracer() { if (this._tracer) return this._tracer; const name = "@sentry/node"; const version = SDK_VERSION; const tracer = trace.getTracer(name, version); this._tracer = tracer; return tracer; } /** @inheritDoc */ async flush(timeout) { await this.traceProvider?.forceFlush(); if (this.getOptions().sendClientReports) this._flushOutcomes(); return super.flush(timeout); } /** @inheritDoc */ async close(timeout) { if (this._clientReportInterval) clearInterval(this._clientReportInterval); if (this._clientReportOnExitFlushListener) process.off("beforeExit", this._clientReportOnExitFlushListener); if (this._onExitFlushListener) process.off("beforeExit", this._onExitFlushListener); const allEventsSent = await super.close(timeout); if (this.traceProvider) await this.traceProvider.shutdown(); return allEventsSent; } /** * Will start tracking client reports for this client. * * NOTICE: This method will create an interval that is periodically called and attach a `process.on('beforeExit')` * hook. To clean up these resources, call `.close()` when you no longer intend to use the client. Not doing so will * result in a memory leak. */ startClientReportTracking() { const clientOptions = this.getOptions(); if (clientOptions.sendClientReports) { this._clientReportOnExitFlushListener = () => { this._flushOutcomes(); }; this._clientReportInterval = setInterval(() => { DEBUG_BUILD && debug.log("Flushing client reports based on interval."); this._flushOutcomes(); }, clientOptions.clientReportFlushInterval ?? DEFAULT_CLIENT_REPORT_FLUSH_INTERVAL_MS).unref(); process.on("beforeExit", this._clientReportOnExitFlushListener); } } /** @inheritDoc */ _setupIntegrations() { _INTERNAL_clearAiProviderSkips(); super._setupIntegrations(); } }; export { NodeClient }; //# sourceMappingURL=client.js.map