light-sentry-sdk/dist/types/index.d.ts

303 lines
8.4 KiB
TypeScript

export type EventLevel = 'fatal' | 'error' | 'warning' | 'info' | 'debug';
export interface ContextLevelConfig {
maxStackFrames: number;
maxBreadcrumbs: number;
}
export type ContextLevelMap = Record<EventLevel, ContextLevelConfig>;
export interface UserInfo {
id?: string;
username?: string;
email?: string;
[key: string]: unknown;
}
export interface StackFrame {
filename: string;
function?: string;
lineno?: number;
colno?: number;
in_app?: boolean;
}
export interface ExceptionInfo {
type: string;
value: string;
stacktrace?: {
frames: StackFrame[];
};
}
export interface RequestInfo {
url: string;
method?: string;
headers?: Record<string, string>;
referrer?: string;
}
export interface BrowserContext {
name: string;
version?: string;
}
export interface OSContext {
name: string;
version?: string;
}
export interface DeviceContext {
family?: string;
model?: string;
brand?: string;
type?: string;
}
export interface TraceContext {
trace_id: string;
span_id: string;
parent_span_id?: string;
op?: string;
status?: string;
description?: string;
}
export interface BaggageFields {
trace_id?: string;
public_key?: string;
sample_rate?: string;
sampled?: string;
user_id?: string;
user_segment?: string;
release?: string;
environment?: string;
replay_id?: string;
[key: string]: string | undefined;
}
export interface Contexts {
browser?: BrowserContext;
os?: OSContext;
device?: DeviceContext;
trace?: TraceContext;
}
export interface Breadcrumb {
type: string;
message: string;
timestamp: number;
category?: string;
data?: Record<string, unknown>;
level?: EventLevel;
}
export interface BaseEvent {
type: string;
level: EventLevel;
timestamp: number;
release?: string;
environment?: string;
user?: UserInfo;
anonymousId?: string;
tags?: Record<string, string>;
extra?: Record<string, unknown>;
breadcrumbs?: Breadcrumb[];
request?: RequestInfo;
contexts?: Contexts;
context_id?: string;
}
export interface ErrorEvent extends BaseEvent {
type: 'error';
message: string;
exception?: ExceptionInfo;
fingerprint?: string;
title?: string;
}
export interface PerformanceEvent extends BaseEvent {
type: 'performance';
metric: string;
value: number;
unit: string;
rating?: 'good' | 'needs-improvement' | 'poor';
}
export interface NetworkEvent extends BaseEvent {
type: 'network';
sub_type: 'fetch' | 'xhr';
method: string;
url: string;
status_code?: number;
duration?: number;
request_size?: number;
response_size?: number;
success: boolean;
error?: string;
}
export interface BehaviorEvent extends BaseEvent {
type: 'behavior';
sub_type: 'pv' | 'click' | 'route' | 'scroll' | 'duration';
page_url: string;
referrer?: string;
properties?: Record<string, unknown>;
}
export interface CountEvent extends BaseEvent {
type: 'count';
fingerprint: string;
count: number;
ts_start: number;
ts_end: number;
}
export type SpanStatus = 'ok' | 'cancelled' | 'unknown' | 'unknown_error' | 'invalid_argument' | 'deadline_exceeded' | 'not_found' | 'already_exists' | 'permission_denied' | 'resource_exhausted' | 'failed_precondition' | 'aborted' | 'out_of_range' | 'unimplemented' | 'internal_error' | 'unavailable' | 'data_loss' | 'unauthenticated';
export interface SpanContext {
op?: string;
description?: string;
startTimestamp?: number;
status?: SpanStatus;
tags?: Record<string, string>;
data?: Record<string, unknown>;
traceId?: string;
spanId?: string;
parentSpanId?: string;
sampled?: boolean;
}
export interface SamplingContext extends TransactionContext {
parentSampled?: boolean;
}
export interface TransactionContext extends SpanContext {
name: string;
type?: string;
}
export interface SpanJSON {
trace_id: string;
span_id: string;
parent_span_id?: string;
op?: string;
description?: string;
status?: SpanStatus;
tags?: Record<string, string>;
data?: Record<string, unknown>;
start_timestamp: number;
timestamp: number;
}
export interface TransactionJSON extends SpanJSON {
transaction: string;
type?: string;
spans: SpanJSON[];
sampled?: boolean;
}
export interface Span {
readonly traceId: string;
readonly spanId: string;
readonly parentSpanId?: string;
op?: string;
description?: string;
status?: SpanStatus;
tags: Record<string, string>;
data: Record<string, unknown>;
readonly startTimestamp: number;
readonly endTimestamp?: number;
readonly transaction?: Transaction;
setTag(key: string, value: string): this;
setData(key: string, value: unknown): this;
setStatus(status: SpanStatus): this;
setHttpStatus(statusCode: number): this;
setStatusFromResponse(status: string | number | undefined, tagKey?: string): this;
startChild(context?: SpanContext): Span;
finish(endTimestamp?: number): void;
toTraceparent(): string;
toJSON(): SpanJSON;
}
export interface Transaction extends Omit<Span, 'toJSON'> {
name: string;
type?: string;
spans: Span[];
sampled?: boolean;
maxSpans?: number;
toJSON(): TransactionJSON;
toEvent(): TransactionEvent;
}
export interface MeasurementValue {
value: number;
unit?: string;
rating?: 'good' | 'needs-improvement' | 'poor';
}
export interface TransactionEvent extends BaseEvent {
type: 'transaction';
transaction: string;
transaction_info?: {
source: string;
};
spans: Array<ReturnType<Span['toJSON']>>;
start_timestamp: number;
measurements?: Record<string, MeasurementValue>;
}
export type SentryEvent = ErrorEvent | PerformanceEvent | NetworkEvent | BehaviorEvent | CountEvent | TransactionEvent;
export interface DSNInfo {
protocol: string;
publicKey: string;
host: string;
projectId: string;
}
export interface SamplingConfig {
rates?: {
error?: number;
performance?: number;
network?: number;
behavior?: number;
[key: string]: number | undefined;
};
}
export interface LightConfig {
dsn: string;
release?: string;
environment?: string;
enabled?: boolean;
debug?: boolean;
sampleRate?: number;
tracesSampleRate?: number;
tracesSampler?: (context: SamplingContext) => number | boolean;
/**
* @deprecated 请使用 tracePropagationTargets 替代
*/
tracingOrigins?: (string | RegExp)[];
tracePropagationTargets?: (string | RegExp)[];
maxQueueSize?: number;
flushInterval?: number;
maxRetries?: number;
retryDelay?: number;
ignoreErrors?: (string | RegExp)[];
ignoreUrls?: (string | RegExp)[];
includePaths?: (string | RegExp)[];
maxBreadcrumbs?: number;
beforeSend?: (event: SentryEvent) => SentryEvent | null;
user?: UserInfo;
trackAnonymousUsers?: boolean;
plugins?: (LightPlugin | string)[];
contextLevel?: Partial<ContextLevelMap>;
sampling?: SamplingConfig;
[pluginName: string]: unknown;
}
export interface LightPlugin {
name: string;
version: string;
setup(client: LightClient): void;
destroy?(): void;
beforeReport?(event: SentryEvent): SentryEvent | null;
afterReport?(event: SentryEvent): void;
}
export interface LightClient {
config: LightConfig;
dsn: DSNInfo;
on(event: string, handler: (...args: unknown[]) => void): void;
off(event: string, handler: (...args: unknown[]) => void): void;
emit(event: string, ...args: unknown[]): void;
captureException(error: Error | unknown): void;
captureMessage(message: string, level?: EventLevel): void;
captureEvent(event: Partial<SentryEvent> & {
type: string;
}): void;
setUser(user: UserInfo | null): void;
setAnonymousId(id: string): void;
setTag(key: string, value: string): void;
setTags(tags: Record<string, string>): void;
setExtra(key: string, value: unknown): void;
addBreadcrumb(breadcrumb: Omit<Breadcrumb, 'timestamp'>): void;
startTransaction(context: TransactionContext): Transaction | null;
getCurrentTransaction(): Transaction | null;
getCurrentSpan(): Span | null;
setSpan(span: Span | null): void;
startSpan(context: SpanContext): Span | null;
flush(): Promise<void>;
disable(): void;
enable(): void;
use(plugin: LightPlugin): void;
init(): void;
destroy(): void;
}