@sentry/core
Version:
Base implementation for all Sentry JavaScript SDKs
163 lines (162 loc) • 6.29 kB
JavaScript
import { createCheckInEnvelope } from "./checkin.js";
import { Client } from "./client.js";
import { getIsolationScope } from "./currentScopes.js";
import { DEBUG_BUILD } from "./debug-build.js";
import { spanStreamingIntegration } from "./integrations/spanStreaming.js";
import "./transports/base.js";
import { addUserAgentToTransportHeaders } from "./transports/userAgent.js";
import { debug } from "./utils/debug-logger.js";
import { eventFromMessage, eventFromUnknownInput } from "./utils/eventbuilder.js";
import { uuid4 } from "./utils/misc.js";
import { makePromiseBuffer } from "./utils/promisebuffer.js";
import { resolvedSyncPromise } from "./utils/syncpromise.js";
import { _getTraceInfoFromScope } from "./utils/trace-info.js";
import { applyServerTunnelDefaults } from "./utils/tunnelDefaults.js";
import { SERVER_ADDRESS } from "@sentry/conventions/attributes";
// src/server-runtime-client.ts
/**
* The Sentry Server Runtime Client SDK.
*/
var ServerRuntimeClient = class extends Client {
/**
* Creates a new Edge SDK instance.
* @param options Configuration options for this SDK.
*/
constructor(options) {
addUserAgentToTransportHeaders(options);
if ((typeof __SENTRY_TRACING__ === "undefined" || __SENTRY_TRACING__) && options.traceLifecycle !== "static" && !options.integrations.some((i) => i.name === "SpanStreaming")) options.integrations.push(spanStreamingIntegration());
super(options);
this._disposeCallbacks = [];
applyServerTunnelDefaults(this);
this._setUpMetricsProcessing();
}
/**
* @inheritDoc
*/
eventFromException(exception, hint) {
const event = eventFromUnknownInput(this, this._options.stackParser, exception, hint);
event.level = "error";
return resolvedSyncPromise(event);
}
/**
* @inheritDoc
*/
eventFromMessage(message, level = "info", hint) {
return resolvedSyncPromise(eventFromMessage(this._options.stackParser, message, level, hint, this._options.attachStacktrace));
}
/**
* @inheritDoc
*/
captureException(exception, hint, scope) {
setCurrentRequestSessionErroredOrCrashed(hint);
return super.captureException(exception, hint, scope);
}
/**
* @inheritDoc
*/
captureEvent(event, hint, scope) {
if (!event.type && event.exception?.values && event.exception.values.length > 0) setCurrentRequestSessionErroredOrCrashed(hint);
return super.captureEvent(event, hint, scope);
}
/**
* Create a cron monitor check in and send it to Sentry.
*
* @param checkIn An object that describes a check in.
* @param upsertMonitorConfig An optional object that describes a monitor config. Use this if you want
* to create a monitor automatically when sending a check in.
*/
captureCheckIn(checkIn, monitorConfig, scope) {
const id = "checkInId" in checkIn && checkIn.checkInId ? checkIn.checkInId : uuid4();
if (!this._isEnabled()) {
DEBUG_BUILD && debug.warn("SDK not enabled, will not capture check-in.");
return id;
}
const { release, environment, tunnel } = this.getOptions();
const serializedCheckIn = {
check_in_id: id,
monitor_slug: checkIn.monitorSlug,
status: checkIn.status,
release,
environment
};
if ("duration" in checkIn) serializedCheckIn.duration = checkIn.duration;
if (monitorConfig) serializedCheckIn.monitor_config = {
schedule: monitorConfig.schedule,
checkin_margin: monitorConfig.checkinMargin,
max_runtime: monitorConfig.maxRuntime,
timezone: monitorConfig.timezone,
failure_issue_threshold: monitorConfig.failureIssueThreshold,
recovery_threshold: monitorConfig.recoveryThreshold
};
const [dynamicSamplingContext, traceContext] = _getTraceInfoFromScope(this, scope);
if (traceContext) serializedCheckIn.contexts = { trace: traceContext };
const envelope = createCheckInEnvelope(serializedCheckIn, dynamicSamplingContext, this.getSdkMetadata(), tunnel, this.getDsn());
DEBUG_BUILD && debug.log("Sending checkin:", checkIn.monitorSlug, checkIn.status);
this.sendEnvelope(envelope);
return id;
}
/**
* @inheritDoc
*/
registerCleanup(callback) {
this._disposeCallbacks.push(callback);
}
/**
* Disposes of the client and releases all resources.
*
* This method clears all internal state to allow the client to be garbage collected.
* It clears hooks, event processors, integrations, transport, and other internal references.
*
* Call this method after flushing to allow the client to be garbage collected.
* After calling dispose(), the client should not be used anymore.
*
* Subclasses should override this method to clean up their own resources and call `super.dispose()`.
*/
dispose() {
DEBUG_BUILD && debug.log("Disposing client...");
for (const callback of this._disposeCallbacks) try {
callback();
} catch {}
this._disposeCallbacks.length = 0;
for (const hookName of Object.keys(this._hooks)) this._hooks[hookName]?.clear();
this._hooks = {};
this._eventProcessors.length = 0;
this._integrations = {};
this._outcomes = {};
this._transport = void 0;
this._promiseBuffer = makePromiseBuffer(64);
}
/**
* @inheritDoc
*/
_prepareEvent(event, hint, currentScope, isolationScope) {
if (this._options.platform) event.platform = event.platform || this._options.platform;
if (this._options.runtime) event.contexts = {
...event.contexts,
runtime: event.contexts?.runtime || this._options.runtime
};
if (this._options.serverName) event.server_name = event.server_name || this._options.serverName;
return super._prepareEvent(event, hint, currentScope, isolationScope);
}
/**
* Process a server-side metric before it is captured.
*/
_setUpMetricsProcessing() {
this.on("processMetric", (metric) => {
if (this._options.serverName) metric.attributes = {
[SERVER_ADDRESS]: this._options.serverName,
...metric.attributes
};
});
}
};
function setCurrentRequestSessionErroredOrCrashed(eventHint) {
const requestSession = getIsolationScope().getScopeData().sdkProcessingMetadata.requestSession;
if (requestSession) {
const isHandledException = eventHint?.mechanism?.handled ?? true;
if (isHandledException && requestSession.status !== "crashed") requestSession.status = "errored";
else if (!isHandledException) requestSession.status = "crashed";
}
}
export { ServerRuntimeClient };
//# sourceMappingURL=server-runtime-client.js.map