1 |
- {"version":3,"file":"server-runtime-client.js","sources":["../../src/server-runtime-client.ts"],"sourcesContent":["import type {\n BaseTransportOptions,\n CheckIn,\n ClientOptions,\n DynamicSamplingContext,\n Event,\n EventHint,\n MonitorConfig,\n ParameterizedString,\n SerializedCheckIn,\n Severity,\n SeverityLevel,\n TraceContext,\n} from '@sentry/types';\nimport { eventFromMessage, eventFromUnknownInput, logger, resolvedSyncPromise, uuid4 } from '@sentry/utils';\n\nimport { BaseClient } from './baseclient';\nimport { createCheckInEnvelope } from './checkin';\nimport { DEBUG_BUILD } from './debug-build';\nimport { getClient } from './exports';\nimport { MetricsAggregator } from './metrics/aggregator';\nimport type { Scope } from './scope';\nimport { SessionFlusher } from './sessionflusher';\nimport {\n addTracingExtensions,\n getDynamicSamplingContextFromClient,\n getDynamicSamplingContextFromSpan,\n} from './tracing';\nimport { getRootSpan } from './utils/getRootSpan';\nimport { spanToTraceContext } from './utils/spanUtils';\n\nexport interface ServerRuntimeClientOptions extends ClientOptions<BaseTransportOptions> {\n platform?: string;\n runtime?: { name: string; version?: string };\n serverName?: string;\n}\n\n/**\n * The Sentry Server Runtime Client SDK.\n */\nexport class ServerRuntimeClient<\n O extends ClientOptions & ServerRuntimeClientOptions = ServerRuntimeClientOptions,\n> extends BaseClient<O> {\n protected _sessionFlusher: SessionFlusher | undefined;\n\n /**\n * Creates a new Edge SDK instance.\n * @param options Configuration options for this SDK.\n */\n public constructor(options: O) {\n // Server clients always support tracing\n addTracingExtensions();\n\n super(options);\n\n if (options._experiments && options._experiments['metricsAggregator']) {\n this.metricsAggregator = new MetricsAggregator(this);\n }\n }\n\n /**\n * @inheritDoc\n */\n public eventFromException(exception: unknown, hint?: EventHint): PromiseLike<Event> {\n return resolvedSyncPromise(eventFromUnknownInput(getClient(), this._options.stackParser, exception, hint));\n }\n\n /**\n * @inheritDoc\n */\n public eventFromMessage(\n message: ParameterizedString,\n // eslint-disable-next-line deprecation/deprecation\n level: Severity | SeverityLevel = 'info',\n hint?: EventHint,\n ): PromiseLike<Event> {\n return resolvedSyncPromise(\n eventFromMessage(this._options.stackParser, message, level, hint, this._options.attachStacktrace),\n );\n }\n\n /**\n * @inheritDoc\n */\n // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types\n public captureException(exception: any, hint?: EventHint, scope?: Scope): string | undefined {\n // Check if the flag `autoSessionTracking` is enabled, and if `_sessionFlusher` exists because it is initialised only\n // when the `requestHandler` middleware is used, and hence the expectation is to have SessionAggregates payload\n // sent to the Server only when the `requestHandler` middleware is used\n if (this._options.autoSessionTracking && this._sessionFlusher && scope) {\n const requestSession = scope.getRequestSession();\n\n // Necessary checks to ensure this is code block is executed only within a request\n // Should override the status only if `requestSession.status` is `Ok`, which is its initial stage\n if (requestSession && requestSession.status === 'ok') {\n requestSession.status = 'errored';\n }\n }\n\n return super.captureException(exception, hint, scope);\n }\n\n /**\n * @inheritDoc\n */\n public captureEvent(event: Event, hint?: EventHint, scope?: Scope): string | undefined {\n // Check if the flag `autoSessionTracking` is enabled, and if `_sessionFlusher` exists because it is initialised only\n // when the `requestHandler` middleware is used, and hence the expectation is to have SessionAggregates payload\n // sent to the Server only when the `requestHandler` middleware is used\n if (this._options.autoSessionTracking && this._sessionFlusher && scope) {\n const eventType = event.type || 'exception';\n const isException =\n eventType === 'exception' && event.exception && event.exception.values && event.exception.values.length > 0;\n\n // If the event is of type Exception, then a request session should be captured\n if (isException) {\n const requestSession = scope.getRequestSession();\n\n // Ensure that this is happening within the bounds of a request, and make sure not to override\n // Session Status if Errored / Crashed\n if (requestSession && requestSession.status === 'ok') {\n requestSession.status = 'errored';\n }\n }\n }\n\n return super.captureEvent(event, hint, scope);\n }\n\n /**\n *\n * @inheritdoc\n */\n public close(timeout?: number): PromiseLike<boolean> {\n if (this._sessionFlusher) {\n this._sessionFlusher.close();\n }\n return super.close(timeout);\n }\n\n /** Method that initialises an instance of SessionFlusher on Client */\n public initSessionFlusher(): void {\n const { release, environment } = this._options;\n if (!release) {\n DEBUG_BUILD && logger.warn('Cannot initialise an instance of SessionFlusher if no release is provided!');\n } else {\n this._sessionFlusher = new SessionFlusher(this, {\n release,\n environment,\n });\n }\n }\n\n /**\n * Create a cron monitor check in and send it to Sentry.\n *\n * @param checkIn An object that describes a check in.\n * @param upsertMonitorConfig An optional object that describes a monitor config. Use this if you want\n * to create a monitor automatically when sending a check in.\n */\n public captureCheckIn(checkIn: CheckIn, monitorConfig?: MonitorConfig, scope?: Scope): string {\n const id = 'checkInId' in checkIn && checkIn.checkInId ? checkIn.checkInId : uuid4();\n if (!this._isEnabled()) {\n DEBUG_BUILD && logger.warn('SDK not enabled, will not capture checkin.');\n return id;\n }\n\n const options = this.getOptions();\n const { release, environment, tunnel } = options;\n\n const serializedCheckIn: SerializedCheckIn = {\n check_in_id: id,\n monitor_slug: checkIn.monitorSlug,\n status: checkIn.status,\n release,\n environment,\n };\n\n if ('duration' in checkIn) {\n serializedCheckIn.duration = checkIn.duration;\n }\n\n if (monitorConfig) {\n serializedCheckIn.monitor_config = {\n schedule: monitorConfig.schedule,\n checkin_margin: monitorConfig.checkinMargin,\n max_runtime: monitorConfig.maxRuntime,\n timezone: monitorConfig.timezone,\n };\n }\n\n const [dynamicSamplingContext, traceContext] = this._getTraceInfoFromScope(scope);\n if (traceContext) {\n serializedCheckIn.contexts = {\n trace: traceContext,\n };\n }\n\n const envelope = createCheckInEnvelope(\n serializedCheckIn,\n dynamicSamplingContext,\n this.getSdkMetadata(),\n tunnel,\n this.getDsn(),\n );\n\n DEBUG_BUILD && logger.info('Sending checkin:', checkIn.monitorSlug, checkIn.status);\n\n // _sendEnvelope should not throw\n // eslint-disable-next-line @typescript-eslint/no-floating-promises\n this._sendEnvelope(envelope);\n\n return id;\n }\n\n /**\n * Method responsible for capturing/ending a request session by calling `incrementSessionStatusCount` to increment\n * appropriate session aggregates bucket\n */\n protected _captureRequestSession(): void {\n if (!this._sessionFlusher) {\n DEBUG_BUILD && logger.warn('Discarded request mode session because autoSessionTracking option was disabled');\n } else {\n this._sessionFlusher.incrementSessionStatusCount();\n }\n }\n\n /**\n * @inheritDoc\n */\n protected _prepareEvent(\n event: Event,\n hint: EventHint,\n scope?: Scope,\n isolationScope?: Scope,\n ): PromiseLike<Event | null> {\n if (this._options.platform) {\n event.platform = event.platform || this._options.platform;\n }\n\n if (this._options.runtime) {\n event.contexts = {\n ...event.contexts,\n runtime: (event.contexts || {}).runtime || this._options.runtime,\n };\n }\n\n if (this._options.serverName) {\n event.server_name = event.server_name || this._options.serverName;\n }\n\n return super._prepareEvent(event, hint, scope, isolationScope);\n }\n\n /** Extract trace information from scope */\n private _getTraceInfoFromScope(\n scope: Scope | undefined,\n ): [dynamicSamplingContext: Partial<DynamicSamplingContext> | undefined, traceContext: TraceContext | undefined] {\n if (!scope) {\n return [undefined, undefined];\n }\n\n // eslint-disable-next-line deprecation/deprecation\n const span = scope.getSpan();\n if (span) {\n const samplingContext = getRootSpan(span) ? getDynamicSamplingContextFromSpan(span) : undefined;\n return [samplingContext, spanToTraceContext(span)];\n }\n\n const { traceId, spanId, parentSpanId, dsc } = scope.getPropagationContext();\n const traceContext: TraceContext = {\n trace_id: traceId,\n span_id: spanId,\n parent_span_id: parentSpanId,\n };\n if (dsc) {\n return [dsc, traceContext];\n }\n\n return [getDynamicSamplingContextFromClient(traceId, this, scope), traceContext];\n }\n}\n"],"names":["BaseClient","addTracingExtensions","MetricsAggregator","resolvedSyncPromise","eventFromUnknownInput","getClient","eventFromMessage","DEBUG_BUILD","logger","SessionFlusher","uuid4","createCheckInEnvelope","getRootSpan","getDynamicSamplingContextFromSpan","spanToTraceContext","getDynamicSamplingContextFromClient"],"mappings":";;;;;;;;;;;;;;;AAqCA;AACA;AACA;AACO,MAAM,mBAAmB;AAC9B;AACF,SAAUA,qBAAU,CAAI;;AAGxB;AACA;AACA;AACA;AACA,GAAS,WAAW,CAAC,OAAO,EAAK;AACjC;AACA,IAAIC,kCAAoB,EAAE,CAAA;AAC1B;AACA,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