@blueking/open-telemetry 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2 @@
1
+ import { BkOpenTelemetry } from './core/sdk';
2
+ export default BkOpenTelemetry;
@@ -0,0 +1,134 @@
1
+ import type { BkOTPlugin } from './plugin';
2
+ import type { Attributes } from '@opentelemetry/api';
3
+ import type { Instrumentation } from '@opentelemetry/instrumentation';
4
+ export interface BkOTAttributesConfig {
5
+ custom?: () => Attributes;
6
+ error?: () => Attributes;
7
+ metric?: () => Attributes;
8
+ page?: () => Attributes;
9
+ }
10
+ export type BkOTAttributeValue = boolean | number | string;
11
+ export interface BkOTBatchConfig {
12
+ exportTimeoutMillis?: number;
13
+ maxExportBatchSize?: number;
14
+ maxQueueSize?: number;
15
+ scheduledDelayMillis?: number;
16
+ }
17
+ export interface BkOTConfig {
18
+ attributes?: BkOTAttributesConfig;
19
+ autoStart?: boolean;
20
+ debug?: boolean;
21
+ enabled?: boolean;
22
+ endpoint?: string;
23
+ environment?: string;
24
+ headers?: Record<string, string>;
25
+ instrumentations?: Instrumentation[];
26
+ logs?: SignalExporterInputConfig;
27
+ metrics?: SignalExporterInputConfig;
28
+ plugins?: BkOTPlugin[];
29
+ redact?: BkOTRedactConfig;
30
+ rum?: BkOTRumConfig;
31
+ sampleRate?: number;
32
+ signalEndpoints?: SignalEndpointsConfig;
33
+ spanBatch?: BkOTBatchConfig;
34
+ token?: string;
35
+ traces?: SignalExporterInputConfig;
36
+ }
37
+ export interface BkOTCustomEventPayload {
38
+ attributes?: Attributes;
39
+ error?: Error;
40
+ name: string;
41
+ }
42
+ export interface BkOTHttpBodyConfig {
43
+ maxBodySize?: number;
44
+ redact?: (payload: BkOTHttpBodyRedactPayload) => string;
45
+ }
46
+ export interface BkOTHttpBodyRedactPayload {
47
+ body: string;
48
+ contentType?: string;
49
+ method: string;
50
+ status?: number;
51
+ truncated: boolean;
52
+ type: 'request' | 'response';
53
+ url: string;
54
+ }
55
+ export interface BkOTInstrumentationsConfig {
56
+ documentLoad?: boolean;
57
+ fetch?: boolean;
58
+ userInteraction?: boolean | {
59
+ eventNames?: string[];
60
+ };
61
+ xhr?: boolean;
62
+ }
63
+ export interface BkOTRedactConfig {
64
+ attributes?: (attributes: Attributes) => Attributes;
65
+ url?: (url: string) => string;
66
+ }
67
+ export interface BkOTRumConfig extends BkOTInstrumentationsConfig {
68
+ cspViolation?: boolean;
69
+ device?: boolean | {
70
+ storageKey?: string;
71
+ };
72
+ httpBody?: BkOTHttpBodyConfig | boolean;
73
+ pageView?: boolean;
74
+ routeTiming?: boolean;
75
+ websocket?: boolean;
76
+ webVitals?: boolean;
77
+ blankScreen?: {
78
+ checkDelay?: number;
79
+ ignoreSelectors?: string[];
80
+ rootSelector?: string;
81
+ threshold?: number;
82
+ } | boolean;
83
+ error?: {
84
+ maxPerWindow?: number;
85
+ windowMs?: number;
86
+ } | boolean;
87
+ longTask?: {
88
+ threshold?: number;
89
+ } | boolean;
90
+ session?: {
91
+ inactivityMs?: number;
92
+ storageKey?: string;
93
+ } | boolean;
94
+ }
95
+ export interface NormalizedBkOTConfig extends Omit<BkOTConfig, 'attributes' | 'autoStart' | 'debug' | 'enabled' | 'endpoint' | 'environment' | 'instrumentations' | 'logs' | 'metrics' | 'plugins' | 'redact' | 'signalEndpoints' | 'token' | 'traces'> {
96
+ autoStart: boolean;
97
+ debug: boolean;
98
+ enabled: boolean;
99
+ endpoint: string;
100
+ environment: string;
101
+ instrumentations: Instrumentation[];
102
+ logs: SignalExporterConfig;
103
+ metricIntervalMillis: number;
104
+ metrics: SignalExporterConfig;
105
+ plugins: BkOTPlugin[];
106
+ resourceAttributes: Attributes;
107
+ sampleRate: number;
108
+ traces: SignalExporterConfig;
109
+ getCustomAttributes: () => Attributes;
110
+ getErrorAttributes: () => Attributes;
111
+ getMetricAttributes: () => Attributes;
112
+ getPageAttributes: () => Attributes;
113
+ redactAttributes: (attributes: Attributes) => Attributes;
114
+ redactUrl: (url: string) => string;
115
+ commonInstrumentations: Required<Pick<BkOTInstrumentationsConfig, 'documentLoad' | 'fetch' | 'xhr'>> & {
116
+ userInteraction: BkOTInstrumentationsConfig['userInteraction'];
117
+ };
118
+ rum: Omit<Required<BkOTRumConfig>, 'documentLoad' | 'fetch' | 'httpBody' | 'userInteraction' | 'xhr'> & {
119
+ httpBody: false | Required<BkOTHttpBodyConfig>;
120
+ };
121
+ }
122
+ export interface SignalEndpointsConfig {
123
+ logs?: string;
124
+ metrics?: string;
125
+ traces?: string;
126
+ }
127
+ export interface SignalExporterConfig {
128
+ concurrencyLimit?: number;
129
+ endpoint: string;
130
+ headers?: Record<string, string>;
131
+ timeoutMillis?: number;
132
+ }
133
+ export type SignalExporterInputConfig = Partial<SignalExporterConfig>;
134
+ export declare const normalizeConfig: (config: BkOTConfig) => NormalizedBkOTConfig;
@@ -0,0 +1,33 @@
1
+ import type { NormalizedBkOTConfig } from './config';
2
+ import type { Attributes, Meter, Span, Tracer } from '@opentelemetry/api';
3
+ import type { Logger, LogRecord } from '@opentelemetry/api-logs';
4
+ export interface BkOTPlugin {
5
+ enabled?: ((config: NormalizedBkOTConfig) => boolean) | boolean;
6
+ name: string;
7
+ flush?: () => Promise<void> | void;
8
+ init: (context: BkOTRuntimeContext) => Promise<void> | void;
9
+ shutdown?: () => Promise<void> | void;
10
+ }
11
+ export interface BkOTRuntimeContext {
12
+ config: NormalizedBkOTConfig;
13
+ logger: Logger;
14
+ meter: Meter;
15
+ tracer: Tracer;
16
+ /** 对外提供的属性脱敏入口,所有插件在 emit 前都应过一次 */
17
+ applyRedact: (attributes: Attributes) => Attributes;
18
+ /** 包装 logger.emit,自动应用 redact 与 runtime attributes */
19
+ emitLog: (record: LogRecord) => void;
20
+ getRuntimeAttributes: () => Attributes;
21
+ setRuntimeAttributes: (attributes: Attributes) => void;
22
+ startSpan: (name: string, attributes?: Attributes) => Span;
23
+ }
24
+ export declare const isPluginEnabled: (plugin: BkOTPlugin, config: NormalizedBkOTConfig) => boolean;
25
+ export declare const createPlugin: (plugin: BkOTPlugin) => BkOTPlugin;
26
+ export declare class PluginManager {
27
+ private readonly plugins;
28
+ private startedPlugins;
29
+ constructor(plugins: BkOTPlugin[]);
30
+ flush(): Promise<void>;
31
+ shutdown(): Promise<void>;
32
+ start(context: BkOTRuntimeContext): Promise<void>;
33
+ }
@@ -0,0 +1,16 @@
1
+ import type { NormalizedBkOTConfig } from './config';
2
+ import type { Context } from '@opentelemetry/api';
3
+ import type { ReadableSpan, Span, SpanProcessor } from '@opentelemetry/sdk-trace-base';
4
+ /**
5
+ * 包装 SpanProcessor,在 onEnd 阶段过滤指向自身上报 endpoint 的 span,
6
+ * 兜底防止 official fetch/xhr 拦截器对 OTel 自身请求产生回环 span。
7
+ */
8
+ export declare class FilteringSpanProcessor implements SpanProcessor {
9
+ private readonly inner;
10
+ private readonly config;
11
+ constructor(inner: SpanProcessor, config: NormalizedBkOTConfig);
12
+ forceFlush(): Promise<void>;
13
+ onEnd(span: ReadableSpan): void;
14
+ onStart(span: Span, parentContext: Context): void;
15
+ shutdown(): Promise<void>;
16
+ }
@@ -0,0 +1,9 @@
1
+ export interface RouteChangeEvent {
2
+ fromUrl: string;
3
+ source: RouteChangeSource;
4
+ toUrl: string;
5
+ }
6
+ export type RouteChangeSource = 'hashchange' | 'popstate' | 'pushState' | 'replaceState';
7
+ type RouteChangeHandler = (event: RouteChangeEvent) => void;
8
+ export declare const subscribeRouteChange: (handler: RouteChangeHandler) => () => void;
9
+ export {};
@@ -0,0 +1,2 @@
1
+ import type { NormalizedBkOTConfig } from './config';
2
+ export declare const isBkOTSampled: ({ sampleRate }: Pick<NormalizedBkOTConfig, "sampleRate">) => boolean;
@@ -0,0 +1,44 @@
1
+ import { type Attributes, type Span } from '@opentelemetry/api';
2
+ import { type Instrumentation } from '@opentelemetry/instrumentation';
3
+ import { type BkOTConfig, type BkOTCustomEventPayload, type NormalizedBkOTConfig } from './config';
4
+ import { type BkOTPlugin, type BkOTRuntimeContext } from './plugin';
5
+ import type { LogRecord } from '@opentelemetry/api-logs';
6
+ export interface BkOTInstance extends BkOTRuntimeContext {
7
+ flush: () => Promise<void>;
8
+ reportCustomEvent: (payload: BkOTCustomEventPayload) => void;
9
+ shutdown: () => Promise<void>;
10
+ start: () => Promise<void>;
11
+ use: (plugin: BkOTPlugin) => BkOTInstance;
12
+ useInstrumentation: (instrumentation: Instrumentation) => BkOTInstance;
13
+ }
14
+ export declare class BkOpenTelemetry implements BkOTInstance {
15
+ private readonly loggerProvider;
16
+ private readonly meterProvider;
17
+ private phase;
18
+ private readonly pluginManager;
19
+ private readonly plugins;
20
+ private readonly registeredInstrumentations;
21
+ private readonly runtimeAttributes;
22
+ private readonly sampled;
23
+ private readonly teardownCallbacks;
24
+ private readonly tracerProvider;
25
+ private assertBeforeStart;
26
+ readonly applyRedact: (attributes: Attributes) => Attributes;
27
+ readonly config: NormalizedBkOTConfig;
28
+ readonly emitLog: (record: LogRecord) => void;
29
+ readonly flush: () => Promise<void>;
30
+ readonly getRuntimeAttributes: () => {
31
+ [attributeKey: string]: import("@opentelemetry/api").AttributeValue | undefined;
32
+ };
33
+ readonly logger: BkOTRuntimeContext['logger'];
34
+ readonly meter: BkOTRuntimeContext['meter'];
35
+ readonly reportCustomEvent: ({ attributes, error, name }: BkOTCustomEventPayload) => void;
36
+ readonly setRuntimeAttributes: (attributes: Attributes) => void;
37
+ readonly shutdown: () => Promise<void>;
38
+ readonly start: () => Promise<void>;
39
+ readonly startSpan: (name: string, attributes?: Attributes) => Span;
40
+ readonly tracer: BkOTRuntimeContext['tracer'];
41
+ readonly use: (plugin: BkOTPlugin) => this;
42
+ readonly useInstrumentation: (instrumentation: Instrumentation) => this;
43
+ constructor(inputConfig: BkOTConfig);
44
+ }
@@ -0,0 +1,4 @@
1
+ import type { NormalizedBkOTConfig } from './config';
2
+ export declare const isBkOTEndpoint: (config: NormalizedBkOTConfig, url: string) => boolean;
3
+ export declare const shouldIgnoreUrl: (config: NormalizedBkOTConfig, url: string) => boolean;
4
+ export declare const shouldPropagateTraceHeader: (_config: NormalizedBkOTConfig, url: string) => boolean;
@@ -0,0 +1,16 @@
1
+ export { type BkOTAttributesConfig, type BkOTAttributeValue, type BkOTBatchConfig, type BkOTConfig, type BkOTCustomEventPayload, type BkOTHttpBodyConfig, type BkOTHttpBodyRedactPayload, type BkOTRedactConfig, type BkOTRumConfig, normalizeConfig, type NormalizedBkOTConfig, type SignalExporterConfig, } from './core/config';
2
+ export { type BkOTPlugin, type BkOTRuntimeContext, createPlugin, type PluginManager } from './core/plugin';
3
+ export { FilteringSpanProcessor } from './core/processor';
4
+ export { BkOpenTelemetry, type BkOTInstance } from './core/sdk';
5
+ export { createBlankScreenPlugin } from './plugins/blank-screen';
6
+ export { createCommonInstrumentationsPlugin } from './plugins/common';
7
+ export { createCspViolationPlugin } from './plugins/csp-violation';
8
+ export { createDevicePlugin } from './plugins/device';
9
+ export { createErrorPlugin } from './plugins/error';
10
+ export { createHttpBodyPlugin } from './plugins/http-body';
11
+ export { createLongTaskPlugin } from './plugins/long-task';
12
+ export { createPageViewPlugin } from './plugins/page-view';
13
+ export { createRouteTimingPlugin } from './plugins/route-timing';
14
+ export { createSessionPlugin } from './plugins/session';
15
+ export { createWebVitalsPlugin } from './plugins/web-vitals';
16
+ export { createWebSocketPlugin } from './plugins/websocket';