@lark-apaas/observable-web 1.0.0-alpha.0 → 1.0.0-alpha.10

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,11 @@
1
+ declare const defaultResourceAttr: {
2
+ sdk_version: string;
3
+ sdk_lang: string;
4
+ sdk_type: string;
5
+ };
6
+ declare enum AppEnv {
7
+ Dev = "preview",
8
+ Prod = "runtime"
9
+ }
10
+
11
+ export { AppEnv, defaultResourceAttr };
package/dist/lib/const.js CHANGED
@@ -1,3 +1,4 @@
1
+ "use strict";
1
2
  var __defProp = Object.defineProperty;
2
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
@@ -23,8 +24,8 @@ __export(const_exports, {
23
24
  module.exports = __toCommonJS(const_exports);
24
25
  const defaultResourceAttr = {
25
26
  sdk_version: "1.0.1-alpha.18",
26
- sdk_lang: "web",
27
- sdk_type: "client-web"
27
+ sdk_lang: "javascript",
28
+ sdk_type: "client-frontend"
28
29
  };
29
30
  var AppEnv = /* @__PURE__ */ ((AppEnv2) => {
30
31
  AppEnv2["Dev"] = "preview";
@@ -0,0 +1,17 @@
1
+ import { Meter } from '@opentelemetry/api';
2
+
3
+ declare class PvInstrumentation {
4
+ private meter;
5
+ private lastUrl;
6
+ constructor(meter: Meter);
7
+ /**
8
+ * 开启自动采集
9
+ */
10
+ enable(): void;
11
+ /**
12
+ * Monkey Patch 历史记录 API
13
+ */
14
+ private patchHistory;
15
+ }
16
+
17
+ export { PvInstrumentation };
@@ -1,8 +1,8 @@
1
+ "use strict";
1
2
  var __defProp = Object.defineProperty;
2
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
4
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
5
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
6
6
  var __export = (target, all) => {
7
7
  for (var name in all)
8
8
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -16,24 +16,20 @@ var __copyProps = (to, from, except, desc) => {
16
16
  return to;
17
17
  };
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
- var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
20
19
  var PvInstrumentation_exports = {};
21
20
  __export(PvInstrumentation_exports, {
22
21
  PvInstrumentation: () => PvInstrumentation
23
22
  });
24
23
  module.exports = __toCommonJS(PvInstrumentation_exports);
25
- var import_getDeviceID = require("../../utils/getDeviceID");
26
24
  class PvInstrumentation {
27
25
  constructor(meter) {
28
- __publicField(this, "meter");
29
- __publicField(this, "lastUrl");
30
26
  this.meter = meter;
31
27
  }
32
28
  /**
33
29
  * 开启自动采集
34
30
  */
35
31
  enable() {
36
- const pvCounter = this.meter.createCounter("page_view", {
32
+ const pvCounter = this.meter.createCounter("apaas_internal_metrics_pv_uv", {
37
33
  description: "Count of page views"
38
34
  });
39
35
  const reportPv = () => {
@@ -41,14 +37,9 @@ class PvInstrumentation {
41
37
  if (this.lastUrl === currentUrl) return;
42
38
  this.lastUrl = currentUrl;
43
39
  pvCounter.add(1, {
44
- "page_url": window.location.href,
45
- "page_path": window.location.pathname,
46
- "page_title": document.title,
47
- "page_referrer": document.referrer,
48
- "device_id": (0, import_getDeviceID.getDeviceId)(),
49
- "user_id": window.userId || "",
50
- "tenant_id": window.tenantID || "",
51
- "app_id": window.appId || ""
40
+ url: window.location.href,
41
+ path: window.location.pathname,
42
+ referrer: document.referrer
52
43
  });
53
44
  };
54
45
  window.addEventListener("load", reportPv);
@@ -0,0 +1,25 @@
1
+ import { LogRecordExporter, ReadableLogRecord } from '@opentelemetry/sdk-logs';
2
+ import { ExportResult } from '@opentelemetry/core';
3
+ import { AppEnv } from '../const.js';
4
+
5
+ interface CustomExporterConfig {
6
+ url: string;
7
+ /** 环境标识 */
8
+ env: AppEnv;
9
+ /** 自定义请求头 (例如鉴权 Token) */
10
+ headers?: Record<string, string>;
11
+ }
12
+ declare class CustomWebLogExporter implements LogRecordExporter {
13
+ private url;
14
+ private headers;
15
+ private appEnv;
16
+ constructor(config: CustomExporterConfig);
17
+ export(logs: ReadableLogRecord[], resultCallback: (result: ExportResult) => void): void;
18
+ /**
19
+ * 封装 Fetch 逻辑
20
+ */
21
+ private sendWithFetch;
22
+ shutdown(): Promise<void>;
23
+ }
24
+
25
+ export { type CustomExporterConfig, CustomWebLogExporter };
@@ -1,3 +1,4 @@
1
+ "use strict";
1
2
  var __defProp = Object.defineProperty;
2
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
@@ -29,7 +30,6 @@ var __copyProps = (to, from, except, desc) => {
29
30
  return to;
30
31
  };
31
32
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
32
- var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
33
33
  var __async = (__this, __arguments, generator) => {
34
34
  return new Promise((resolve, reject) => {
35
35
  var fulfilled = (value) => {
@@ -60,11 +60,9 @@ var import_convertAllAttrIntoString = require("../utils/convertAllAttrIntoString
60
60
  var import_hrTimeToNanoNumber = require("../utils/hrTimeToNanoNumber");
61
61
  var import_generateUUID = require("../utils/generateUUID");
62
62
  var import_const = require("../const");
63
+ var import_getDefaultAttr = require("../utils/getDefaultAttr");
63
64
  class CustomWebLogExporter {
64
65
  constructor(config) {
65
- __publicField(this, "url");
66
- __publicField(this, "headers");
67
- __publicField(this, "appEnv");
68
66
  this.url = config.url;
69
67
  this.appEnv = config.env;
70
68
  this.headers = config.headers || {};
@@ -87,7 +85,7 @@ class CustomWebLogExporter {
87
85
  severityNumber: log.severityNumber,
88
86
  severityText: log.severityText,
89
87
  body: log.body,
90
- attributes: (0, import_convertAllAttrIntoString.convertAttributesToString)(__spreadValues(__spreadValues({}, defaultAttributes), (_a = log.attributes) != null ? _a : {})),
88
+ attributes: (0, import_convertAllAttrIntoString.convertAttributesToString)(__spreadValues(__spreadValues(__spreadValues({}, (_a = log.attributes) != null ? _a : {}), defaultAttributes), (0, import_getDefaultAttr.getDefaultAttr)())),
91
89
  traceID: (_b = log.spanContext) == null ? void 0 : _b.traceId,
92
90
  spanID: (_c = log.spanContext) == null ? void 0 : _c.spanId
93
91
  };
@@ -105,7 +103,8 @@ class CustomWebLogExporter {
105
103
  fetch(this.url, {
106
104
  method: "POST",
107
105
  headers: __spreadValues({
108
- "Content-Type": "application/json"
106
+ "Content-Type": "application/json",
107
+ "X-Suda-Csrf-Token": window.csrfToken || ""
109
108
  }, this.headers),
110
109
  body,
111
110
  // 关键: keepalive 允许请求在页面关闭后继续发送 (替代 sendBeacon, 避免存在 header 需传递但是 sendBeacon 不支持)
@@ -0,0 +1,42 @@
1
+ import { PushMetricExporter, ResourceMetrics, AggregationTemporality } from '@opentelemetry/sdk-metrics';
2
+ import { ExportResult } from '@opentelemetry/core';
3
+
4
+ interface CustomExporterConfig {
5
+ url: string;
6
+ headers?: Record<string, string>;
7
+ }
8
+ declare class CustomWebMetricExporter implements PushMetricExporter {
9
+ private url;
10
+ private headers;
11
+ private _shutdown;
12
+ constructor(config: CustomExporterConfig);
13
+ export(metrics: ResourceMetrics, resultCallback: (result: ExportResult) => void): void;
14
+ /**
15
+ * 将 OTel ResourceMetrics 转换为目标 MetricsData
16
+ */
17
+ private transform;
18
+ /**
19
+ * 转换数值型数据点 (Sum / Gauge)
20
+ */
21
+ private transformNumberDataPoints;
22
+ /**
23
+ * 转换直方图数据点
24
+ */
25
+ private transformHistogramDataPoints;
26
+ /**
27
+ * 映射聚合时间枚举
28
+ */
29
+ private mapTemporality;
30
+ /**
31
+ * 强制使用 DELTA 模式 (Web 端通常推荐)
32
+ */
33
+ forceFlush(): Promise<void>;
34
+ selectAggregationTemporality(_instrumentType: any): AggregationTemporality;
35
+ shutdown(): Promise<void>;
36
+ /**
37
+ * 发送逻辑
38
+ */
39
+ private send;
40
+ }
41
+
42
+ export { type CustomExporterConfig, CustomWebMetricExporter };
@@ -1,3 +1,4 @@
1
+ "use strict";
1
2
  var __defProp = Object.defineProperty;
2
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
@@ -29,7 +30,6 @@ var __copyProps = (to, from, except, desc) => {
29
30
  return to;
30
31
  };
31
32
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
32
- var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
33
33
  var __async = (__this, __arguments, generator) => {
34
34
  return new Promise((resolve, reject) => {
35
35
  var fulfilled = (value) => {
@@ -60,11 +60,11 @@ var import_core = require("@opentelemetry/core");
60
60
  var import_types = require("../types");
61
61
  var import_convertAllAttrIntoString = require("../utils/convertAllAttrIntoString");
62
62
  var import_hrTimeToNanoNumber = require("../utils/hrTimeToNanoNumber");
63
+ var import_getDefaultAttr = require("../utils/getDefaultAttr");
64
+ var import_const = require("../const");
63
65
  class CustomWebMetricExporter {
64
66
  constructor(config) {
65
- __publicField(this, "url");
66
- __publicField(this, "headers");
67
- __publicField(this, "_shutdown", false);
67
+ this._shutdown = false;
68
68
  this.url = config.url;
69
69
  this.headers = config.headers || {};
70
70
  }
@@ -125,7 +125,7 @@ class CustomWebMetricExporter {
125
125
  }
126
126
  return {
127
127
  resource: {
128
- attributes: (0, import_convertAllAttrIntoString.convertAttributesToString)(resourceMetrics.resource.attributes)
128
+ attributes: (0, import_convertAllAttrIntoString.convertAttributesToString)(__spreadValues(__spreadValues({}, resourceMetrics.resource.attributes), import_const.defaultResourceAttr))
129
129
  },
130
130
  metrics: targetMetrics
131
131
  };
@@ -135,7 +135,7 @@ class CustomWebMetricExporter {
135
135
  */
136
136
  transformNumberDataPoints(dataPoints) {
137
137
  return dataPoints.map((dp) => ({
138
- attributes: (0, import_convertAllAttrIntoString.convertAttributesToString)(dp.attributes),
138
+ attributes: __spreadValues(__spreadValues({}, (0, import_convertAllAttrIntoString.convertAttributesToString)(dp.attributes)), (0, import_getDefaultAttr.getDefaultAttr)()),
139
139
  startTimeUnixNano: (0, import_hrTimeToNanoNumber.hrTimeToNanosNumber)(dp.startTime),
140
140
  timeUnixNano: (0, import_hrTimeToNanoNumber.hrTimeToNanosNumber)(dp.endTime),
141
141
  // JS 中所有数字都是双精度浮点数,默认映射到 asDouble
@@ -149,7 +149,7 @@ class CustomWebMetricExporter {
149
149
  transformHistogramDataPoints(dataPoints) {
150
150
  return dataPoints.map((dp) => {
151
151
  return {
152
- attributes: (0, import_convertAllAttrIntoString.convertAttributesToString)(dp.attributes),
152
+ attributes: __spreadValues(__spreadValues({}, (0, import_convertAllAttrIntoString.convertAttributesToString)(dp.attributes)), (0, import_getDefaultAttr.getDefaultAttr)()),
153
153
  startTimeUnixNano: (0, import_hrTimeToNanoNumber.hrTimeToNanosNumber)(dp.startTime),
154
154
  timeUnixNano: (0, import_hrTimeToNanoNumber.hrTimeToNanosNumber)(dp.endTime),
155
155
  count: dp.value.count,
@@ -175,13 +175,13 @@ class CustomWebMetricExporter {
175
175
  }
176
176
  }
177
177
  /**
178
- * 强制使用 Cumulative 模式 (Web 端通常推荐)
178
+ * 强制使用 DELTA 模式 (Web 端通常推荐)
179
179
  */
180
180
  forceFlush() {
181
181
  return Promise.resolve();
182
182
  }
183
183
  selectAggregationTemporality(_instrumentType) {
184
- return import_sdk_metrics.AggregationTemporality.CUMULATIVE;
184
+ return import_sdk_metrics.AggregationTemporality.DELTA;
185
185
  }
186
186
  shutdown() {
187
187
  return __async(this, null, function* () {
@@ -197,7 +197,8 @@ class CustomWebMetricExporter {
197
197
  fetch(this.url, {
198
198
  method: "POST",
199
199
  headers: __spreadValues({
200
- "Content-Type": "application/json"
200
+ "Content-Type": "application/json",
201
+ "X-Suda-Csrf-Token": window.csrfToken || ""
201
202
  }, this.headers),
202
203
  body,
203
204
  keepalive: true
@@ -0,0 +1,21 @@
1
+ import { ResourceDetector, ResourceDetectionConfig, DetectedResource } from '@opentelemetry/resources';
2
+
3
+ declare global {
4
+ interface Window {
5
+ appId: string;
6
+ tenantId: string;
7
+ userId: string;
8
+ }
9
+ }
10
+ /**
11
+ * 自定义 Web 环境探测器
12
+ */
13
+ declare class CustomWebDetector implements ResourceDetector {
14
+ /**
15
+ * 核心探测方法
16
+ * @param config 探测配置(通常用不到,但接口要求)
17
+ */
18
+ detect(config?: ResourceDetectionConfig): DetectedResource;
19
+ }
20
+
21
+ export { CustomWebDetector };
@@ -1,3 +1,4 @@
1
+ "use strict";
1
2
  var __defProp = Object.defineProperty;
2
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
@@ -41,12 +42,11 @@ class CustomWebDetector {
41
42
  */
42
43
  detect(config) {
43
44
  const browserAttributes = {
44
- "browser_language": navigator.language,
45
- "page_url": window.location.href,
46
- "page_referrer": document.referrer
45
+ "url": window.location.href,
46
+ "referrer": document.referrer
47
47
  };
48
48
  const appId = window.appId || "";
49
- const tenantId = window.tenantID || "";
49
+ const tenantId = window.tenantId || "";
50
50
  const userId = window.userId || "";
51
51
  const appAttributes = {
52
52
  app_id: appId,
@@ -0,0 +1,33 @@
1
+ import { AppEnv } from '../const.js';
2
+
3
+ interface WebSdkConfig {
4
+ serviceName: string;
5
+ env: AppEnv;
6
+ collectorUrl: {
7
+ log: string;
8
+ metric: string;
9
+ };
10
+ headers?: Record<string, string>;
11
+ }
12
+ declare class WebObservableSdk {
13
+ private static _instance;
14
+ private loggerProvider;
15
+ private meterProvider;
16
+ private logger;
17
+ private meter;
18
+ private resource;
19
+ private config;
20
+ private isStarted;
21
+ constructor();
22
+ static get instance(): WebObservableSdk;
23
+ start(config: WebSdkConfig): void;
24
+ private initLogs;
25
+ private initMetrics;
26
+ private initAutoPv;
27
+ log(level: 'INFO' | 'ERROR' | 'WARN', message: string, attributes?: Record<string, any>): void;
28
+ addCount(name: string, value?: number, attributes?: Record<string, any>): void;
29
+ shutdown(): Promise<void>;
30
+ }
31
+ declare const observable: WebObservableSdk;
32
+
33
+ export { WebObservableSdk, type WebSdkConfig, observable };
@@ -1,8 +1,8 @@
1
+ "use strict";
1
2
  var __defProp = Object.defineProperty;
2
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
4
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
5
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
6
6
  var __export = (target, all) => {
7
7
  for (var name in all)
8
8
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -16,7 +16,6 @@ var __copyProps = (to, from, except, desc) => {
16
16
  return to;
17
17
  };
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
- var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
20
19
  var __async = (__this, __arguments, generator) => {
21
20
  return new Promise((resolve, reject) => {
22
21
  var fulfilled = (value) => {
@@ -45,29 +44,24 @@ __export(sdk_exports, {
45
44
  module.exports = __toCommonJS(sdk_exports);
46
45
  var import_resources = require("@opentelemetry/resources");
47
46
  var import_sdk_logs = require("@opentelemetry/sdk-logs");
48
- var import_exporter_logs_otlp_http = require("@opentelemetry/exporter-logs-otlp-http");
49
47
  var import_sdk_metrics = require("@opentelemetry/sdk-metrics");
50
48
  var import_resource_detector = require("./resource-detector");
49
+ var import_const = require("../const");
51
50
  var import_metric_exporter = require("./metric-exporter");
52
51
  var import_PvInstrumentation = require("./instrumentation/PvInstrumentation");
53
- const _WebObservableSdk = class _WebObservableSdk {
52
+ var import_log_exporter = require("./log-exporter");
53
+ class WebObservableSdk {
54
54
  constructor() {
55
- __publicField(this, "loggerProvider");
56
- __publicField(this, "meterProvider");
57
- __publicField(this, "logger");
58
- __publicField(this, "meter");
59
- __publicField(this, "resource");
60
- __publicField(this, "config");
61
- __publicField(this, "isStarted", false);
55
+ this.isStarted = false;
62
56
  }
63
57
  static get instance() {
64
58
  if (!this._instance) {
65
- this._instance = new _WebObservableSdk();
59
+ this._instance = new WebObservableSdk();
66
60
  }
67
61
  return this._instance;
68
62
  }
69
63
  start(config) {
70
- if (this.isStarted) return;
64
+ if (this.isStarted || config.env === import_const.AppEnv.Dev) return;
71
65
  this.config = config;
72
66
  try {
73
67
  this.resource = (0, import_resources.detectResources)({
@@ -83,10 +77,12 @@ const _WebObservableSdk = class _WebObservableSdk {
83
77
  }
84
78
  // --- 初始化日志模块 ---
85
79
  initLogs() {
86
- var _a;
80
+ var _a, _b;
87
81
  if (!((_a = this.config) == null ? void 0 : _a.collectorUrl.log)) return;
88
- const exporter = new import_exporter_logs_otlp_http.OTLPLogExporter({
89
- url: this.config.collectorUrl.log
82
+ const exporter = new import_log_exporter.CustomWebLogExporter({
83
+ env: this.config.env,
84
+ url: this.config.collectorUrl.log,
85
+ headers: ((_b = this.config) == null ? void 0 : _b.headers) || {}
90
86
  });
91
87
  this.loggerProvider = new import_sdk_logs.LoggerProvider({
92
88
  resource: this.resource,
@@ -101,10 +97,11 @@ const _WebObservableSdk = class _WebObservableSdk {
101
97
  }
102
98
  // --- 初始化指标模块 ---
103
99
  initMetrics() {
104
- var _a, _b;
100
+ var _a, _b, _c;
105
101
  if (!((_a = this.config) == null ? void 0 : _a.collectorUrl.metric)) return;
106
102
  const exporter = new import_metric_exporter.CustomWebMetricExporter({
107
- url: (_b = this.config) == null ? void 0 : _b.collectorUrl.metric
103
+ url: (_b = this.config) == null ? void 0 : _b.collectorUrl.metric,
104
+ headers: ((_c = this.config) == null ? void 0 : _c.headers) || {}
108
105
  });
109
106
  this.meterProvider = new import_sdk_metrics.MeterProvider({
110
107
  resource: this.resource,
@@ -121,7 +118,7 @@ const _WebObservableSdk = class _WebObservableSdk {
121
118
  }
122
119
  initAutoPv() {
123
120
  var _a;
124
- const meter = (_a = this.meterProvider) == null ? void 0 : _a.getMeter("web-sdk-pv-instrumentation");
121
+ const meter = (_a = this.meterProvider) == null ? void 0 : _a.getMeter("apaas_internal_metrics_pv_uv");
125
122
  if (!meter) return;
126
123
  const pvTracker = new import_PvInstrumentation.PvInstrumentation(meter);
127
124
  pvTracker.enable();
@@ -151,7 +148,5 @@ const _WebObservableSdk = class _WebObservableSdk {
151
148
  ]);
152
149
  });
153
150
  }
154
- };
155
- __publicField(_WebObservableSdk, "_instance");
156
- let WebObservableSdk = _WebObservableSdk;
151
+ }
157
152
  const observable = WebObservableSdk.instance;
@@ -0,0 +1,2 @@
1
+ export { observable } from './core/sdk.js';
2
+ export { AppEnv } from './const.js';
package/dist/lib/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ "use strict";
1
2
  var __defProp = Object.defineProperty;
2
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
@@ -0,0 +1,336 @@
1
+ declare enum AggregationTemporality {
2
+ AGGREGATION_TEMPORALITY_UNSPECIFIED = 0,//未指定(禁止使用)
3
+ AGGREGATION_TEMPORALITY_DELTA = 1,//增量聚合(自上次报告以来的变化)
4
+ AGGREGATION_TEMPORALITY_CUMULATIVE = 2
5
+ }
6
+ interface Buckets {
7
+ /**
8
+ *桶偏移量(必选)
9
+ */
10
+ offset?: number;
11
+ /**
12
+ *桶计数列表(必选)
13
+ */
14
+ bucketCounts?: number[];
15
+ }
16
+ interface CollectMetricsRequest {
17
+ metricsData: MetricsData;
18
+ }
19
+ interface Exemplar {
20
+ /**
21
+ *过滤后的属性(必选)
22
+ */
23
+ filteredAttributes?: Record<string, string>;
24
+ /**
25
+ *样本时间戳(纳秒级 Unix 时间,必选)
26
+ */
27
+ timeUnixNano?: number;
28
+ /**
29
+ *浮点型样本值(与 AsInt 二选一)
30
+ */
31
+ asDouble?: number;
32
+ /**
33
+ *整型样本值(与 AsDouble 二选一)
34
+ */
35
+ asInt?: number;
36
+ /**
37
+ *关联的追踪 ID(16 字节,可选)
38
+ */
39
+ traceID?: string;
40
+ /**
41
+ *关联的跨度 ID(8 字节,可选)
42
+ */
43
+ spanID?: string;
44
+ }
45
+ interface ExponentialHistogram {
46
+ /**
47
+ *数据点列表(至少为空列表,必选)
48
+ */
49
+ dataPoints?: ExponentialHistogramDataPoint[];
50
+ /**
51
+ *聚合时间特性(必选)
52
+ */
53
+ aggregationTemporality?: AggregationTemporality;
54
+ }
55
+ interface ExponentialHistogramDataPoint {
56
+ /**
57
+ *数据点属性(键唯一,可选)
58
+ */
59
+ attributes?: Record<string, string>;
60
+ /**
61
+ *起始时间戳(纳秒级,可选,强烈建议设置)
62
+ */
63
+ startTimeUnixNano?: number;
64
+ /**
65
+ *结束时间戳(纳秒级,必选)
66
+ */
67
+ timeUnixNano?: number;
68
+ /**
69
+ *样本总数(非负,必选)
70
+ */
71
+ count?: number;
72
+ /**
73
+ *样本总和(可选,count 为 0 时必须为 0)
74
+ */
75
+ sum?: number;
76
+ /**
77
+ *分辨率缩放因子(必选)
78
+ */
79
+ scale?: number;
80
+ /**
81
+ *零值样本数(必选)
82
+ */
83
+ zeroCount?: number;
84
+ /**
85
+ *正数值桶计数(可选)
86
+ */
87
+ positive?: Buckets;
88
+ /**
89
+ *负数值桶计数(可选)
90
+ */
91
+ negative?: Buckets;
92
+ /**
93
+ *标志位(参考 DataPointFlags,可选)
94
+ */
95
+ flags?: number;
96
+ /**
97
+ *样本列表(可选)
98
+ */
99
+ exemplars?: Exemplar[];
100
+ /**
101
+ *最小值(可选)
102
+ */
103
+ min?: number;
104
+ /**
105
+ *最大值(可选)
106
+ */
107
+ max?: number;
108
+ /**
109
+ *零值阈值(可选)
110
+ */
111
+ zeroThreshold?: number;
112
+ }
113
+ interface Gauge {
114
+ /**
115
+ *数据点列表(至少为空列表,必选)
116
+ */
117
+ dataPoints?: NumberDataPoint[];
118
+ }
119
+ interface Histogram {
120
+ /**
121
+ *数据点列表(至少为空列表,必选)
122
+ */
123
+ dataPoints?: HistogramDataPoint[];
124
+ /**
125
+ *聚合时间特性(必选)
126
+ */
127
+ aggregationTemporality?: AggregationTemporality;
128
+ }
129
+ interface HistogramDataPoint {
130
+ /**
131
+ *数据点属性(键唯一,可选)
132
+ */
133
+ attributes?: Record<string, string>;
134
+ /**
135
+ *起始时间戳(纳秒级,可选,强烈建议设置)
136
+ */
137
+ startTimeUnixNano?: number;
138
+ /**
139
+ *结束时间戳(纳秒级,必选)
140
+ */
141
+ timeUnixNano?: number;
142
+ /**
143
+ *样本总数(非负,必选)
144
+ */
145
+ count?: number;
146
+ /**
147
+ *样本总和(可选,count 为 0 时必须为 0)
148
+ */
149
+ sum?: number;
150
+ /**
151
+ *每个桶的样本数(可选)
152
+ */
153
+ bucketCounts?: number[];
154
+ /**
155
+ *桶的边界值(可选)
156
+ */
157
+ explicitBounds?: number[];
158
+ /**
159
+ *最小值(可选)
160
+ */
161
+ min?: number;
162
+ /**
163
+ *最大值(可选)
164
+ */
165
+ max?: number;
166
+ /**
167
+ *样本列表(可选)
168
+ */
169
+ exemplars?: Exemplar[];
170
+ /**
171
+ *标志位(参考 DataPointFlags,可选)
172
+ */
173
+ flags?: number;
174
+ }
175
+ interface Metric {
176
+ /**
177
+ *指标名称(必选)
178
+ */
179
+ name?: string;
180
+ /**
181
+ *指标描述(可选)
182
+ */
183
+ description?: string;
184
+ /**
185
+ *指标单位(遵循 UCUM 格式,可选)
186
+ */
187
+ unit?: string;
188
+ /**
189
+ *gauge 类型数据(与其他数据类型互斥)
190
+ */
191
+ gauge?: Gauge;
192
+ /**
193
+ *sum 类型数据(与其他数据类型互斥)
194
+ */
195
+ sum?: Sum;
196
+ /**
197
+ *直方图类型数据(与其他数据类型互斥)
198
+ */
199
+ histogram?: Histogram;
200
+ /**
201
+ *指数直方图类型数据(与其他数据类型互斥)
202
+ */
203
+ exponentialHistogram?: ExponentialHistogram;
204
+ /**
205
+ *摘要类型数据(与其他数据类型互斥)
206
+ */
207
+ summary?: Summary;
208
+ /**
209
+ *附加元数据(非标识性,可选)
210
+ */
211
+ metadata?: Record<string, string>;
212
+ /**
213
+ *指标范围名称(可选)
214
+ */
215
+ scopeName?: string;
216
+ }
217
+ interface MetricsData {
218
+ /**
219
+ *资源信息(可选,未设置表示未知)
220
+ */
221
+ resource?: Resource;
222
+ /**
223
+ *范围指标列表(至少为空列表,语义必填)
224
+ */
225
+ metrics?: Metric[];
226
+ /**
227
+ *资源数据遵循的 schema URL(可选)
228
+ */
229
+ schemaURL?: string;
230
+ }
231
+ interface NumberDataPoint {
232
+ /**
233
+ *起始时间戳(纳秒级,可选,强烈建议设置)
234
+ */
235
+ startTimeUnixNano?: number;
236
+ /**
237
+ *结束时间戳(纳秒级,必选)
238
+ */
239
+ timeUnixNano?: number;
240
+ /**
241
+ *浮点值(与 AsInt 二选一,必选其一)
242
+ */
243
+ asDouble?: number;
244
+ /**
245
+ *样本列表(可选)
246
+ */
247
+ exemplars?: Exemplar[];
248
+ /**
249
+ *整数值(与 AsDouble 二选一,必选其一)
250
+ */
251
+ asInt?: number;
252
+ /**
253
+ *数据点属性(键唯一,可选)
254
+ */
255
+ attributes?: Record<string, string>;
256
+ /**
257
+ *标志位(参考 DataPointFlags,可选)
258
+ */
259
+ flags?: number;
260
+ }
261
+ interface Resource {
262
+ /**
263
+ *资源属性列表,键必须唯一
264
+ */
265
+ attributes?: Record<string, string>;
266
+ /**
267
+ *被丢弃的属性数量,默认 0
268
+ */
269
+ droppedAttributesCount?: number;
270
+ /**
271
+ *资源数据遵循的 schema URL
272
+ */
273
+ schemaURL?: string;
274
+ }
275
+ interface Sum {
276
+ /**
277
+ *数据点列表(至少为空列表,必选)
278
+ */
279
+ dataPoints?: NumberDataPoint[];
280
+ /**
281
+ *聚合时间特性(必选)
282
+ */
283
+ aggregationTemporality?: AggregationTemporality;
284
+ /**
285
+ *是否单调递增(必选)
286
+ */
287
+ isMonotonic?: boolean;
288
+ }
289
+ interface Summary {
290
+ /**
291
+ *数据点列表(至少为空列表,必选)
292
+ */
293
+ dataPoints?: SummaryDataPoint[];
294
+ }
295
+ interface SummaryDataPoint {
296
+ /**
297
+ *数据点属性(键唯一,可选)
298
+ */
299
+ attributes?: Record<string, string>;
300
+ /**
301
+ *起始时间戳(纳秒级,可选,强烈建议设置)
302
+ */
303
+ startTimeUnixNano?: number;
304
+ /**
305
+ *结束时间戳(纳秒级,必选)
306
+ */
307
+ timeUnixNano?: number;
308
+ /**
309
+ *样本总数(非负,必选)
310
+ */
311
+ count?: number;
312
+ /**
313
+ *样本总和(必选)
314
+ */
315
+ sum?: number;
316
+ /**
317
+ *分位数对应的数值(必选)
318
+ */
319
+ quantileValues?: ValueAtQuantile[];
320
+ /**
321
+ *标志位(参考 DataPointFlags,可选)
322
+ */
323
+ flags?: number;
324
+ }
325
+ interface ValueAtQuantile {
326
+ /**
327
+ *分位数(0 ≤ quantile ≤ 1,必选)
328
+ */
329
+ quantile?: number;
330
+ /**
331
+ *分位数值(必选)
332
+ */
333
+ value?: number;
334
+ }
335
+
336
+ export { AggregationTemporality, type Buckets, type CollectMetricsRequest, type Exemplar, type ExponentialHistogram, type ExponentialHistogramDataPoint, type Gauge, type Histogram, type HistogramDataPoint, type Metric, type MetricsData, type NumberDataPoint, type Resource, type Sum, type Summary, type SummaryDataPoint, type ValueAtQuantile };
package/dist/lib/types.js CHANGED
@@ -1,3 +1,4 @@
1
+ "use strict";
1
2
  var __defProp = Object.defineProperty;
2
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
@@ -0,0 +1,3 @@
1
+ declare function convertAttributesToString(attributes: Record<string, unknown>): Record<string, string>;
2
+
3
+ export { convertAttributesToString };
@@ -1,3 +1,4 @@
1
+ "use strict";
1
2
  var __defProp = Object.defineProperty;
2
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
@@ -0,0 +1,3 @@
1
+ declare const generateUUID: () => string;
2
+
3
+ export { generateUUID };
@@ -1,3 +1,4 @@
1
+ "use strict";
1
2
  var __defProp = Object.defineProperty;
2
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
@@ -0,0 +1,15 @@
1
+ import { DeviceType } from './getDeviceTypeByAgent.js';
2
+ import { AppEnv } from '../const.js';
3
+
4
+ declare const getDefaultAttr: () => {
5
+ app_id: string;
6
+ tenant_id: string;
7
+ user_id: string;
8
+ did: string;
9
+ module: string;
10
+ app_env: AppEnv;
11
+ source_type: string;
12
+ device_type: DeviceType;
13
+ };
14
+
15
+ export { getDefaultAttr };
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var getDefaultAttr_exports = {};
20
+ __export(getDefaultAttr_exports, {
21
+ getDefaultAttr: () => getDefaultAttr
22
+ });
23
+ module.exports = __toCommonJS(getDefaultAttr_exports);
24
+ var import_const = require("../const");
25
+ var import_getDeviceID = require("./getDeviceID");
26
+ var import_getDeviceTypeByAgent = require("./getDeviceTypeByAgent");
27
+ const getDefaultAttr = () => ({
28
+ app_id: window.appId || "",
29
+ tenant_id: window.tenantId || "",
30
+ user_id: window.userId || "",
31
+ did: (0, import_getDeviceID.getDeviceId)(),
32
+ module: "app_web",
33
+ // 默认为 Prod 环境,preview 环境不上报
34
+ app_env: import_const.AppEnv.Prod,
35
+ source_type: "user",
36
+ device_type: (0, import_getDeviceTypeByAgent.getDeviceTypeByAgent)(navigator.userAgent)
37
+ });
@@ -0,0 +1,3 @@
1
+ declare const getDeviceId: () => string;
2
+
3
+ export { getDeviceId };
@@ -1,3 +1,4 @@
1
+ "use strict";
1
2
  var __defProp = Object.defineProperty;
2
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
@@ -0,0 +1,11 @@
1
+ /**
2
+ * 设备类型定义:只有 Mobile 和 PC (平板归为 PC)
3
+ */
4
+ type DeviceType = 'desktop' | 'mobile';
5
+ /**
6
+ * 根据 UserAgent 判断设备类型
7
+ * 策略:只识别手机,其余全算 PC
8
+ */
9
+ declare function getDeviceTypeByAgent(customAgent?: string): DeviceType;
10
+
11
+ export { type DeviceType, getDeviceTypeByAgent };
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var getDeviceTypeByAgent_exports = {};
20
+ __export(getDeviceTypeByAgent_exports, {
21
+ getDeviceTypeByAgent: () => getDeviceTypeByAgent
22
+ });
23
+ module.exports = __toCommonJS(getDeviceTypeByAgent_exports);
24
+ function getDeviceTypeByAgent(customAgent) {
25
+ const ua = customAgent || (typeof navigator !== "undefined" ? navigator.userAgent : "");
26
+ if (!ua) {
27
+ return "desktop";
28
+ }
29
+ const isAndroidPhone = /Android/i.test(ua) && /Mobile/i.test(ua);
30
+ const isIosPhone = /iPhone|iPod/i.test(ua);
31
+ const isOtherMobile = /BlackBerry|IEMobile|Opera Mini/i.test(ua);
32
+ if (isAndroidPhone || isIosPhone || isOtherMobile) {
33
+ return "mobile";
34
+ }
35
+ return "desktop";
36
+ }
@@ -0,0 +1,10 @@
1
+ import { HrTime } from '@opentelemetry/api';
2
+
3
+ /**
4
+ * 将 HrTime 转换为纳秒数字
5
+ * 注意:JavaScript Number 类型只能精确表示整数到 2^53,对于超过此范围的纳秒值会丢失精度
6
+ * 此实现不使用 BigInt,具有更好的浏览器兼容性
7
+ */
8
+ declare function hrTimeToNanosNumber(hrTime: HrTime): number;
9
+
10
+ export { hrTimeToNanosNumber };
@@ -1,3 +1,4 @@
1
+ "use strict";
1
2
  var __defProp = Object.defineProperty;
2
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lark-apaas/observable-web",
3
- "version": "1.0.0-alpha.0",
3
+ "version": "1.0.0-alpha.10",
4
4
  "description": "Observable SDK For Web",
5
5
  "main": "./dist/lib/index.js",
6
6
  "files": [