@blueking/open-telemetry 0.0.14 → 0.0.15

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.
Files changed (46) hide show
  1. package/README.md +170 -554
  2. package/dist/bk-rum.global.js +9 -9
  3. package/dist/bk-rum.global.js.map +1 -1
  4. package/dist/core/action-activity.d.ts +28 -0
  5. package/dist/core/config.d.ts +113 -93
  6. package/dist/core/page-activity.d.ts +14 -0
  7. package/dist/core/plugin.d.ts +23 -6
  8. package/dist/core/processor.d.ts +25 -4
  9. package/dist/core/request-timing.d.ts +5 -0
  10. package/dist/core/runtime-context.d.ts +51 -3
  11. package/dist/core/sampling.d.ts +5 -1
  12. package/dist/core/sdk.d.ts +29 -10
  13. package/dist/core/url.d.ts +4 -1
  14. package/dist/index.d.ts +12 -14
  15. package/dist/index.js +2184 -1175
  16. package/dist/index.js.map +1 -1
  17. package/dist/plugins/action.d.ts +6 -2
  18. package/dist/plugins/activity.d.ts +6 -0
  19. package/dist/plugins/blank-screen.d.ts +17 -0
  20. package/dist/plugins/common.d.ts +11 -3
  21. package/dist/plugins/device.d.ts +2 -0
  22. package/dist/plugins/long-task.d.ts +3 -12
  23. package/dist/plugins/page-view.d.ts +1 -0
  24. package/dist/plugins/request-body.d.ts +14 -0
  25. package/dist/plugins/resource.d.ts +17 -1
  26. package/dist/plugins/session-state.d.ts +18 -0
  27. package/dist/plugins/session.d.ts +4 -4
  28. package/dist/plugins/web-vitals.d.ts +74 -0
  29. package/dist/schema/attributes.d.ts +31 -10
  30. package/dist/schema/span.d.ts +4 -12
  31. package/package.json +3 -7
  32. package/skills/bk-rum-otele/SKILL.md +34 -33
  33. package/skills/bk-rum-otele/evals/evals.json +1 -1
  34. package/skills/bk-rum-otele/references/api.md +45 -13
  35. package/skills/bk-rum-otele/references/concepts.md +38 -112
  36. package/skills/bk-rum-otele/references/configuration.md +135 -230
  37. package/skills/bk-rum-otele/references/custom-plugin.md +8 -5
  38. package/skills/bk-rum-otele/references/data-model.md +13 -8
  39. package/skills/bk-rum-otele/references/framework-integration.md +25 -17
  40. package/skills/bk-rum-otele/references/getting-started.md +27 -87
  41. package/skills/bk-rum-otele/references/plugins.md +165 -99
  42. package/skills/bk-rum-otele/references/privacy.md +42 -33
  43. package/skills/bk-rum-otele/references/telemetry-reference.md +73 -44
  44. package/skills/bk-rum-otele/references/troubleshooting.md +71 -37
  45. package/dist/plugins/http-body.d.ts +0 -3
  46. package/dist/plugins/route-timing.d.ts +0 -2
@@ -0,0 +1,28 @@
1
+ export type BkOTActivityEvent = {
2
+ endTime: number;
3
+ startTime: number;
4
+ type: 'long_task';
5
+ url?: never;
6
+ } | {
7
+ endTime: number;
8
+ startTime: number;
9
+ type: 'resource';
10
+ url?: string;
11
+ } | {
12
+ id: string;
13
+ time: number;
14
+ type: 'request_end';
15
+ } | {
16
+ id: string;
17
+ time: number;
18
+ type: 'request_start';
19
+ url?: string;
20
+ } | {
21
+ time: number;
22
+ type: 'dom_mutation' | 'error' | 'window_open';
23
+ };
24
+ export declare class RumActivityObserver {
25
+ private readonly listeners;
26
+ notify: (event: BkOTActivityEvent) => void;
27
+ subscribe: (listener: (event: BkOTActivityEvent) => void) => () => boolean;
28
+ }
@@ -3,20 +3,18 @@ import type { BkOTBlankScreenPluginOptions } from '../plugins/blank-screen';
3
3
  import type { BkOTDevicePluginOptions } from '../plugins/device';
4
4
  import type { BkOTErrorPluginOptions } from '../plugins/error';
5
5
  import type { BkOTLongTaskPluginOptions } from '../plugins/long-task';
6
- import type { BkOTSessionPluginOptions } from '../plugins/session';
6
+ import type { BkOTSessionConfig } from '../plugins/session';
7
7
  import type { BkOTPlugin } from './plugin';
8
8
  import type { BkOTUserContext } from './runtime-context';
9
9
  import type { Attributes } from '@opentelemetry/api';
10
10
  import type { Instrumentation } from '@opentelemetry/instrumentation';
11
11
  export interface BkOTActionEventPayload {
12
12
  attributes?: Attributes;
13
- targetTag?: string;
14
- targetText?: string;
15
- type: 'click' | 'custom' | 'input' | 'keydown' | 'pointerdown' | 'scroll' | 'submit' | string;
13
+ name: string;
16
14
  }
