@lark-apaas/observable-web 1.0.0-alpha.4 → 1.0.0-alpha.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.
@@ -21,7 +21,6 @@ __export(PvInstrumentation_exports, {
21
21
  PvInstrumentation: () => PvInstrumentation
22
22
  });
23
23
  module.exports = __toCommonJS(PvInstrumentation_exports);
24
- var import_getDeviceID = require("../../utils/getDeviceID");
25
24
  class PvInstrumentation {
26
25
  constructor(meter) {
27
26
  this.meter = meter;
@@ -38,14 +37,9 @@ class PvInstrumentation {
38
37
  if (this.lastUrl === currentUrl) return;
39
38
  this.lastUrl = currentUrl;
40
39
  pvCounter.add(1, {
41
- "page_url": window.location.href,
42
- "page_path": window.location.pathname,
43
- "page_title": document.title,
44
- "page_referrer": document.referrer,
45
- "device_id": (0, import_getDeviceID.getDeviceId)(),
46
- "user_id": window.userId || "",
47
- "tenant_id": window.tenantID || "",
48
- "app_id": window.appId || ""
40
+ url: window.location.href,
41
+ path: window.location.pathname,
42
+ referrer: document.referrer
49
43
  });
50
44
  };
51
45
  window.addEventListener("load", reportPv);
@@ -60,6 +60,7 @@ 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");
63
64
  class CustomWebMetricExporter {
64
65
  constructor(config) {
65
66
  this._shutdown = false;
@@ -133,7 +134,7 @@ class CustomWebMetricExporter {
133
134
  */
134
135
  transformNumberDataPoints(dataPoints) {
135
136
  return dataPoints.map((dp) => ({
136
- attributes: (0, import_convertAllAttrIntoString.convertAttributesToString)(dp.attributes),
137
+ attributes: __spreadValues(__spreadValues({}, (0, import_convertAllAttrIntoString.convertAttributesToString)(dp.attributes)), (0, import_getDefaultAttr.getDefaultAttr)()),
137
138
  startTimeUnixNano: (0, import_hrTimeToNanoNumber.hrTimeToNanosNumber)(dp.startTime),
138
139
  timeUnixNano: (0, import_hrTimeToNanoNumber.hrTimeToNanosNumber)(dp.endTime),
139
140
  // JS 中所有数字都是双精度浮点数,默认映射到 asDouble
@@ -147,7 +148,7 @@ class CustomWebMetricExporter {
147
148
  transformHistogramDataPoints(dataPoints) {
148
149
  return dataPoints.map((dp) => {
149
150
  return {
150
- attributes: (0, import_convertAllAttrIntoString.convertAttributesToString)(dp.attributes),
151
+ attributes: __spreadValues(__spreadValues({}, (0, import_convertAllAttrIntoString.convertAttributesToString)(dp.attributes)), (0, import_getDefaultAttr.getDefaultAttr)()),
151
152
  startTimeUnixNano: (0, import_hrTimeToNanoNumber.hrTimeToNanosNumber)(dp.startTime),
152
153
  timeUnixNano: (0, import_hrTimeToNanoNumber.hrTimeToNanosNumber)(dp.endTime),
153
154
  count: dp.value.count,
@@ -3,7 +3,7 @@ import { ResourceDetector, ResourceDetectionConfig, DetectedResource } from '@op
3
3
  declare global {
4
4
  interface Window {
5
5
  appId: string;
6
- tenantID: string;
6
+ tenantId: string;
7
7
  userId: string;
8
8
  }
9
9
  }
@@ -42,12 +42,11 @@ class CustomWebDetector {
42
42
  */
43
43
  detect(config) {
44
44
  const browserAttributes = {
45
- "browser_language": navigator.language,
46
- "page_url": window.location.href,
47
- "page_referrer": document.referrer
45
+ "url": window.location.href,
46
+ "referrer": document.referrer
48
47
  };
49
48
  const appId = window.appId || "";
50
- const tenantId = window.tenantID || "";
49
+ const tenantId = window.tenantId || "";
51
50
  const userId = window.userId || "";
52
51
  const appAttributes = {
53
52
  app_id: appId,
@@ -47,6 +47,7 @@ var import_sdk_logs = require("@opentelemetry/sdk-logs");
47
47
  var import_exporter_logs_otlp_http = require("@opentelemetry/exporter-logs-otlp-http");
48
48
  var import_sdk_metrics = require("@opentelemetry/sdk-metrics");
49
49
  var import_resource_detector = require("./resource-detector");
50
+ var import_const = require("../const");
50
51
  var import_metric_exporter = require("./metric-exporter");
51
52
  var import_PvInstrumentation = require("./instrumentation/PvInstrumentation");
52
53
  class WebObservableSdk {
@@ -60,7 +61,7 @@ class WebObservableSdk {
60
61
  return this._instance;
61
62
  }
62
63
  start(config) {
63
- if (this.isStarted) return;
64
+ if (this.isStarted || config.env === import_const.AppEnv.Dev) return;
64
65
  this.config = config;
65
66
  try {
66
67
  this.resource = (0, import_resources.detectResources)({
@@ -0,0 +1,13 @@
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
+ app_env: AppEnv;
10
+ device_type: DeviceType;
11
+ };
12
+
13
+ export { getDefaultAttr };
@@ -0,0 +1,35 @@
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
+ // 默认为 Prod 环境,preview 环境不上报
33
+ app_env: import_const.AppEnv.Prod,
34
+ device_type: (0, import_getDeviceTypeByAgent.getDeviceTypeByAgent)(navigator.userAgent)
35
+ });
@@ -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
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lark-apaas/observable-web",
3
- "version": "1.0.0-alpha.4",
3
+ "version": "1.0.0-alpha.6",
4
4
  "description": "Observable SDK For Web",
5
5
  "main": "./dist/lib/index.js",
6
6
  "files": [