@blueking/open-telemetry 0.0.26 → 0.0.28

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.
@@ -71,8 +71,9 @@ export interface BkOTRequestConfig {
71
71
  allowedTracingUrls?: BkOTUrlMatcher | BkOTUrlMatcher[];
72
72
  excludedUrls?: BkOTUrlMatcher | BkOTUrlMatcher[];
73
73
  beforeSend?: (event: BkOTRequestEvent, context: BkOTRequestContext) => boolean | void;
74
+ /** 返回请求 URL 的低基数模板,例如 `/api/orders/{id}`;返回空值时回退到默认折叠规则。 */
75
+ getUrlTemplate?: (url: string) => string | undefined;
74
76
  }
75
- export type BkOTRequestOutcome = RumRequestOutcome;
76
77
  export type BkOTRequestContext = {
77
78
  error?: unknown;
78
79
  request: Request | RequestInit;
@@ -92,6 +93,7 @@ export interface BkOTRequestEvent {
92
93
  url?: string;
93
94
  };
94
95
  }
96
+ export type BkOTRequestOutcome = RumRequestOutcome;
95
97
  export type BkOTSpanProcessorMode = 'batch' | 'simple';
96
98
  export interface BkOTTrackingConfig {
97
99
  action?: BkOTActionPluginOptions | boolean;
@@ -124,8 +126,8 @@ export interface BkOTViewConfig {
124
126
  /** 自动计算初始加载与 SPA 路由的页面稳定耗时,默认开启。 */
125
127
  loadingTime?: boolean;
126
128
  tracking?: 'auto' | 'manual';
127
- /** 返回低基数路由分组,例如 `/trace/:id`。 */
128
- getPathGroup?: (url: string) => string | undefined;
129
+ /** 返回页面路由的低基数模板,例如 `/trace/:id`;返回空值时回退到默认折叠规则。 */
130
+ getUrlTemplate?: (url: string) => string | undefined;
129
131
  }
130
132
  export interface ResolvedApplicationConfig {
131
133
  environment: string;
@@ -154,7 +156,8 @@ export interface ResolvedConfigHooks {
154
156
  getCustomAttributes: () => Attributes;
155
157
  getErrorAttributes: () => Attributes;
156
158
  getPageAttributes: () => Attributes;
157
- getViewPathGroup: (url: string) => string;
159
+ getRequestUrlTemplate: (url: string) => string;
160
+ getViewUrlTemplate: (url: string) => string;
158
161
  redactAttributes: (attributes: Attributes) => Attributes;
159
162
  redactUrl: (url: string) => string;
160
163
  }
@@ -1,7 +1,7 @@
1
1
  import type { BkOTActivityEvent } from './action-activity';
2
2
  import type { ResolvedBkOTConfig } from './config';
3
3
  import type { BkOTSessionLifecycleEvent, BkOTUserContext, BkOTViewCommand, BkOTViewContext } from './runtime-context';
4
- import type { Attributes, Span, SpanKind, TimeInput, Tracer } from '@opentelemetry/api';
4
+ import type { Attributes, Link, Span, SpanKind, TimeInput, Tracer } from '@opentelemetry/api';
5
5
  import type { Instrumentation } from '@opentelemetry/instrumentation';
6
6
  export interface BkOTPlugin {
7
7
  enabled?: ((config: ResolvedBkOTConfig) => boolean) | boolean;
@@ -49,6 +49,8 @@ export interface BkOTStartSpanOptions {
49
49
  /** 用于从历史中解析 Session/View/Action 归属;默认复用数字类型 startTime。 */
50
50
  contextTime?: number;
51
51
  kind?: SpanKind;
52
+ /** 关联其他 trace 的 Span Link;OTel 要求在创建时传入。 */
53
+ links?: Link[];
52
54
  startTime?: TimeInput;
53
55
  }
54
56
  export declare const isPluginEnabled: (plugin: BkOTPlugin, config: ResolvedBkOTConfig) => boolean;
@@ -37,7 +37,7 @@ export interface BkOTViewContext {
37
37
  name?: string;
38
38
  startedAt?: number;
39
39
  url?: string;
40
- urlPathGroup?: string;
40
+ urlTemplate?: string;
41
41
  }
42
42
  /**
43
43
  * 管理 SDK 运行期公共属性快照。
@@ -0,0 +1,36 @@
1
+ /**
2
+ * 被观测请求的 Trace Context 规划。
3
+ *
4
+ * 后端 APM 只解析 W3C `traceparent`,并且要求一个后端 trace 只对应一个 API 请求,因此这里不复用
5
+ * RUM Span 自身的 trace context,而是为每个命中白名单的请求生成独立的 trace-id/span-id,再通过
6
+ * Span Link 把后端 trace 关联回 RUM Span。
7
+ *
8
+ * 如果业务已经自行设置了 trace header(`traceparent`、`sentry-trace`、b3、Datadog、sw8),则完全
9
+ * 不注入,避免破坏业务既有的传播方式,只把已有链路建成 Link。
10
+ *
11
+ * Link 结构恒定:`context` 承载 trace-id/span-id,`attributes` 记录命中的格式与原始 header。
12
+ * sw8 这类无法映射为合法 OTel id 的格式会补一对占位 id,让 Link 结构保持统一。
13
+ */
14
+ import type { Attributes, Link } from '@opentelemetry/api';
15
+ /** 大小写不敏感地读取一个请求 header */
16
+ export type BkOTHeaderReader = (name: string) => string | undefined;
17
+ export interface BkOTRequestTracePlan {
18
+ /** 关联后端 trace 的 Span Link,恒为一条 */
19
+ links: Link[];
20
+ /** 需要注入的 traceparent;业务已有 trace header 时为 undefined */
21
+ traceparent?: string;
22
+ }
23
+ export type BkOTUpstreamTraceFormat = 'b3' | 'b3-multi' | 'datadog' | 'sentry-trace' | 'sw8' | 'traceparent';
24
+ export declare const TRACEPARENT_HEADER = "traceparent";
25
+ interface UpstreamTrace {
26
+ format: BkOTUpstreamTraceFormat;
27
+ /** 命中格式的原始 header,原样写入 Link Attributes */
28
+ headers: Attributes;
29
+ sampled?: boolean;
30
+ /** 规范化后的合法 OTel id;无法映射时两者同时缺失 */
31
+ spanId?: string;
32
+ traceId?: string;
33
+ }
34
+ export declare const detectUpstreamTrace: (getHeader: BkOTHeaderReader) => UpstreamTrace | undefined;
35
+ export declare const planRequestTrace: (getHeader: BkOTHeaderReader) => BkOTRequestTracePlan;
36
+ export {};
@@ -1,5 +1,10 @@
1
1
  import type { BkOTUrlMatcher, ResolvedBkOTConfig } from './config';
2
2
  export declare const toAbsoluteUrl: (url: string) => string;
3
+ /**
4
+ * 常见数字 ID、UUID 和长十六进制段统一折叠为 :id,便于服务端按路由聚合。
5
+ * 同时服务 `view.url_template`(页面路由)与 `url.template`(请求路径)两个维度。
6
+ */
7
+ export declare const normalizeUrlPathTemplate: (pathGroup: string) => string;
3
8
  export declare const isBkOTEndpoint: (config: ResolvedBkOTConfig, url: string) => boolean;
4
9
  export declare const shouldIgnoreUrl: (config: ResolvedBkOTConfig, url: string) => boolean;
5
10
  export declare const matchesUrlList: (matchers: BkOTUrlMatcher | BkOTUrlMatcher[] | undefined, url: string) => boolean;