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 | 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 72x 72x 1x 1x 1x 1x 1x 1x 172x 172x 1x 1x 6x 6x 1x 1x 1x 1x 1x 1x 5x 5x 1x 1x 3x 3x 1x 1x 4x 4x 4x 4x 4x 1x 1x 3x 3x 3x 3x 3x 1x 1x 7x 7x 1x 1x 2x 2x 1x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 21x 21x 21x 21x 21x 1x 1x 21x 21x 21x 1x 1x 21x 21x 21x 3x 3x 3x 3x 3x 3x 3x 3x 3x 21x 21x 21x 3x 3x 21x 21x 21x 1x 1x 21x 21x 21x 21x 21x 8x 8x 21x 21x 21x 21x 21x 9x 9x 21x 8x 8x 21x 21x 20x 20x 20x 20x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 10x 10x 10x 10x 10x 21x 21x 21x 21x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 21x 21x 21x 1x 1x 1x 1x 1x 1x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 3x 3x 3x 3x 3x 1x 1x 3x 2x 2x 2x 2x 2x 2x 2x 2x 3x 3x 3x 21x 21x 21x 3x 1x 1x 3x 2x 2x 3x 21x 21x 21x 1x | import type { LightConfig, SentryEvent, UserInfo, ContextLevelMap, ErrorEvent, StackFrame, Breadcrumb, EventLevel } from '../types';
import { getContexts, getContextId, getPageUrl, getReferrer } from '../utils/env';
import { matchPatterns } from '../utils/helper';
import { getAnonymousId, isAnonymousTrackingEnabled } from '../utils/identity';
const DEFAULT_CONFIG: Partial<LightConfig> = {
enabled: true,
sampleRate: 1,
maxQueueSize: 100,
flushInterval: 5000,
maxRetries: 3,
retryDelay: 1000,
environment: 'production',
// 上下文分层策略:按事件级别决定堆栈深度和 breadcrumbs 数量
contextLevel: {
fatal: { maxStackFrames: 5, maxBreadcrumbs: 20 },
error: { maxStackFrames: 5, maxBreadcrumbs: 10 },
warning: { maxStackFrames: 3, maxBreadcrumbs: 5 },
info: { maxStackFrames: 0, maxBreadcrumbs: 0 },
debug: { maxStackFrames: 0, maxBreadcrumbs: 0 },
},
};
export class ConfigManager {
private config: LightConfig;
private contexts: ReturnType<typeof getContexts> | null = null;
private contextId: string | null = null;
private contextReported: boolean = false;
constructor(config: LightConfig) {
this.config = { ...DEFAULT_CONFIG, ...config } as LightConfig;
}
markContextReported(): void {
this.contextReported = true;
}
get<K extends keyof LightConfig>(key: K): LightConfig[K] {
return this.config[key];
}
getAll(): LightConfig {
return { ...this.config };
}
set<K extends keyof LightConfig>(key: K, value: LightConfig[K]): void {
this.config[key] = value;
}
setUser(user: UserInfo | null): void {
this.config.user = user || undefined;
}
setTags(tags: Record<string, string>): void {
this.config.tags = { ...(this.config.tags || {}), ...tags };
}
setTag(key: string, value: string): void {
if (!this.config.tags) {
this.config.tags = {};
}
(this.config.tags as Record<string, string>)[key] = value;
}
setExtra(key: string, value: unknown): void {
if (!this.config.extra) {
this.config.extra = {};
}
(this.config.extra as Record<string, unknown>)[key] = value;
}
isIgnoredError(message: string): boolean {
return matchPatterns(message, this.config.ignoreErrors || []);
}
shouldSample(): boolean {
const rate = this.config.sampleRate ?? 1;
if (rate >= 1) return true;
if (rate <= 0) return false;
return Math.random() < rate;
}
/**
* 获取编码后的环境信息(延迟初始化,只在首次上报时计算)
* 注意:env.ts 的 getContexts 已经返回编码后的数据
*/
private getEncodedContext(): ReturnType<typeof getContexts> {
if (!this.contexts) {
this.contexts = getContexts();
}
return this.contexts;
}
/**
* 将配置应用到事件
* @param event 事件对象
* @param includeFullContext 是否包含完整环境信息(批次内只有首个事件需要)
*/
applyToEvent(event: SentryEvent, includeFullContext: boolean = true): SentryEvent {
// 使用 Record 来避免联合类型的问题
const enriched: Record<string, unknown> = { ...event };
// 处理 tags
if (event.tags) {
enriched.tags = { ...event.tags };
}
// 处理 request
if (event.request) {
enriched.request = { ...event.request };
}
// 处理 exception(仅 error 事件有)
if ('exception' in event && event.exception) {
const exc = event.exception;
enriched.exception = {
...exc,
stacktrace: exc.stacktrace ? {
...exc.stacktrace,
frames: exc.stacktrace.frames ? exc.stacktrace.frames.map((f: StackFrame) => ({ ...f })) : undefined,
} : undefined,
};
}
// 处理 breadcrumbs
if (event.breadcrumbs) {
enriched.breadcrumbs = event.breadcrumbs.map((b: Breadcrumb) => ({ ...b }));
}
// 处理 user
if (event.user) {
enriched.user = { ...event.user };
}
// 处理 contexts
if (event.contexts) {
enriched.contexts = { ...event.contexts };
}
if (this.config.release) {
enriched.release = this.config.release;
}
if (this.config.environment) {
enriched.environment = this.config.environment;
}
enriched.platform = 'javascript';
if (this.config.user) {
enriched.user = this.config.user;
}
if (this.config.tags) {
enriched.tags = { ...this.config.tags, ...(event.tags as Record<string, string> || {}) };
}
if (!enriched.request) {
enriched.request = {
url: getPageUrl(),
referrer: getReferrer(),
};
} else if (!(enriched.request as Record<string, unknown>).url) {
(enriched.request as Record<string, unknown>).url = getPageUrl();
}
if (!this.contextId) {
this.contextId = getContextId();
}
enriched.context_id = this.contextId;
// 处理匿名用户ID
// 只有在没有设置 user.id 且启用了匿名追踪时,才添加 anonymousId
const hasUserId = this.config.user?.id || (event.user as Record<string, unknown>)?.id;
if (!hasUserId && isAnonymousTrackingEnabled(this.config)) {
const anonymousId = getAnonymousId();
if (anonymousId) {
enriched.anonymousId = anonymousId;
}
}
const result = this.applyContextLevel(enriched);
if (includeFullContext && !this.contextReported) {
if (!this.contexts) {
this.contexts = getContexts();
}
(result as Record<string, unknown>).contexts = this.contexts;
(result as Record<string, unknown>).env = {
b: this.contexts.browser?.name,
bv: this.contexts.browser?.version,
os: this.contexts.os?.name,
osv: this.contexts.os?.version,
};
}
return result as unknown as SentryEvent;
}
/**
* 应用上下文分层策略:根据事件级别裁剪堆栈和 breadcrumbs
* 注意:此方法不修改原始 event 对象,返回裁剪后的副本
*/
private applyContextLevel(event: Record<string, unknown>): Record<string, unknown> {
const contextLevel = this.config.contextLevel as ContextLevelMap | undefined;
if (!contextLevel) return event;
const level = (event.level || 'error') as EventLevel;
const levelConfig = contextLevel[level] || contextLevel['error'];
// 创建浅拷贝用于裁剪
const enriched: Record<string, unknown> = { ...event };
// 裁剪堆栈帧(仅 error 事件有 exception)
if ('exception' in event && event.exception && typeof event.exception === 'object') {
const exc = event.exception as Record<string, unknown>;
if (exc.stacktrace && typeof exc.stacktrace === 'object') {
const st = exc.stacktrace as Record<string, unknown>;
if (Array.isArray(st.frames)) {
if (levelConfig.maxStackFrames === 0) {
// info/debug 级别不需要堆栈
delete enriched.exception;
} else {
enriched.exception = {
...exc,
stacktrace: {
...st,
frames: (st.frames as StackFrame[]).slice(0, levelConfig.maxStackFrames),
},
};
}
}
}
}
// 裁剪 breadcrumbs
if (event.breadcrumbs && Array.isArray(event.breadcrumbs)) {
if (levelConfig.maxBreadcrumbs === 0) {
// info/debug 级别不需要 breadcrumbs
delete enriched.breadcrumbs;
} else {
enriched.breadcrumbs = (event.breadcrumbs as Breadcrumb[]).slice(-levelConfig.maxBreadcrumbs);
}
}
return enriched;
}
}
|