@blueking/open-telemetry 0.0.6 → 0.0.7

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.
@@ -1,6 +1,18 @@
1
1
  import type { BkOTPlugin } from './plugin';
2
+ import type { BkOTUserContext } from './runtime-context';
2
3
  import type { Attributes } from '@opentelemetry/api';
3
4
  import type { Instrumentation } from '@opentelemetry/instrumentation';
5
+ export interface BkOTActionEventPayload {
6
+ attributes?: Attributes;
7
+ targetTag?: string;
8
+ targetText?: string;
9
+ type: 'click' | 'custom' | 'input' | 'keydown' | 'pointerdown' | 'scroll' | 'submit' | string;
10
+ }
11
+ export interface BkOTAppConfig {
12
+ environment?: string;
13
+ name?: string;
14
+ version?: string;
15
+ }
4
16
  export interface BkOTAttributesConfig {
5
17
  custom?: () => Attributes;
6
18
  error?: () => Attributes;
@@ -14,27 +26,6 @@ export interface BkOTBatchConfig {
14
26
  maxQueueSize?: number;
15
27
  scheduledDelayMillis?: number;
16
28
  }
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
- spanProcessor?: BkOTSpanProcessorMode;
35
- token?: string;
36
- traces?: SignalExporterInputConfig;
37
- }
38
29
  export interface BkOTCustomEventPayload {
39
30
  attributes?: Attributes;
40
31
  error?: Error;
@@ -61,20 +52,52 @@ export interface BkOTInstrumentationsConfig {
61
52
  };
62
53
  xhr?: boolean;
63
54
  }