17
- export interface BkOTAppConfig {
15
+ export interface BkOTApplicationConfig {
18
16
  environment?: string;
19
- name?: string;
17
+ name: string;
20
18
  version?: string;
21
19
  }
22
20
  export interface BkOTAttributesConfig {
@@ -32,125 +30,144 @@ export interface BkOTBatchConfig {
32
30
  maxQueueSize?: number;
33
31
  scheduledDelayMillis?: number;
34
32
  }
33
+ export interface BkOTBeforeSendEvent {
34
+ attributes: Record<string, unknown>;
35
+ name: string;
36
+ signal: 'log' | 'span';
37
+ }
38
+ export interface BkOTContextConfig {
39
+ attributes?: BkOTAttributesConfig;
40
+ user?: BkOTUserContext;
41
+ }
35
42
  export interface BkOTCustomEventPayload {
36
43
  attributes?: Attributes;
37
44
  error?: Error;
38
45
  name: string;
39
46
  }
40
- export interface BkOTHttpBodyConfig {
41
- maxBodySize?: number;
42
- redact?: (payload: BkOTHttpBodyRedactPayload) => string;
43
- }
44
- export interface BkOTHttpBodyRedactPayload {
45
- body: string;
46
- contentType?: string;
47
- method: string;
48
- status?: number;
49
- truncated: boolean;
50
- type: 'request' | 'response';
51
- url: string;
52
- }
53
- export interface BkOTInstrumentationsConfig {
54
- custom?: Instrumentation[];
55
- documentLoad?: boolean;
56
- fetch?: boolean;
57
- /**
58
- * 控制 fetch/xhr 请求向哪些后台 API 注入 W3C `traceparent`(内含 OpenTelemetry traceId)请求头。
59
- *
60
- * 同源请求始终注入;该配置用于把跨域后台 API 也纳入注入范围(在同源基础上扩展):
61
- * - `'all'`:所有请求都注入(含跨域,使用前确认涉及的隐私/CORS 影响)。
62
- * - `string`:URL 包含该子串即注入,例如 `'api.example.com'`。
63
- * - `RegExp`:URL 匹配该正则即注入。
64
- * - 以上字符串/正则的数组:命中任意一个即注入。
65
- * - `(url: string) => boolean`:完全自定义判断。
66
- *
67
- * 注意:向跨域服务注入自定义头会触发 CORS 预检,目标服务需在
68
- * `Access-Control-Allow-Headers` 中放行 `traceparent`(以及按需的 `tracestate`)。
69
- *
70
- * 默认未配置时仅同源请求注入,保持既有行为。
71
- */
72
- propagateTraceHeaderUrls?: BkOTPropagateTraceHeaderUrls;
73
- userInteraction?: boolean | {
74
- eventNames?: string[];
75
- };
76
- xhr?: boolean;
47
+ export interface BkOTExtensionsConfig {
48
+ instrumentations?: Instrumentation[];
49
+ plugins?: BkOTPlugin[];
77
50
  }
78
51
  export interface BkOTOptions {
79
- app?: BkOTAppConfig;
80
- attributes?: BkOTAttributesConfig;
52
+ application: BkOTApplicationConfig;
81
53
  autoStart?: boolean;
54
+ context?: BkOTContextConfig;
82
55
  debug?: boolean;
83
56
  enabled?: boolean;
84
- instrumentations?: BkOTInstrumentationsConfig;
85
- plugins?: BkOTPluginsConfig;
57
+ extensions?: BkOTExtensionsConfig;
86
58
  privacy?: BkOTPrivacyConfig;
87
- route?: BkOTRouteConfig;
88
- sampling?: BkOTSamplingConfig;
89
- spanBatch?: BkOTBatchConfig;
90
- spanProcessor?: BkOTSpanProcessorMode;
91
- transport?: BkOTTransportConfig;
92
- user?: BkOTUserContext;
59
+ session?: BkOTSessionConfig;
60
+ tracking?: BkOTTrackingConfig;
61
+ transport: BkOTTransportConfig;
62
+ }
63
+ export interface BkOTPrivacyConfig {
64
+ /** 初始化时的采集授权状态;运行期可通过 setTrackingConsent 更新。 */
65
+ trackingConsent?: BkOTTrackingConsent;
66
+ redactAttributes?: (attributes: Attributes) => Attributes;
67
+ redactUrl?: (url: string) => string;
93
68
  }
94
- export interface BkOTPluginsConfig {
69
+ export interface BkOTRequestBodyConfig {
70
+ allowedContentTypes?: Array<RegExp | string>;
71
+ capture?: 'all' | 'errors';
72
+ maxSize?: number;
73
+ redact: (payload: BkOTRequestBodyRedactPayload) => string | undefined;
74
+ }
75
+ export interface BkOTRequestBodyRedactPayload {
76
+ body: string;
77
+ contentType?: string;
78
+ kind: 'request' | 'response';
79
+ method?: string;
80
+ statusCode?: number;
81
+ truncated: boolean;
82
+ type: 'fetch' | 'xhr';
83
+ url?: string;
84
+ }
85
+ export interface BkOTRequestConfig {
86
+ allowedTracingUrls?: BkOTUrlMatcher | BkOTUrlMatcher[];
87
+ body?: BkOTRequestBodyConfig;
88
+ excludedUrls?: BkOTUrlMatcher | BkOTUrlMatcher[];
89
+ beforeSend?: (event: BkOTRequestEvent, context: BkOTRequestContext) => boolean | void;
90
+ }
91
+ export type BkOTRequestContext = {
92
+ error?: unknown;
93
+ request: Request | RequestInit;
94
+ response?: Response;
95
+ type: 'fetch';
96
+ } | {
97
+ requestBody?: Document | null | XMLHttpRequestBodyInit;
98
+ type: 'xhr';
99
+ xhr: XMLHttpRequest;
100
+ };
101
+ export interface BkOTRequestEvent {
102
+ attributes: Attributes;
103
+ type: 'resource';
104
+ resource: {
105
+ method?: string;
106
+ statusCode?: number;
107
+ type: 'fetch' | 'xhr';
108
+ url?: string;
109
+ };
110
+ }
111
+ export type BkOTSpanProcessorMode = 'batch' | 'simple';
112
+ export interface BkOTTrackingConfig {
95
113
  action?: BkOTActionPluginOptions | boolean;
96
114
  blankScreen?: BkOTBlankScreenPluginOptions | boolean;
97
115
  cspViolation?: boolean;
98
- custom?: BkOTPlugin[];
99
116
  device?: BkOTDevicePluginOptions | boolean;
100
117
  error?: BkOTErrorPluginOptions | boolean;
101
- httpBody?: BkOTHttpBodyConfig | boolean;
102
118
  longTask?: BkOTLongTaskPluginOptions | boolean;
103
- pageView?: boolean;
119
+ request?: BkOTRequestConfig | boolean;
104
120
  resource?: boolean;
105
- routeTiming?: boolean;
106
- session?: BkOTSessionPluginOptions | boolean;
121
+ view?: BkOTViewConfig;
107
122
  websocket?: boolean;
108
123
  webVitals?: boolean;
124
+ beforeSend?: (event: BkOTBeforeSendEvent) => boolean | void;
109
125
  }
110
- export interface BkOTPrivacyConfig {
111
- redactAttributes?: (attributes: Attributes) => Attributes;
112
- redactUrl?: (url: string) => string;
113
- }
114
- export type BkOTPropagateTraceHeaderUrls = 'all' | ((url: string) => boolean) | Array<RegExp | string> | RegExp | string;
115
- export interface BkOTRouteConfig {
116
- /**
117
- * 返回低基数路由分组,例如 `/trace/:id`。
118
- * SDK 默认退化为 URL pathname;复杂业务路由建议接入方显式提供模板。
119
- */
120
- getPathGroup?: (url: string) => string | undefined;
121
- }
122
- export interface BkOTSamplingConfig {
123
- rate?: number;
124
- }
125
- export type BkOTSpanProcessorMode = 'batch' | 'simple';
126
+ export type BkOTTrackingConsent = 'granted' | 'not-granted';
126
127
  export interface BkOTTransportConfig {
127
128
  endpoint?: string;
128
129
  headers?: Record<string, string>;
129
130
  signals?: SignalExportersInputConfig;
130
131
  token?: string;
131
132
  }
132
- export interface ResolvedAppConfig {
133
+ export type BkOTUrlMatcher = ((url: string) => boolean) | RegExp | string;
134
+ export interface BkOTViewConfig {
135
+ /** 不参与 View loading time 的长轮询、流式请求或埋点 URL。 */
136
+ excludedActivityUrls?: BkOTUrlMatcher | BkOTUrlMatcher[];
137
+ /** 自动计算初始加载与 SPA 路由的页面稳定耗时,默认开启。 */
138
+ loadingTime?: boolean;
139
+ tracking?: 'auto' | 'manual';
140
+ /** 返回低基数路由分组,例如 `/trace/:id`。 */
141
+ getPathGroup?: (url: string) => string | undefined;
142
+ }
143
+ export interface MetricExporterInputConfig extends SignalExporterInputConfig {
144
+ intervalMillis?: number;
145
+ }
146
+ export interface ResolvedApplicationConfig {
133
147
  environment: string;
134
148
  name: string;
135
149
  version?: string;
136
150
  }
137
151
  export interface ResolvedBkOTConfig {
138
- app: ResolvedAppConfig;
152
+ application: ResolvedApplicationConfig;
139
153
  autoStart: boolean;
140
154
  debug: boolean;
141
155
  enabled: boolean;
156
+ extensions: BkOTExtensionsConfig;
142
157
  hooks: ResolvedConfigHooks;
143
- instrumentations: BkOTInstrumentationsConfig;
144
158
  metricIntervalMillis: number;
145
- plugins: BkOTPluginsConfig;
146
159
  resourceAttributes: Attributes;
147
- sampling: ResolvedSamplingConfig;
160
+ session: ResolvedSessionConfig;
148
161
  spanBatch?: BkOTBatchConfig;
149
162
  spanProcessor: BkOTSpanProcessorMode;
163
+ tracking: BkOTTrackingConfig;
164
+ trackingConsent: BkOTTrackingConsent;
150
165
  transport: ResolvedTransportConfig;
151
166
  user?: BkOTUserContext;
167
+ view: ResolvedViewConfig;
152
168
  }
153
169
  export interface ResolvedConfigHooks {
170
+ beforeSend?: (event: BkOTBeforeSendEvent) => boolean | void;
154
171
  getCustomAttributes: () => Attributes;
155
172
  getErrorAttributes: () => Attributes;
156
173
  getMetricAttributes: () => Attributes;
@@ -159,19 +176,19 @@ export interface ResolvedConfigHooks {
159
176
  redactAttributes: (attributes: Attributes) => Attributes;
160
177
  redactUrl: (url: string) => string;
161
178
  }
162
- export interface ResolvedSamplingConfig {
163
- rate: number;
179
+ export interface ResolvedSessionConfig extends BkOTSessionConfig {
180
+ sampleRate: number;
164
181
  }
165
182
  export interface ResolvedTransportConfig {
166
183
  endpoint: string;
167
- logs: SignalExporterConfig;
168
- metrics: SignalExporterConfig;
169
- traces: SignalExporterConfig;
184
+ logs: false | SignalExporterConfig;
185
+ metrics: false | SignalExporterConfig;
186
+ traces: false | SignalExporterConfig;
170
187
  }
171
- export interface SignalEndpointsConfig {
172
- logs?: string;
173
- metrics?: string;
174
- traces?: string;
188
+ export interface ResolvedViewConfig {
189
+ excludedActivityUrls?: BkOTUrlMatcher | BkOTUrlMatcher[];
190
+ loadingTime: boolean;
191
+ tracking: 'auto' | 'manual';
175
192
  }
176
193
  export interface SignalExporterConfig {
177
194
  concurrencyLimit?: number;
@@ -181,9 +198,12 @@ export interface SignalExporterConfig {
181
198
  }
182
199
  export type SignalExporterInputConfig = Partial<SignalExporterConfig>;
183
200
  export interface SignalExportersInputConfig {
184
- endpoints?: SignalEndpointsConfig;
185
- logs?: SignalExporterInputConfig;
186
- metrics?: SignalExporterInputConfig;
187
- traces?: SignalExporterInputConfig;
201
+ logs?: false | SignalExporterInputConfig;
202
+ metrics?: false | MetricExporterInputConfig;
203
+ traces?: false | TraceExporterInputConfig;
204
+ }
205
+ export interface TraceExporterInputConfig extends SignalExporterInputConfig {
206
+ batch?: BkOTBatchConfig;
207
+ processor?: BkOTSpanProcessorMode;
188
208
  }
189
209
  export declare const resolveConfig: (config: BkOTOptions) => ResolvedBkOTConfig;
@@ -0,0 +1,14 @@
1
+ import type { BkOTRuntimeContext } from './plugin';
2
+ export declare const PAGE_ACTIVITY_VALIDATION_DELAY = 100;
3
+ export declare const PAGE_ACTIVITY_END_DELAY = 100;
4
+ export type PageActivityEnd = {
5
+ endTime: number;
6
+ hadActivity: true;
7
+ } | {
8
+ hadActivity: false;
9
+ };
10
+ /**
11
+ * 等待 View/Action 启动后的请求、静态资源和 DOM 变更全部安静 100ms。
12
+ * 请求用 id 精确配对,避免并发请求提前结束等待。
13
+ */
14
+ export declare const waitForPageActivityEnd: (context: BkOTRuntimeContext, startTime: number, callback: (result: PageActivityEnd) => void) => () => void;
@@ -1,7 +1,9 @@
1
+ import type { BkOTActivityEvent } from './action-activity';
1
2
  import type { ResolvedBkOTConfig } from './config';
2
- import type { BkOTUserContext, BkOTViewContext } from './runtime-context';
3
- import type { Attributes, Meter, Span, TimeInput, Tracer } from '@opentelemetry/api';
3
+ import type { BkOTSessionLifecycleEvent, BkOTUserContext, BkOTViewCommand, BkOTViewContext } from './runtime-context';
4
+ import type { Attributes, Meter, Span, SpanKind, TimeInput, Tracer } from '@opentelemetry/api';
4
5
  import type { Logger, LogRecord } from '@opentelemetry/api-logs';
6
+ import type { Instrumentation } from '@opentelemetry/instrumentation';
5
7
  export interface BkOTPlugin {
6
8
  enabled?: ((config: ResolvedBkOTConfig) => boolean) | boolean;
7
9
  name: string;
@@ -18,25 +20,40 @@ export interface BkOTRuntimeContext {
18
20
  applyRedact: (attributes: Attributes) => Attributes;
19
21
  /** 包装 logger.emit,自动应用 redact 与 runtime attributes */
20
22
  emitLog: (record: LogRecord) => void;
23
+ flush: () => Promise<void>;
21
24
  getRuntimeAttributes: () => Attributes;
22
- setRuntimeAttributes: (attributes: Attributes) => void;
25
+ getRuntimeAttributesAt: (timestamp: number) => Attributes;
26
+ isSampled: () => boolean;
27
+ isSessionActive: () => boolean;
28
+ isViewActive: () => boolean;
29
+ notifyActivity: (event: BkOTActivityEvent) => void;
30
+ notifySessionLifecycle: (event: BkOTSessionLifecycleEvent) => void;
31
+ onActivity: (listener: (event: BkOTActivityEvent) => void) => () => void;
32
+ onSessionLifecycle: (listener: (event: BkOTSessionLifecycleEvent) => void) => () => void;
33
+ onViewChange: (listener: (view: BkOTViewContext) => void) => () => void;
34
+ onViewCommand: (listener: (command: BkOTViewCommand) => void) => () => void;
35
+ registerInstrumentations: (instrumentations: Instrumentation[]) => void;
36
+ setRuntimeAttributes: (attributes: Attributes, changedAt?: number) => void;
23
37
  /** 更新用户上下文;user.id 由接入方传入,SDK 原样透传 */
24
38
  setUser: (user: BkOTUserContext) => void;
25
39
  /** 更新当前 View 上下文,后续 span/log 会自动带上 view.* 关联字段 */
26
- setView: (view: BkOTViewContext) => void;
40
+ setView: (view: BkOTViewContext, changedAt?: number) => void;
27
41
  startSpan: (name: string, attributes?: Attributes, options?: BkOTStartSpanOptions) => Span;
28
42
  }
29
43
  /**
30
44
  * startSpan 的扩展选项。
31
45
  *
32
46
  * 用于"事件实际发生在过去某个时刻,但 span 现在才创建"的场景:
33
- * 例如 route-timing 在 raf+setTimeout 链路结束后才上报,但希望 span 的时间窗
34
- * 反映真实的"路由切换瞬间到渲染完成"区间,方便 trace 视图与并发请求对齐。
47
+ * 例如 PerformanceObserver 在条目结束后才回调,但 span 的时间窗仍需反映
48
+ * 条目真实发生的区间,方便 trace 视图与并发请求对齐。
35
49
  *
36
50
  * startTime 接受 OT TimeInput;常用 epoch 毫秒(performance.timeOrigin + performance.now())。
37
51
  * 配合 span.end(endTime?) 可控制结束时间。
38
52
  */
39
53
  export interface BkOTStartSpanOptions {
54
+ /** 用于从历史中解析 Session/View/Action 归属;默认复用数字类型 startTime。 */
55
+ contextTime?: number;
56
+ kind?: SpanKind;
40
57
  startTime?: TimeInput;
41
58
  }
42
59
  export declare const isPluginEnabled: (plugin: BkOTPlugin, config: ResolvedBkOTConfig) => boolean;
@@ -1,16 +1,37 @@
1
+ import { AggregationTemporality } from '@opentelemetry/sdk-metrics';
1
2
  import type { ResolvedBkOTConfig } from './config';
2
- import type { Context } from '@opentelemetry/api';
3
+ import type { Attributes, Context } from '@opentelemetry/api';
4
+ import type { LogRecordProcessor, SdkLogRecord } from '@opentelemetry/sdk-logs';
5
+ import type { InstrumentType, PushMetricExporter, ResourceMetrics } from '@opentelemetry/sdk-metrics';
3
6
  import type { ReadableSpan, Span, SpanProcessor } from '@opentelemetry/sdk-trace-base';
7
+ export declare const REQUEST_DISCARD_ATTRIBUTE = "bk.rum.request.discard";
8
+ export declare class FilteringLogRecordProcessor implements LogRecordProcessor {
9
+ private readonly inner;
10
+ private readonly config?;
11
+ constructor(inner: LogRecordProcessor, config?: ResolvedBkOTConfig | undefined);
12
+ enabled(options: Parameters<NonNullable<LogRecordProcessor['enabled']>>[0]): boolean;
13
+ forceFlush(): Promise<void>;
14
+ onEmit(logRecord: SdkLogRecord, context?: Context): void;
15
+ shutdown(): Promise<void>;
16
+ }
4
17
  /**
5
- * 包装 SpanProcessor,在 onEnd 阶段过滤指向自身上报 endpoint 的 span,
6
- * 兜底防止 official fetch/xhr 拦截器对 OTel 自身请求产生回环 span。
18
+ * 给所有 span 固化创建时的运行时上下文,并在 onEnd 阶段过滤自身上报请求和无 RUM 上下文 span。
7
19
  */
8
20
  export declare class FilteringSpanProcessor implements SpanProcessor {
9
21
  private readonly inner;
10
22
  private readonly config;
11
- constructor(inner: SpanProcessor, config: ResolvedBkOTConfig);
23
+ private readonly getRuntimeAttributes;
24
+ constructor(inner: SpanProcessor, config: ResolvedBkOTConfig, getRuntimeAttributes: () => Attributes);
12
25
  forceFlush(): Promise<void>;
13
26
  onEnd(span: ReadableSpan): void;
14
27
  onStart(span: Span, parentContext: Context): void;
15
28
  shutdown(): Promise<void>;
16
29
  }
30
+ export declare class FilteringMetricExporter implements PushMetricExporter {
31
+ private readonly inner;
32
+ constructor(inner: PushMetricExporter);
33
+ export(metrics: ResourceMetrics, resultCallback: Parameters<PushMetricExporter['export']>[1]): void;
34
+ forceFlush(): Promise<void>;
35
+ selectAggregationTemporality(instrumentType: InstrumentType): AggregationTemporality;
36
+ shutdown(): Promise<void>;
37
+ }
@@ -0,0 +1,5 @@
1
+ export declare const notifyRequestTiming: (entry: PerformanceResourceTiming) => void;
2
+ export declare const trackRequestTiming: (url: string, startTime?: number) => {
3
+ cancel: () => void;
4
+ complete: (callback: (entry?: PerformanceResourceTiming) => void) => void;
5
+ };
@@ -1,11 +1,41 @@
1
1
  import type { Attributes } from '@opentelemetry/api';
2
+ export type BkOTSessionLifecycleEvent = {
3
+ expiredAt: number;
4
+ id: string;
5
+ reason: 'external' | 'inactivity' | 'maxLifetime';
6
+ type: 'expired';
7
+ } | {
8
+ id: string;
9
+ previousId?: string;
10
+ reason: 'inactivity' | 'init' | 'maxLifetime' | 'shared';
11
+ startedAt: number;
12
+ type: 'renewed';
13
+ };
14
+ export interface BkOTStartViewOptions {
15
+ attributes?: Attributes;
16
+ name?: string;
17
+ url?: string;
18
+ }
2
19
  export interface BkOTUserContext {
3
20
  /** 由接入方传入并由 SDK 原样透传;SDK 不 hash、不脱敏、不改写。 */
4
21
  id?: string;
5
22
  }
23
+ export type BkOTViewCommand = {
24
+ name: string;
25
+ type: 'set_name';
26
+ } | {
27
+ options: BkOTStartViewOptions;
28
+ type: 'start';
29
+ } | {
30
+ time: number;
31
+ type: 'set_loading_time';
32
+ };
6
33
  export interface BkOTViewContext {
34
+ attributes?: Attributes;
7
35
  id?: string;
8
- loadingType?: 'initial_load' | 'route_change';
36
+ loadingType?: 'bf_cache' | 'initial_load' | 'route_change' | 'session_renewal';
37
+ name?: string;
38
+ startedAt?: number;
9
39
  url?: string;
10
40
  urlPathGroup?: string;
11
41
  }
@@ -16,15 +46,33 @@ export interface BkOTViewContext {
16
46
  * 插件发 span/log 时读取当前快照即可,避免每个插件重复维护公共字段。
17
47
  */
18
48
  export declare class RumRuntimeContext {
49
+ private readonly actionHistory;
19
50
  private readonly attributes;
51
+ private readonly queuedViewCommands;
52
+ private recordAssociation;
53
+ private readonly sessionHistory;
54
+ private readonly sessionLifecycleListeners;
20
55
  private readonly setOptionalAttribute;
21
56
  private user;
22
57
  private view;
58
+ private readonly viewAttributeKeys;
59
+ private readonly viewChangeListeners;
60
+ private readonly viewCommandListeners;
61
+ private readonly viewHistory;
62
+ private readonly viewScopedAttributeKeys;
63
+ dispatchViewCommand: (command: BkOTViewCommand) => void;
23
64
  getAttributes: () => Attributes;
65
+ getAttributesAt: (timestamp: number) => Attributes;
24
66
  getUser: () => BkOTUserContext;
25
67
  getView: () => BkOTViewContext;
26
- setAttributes: (attributes: Attributes) => void;
68
+ isSessionActive: () => boolean;
69
+ isViewActive: () => boolean;
70
+ notifySessionLifecycle: (event: BkOTSessionLifecycleEvent) => void;
71
+ onSessionLifecycle: (listener: (event: BkOTSessionLifecycleEvent) => void) => () => boolean;
72
+ onViewChange: (listener: (view: BkOTViewContext) => void) => () => boolean;
73
+ onViewCommand: (listener: (command: BkOTViewCommand) => void) => () => boolean;
74
+ setAttributes: (attributes: Attributes, changedAt?: number) => void;
27
75
  setUser: (user: BkOTUserContext) => void;
28
- setView: (view: BkOTViewContext) => void;
76
+ setView: (view: BkOTViewContext, changedAt?: number) => void;
29
77
  constructor(initialAttributes?: Attributes, user?: BkOTUserContext);
30
78
  }
@@ -1,2 +1,6 @@
1
1
  import type { ResolvedBkOTConfig } from './config';
2
- export declare const isBkOTSampled: ({ sampling }: Pick<ResolvedBkOTConfig, "sampling">) => boolean;
2
+ export declare const RUM_SAMPLED_ATTRIBUTE = "bk.rum.sampled";
3
+ /**
4
+ * 按应用和 RUM Session ID 做确定性采样。采样决定随 Session 更新,不再依赖 Tab 级 sessionStorage。
5
+ */
6
+ export declare const isBkOTSampled: ({ application, session }: Pick<ResolvedBkOTConfig, "application" | "session">, sessionId: string) => boolean;
@@ -1,47 +1,66 @@
1
1
  import { type Attributes, type Span } from '@opentelemetry/api';
2
2
  import { type Instrumentation } from '@opentelemetry/instrumentation';
3
- import { type BkOTActionEventPayload, type BkOTCustomEventPayload, type BkOTOptions, type ResolvedBkOTConfig } from './config';
3
+ import { type BkOTActionEventPayload, type BkOTCustomEventPayload, type BkOTOptions, type BkOTTrackingConsent, type ResolvedBkOTConfig } from './config';
4
4
  import { type BkOTPlugin, type BkOTRuntimeContext, type BkOTStartSpanOptions } from './plugin';
5
- import { type BkOTUserContext, type BkOTViewContext } from './runtime-context';
5
+ import { type BkOTSessionLifecycleEvent, type BkOTStartViewOptions, type BkOTUserContext } from './runtime-context';
6
6
  import type { LogRecord } from '@opentelemetry/api-logs';
7
- export interface BkOTInstance extends BkOTRuntimeContext {
7
+ export interface BkOTInstance extends Omit<BkOTRuntimeContext, 'notifyActivity' | 'onActivity' | 'onViewChange' | 'onViewCommand' | 'registerInstrumentations' | 'setView'> {
8
8
  flush: () => Promise<void>;
9
9
  reportAction: (payload: BkOTActionEventPayload) => void;
10
10
  reportCustomEvent: (payload: BkOTCustomEventPayload) => void;
11
+ setTrackingConsent: (consent: BkOTTrackingConsent) => Promise<void>;
11
12
  setUser: (user: BkOTUserContext) => void;
12
- setView: (view: BkOTViewContext) => void;
13
+ setViewLoadingTime: () => void;
14
+ setViewName: (name: string) => void;
13
15
  shutdown: () => Promise<void>;
14
16
  start: () => Promise<void>;
17
+ startView: (options?: BkOTStartViewOptions) => void;
15
18
  use: (plugin: BkOTPlugin) => BkOTInstance;
16
19
  useInstrumentation: (instrumentation: Instrumentation) => BkOTInstance;
17
20
  }
18
21
  export declare class BkOpenTelemetry implements BkOTInstance {
22
+ private readonly activityObserver;
23
+ private readonly flushProviders;
19
24
  private readonly loggerProvider;
20
25
  private readonly meterProvider;
21
26
  private phase;
22
- private readonly pluginManager;
23
- private readonly plugins;
27
+ private pluginManager?;
24
28
  private readonly registeredInstrumentations;
29
+ private releaseOtelGlobals?;
25
30
  private readonly runtimeContext;
26
- private readonly sampled;
31
+ private sampled;
32
+ private readonly startCollection;
33
+ private readonly stopCollection;
27
34
  private readonly teardownCallbacks;
28
35
  private readonly tracerProvider;
29
36
  private assertBeforeStart;
37
+ private createPluginContext;
38
+ private createPluginManager;
30
39
  readonly applyRedact: (attributes: Attributes) => Attributes;
31
40
  readonly config: ResolvedBkOTConfig;
32
41
  readonly emitLog: (record: LogRecord) => void;
33
42
  readonly flush: () => Promise<void>;
34
43
  readonly getRuntimeAttributes: () => Attributes;
44
+ readonly getRuntimeAttributesAt: (timestamp: number) => Attributes;
45
+ readonly isSampled: () => boolean;
46
+ readonly isSessionActive: () => boolean;
47
+ readonly isViewActive: () => boolean;
35
48
  readonly logger: BkOTRuntimeContext['logger'];
36
49
  readonly meter: BkOTRuntimeContext['meter'];
37
- readonly reportAction: ({ attributes, targetTag, targetText, type }: BkOTActionEventPayload) => void;
50
+ readonly notifySessionLifecycle: (event: BkOTSessionLifecycleEvent) => void;
51
+ readonly onSessionLifecycle: (listener: (event: BkOTSessionLifecycleEvent) => void) => () => boolean;
52
+ readonly registerInstrumentations: (instrumentations: Instrumentation[]) => void;
53
+ readonly reportAction: ({ attributes, name }: BkOTActionEventPayload) => void;
38
54
  readonly reportCustomEvent: ({ attributes, error, name }: BkOTCustomEventPayload) => void;
39
- readonly setRuntimeAttributes: (attributes: Attributes) => void;
55
+ readonly setRuntimeAttributes: (attributes: Attributes, changedAt?: number) => void;
56
+ readonly setTrackingConsent: (consent: BkOTTrackingConsent) => Promise<void>;
40
57
  readonly setUser: (user: BkOTUserContext) => void;
41
- readonly setView: (view: BkOTViewContext) => void;
58
+ readonly setViewLoadingTime: () => void;
59
+ readonly setViewName: (name: string) => void;
42
60
  readonly shutdown: () => Promise<void>;
43
61
  readonly start: () => Promise<void>;
44
62
  readonly startSpan: (name: string, attributes?: Attributes, options?: BkOTStartSpanOptions) => Span;
63
+ readonly startView: (options?: BkOTStartViewOptions) => void;
45
64
  readonly tracer: BkOTRuntimeContext['tracer'];
46
65
  readonly use: (plugin: BkOTPlugin) => this;
47
66
  readonly useInstrumentation: (instrumentation: Instrumentation) => this;
@@ -1,4 +1,7 @@
1
- import type { ResolvedBkOTConfig } from './config';
1
+ import type { BkOTUrlMatcher, ResolvedBkOTConfig } from './config';
2
+ export declare const toAbsoluteUrl: (url: string) => string;
2
3
  export declare const isBkOTEndpoint: (config: ResolvedBkOTConfig, url: string) => boolean;
3
4
  export declare const shouldIgnoreUrl: (config: ResolvedBkOTConfig, url: string) => boolean;
5
+ export declare const matchesUrlList: (matchers: BkOTUrlMatcher | BkOTUrlMatcher[] | undefined, url: string) => boolean;
6
+ export declare const shouldIgnoreRequest: (config: ResolvedBkOTConfig, url: string) => boolean;
4
7
  export declare const shouldPropagateTraceHeader: (config: ResolvedBkOTConfig, url: string) => boolean;
package/dist/index.d.ts CHANGED
@@ -1,21 +1,19 @@
1
- export { type BkOTActionEventPayload, type BkOTAppConfig, type BkOTAttributesConfig, type BkOTAttributeValue, type BkOTBatchConfig, type BkOTCustomEventPayload, type BkOTHttpBodyConfig, type BkOTHttpBodyRedactPayload, type BkOTInstrumentationsConfig, type BkOTOptions, type BkOTPluginsConfig, type BkOTPrivacyConfig, type BkOTPropagateTraceHeaderUrls, type BkOTSamplingConfig, type BkOTTransportConfig, resolveConfig, type ResolvedBkOTConfig, type ResolvedConfigHooks, type ResolvedTransportConfig, type SignalExporterConfig, type SignalExportersInputConfig, } from './core/config';
1
+ export { type BkOTActionEventPayload, type BkOTApplicationConfig, type BkOTAttributesConfig, type BkOTAttributeValue, type BkOTBatchConfig, type BkOTBeforeSendEvent, type BkOTContextConfig, type BkOTCustomEventPayload, type BkOTExtensionsConfig, type BkOTOptions, type BkOTPrivacyConfig, type BkOTRequestBodyConfig, type BkOTRequestBodyRedactPayload, type BkOTRequestConfig, type BkOTRequestContext, type BkOTRequestEvent, type BkOTTrackingConfig, type BkOTTrackingConsent, type BkOTTransportConfig, type BkOTUrlMatcher, type BkOTViewConfig, type MetricExporterInputConfig, resolveConfig, type ResolvedApplicationConfig, type ResolvedBkOTConfig, type ResolvedConfigHooks, type ResolvedTransportConfig, type ResolvedViewConfig, type SignalExporterConfig, type SignalExportersInputConfig, type TraceExporterInputConfig, } from './core/config';
2
2
  export { type BkOTPlugin, type BkOTRuntimeContext, createPlugin, type PluginManager } from './core/plugin';
3
- export { FilteringSpanProcessor } from './core/processor';
4
- export { type BkOTUserContext, type BkOTViewContext, RumRuntimeContext } from './core/runtime-context';
3
+ export { FilteringLogRecordProcessor, FilteringMetricExporter, FilteringSpanProcessor } from './core/processor';
4
+ export { type BkOTSessionLifecycleEvent, type BkOTStartViewOptions, type BkOTUserContext, type BkOTViewContext, RumRuntimeContext, } from './core/runtime-context';
5
5
  export { BkOpenTelemetry, type BkOTInstance } from './core/sdk';
6
- export { RUM_ATTRIBUTES, type RumAttributeKey } from './schema/attributes';
7
- export { buildRumSpanAttributes, getDurationBucket, RUM_SPAN_RESULT, RUM_SPAN_TYPE, type RumSpanClassification, type RumSpanResult, type RumSpanType, } from './schema/span';
8
- export { createActionPlugin, type BkOTActionEventName, type BkOTActionPluginOptions } from './plugins/action';
9
- export { createBlankScreenPlugin, type BkOTBlankScreenPluginOptions } from './plugins/blank-screen';
6
+ export { type BkOTActionPluginOptions, createActionPlugin } from './plugins/action';
7
+ export { EXCLUDED_ACTIVITY_MUTATIONS_ATTRIBUTE } from './plugins/activity';
8
+ export { type BkOTBlankScreenPluginOptions, createBlankScreenPlugin } from './plugins/blank-screen';
10
9
  export { createCommonInstrumentationsPlugin } from './plugins/common';
11
10
  export { createCspViolationPlugin } from './plugins/csp-violation';
12
- export { createDevicePlugin, type BkOTDevicePluginOptions } from './plugins/device';
13
- export { createErrorPlugin, type BkOTErrorPluginOptions } from './plugins/error';
14
- export { createHttpBodyPlugin } from './plugins/http-body';
15
- export { createLongTaskPlugin, type BkOTLongTaskPluginOptions } from './plugins/long-task';
16
- export { createPageViewPlugin } from './plugins/page-view';
11
+ export { type BkOTDevicePluginOptions, createDevicePlugin } from './plugins/device';
12
+ export { type BkOTErrorPluginOptions, createErrorPlugin } from './plugins/error';
13
+ export { type BkOTLongTaskPluginOptions, createLongTaskPlugin } from './plugins/long-task';
17
14
  export { createResourcePlugin } from './plugins/resource';
18
- export { createRouteTimingPlugin } from './plugins/route-timing';
19
- export { createSessionPlugin, type BkOTSessionPluginOptions } from './plugins/session';
15
+ export { type BkOTSessionConfig, createSessionPlugin } from './plugins/session';
20
16
  export { createWebVitalsPlugin } from './plugins/web-vitals';
21
17
  export { createWebSocketPlugin } from './plugins/websocket';
18
+ export { RUM_ATTRIBUTES, type RumAttributeKey } from './schema/attributes';
19
+ export { buildRumSpanAttributes, getDurationBucket, RUM_SPAN_RESULT, RUM_SPAN_TYPE, type RumSpanClassification, type RumSpanResult, type RumSpanType, } from './schema/span';