|
| 1 | +import type { |
| 2 | + BaseTransportOptions, |
| 3 | + CheckIn, |
| 4 | + ClientOptions, |
| 5 | + DynamicSamplingContext, |
| 6 | + Event, |
| 7 | + EventHint, |
| 8 | + MonitorConfig, |
| 9 | + SerializedCheckIn, |
| 10 | + Severity, |
| 11 | + SeverityLevel, |
| 12 | + TraceContext, |
| 13 | +} from '@sentry/types'; |
| 14 | +import { eventFromMessage, eventFromUnknownInput, logger, uuid4 } from '@sentry/utils'; |
| 15 | + |
| 16 | +import { BaseClient } from './baseclient'; |
| 17 | +import { createCheckInEnvelope } from './checkin'; |
| 18 | +import { getCurrentHub } from './hub'; |
| 19 | +import type { Scope } from './scope'; |
| 20 | +import { addTracingExtensions, getDynamicSamplingContextFromClient } from './tracing'; |
| 21 | + |
| 22 | +export interface ServerRuntimeClientOptions extends ClientOptions<BaseTransportOptions> { |
| 23 | + platform?: string; |
| 24 | + runtime?: { name: string; version?: string }; |
| 25 | + serverName?: string; |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * The Sentry Server Runtime Client SDK. |
| 30 | + */ |
| 31 | +export class ServerRuntimeClient< |
| 32 | + O extends ClientOptions & ServerRuntimeClientOptions = ServerRuntimeClientOptions, |
| 33 | +> extends BaseClient<O> { |
| 34 | + /** |
| 35 | + * Creates a new Edge SDK instance. |
| 36 | + * @param options Configuration options for this SDK. |
| 37 | + */ |
| 38 | + public constructor(options: O) { |
| 39 | + // Server clients always support tracing |
| 40 | + addTracingExtensions(); |
| 41 | + |
| 42 | + super(options); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * @inheritDoc |
| 47 | + */ |
| 48 | + public eventFromException(exception: unknown, hint?: EventHint): PromiseLike<Event> { |
| 49 | + return Promise.resolve(eventFromUnknownInput(getCurrentHub, this._options.stackParser, exception, hint)); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * @inheritDoc |
| 54 | + */ |
| 55 | + public eventFromMessage( |
| 56 | + message: string, |
| 57 | + // eslint-disable-next-line deprecation/deprecation |
| 58 | + level: Severity | SeverityLevel = 'info', |
| 59 | + hint?: EventHint, |
| 60 | + ): PromiseLike<Event> { |
| 61 | + return Promise.resolve( |
| 62 | + eventFromMessage(this._options.stackParser, message, level, hint, this._options.attachStacktrace), |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Create a cron monitor check in and send it to Sentry. |
| 68 | + * |
| 69 | + * @param checkIn An object that describes a check in. |
| 70 | + * @param upsertMonitorConfig An optional object that describes a monitor config. Use this if you want |
| 71 | + * to create a monitor automatically when sending a check in. |
| 72 | + */ |
| 73 | + public captureCheckIn(checkIn: CheckIn, monitorConfig?: MonitorConfig, scope?: Scope): string { |
| 74 | + const id = checkIn.status !== 'in_progress' && checkIn.checkInId ? checkIn.checkInId : uuid4(); |
| 75 | + if (!this._isEnabled()) { |
| 76 | + __DEBUG_BUILD__ && logger.warn('SDK not enabled, will not capture checkin.'); |
| 77 | + return id; |
| 78 | + } |
| 79 | + |
| 80 | + const options = this.getOptions(); |
| 81 | + const { release, environment, tunnel } = options; |
| 82 | + |
| 83 | + const serializedCheckIn: SerializedCheckIn = { |
| 84 | + check_in_id: id, |
| 85 | + monitor_slug: checkIn.monitorSlug, |
| 86 | + status: checkIn.status, |
| 87 | + release, |
| 88 | + environment, |
| 89 | + }; |
| 90 | + |
| 91 | + if (checkIn.status !== 'in_progress') { |
| 92 | + serializedCheckIn.duration = checkIn.duration; |
| 93 | + } |
| 94 | + |
| 95 | + if (monitorConfig) { |
| 96 | + serializedCheckIn.monitor_config = { |
| 97 | + schedule: monitorConfig.schedule, |
| 98 | + checkin_margin: monitorConfig.checkinMargin, |
| 99 | + max_runtime: monitorConfig.maxRuntime, |
| 100 | + timezone: monitorConfig.timezone, |
| 101 | + }; |
| 102 | + } |
| 103 | + |
| 104 | + const [dynamicSamplingContext, traceContext] = this._getTraceInfoFromScope(scope); |
| 105 | + if (traceContext) { |
| 106 | + serializedCheckIn.contexts = { |
| 107 | + trace: traceContext, |
| 108 | + }; |
| 109 | + } |
| 110 | + |
| 111 | + const envelope = createCheckInEnvelope( |
| 112 | + serializedCheckIn, |
| 113 | + dynamicSamplingContext, |
| 114 | + this.getSdkMetadata(), |
| 115 | + tunnel, |
| 116 | + this.getDsn(), |
| 117 | + ); |
| 118 | + |
| 119 | + __DEBUG_BUILD__ && logger.info('Sending checkin:', checkIn.monitorSlug, checkIn.status); |
| 120 | + void this._sendEnvelope(envelope); |
| 121 | + return id; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * @inheritDoc |
| 126 | + */ |
| 127 | + protected _prepareEvent(event: Event, hint: EventHint, scope?: Scope): PromiseLike<Event | null> { |
| 128 | + if (this._options.platform) { |
| 129 | + event.platform = event.platform || this._options.platform; |
| 130 | + } |
| 131 | + |
| 132 | + if (this._options.runtime) { |
| 133 | + event.contexts = { |
| 134 | + ...event.contexts, |
| 135 | + runtime: (event.contexts || {}).runtime || this._options.runtime, |
| 136 | + }; |
| 137 | + } |
| 138 | + |
| 139 | + if (this._options.serverName) { |
| 140 | + event.server_name = event.server_name || this._options.serverName; |
| 141 | + } |
| 142 | + |
| 143 | + return super._prepareEvent(event, hint, scope); |
| 144 | + } |
| 145 | + |
| 146 | + /** Extract trace information from scope */ |
| 147 | + private _getTraceInfoFromScope( |
| 148 | + scope: Scope | undefined, |
| 149 | + ): [dynamicSamplingContext: Partial<DynamicSamplingContext> | undefined, traceContext: TraceContext | undefined] { |
| 150 | + if (!scope) { |
| 151 | + return [undefined, undefined]; |
| 152 | + } |
| 153 | + |
| 154 | + const span = scope.getSpan(); |
| 155 | + if (span) { |
| 156 | + const samplingContext = span.transaction ? span.transaction.getDynamicSamplingContext() : undefined; |
| 157 | + return [samplingContext, span.getTraceContext()]; |
| 158 | + } |
| 159 | + |
| 160 | + const { traceId, spanId, parentSpanId, dsc } = scope.getPropagationContext(); |
| 161 | + const traceContext: TraceContext = { |
| 162 | + trace_id: traceId, |
| 163 | + span_id: spanId, |
| 164 | + parent_span_id: parentSpanId, |
| 165 | + }; |
| 166 | + if (dsc) { |
| 167 | + return [dsc, traceContext]; |
| 168 | + } |
| 169 | + |
| 170 | + return [getDynamicSamplingContextFromClient(traceId, this, scope), traceContext]; |
| 171 | + } |
| 172 | +} |
0 commit comments