@blueking/open-telemetry 0.0.5 → 0.0.6

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.
@@ -31,6 +31,7 @@ export interface BkOTConfig {
31
31
  sampleRate?: number;
32
32
  signalEndpoints?: SignalEndpointsConfig;
33
33
  spanBatch?: BkOTBatchConfig;
34
+ spanProcessor?: BkOTSpanProcessorMode;
34
35
  token?: string;
35
36
  traces?: SignalExporterInputConfig;
36
37
  }
@@ -89,10 +90,12 @@ export interface BkOTRumConfig extends BkOTInstrumentationsConfig {
89
90
  } | boolean;
90
91
  session?: {
91
92
  inactivityMs?: number;
93
+ maxLifetimeMs?: number;
92
94
  storageKey?: string;
93
95
  } | boolean;
94
96
  }
95
- export interface NormalizedBkOTConfig extends Omit<BkOTConfig, 'attributes' | 'autoStart' | 'debug' | 'enabled' | 'endpoint' | 'environment' | 'instrumentations' | 'logs' | 'metrics' | 'plugins' | 'redact' | 'signalEndpoints' | 'token' | 'traces'> {
97
+ 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'> {
96
99
  autoStart: boolean;
97
100
  debug: boolean;
98
101
  enabled: boolean;
@@ -105,6 +108,7 @@ export interface NormalizedBkOTConfig extends Omit<BkOTConfig, 'attributes' | 'a
105
108
  plugins: BkOTPlugin[];
106
109
  resourceAttributes: Attributes;
107
110
  sampleRate: number;
111
+ spanProcessor: BkOTSpanProcessorMode;
108
112
  traces: SignalExporterConfig;
109
113
  getCustomAttributes: () => Attributes;
110
114
  getErrorAttributes: () => Attributes;
@@ -0,0 +1,8 @@
1
+ /**
2
+ * 生成随机 ID。优先用 `crypto.randomUUID`(standards-compliant UUID v4),不可用时回退到
3
+ * `${prefix}-${timestamp}-${rand}` 形式。
4
+ *
5
+ * 注意:fallback 实现使用 Math.random,**不是密码学安全**,仅适用于设备/会话/事件标识等场景,
6
+ * 不要复用此函数生成 token、CSRF 防护等安全相关的值。
7
+ */
8
+ export declare const createRandomId: (prefix: string) => string;
@@ -1,5 +1,5 @@
1
1
  import type { NormalizedBkOTConfig } from './config';
2
- import type { Attributes, Meter, Span, Tracer } from '@opentelemetry/api';
2
+ import type { Attributes, Meter, Span, TimeInput, Tracer } from '@opentelemetry/api';
3
3
  import type { Logger, LogRecord } from '@opentelemetry/api-logs';
4
4
  export interface BkOTPlugin {
5
5
  enabled?: ((config: NormalizedBkOTConfig) => boolean) | boolean;
@@ -19,7 +19,20 @@ export interface BkOTRuntimeContext {
19
19
  emitLog: (record: LogRecord) => void;
20
20
  getRuntimeAttributes: () => Attributes;
21
21
  setRuntimeAttributes: (attributes: Attributes) => void;
22
- startSpan: (name: string, attributes?: Attributes) => Span;
22
+ startSpan: (name: string, attributes?: Attributes, options?: BkOTStartSpanOptions) => Span;
23
+ }
24
+ /**
25
+ * startSpan 的扩展选项。
26
+ *
27
+ * 用于"事件实际发生在过去某个时刻,但 span 现在才创建"的场景:
28
+ * 例如 route-timing 在 raf+setTimeout 链路结束后才上报,但希望 span 的时间窗
29
+ * 反映真实的"路由切换瞬间到渲染完成"区间,方便 trace 视图与并发请求对齐。
30
+ *
31
+ * startTime 接受 OT TimeInput;常用 epoch 毫秒(performance.timeOrigin + performance.now())。
32
+ * 配合 span.end(endTime?) 可控制结束时间。
33
+ */
34
+ export interface BkOTStartSpanOptions {
35
+ startTime?: TimeInput;
23
36
  }
24
37
  export declare const isPluginEnabled: (plugin: BkOTPlugin, config: NormalizedBkOTConfig) => boolean;
25
38
  export declare const createPlugin: (plugin: BkOTPlugin) => BkOTPlugin;
@@ -1,7 +1,7 @@
1
1
  import { type Attributes, type Span } from '@opentelemetry/api';
2
2
  import { type Instrumentation } from '@opentelemetry/instrumentation';
3
3
  import { type BkOTConfig, type BkOTCustomEventPayload, type NormalizedBkOTConfig } from './config';
4
- import { type BkOTPlugin, type BkOTRuntimeContext } from './plugin';
4
+ import { type BkOTPlugin, type BkOTRuntimeContext, type BkOTStartSpanOptions } from './plugin';
5
5
  import type { LogRecord } from '@opentelemetry/api-logs';
6
6
  export interface BkOTInstance extends BkOTRuntimeContext {
7
7
  flush: () => Promise<void>;
@@ -36,7 +36,7 @@ export declare class BkOpenTelemetry implements BkOTInstance {
36
36
  readonly setRuntimeAttributes: (attributes: Attributes) => void;
37
37
  readonly shutdown: () => Promise<void>;
38
38
  readonly start: () => Promise<void>;
39
- readonly startSpan: (name: string, attributes?: Attributes) => Span;
39
+ readonly startSpan: (name: string, attributes?: Attributes, options?: BkOTStartSpanOptions) => Span;
40
40
  readonly tracer: BkOTRuntimeContext['tracer'];
41
41
  readonly use: (plugin: BkOTPlugin) => this;
42
42
  readonly useInstrumentation: (instrumentation: Instrumentation) => this;
@@ -0,0 +1,20 @@
1
+ /**
2
+ * 简易 djb2 字符串 hash。结果稳定、短(base36),足以区分常见 fingerprint 字符串组合。
3
+ * 不是密码学安全 hash,仅用于事件聚合分组场景。
4
+ */
5
+ export declare const hashString: (input: string) => string;
6
+ export interface ThrottleResult {
7
+ /** 是否允许本次上报 */
8
+ allowed: boolean;
9
+ /** 当前窗口内同 fingerprint 的总触发次数(含本次,含被丢弃次数),便于下游感知是否被节流 */
10
+ count: number;
11
+ }
12
+ /**
13
+ * 时间窗节流:同一 key 在 windowMs 窗口内最多允许 maxPerWindow 次通过;
14
+ * 超出则被拦截,但 `count` 仍累加,便于下游感知风暴规模。
15
+ *
16
+ * 长会话场景下避免 records 无限累积——按窗口长度节奏轻量清理一次过期 entry。
17
+ */
18
+ export declare const createThrottle: (windowMs: number, maxPerWindow: number) => {
19
+ allow(key: string): ThrottleResult;
20
+ };