64
- export interface BkOTRedactConfig {
65
- attributes?: (attributes: Attributes) => Attributes;
66
- url?: (url: string) => string;
55
+ export interface BkOTOptions {
56
+ app?: BkOTAppConfig;
57
+ attributes?: BkOTAttributesConfig;
58
+ autoStart?: boolean;
59
+ debug?: boolean;
60
+ enabled?: boolean;
61
+ instrumentations?: Instrumentation[];
62
+ plugins?: BkOTPlugin[];
63
+ privacy?: BkOTPrivacyConfig;
64
+ route?: BkOTRouteConfig;
65
+ rum?: BkOTRumOptions;
66
+ sampling?: BkOTSamplingConfig;
67
+ spanBatch?: BkOTBatchConfig;
68
+ spanProcessor?: BkOTSpanProcessorMode;
69
+ transport?: BkOTTransportConfig;
70
+ user?: BkOTUserContext;
71
+ }
72
+ export interface BkOTPrivacyConfig {
73
+ redactAttributes?: (attributes: Attributes) => Attributes;
74
+ redactUrl?: (url: string) => string;
75
+ }
76
+ export interface BkOTRouteConfig {
77
+ /**
78
+ * 返回低基数路由分组,例如 `/trace/:id`。
79
+ * SDK 默认退化为 URL pathname;复杂业务路由建议接入方显式提供模板。
80
+ */
81
+ getPathGroup?: (url: string) => string | undefined;
67
82
  }
68
- export interface BkOTRumConfig extends BkOTInstrumentationsConfig {
83
+ export interface BkOTRumOptions {
84
+ instrumentations?: BkOTInstrumentationsConfig;
85
+ plugins?: BkOTRumPluginOptions;
86
+ }
87
+ export interface BkOTRumPluginOptions {
69
88
  cspViolation?: boolean;
70
89
  device?: boolean | {
71
90
  storageKey?: string;
72
91
  };
73
92
  httpBody?: BkOTHttpBodyConfig | boolean;
74
93
  pageView?: boolean;
94
+ resource?: boolean;
75
95
  routeTiming?: boolean;
76
96
  websocket?: boolean;
77
97
  webVitals?: boolean;
98
+ action?: boolean | {
99
+ eventNames?: Array<'click' | 'input' | 'keydown' | 'pointerdown' | 'scroll' | 'submit'>;
100
+ };
78
101
  blankScreen?: {
79
102
  checkDelay?: number;
80
103
  ignoreSelectors?: string[];
@@ -94,34 +117,99 @@ export interface BkOTRumConfig extends BkOTInstrumentationsConfig {
94
117
  storageKey?: string;
95
118
  } | boolean;
96
119
  }
120
+ export interface BkOTSamplingConfig {
121
+ rate?: number;
122
+ }
97
123
  export type BkOTSpanProcessorMode = 'batch' | 'simple';
98
- export interface NormalizedBkOTConfig extends Omit<BkOTConfig, 'attributes' | 'autoStart' | 'debug' | 'enabled' | 'endpoint' | 'environment' | 'instrumentations' | 'logs' | 'metrics' | 'plugins' | 'redact' | 'signalEndpoints' | 'spanProcessor' | 'token' | 'traces'> {
124
+ export interface BkOTTransportConfig {
125
+ endpoint?: string;
126
+ headers?: Record<string, string>;
127
+ signals?: SignalExportersInputConfig;
128
+ token?: string;
129
+ }
130
+ export interface ResolvedAppConfig {
131
+ environment: string;
132
+ name: string;
133
+ version?: string;
134
+ }
135
+ export interface ResolvedBkOTConfig {
136
+ app: ResolvedAppConfig;
99
137
  autoStart: boolean;
100
138
  debug: boolean;
101
139
  enabled: boolean;
102
- endpoint: string;
103
- environment: string;
140
+ hooks: ResolvedConfigHooks;
104
141
  instrumentations: Instrumentation[];
105
- logs: SignalExporterConfig;
106
142
  metricIntervalMillis: number;
107
- metrics: SignalExporterConfig;
108
143
  plugins: BkOTPlugin[];
109
144
  resourceAttributes: Attributes;
110
- sampleRate: number;
145
+ rum: ResolvedRumConfig;
146
+ sampling: ResolvedSamplingConfig;
147
+ spanBatch?: BkOTBatchConfig;
111
148
  spanProcessor: BkOTSpanProcessorMode;
112
- traces: SignalExporterConfig;
149
+ transport: ResolvedTransportConfig;
150
+ user?: BkOTUserContext;
151
+ }
152
+ export interface ResolvedConfigHooks {
113
153
  getCustomAttributes: () => Attributes;
114
154
  getErrorAttributes: () => Attributes;
115
155
  getMetricAttributes: () => Attributes;
116
156
  getPageAttributes: () => Attributes;
157
+ getViewPathGroup: (url: string) => string;
117
158
  redactAttributes: (attributes: Attributes) => Attributes;
118
159
  redactUrl: (url: string) => string;
119
- commonInstrumentations: Required<Pick<BkOTInstrumentationsConfig, 'documentLoad' | 'fetch' | 'xhr'>> & {
160
+ }
161
+ export type ResolvedPluginOption<TConfig extends object = object> = (TConfig & {
162
+ enabled: true;
163
+ }) | {
164
+ enabled: false;
165
+ };
166
+ export interface ResolvedRumConfig {
167
+ plugins: ResolvedRumPluginConfig;
168
+ instrumentations: Required<Pick<BkOTInstrumentationsConfig, 'documentLoad' | 'fetch' | 'xhr'>> & {
120
169
  userInteraction: BkOTInstrumentationsConfig['userInteraction'];
121
170
  };
122
- rum: Omit<Required<BkOTRumConfig>, 'documentLoad' | 'fetch' | 'httpBody' | 'userInteraction' | 'xhr'> & {
123
- httpBody: false | Required<BkOTHttpBodyConfig>;
124
- };
171
+ }
172
+ export interface ResolvedRumPluginConfig {
173
+ cspViolation: ResolvedPluginOption;
174
+ device: ResolvedPluginOption<{
175
+ storageKey?: string;
176
+ }>;
177
+ error: ResolvedPluginOption<{
178
+ maxPerWindow: number;
179
+ windowMs: number;
180
+ }>;
181
+ httpBody: ResolvedPluginOption<Required<BkOTHttpBodyConfig>>;
182
+ longTask: ResolvedPluginOption<{
183
+ threshold: number;
184
+ }>;
185
+ pageView: ResolvedPluginOption;
186
+ resource: ResolvedPluginOption;
187
+ routeTiming: ResolvedPluginOption;
188
+ websocket: ResolvedPluginOption;
189
+ webVitals: ResolvedPluginOption;
190
+ action: ResolvedPluginOption<{
191
+ eventNames: Array<'click' | 'input' | 'keydown' | 'pointerdown' | 'scroll' | 'submit'>;
192
+ }>;
193
+ blankScreen: ResolvedPluginOption<{
194
+ checkDelay: number;
195
+ ignoreSelectors?: string[];
196
+ rootSelector?: string;
197
+ threshold: number;
198
+ }>;
199
+ session: ResolvedPluginOption<{
200
+ inactivityMs: number;
201
+ maxLifetimeMs: number;
202
+ storageKey?: string;
203
+ }>;
204
+ }
205
+ export interface ResolvedSamplingConfig {
206
+ rate: number;
207
+ }
208
+ export interface ResolvedTransportConfig {
209
+ endpoint: string;
210
+ logs: SignalExporterConfig;
211
+ metrics: SignalExporterConfig;
212
+ traces: SignalExporterConfig;
125
213
  }
126
214
  export interface SignalEndpointsConfig {
127
215
  logs?: string;
@@ -135,4 +223,10 @@ export interface SignalExporterConfig {
135
223
  timeoutMillis?: number;
136
224
  }
137
225
  export type SignalExporterInputConfig = Partial<SignalExporterConfig>;
138
- export declare const normalizeConfig: (config: BkOTConfig) => NormalizedBkOTConfig;
226
+ export interface SignalExportersInputConfig {
227
+ endpoints?: SignalEndpointsConfig;
228
+ logs?: SignalExporterInputConfig;
229
+ metrics?: SignalExporterInputConfig;
230
+ traces?: SignalExporterInputConfig;
231
+ }
232
+ export declare const resolveConfig: (config: BkOTOptions) => ResolvedBkOTConfig;
@@ -1,15 +1,16 @@
1
- import type { NormalizedBkOTConfig } from './config';
1
+ import type { ResolvedBkOTConfig } from './config';
2
+ import type { BkOTUserContext, BkOTViewContext } from './runtime-context';
2
3
  import type { Attributes, Meter, Span, TimeInput, Tracer } from '@opentelemetry/api';
3
4
  import type { Logger, LogRecord } from '@opentelemetry/api-logs';
4
5
  export interface BkOTPlugin {
5
- enabled?: ((config: NormalizedBkOTConfig) => boolean) | boolean;
6
+ enabled?: ((config: ResolvedBkOTConfig) => boolean) | boolean;
6
7
  name: string;
7
8
  flush?: () => Promise<void> | void;
8
9
  init: (context: BkOTRuntimeContext) => Promise<void> | void;
9
10
  shutdown?: () => Promise<void> | void;
10
11
  }
11
12
  export interface BkOTRuntimeContext {
12
- config: NormalizedBkOTConfig;
13
+ config: ResolvedBkOTConfig;
13
14
  logger: Logger;
14
15
  meter: Meter;
15
16
  tracer: Tracer;
@@ -19,6 +20,10 @@ export interface BkOTRuntimeContext {
19
20
  emitLog: (record: LogRecord) => void;
20
21
  getRuntimeAttributes: () => Attributes;
21
22
  setRuntimeAttributes: (attributes: Attributes) => void;
23
+ /** 更新用户上下文;user.id 由接入方传入,SDK 原样透传 */
24
+ setUser: (user: BkOTUserContext) => void;
25
+ /** 更新当前 View 上下文,后续 span/log 会自动带上 view.* 关联字段 */
26
+ setView: (view: BkOTViewContext) => void;
22
27
  startSpan: (name: string, attributes?: Attributes, options?: BkOTStartSpanOptions) => Span;
23
28
  }
24
29
  /**
@@ -34,7 +39,7 @@ export interface BkOTRuntimeContext {
34
39
  export interface BkOTStartSpanOptions {
35
40
  startTime?: TimeInput;
36
41
  }
37
- export declare const isPluginEnabled: (plugin: BkOTPlugin, config: NormalizedBkOTConfig) => boolean;
42
+ export declare const isPluginEnabled: (plugin: BkOTPlugin, config: ResolvedBkOTConfig) => boolean;
38
43
  export declare const createPlugin: (plugin: BkOTPlugin) => BkOTPlugin;
39
44
  export declare class PluginManager {
40
45
  private readonly plugins;
@@ -1,4 +1,4 @@
1
- import type { NormalizedBkOTConfig } from './config';
1
+ import type { ResolvedBkOTConfig } from './config';
2
2
  import type { Context } from '@opentelemetry/api';
3
3
  import type { ReadableSpan, Span, SpanProcessor } from '@opentelemetry/sdk-trace-base';
4
4
  /**
@@ -8,7 +8,7 @@ import type { ReadableSpan, Span, SpanProcessor } from '@opentelemetry/sdk-trace
8
8
  export declare class FilteringSpanProcessor implements SpanProcessor {
9
9
  private readonly inner;
10
10
  private readonly config;
11
- constructor(inner: SpanProcessor, config: NormalizedBkOTConfig);
11
+ constructor(inner: SpanProcessor, config: ResolvedBkOTConfig);
12
12
  forceFlush(): Promise<void>;
13
13
  onEnd(span: ReadableSpan): void;
14
14
  onStart(span: Span, parentContext: Context): void;
@@ -0,0 +1,30 @@
1
+ import type { Attributes } from '@opentelemetry/api';
2
+ export interface BkOTUserContext {
3
+ /** 由接入方传入并由 SDK 原样透传;SDK 不 hash、不脱敏、不改写。 */
4
+ id?: string;
5
+ }
6
+ export interface BkOTViewContext {
7
+ id?: string;
8
+ loadingType?: 'initial_load' | 'route_change';
9
+ url?: string;
10
+ urlPathGroup?: string;
11
+ }
12
+ /**
13
+ * 管理 SDK 运行期公共属性快照。
14
+ *
15
+ * 这里仅保存“后续事件都需要带上”的低成本上下文,Session/View/用户切换时更新快照,
16
+ * 插件发 span/log 时读取当前快照即可,避免每个插件重复维护公共字段。
17
+ */
18
+ export declare class RumRuntimeContext {
19
+ private readonly attributes;
20
+ private readonly setOptionalAttribute;
21
+ private user;
22
+ private view;
23
+ getAttributes: () => Attributes;
24
+ getUser: () => BkOTUserContext;
25
+ getView: () => BkOTViewContext;
26
+ setAttributes: (attributes: Attributes) => void;
27
+ setUser: (user: BkOTUserContext) => void;
28
+ setView: (view: BkOTViewContext) => void;
29
+ constructor(initialAttributes?: Attributes, user?: BkOTUserContext);
30
+ }
@@ -1,2 +1,2 @@
1
- import type { NormalizedBkOTConfig } from './config';
2
- export declare const isBkOTSampled: ({ sampleRate }: Pick<NormalizedBkOTConfig, "sampleRate">) => boolean;
1
+ import type { ResolvedBkOTConfig } from './config';
2
+ export declare const isBkOTSampled: ({ sampling }: Pick<ResolvedBkOTConfig, "sampling">) => boolean;
@@ -1,11 +1,15 @@
1
1
  import { type Attributes, type Span } from '@opentelemetry/api';
2
2
  import { type Instrumentation } from '@opentelemetry/instrumentation';
3
- import { type BkOTConfig, type BkOTCustomEventPayload, type NormalizedBkOTConfig } from './config';
3
+ import { type BkOTActionEventPayload, type BkOTCustomEventPayload, type BkOTOptions, 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
6
  import type { LogRecord } from '@opentelemetry/api-logs';
6
7
  export interface BkOTInstance extends BkOTRuntimeContext {
7
8
  flush: () => Promise<void>;
9
+ reportAction: (payload: BkOTActionEventPayload) => void;
8
10
  reportCustomEvent: (payload: BkOTCustomEventPayload) => void;
11
+ setUser: (user: BkOTUserContext) => void;
12
+ setView: (view: BkOTViewContext) => void;
9
13
  shutdown: () => Promise<void>;
10
14
  start: () => Promise<void>;
11
15
  use: (plugin: BkOTPlugin) => BkOTInstance;
@@ -18,27 +22,28 @@ export declare class BkOpenTelemetry implements BkOTInstance {
18
22
  private readonly pluginManager;
19
23
  private readonly plugins;
20
24
  private readonly registeredInstrumentations;
21
- private readonly runtimeAttributes;
25
+ private readonly runtimeContext;
22
26
  private readonly sampled;
23
27
  private readonly teardownCallbacks;
24
28
  private readonly tracerProvider;
25
29
  private assertBeforeStart;
26
30
  readonly applyRedact: (attributes: Attributes) => Attributes;
27
- readonly config: NormalizedBkOTConfig;
31
+ readonly config: ResolvedBkOTConfig;
28
32
  readonly emitLog: (record: LogRecord) => void;
29
33
  readonly flush: () => Promise<void>;
30
- readonly getRuntimeAttributes: () => {
31
- [attributeKey: string]: import("@opentelemetry/api").AttributeValue | undefined;
32
- };
34
+ readonly getRuntimeAttributes: () => Attributes;
33
35
  readonly logger: BkOTRuntimeContext['logger'];
34
36
  readonly meter: BkOTRuntimeContext['meter'];
37
+ readonly reportAction: ({ attributes, targetTag, targetText, type }: BkOTActionEventPayload) => void;
35
38
  readonly reportCustomEvent: ({ attributes, error, name }: BkOTCustomEventPayload) => void;
36
39
  readonly setRuntimeAttributes: (attributes: Attributes) => void;
40
+ readonly setUser: (user: BkOTUserContext) => void;
41
+ readonly setView: (view: BkOTViewContext) => void;
37
42
  readonly shutdown: () => Promise<void>;
38
43
  readonly start: () => Promise<void>;
39
44
  readonly startSpan: (name: string, attributes?: Attributes, options?: BkOTStartSpanOptions) => Span;
40
45
  readonly tracer: BkOTRuntimeContext['tracer'];
41
46
  readonly use: (plugin: BkOTPlugin) => this;
42
47
  readonly useInstrumentation: (instrumentation: Instrumentation) => this;
43
- constructor(inputConfig: BkOTConfig);
48
+ constructor(inputConfig: BkOTOptions);
44
49
  }
@@ -1,4 +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;
1
+ import type { ResolvedBkOTConfig } from './config';
2
+ export declare const isBkOTEndpoint: (config: ResolvedBkOTConfig, url: string) => boolean;
3
+ export declare const shouldIgnoreUrl: (config: ResolvedBkOTConfig, url: string) => boolean;
4
+ export declare const shouldPropagateTraceHeader: (_config: ResolvedBkOTConfig, url: string) => boolean;
package/dist/index.d.ts CHANGED
@@ -1,7 +1,11 @@
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';
1
+ export { type BkOTActionEventPayload, type BkOTAppConfig, type BkOTAttributesConfig, type BkOTAttributeValue, type BkOTBatchConfig, type BkOTCustomEventPayload, type BkOTHttpBodyConfig, type BkOTHttpBodyRedactPayload, type BkOTInstrumentationsConfig, type BkOTOptions, type BkOTPrivacyConfig, type BkOTRumOptions, type BkOTRumPluginOptions, type BkOTSamplingConfig, type BkOTTransportConfig, resolveConfig, type ResolvedBkOTConfig, type ResolvedConfigHooks, type ResolvedPluginOption, type ResolvedRumConfig, type ResolvedRumPluginConfig, type ResolvedTransportConfig, type SignalExporterConfig, type SignalExportersInputConfig, } from './core/config';
2
2
  export { type BkOTPlugin, type BkOTRuntimeContext, createPlugin, type PluginManager } from './core/plugin';
3
3
  export { FilteringSpanProcessor } from './core/processor';
4
+ export { type BkOTUserContext, type BkOTViewContext, RumRuntimeContext } from './core/runtime-context';
4
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 } from './plugins/action';
5
9
  export { createBlankScreenPlugin } from './plugins/blank-screen';
6
10
  export { createCommonInstrumentationsPlugin } from './plugins/common';
7
11
  export { createCspViolationPlugin } from './plugins/csp-violation';
@@ -10,6 +14,7 @@ export { createErrorPlugin } from './plugins/error';
10
14
  export { createHttpBodyPlugin } from './plugins/http-body';
11
15
  export { createLongTaskPlugin } from './plugins/long-task';
12
16
  export { createPageViewPlugin } from './plugins/page-view';
17
+ export { createResourcePlugin } from './plugins/resource';
13
18
  export { createRouteTimingPlugin } from './plugins/route-timing';
14
19
  export { createSessionPlugin } from './plugins/session';
15
20
  export { createWebVitalsPlugin } from './plugins/web-vitals';