Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 34x 2x 2x 32x 32x 32x 32x 32x 32x 32x 32x 32x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 1x 1x 1x 1x 1x 1x 3x 3x 1x 1x 3x 3x 3x 3x 1x 1x 1x 2x 2x 2x 2x 3x 2x 2x 2x 2x 2x 3x 3x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 1x 1x 4x 4x 4x 4x 4x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 1x 1x 2x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 6x 6x 6x 6x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 1x 1x 33x 33x 33x 33x 33x 33x 33x 33x 1x 1x 1x | import type { LightConfig, LightClient, LightPlugin, DSNInfo, SentryEvent, EventLevel, UserInfo, Breadcrumb, ErrorEvent, TransactionContext, SpanContext, Transaction, Span } from '../types';
import { parseDSN } from '../utils/dsn';
import { now } from '../utils/helper';
import { computeFingerprint } from '../utils/hash';
import { parseStackTrace } from '../utils/stacktrace';
import { logDebug, logError, setDebug } from '../utils/logger';
import { setAnonymousId as setStoredAnonymousId } from '../utils/identity';
import { ConfigManager } from './ConfigManager';
import { EventBus } from './EventBus';
import { EventQueue } from './EventQueue';
import { Reporter } from './Reporter';
import { PluginManager } from './PluginManager';
import { TracingManager } from './TracingManager';
class Client implements LightClient {
dsn: DSNInfo;
private configManager: ConfigManager;
private eventBus: EventBus;
private queue: EventQueue;
private reporter: Reporter;
private pluginManager: PluginManager;
private tracingManager: TracingManager;
private breadcrumbs: Breadcrumb[] = [];
private enabled: boolean = true;
private get maxBreadcrumbs(): number {
return this.configManager.get('maxBreadcrumbs') ?? 20;
}
get config(): LightConfig {
return this.configManager.getAll();
}
constructor(config: LightConfig) {
if (!config.dsn) {
throw new Error('DSN is required');
}
setDebug(config.debug === true);
logDebug('Client', 'Initializing SDK', { dsn: config.dsn, debug: config.debug, enabled: config.enabled !== false });
this.dsn = parseDSN(config.dsn);
this.configManager = new ConfigManager(config);
this.eventBus = new EventBus();
this.reporter = new Reporter(
this.dsn,
this.configManager.get('maxRetries') ?? 3,
this.configManager.get('retryDelay') ?? 1000
);
this.pluginManager = new PluginManager(this);
this.tracingManager = new TracingManager(config, this, this.dsn.publicKey);
this.queue = new EventQueue(
this.configManager.get('maxQueueSize') ?? 100,
this.configManager.get('flushInterval') ?? 5000,
this.eventBus,
async (events) => this.flushEvents(events),
(events) => this.syncFlushEvents(events)
);
this.enabled = this.configManager.get('enabled') !== false;
logDebug('Client', 'SDK initialized', { enabled: this.enabled });
}
init(): void {
try {
if (!this.enabled) return;
this.queue.start();
this.emit('ready');
} catch (e) {
logError('Client', 'init failed', e);
}
}
private async flushEvents(events: SentryEvent[]): Promise<void> {
logDebug('Client', 'flushEvents start', { eventCount: events.length });
const processedEvents: SentryEvent[] = [];
for (const event of events) {
const processed = this.pluginManager.applyBeforeReport(event);
if (processed) {
processedEvents.push(processed);
} else {
logDebug('Client', 'Event filtered by beforeReport plugin', { type: event.type, message: 'message' in event ? event.message : undefined });
}
}
if (processedEvents.length === 0) {
logDebug('Client', 'flushEvents: all events filtered by beforeReport');
return;
}
let isFirstInBatch = true;
const finalEvents: SentryEvent[] = [];
for (const event of processedEvents) {
const withConfig = this.configManager.applyToEvent(event, isFirstInBatch);
isFirstInBatch = false;
const beforeSend = this.configManager.get('beforeSend');
const afterBeforeSend = beforeSend ? beforeSend(withConfig) : withConfig;
if (!afterBeforeSend) {
logDebug('Client', 'Event filtered by beforeSend', { type: event.type });
continue;
}
finalEvents.push(afterBeforeSend);
}
if (finalEvents.length === 0) {
logDebug('Client', 'flushEvents: all events filtered by beforeSend');
return;
}
logDebug('Client', 'Reporting events', { count: finalEvents.length });
await this.reporter.report(finalEvents);
logDebug('Client', 'Report completed successfully');
this.configManager.markContextReported();
for (const event of finalEvents) {
this.pluginManager.applyAfterReport(event);
}
}
private syncFlushEvents(events: SentryEvent[]): void {
const processedEvents: SentryEvent[] = [];
for (const event of events) {
const processed = this.pluginManager.applyBeforeReport(event);
if (processed) {
processedEvents.push(processed);
}
}
if (processedEvents.length === 0) return;
let isFirstInBatch = true;
const finalEvents: SentryEvent[] = [];
for (const event of processedEvents) {
const withConfig = this.configManager.applyToEvent(event, isFirstInBatch);
isFirstInBatch = false;
const beforeSend = this.configManager.get('beforeSend');
const afterBeforeSend = beforeSend ? beforeSend(withConfig) : withConfig;
if (!afterBeforeSend) continue;
finalEvents.push(afterBeforeSend);
}
if (finalEvents.length === 0) return;
this.reporter.reportSync(finalEvents);
this.configManager.markContextReported();
}
on(event: string, handler: (...args: unknown[]) => void): void {
this.eventBus.on(event, handler);
}
off(event: string, handler: (...args: unknown[]) => void): void {
this.eventBus.off(event, handler);
}
emit(event: string, ...args: unknown[]): void {
this.eventBus.emit(event, ...args);
}
captureException(error: Error | unknown): void {
try {
logDebug('Client', 'captureException called', { error: error instanceof Error ? error.message : String(error) });
if (!this.enabled) {
logDebug('Client', 'captureException skipped: SDK disabled');
return;
}
const errorEvent = this.buildErrorEvent(error);
logDebug('Client', 'Error event built', { type: errorEvent.type, message: errorEvent.message, fingerprint: (errorEvent as ErrorEvent).fingerprint });
if (this.configManager.isIgnoredError(errorEvent.message)) {
logDebug('Client', 'captureException skipped: error ignored by ignoreErrors');
return;
}
this.attachTraceContext(errorEvent);
errorEvent.breadcrumbs = [...this.breadcrumbs];
logDebug('Client', 'Enqueue error event', { breadcrumbs: this.breadcrumbs.length });
this.queue.enqueue(errorEvent);
} catch (e) {
logError('Client', 'captureException failed', e);
}
}
captureMessage(message: string, level: EventLevel = 'info'): void {
try {
if (!this.enabled) return;
const event: ErrorEvent = {
type: 'error',
level,
message,
timestamp: now(),
breadcrumbs: [...this.breadcrumbs],
};
if (this.configManager.isIgnoredError(message)) return;
this.attachTraceContext(event);
this.queue.enqueue(event);
} catch (e) {
logError('Client', 'captureMessage failed', e);
}
}
captureEvent(event: Partial<SentryEvent> & { type: string }): void {
try {
if (!this.enabled) return;
const fullEvent = {
timestamp: now(),
level: 'info' as EventLevel,
breadcrumbs: [...this.breadcrumbs],
...event,
} as SentryEvent;
this.attachTraceContext(fullEvent);
this.queue.enqueue(fullEvent);
} catch (e) {
logError('Client', 'captureEvent failed', e);
}
}
private attachTraceContext(event: SentryEvent): void {
const currentSpan = this.tracingManager.getCurrentSpan();
const currentTransaction = this.tracingManager.getCurrentTransaction();
if (currentSpan || currentTransaction) {
const traceId = currentSpan?.traceId || currentTransaction?.traceId;
const spanId = currentSpan?.spanId || currentTransaction?.spanId;
const parentSpanId = currentSpan?.parentSpanId;
const op = currentSpan?.op || currentTransaction?.op;
const status = currentSpan?.status || currentTransaction?.status;
event.contexts = {
...event.contexts,
trace: {
trace_id: traceId || '',
span_id: spanId || '',
parent_span_id: parentSpanId,
op,
status,
},
};
}
}
private buildErrorEvent(error: Error | unknown): ErrorEvent {
const timestamp = now();
if (error instanceof Error) {
const frames = parseStackTrace(error.stack, { maxFrames: 5 });
const fingerprint = computeFingerprint(error.name, error.message, frames);
return {
type: 'error',
level: 'error',
message: error.message,
timestamp,
exception: {
type: error.name,
value: error.message,
stacktrace: {
frames,
},
},
fingerprint,
};
}
const message = String(error);
return {
type: 'error',
level: 'error',
message,
timestamp,
};
}
setUser(user: UserInfo | null): void {
try {
this.configManager.setUser(user);
this.tracingManager.updateConfig(this.configManager.getAll());
} catch (e) {
logError('Client', 'setUser failed', e);
}
}
setAnonymousId(id: string): void {
try {
setStoredAnonymousId(id);
} catch (e) {
logError('Client', 'setAnonymousId failed', e);
}
}
startTransaction(context: TransactionContext): Transaction | null {
try {
if (!this.enabled) return null;
const transaction = this.tracingManager.startTransaction(context);
if (transaction && transaction.sampled) {
logDebug('Client', 'Transaction started and sampled', {
name: transaction.name,
traceId: transaction.traceId,
});
}
return transaction;
} catch (e) {
logError('Client', 'startTransaction failed', e);
return null;
}
}
getCurrentTransaction(): Transaction | null {
try {
return this.tracingManager.getCurrentTransaction();
} catch (e) {
logError('Client', 'getCurrentTransaction failed', e);
return null;
}
}
getCurrentSpan(): Span | null {
try {
return this.tracingManager.getCurrentSpan();
} catch (e) {
logError('Client', 'getCurrentSpan failed', e);
return null;
}
}
setSpan(span: Span | null): void {
try {
this.tracingManager.setCurrentSpan(span);
} catch (e) {
logError('Client', 'setSpan failed', e);
}
}
startSpan(context: SpanContext): Span | null {
try {
if (!this.enabled) return null;
return this.tracingManager.startSpan(context);
} catch (e) {
logError('Client', 'startSpan failed', e);
return null;
}
}
setTag(key: string, value: string): void {
try {
this.configManager.setTag(key, value);
} catch (e) {
logError('Client', 'setTag failed', e);
}
}
setTags(tags: Record<string, string>): void {
try {
this.configManager.setTags(tags);
} catch (e) {
logError('Client', 'setTags failed', e);
}
}
setExtra(key: string, value: unknown): void {
try {
this.configManager.setExtra(key, value);
} catch (e) {
logError('Client', 'setExtra failed', e);
}
}
addBreadcrumb(breadcrumb: Omit<Breadcrumb, 'timestamp'>): void {
try {
const fullBreadcrumb: Breadcrumb = {
...breadcrumb,
timestamp: now(),
};
this.breadcrumbs.push(fullBreadcrumb);
if (this.breadcrumbs.length > this.maxBreadcrumbs) {
this.breadcrumbs.shift();
}
} catch (e) {
logError('Client', 'addBreadcrumb failed', e);
}
}
async flush(): Promise<void> {
try {
await this.queue.flush();
} catch (e) {
logError('Client', 'flush failed', e);
}
}
disable(): void {
try {
this.enabled = false;
} catch (e) {
logError('Client', 'disable failed', e);
}
}
enable(): void {
try {
this.enabled = true;
} catch (e) {
logError('Client', 'enable failed', e);
}
}
use(plugin: LightPlugin): void {
try {
this.pluginManager.add(plugin);
} catch (e) {
logError('Client', 'use plugin failed', e);
}
}
destroy(): void {
try {
this.pluginManager.destroy();
this.tracingManager.destroy();
this.queue.destroy();
this.eventBus.destroy();
this.breadcrumbs = [];
} catch (e) {
logError('Client', 'destroy failed', e);
}
}
}
export default Client;
|