@lark-apaas/observable 1.0.4-alpha.5 → 1.0.4-alpha.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.
package/dist/index.d.cts CHANGED
@@ -7,6 +7,8 @@ declare class AppOTelSDK {
7
7
  private sdk;
8
8
  private logProcessor;
9
9
  private spanProcessor;
10
+ private logger;
11
+ private isStarted;
10
12
  private static CUSTOM_FORMAT_REGEX;
11
13
  static emptySpan: Span;
12
14
  start(): void;
package/dist/index.d.ts CHANGED
@@ -7,6 +7,8 @@ declare class AppOTelSDK {
7
7
  private sdk;
8
8
  private logProcessor;
9
9
  private spanProcessor;
10
+ private logger;
11
+ private isStarted;
10
12
  private static CUSTOM_FORMAT_REGEX;
11
13
  static emptySpan: Span;
12
14
  start(): void;
package/dist/index.js CHANGED
@@ -2,7 +2,7 @@ import { NodeSDK } from '@opentelemetry/sdk-node';
2
2
  import { BatchLogRecordProcessor } from '@opentelemetry/sdk-logs';
3
3
  import { BatchSpanProcessor } from '@opentelemetry/sdk-trace-node';
4
4
  import { detectResources } from '@opentelemetry/resources';
5
- import { context, SpanKind, trace, INVALID_SPAN_CONTEXT, propagation, SpanStatusCode, TraceFlags } from '@opentelemetry/api';
5
+ import { SpanKind, trace, INVALID_SPAN_CONTEXT, propagation, context, SpanStatusCode, TraceFlags } from '@opentelemetry/api';
6
6
  import { logs, SeverityNumber } from '@opentelemetry/api-logs';
7
7
  import { ExportResultCode } from '@opentelemetry/core';
8
8
  import { randomBytes } from 'crypto';
@@ -518,20 +518,20 @@ var require_errorContext = __commonJS({
518
518
  });
519
519
  exports$1.captureError = exports$1.errorContext = void 0;
520
520
  var config_1 = require_config();
521
- var context4 = null;
521
+ var context2 = null;
522
522
  function errorContext(cb) {
523
523
  if (config_1.config.useDeprecatedSynchronousErrorHandling) {
524
- var isRoot = !context4;
524
+ var isRoot = !context2;
525
525
  if (isRoot) {
526
- context4 = {
526
+ context2 = {
527
527
  errorThrown: false,
528
528
  error: null
529
529
  };
530
530
  }
531
531
  cb();
532
532
  if (isRoot) {
533
- var _a = context4, errorThrown = _a.errorThrown, error = _a.error;
534
- context4 = null;
533
+ var _a = context2, errorThrown = _a.errorThrown, error = _a.error;
534
+ context2 = null;
535
535
  if (errorThrown) {
536
536
  throw error;
537
537
  }
@@ -543,9 +543,9 @@ var require_errorContext = __commonJS({
543
543
  __name(errorContext, "errorContext");
544
544
  exports$1.errorContext = errorContext;
545
545
  function captureError(err) {
546
- if (config_1.config.useDeprecatedSynchronousErrorHandling && context4) {
547
- context4.errorThrown = true;
548
- context4.error = err;
546
+ if (config_1.config.useDeprecatedSynchronousErrorHandling && context2) {
547
+ context2.errorThrown = true;
548
+ context2.error = err;
549
549
  }
550
550
  }
551
551
  __name(captureError, "captureError");
@@ -8800,7 +8800,7 @@ var PlatformDetector = class {
8800
8800
  * @param config 探测配置 (可选)
8801
8801
  */
8802
8802
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
8803
- detect(config) {
8803
+ detect(_config) {
8804
8804
  const attributes = {};
8805
8805
  const cleanAttributes = Object.entries(attributes).reduce((acc, [key, value]) => {
8806
8806
  if (value !== void 0 && value !== null && value !== "") {
@@ -8842,7 +8842,7 @@ __name(hrTimeToNanosNumber, "hrTimeToNanosNumber");
8842
8842
 
8843
8843
  // package.json
8844
8844
  var package_default = {
8845
- version: "1.0.4-alpha.4"};
8845
+ version: "1.0.4-alpha.7"};
8846
8846
 
8847
8847
  // src/const.ts
8848
8848
  var AppEnv = /* @__PURE__ */ (function(AppEnv2) {
@@ -8855,6 +8855,7 @@ var defaultResourceAttr = {
8855
8855
  "sdk_type": "client-backend",
8856
8856
  "sdk_language": "node"
8857
8857
  };
8858
+ var pointKillTag = "__point_kill_info";
8858
8859
  var TraceDropToken = "TraceDropToken";
8859
8860
 
8860
8861
  // src/utils/convertAllAttrIntoString.ts
@@ -8874,6 +8875,34 @@ function convertAttributesToString(attributes) {
8874
8875
  }
8875
8876
  __name(convertAttributesToString, "convertAttributesToString");
8876
8877
 
8878
+ // src/utils/getSnippingConfig.ts
8879
+ function processSnipConfig(configStr) {
8880
+ if (!configStr) return;
8881
+ try {
8882
+ const res = JSON.parse(configStr);
8883
+ const { logs: logs2, traces, metrics } = res ?? {};
8884
+ if (res) {
8885
+ return {
8886
+ logs: {
8887
+ modules: Array.isArray(logs2?.modules) ? logs2.modules : []
8888
+ },
8889
+ traces: {
8890
+ modules: Array.isArray(traces?.modules) ? traces.modules : []
8891
+ },
8892
+ metrics: {
8893
+ modules: Array.isArray(metrics?.modules) ? metrics.modules : []
8894
+ }
8895
+ };
8896
+ } else {
8897
+ return void 0;
8898
+ }
8899
+ } catch (error) {
8900
+ console.error("Failed to parse sniping config:", error);
8901
+ return void 0;
8902
+ }
8903
+ }
8904
+ __name(processSnipConfig, "processSnipConfig");
8905
+
8877
8906
  // src/core/log-exporter.ts
8878
8907
  var CustomExporter = class {
8879
8908
  static {
@@ -8884,12 +8913,22 @@ var CustomExporter = class {
8884
8913
  constructor() {
8885
8914
  }
8886
8915
  export(logs2, resultCallback) {
8887
- const pointKillConfig = context.active().getValue(POINT_KILL_TAG);
8888
8916
  const filteredLogs = logs2.filter((log) => {
8889
- if (!pointKillConfig) return true;
8890
- const moduleName = log.attributes?.module;
8891
- if (pointKillConfig.logs?.modules?.includes("*")) return false;
8892
- if (moduleName && pointKillConfig.logs?.modules?.includes(moduleName)) return false;
8917
+ const pkTag = log.attributes?.[pointKillTag];
8918
+ if (pkTag) {
8919
+ try {
8920
+ const snipingConfig = processSnipConfig(pkTag);
8921
+ if (snipingConfig?.logs?.modules?.includes("*")) {
8922
+ return false;
8923
+ }
8924
+ const module = log.attributes?.module;
8925
+ if (module && snipingConfig?.logs?.modules?.includes(module)) {
8926
+ return false;
8927
+ }
8928
+ } catch (e) {
8929
+ console.error(`Failed to parse ${pointKillTag} in log exporter:`, e);
8930
+ }
8931
+ }
8893
8932
  return true;
8894
8933
  });
8895
8934
  if (filteredLogs.length === 0) {
@@ -8909,23 +8948,19 @@ var CustomExporter = class {
8909
8948
  ...defaultResourceAttr
8910
8949
  }
8911
8950
  },
8912
- logRecords: filteredLogs.map((log) => {
8913
- const rawAttributes = {
8951
+ logRecords: filteredLogs.map((log) => ({
8952
+ timeUnixNano: hrTimeToNanosNumber(log.hrTime),
8953
+ observedTimeUnixNano: hrTimeToNanosNumber(log.hrTimeObserved),
8954
+ severityNumber: log.severityNumber,
8955
+ severityText: log.severityText,
8956
+ body: log.body,
8957
+ attributes: convertAttributesToString({
8914
8958
  ...defaultAttributes,
8915
8959
  ...log.attributes ?? {}
8916
- };
8917
- delete rawAttributes["__point_kill_logs"];
8918
- return {
8919
- timeUnixNano: hrTimeToNanosNumber(log.hrTime),
8920
- observedTimeUnixNano: hrTimeToNanosNumber(log.hrTimeObserved),
8921
- severityNumber: log.severityNumber,
8922
- severityText: log.severityText,
8923
- body: log.body,
8924
- attributes: convertAttributesToString(rawAttributes),
8925
- traceID: log.spanContext?.traceId,
8926
- spanID: log.spanContext?.spanId
8927
- };
8928
- })
8960
+ }),
8961
+ traceID: log.spanContext?.traceId,
8962
+ spanID: log.spanContext?.spanId
8963
+ }))
8929
8964
  };
8930
8965
  console.log(this.logPrefix + JSON.stringify(otlpLikeStructure) + this.logSuffix);
8931
8966
  resultCallback({
@@ -8972,17 +9007,23 @@ var CustomTraceExporter = class {
8972
9007
  tracePrefix = "force-trace-prefix";
8973
9008
  traceSuffix = "force-trace-suffix";
8974
9009
  export(spans, resultCallback) {
8975
- const isDev = process.env.NODE_ENV === "development";
8976
- const defaultAttributes = {
8977
- uuid: idGenerator.generateTraceId(),
8978
- app_env: isDev ? AppEnv.Dev : AppEnv.Prod
8979
- };
8980
9010
  const finalSpans = spans.filter((span) => {
8981
- const pointKillConfig = context.active().getValue(POINT_KILL_TAG);
8982
9011
  if ((span.attributes ?? {})[TraceDropToken]) return false;
8983
- if (pointKillConfig?.traces?.modules?.includes("*")) return false;
8984
- const moduleName = span.attributes?.module;
8985
- if (moduleName && pointKillConfig?.traces?.modules?.includes(moduleName)) return false;
9012
+ const pkTag = span.attributes?.[pointKillTag];
9013
+ if (pkTag) {
9014
+ try {
9015
+ const snipingConfig = processSnipConfig(pkTag);
9016
+ if (snipingConfig?.traces?.modules?.includes("*")) {
9017
+ return false;
9018
+ }
9019
+ const module = span.attributes?.module;
9020
+ if (module && snipingConfig?.traces?.modules?.includes(module)) {
9021
+ return false;
9022
+ }
9023
+ } catch (e) {
9024
+ console.error(`Failed to parse in trace exporter:`, e);
9025
+ }
9026
+ }
8986
9027
  return true;
8987
9028
  });
8988
9029
  if (!finalSpans.length) {
@@ -8991,6 +9032,11 @@ var CustomTraceExporter = class {
8991
9032
  });
8992
9033
  return;
8993
9034
  }
9035
+ const isDev = process.env.NODE_ENV === "development";
9036
+ const defaultAttributes = {
9037
+ uuid: idGenerator.generateTraceId(),
9038
+ app_env: isDev ? AppEnv.Dev : AppEnv.Prod
9039
+ };
8994
9040
  const otlpLikeStructure = {
8995
9041
  resource: {
8996
9042
  attributes: {
@@ -9031,36 +9077,7 @@ function isObservable(input) {
9031
9077
  }
9032
9078
  __name(isObservable, "isObservable");
9033
9079
 
9034
- // src/utils/getSnippingConfig.ts
9035
- function processSnipConfig(configStr) {
9036
- if (!configStr) return;
9037
- try {
9038
- const res = JSON.parse(configStr);
9039
- const { logs: logs2, traces, metrics } = res ?? {};
9040
- if (res) {
9041
- return {
9042
- logs: {
9043
- modules: Array.isArray(logs2?.modules) ? logs2.modules : []
9044
- },
9045
- traces: {
9046
- modules: Array.isArray(traces?.modules) ? traces.modules : []
9047
- },
9048
- metrics: {
9049
- modules: Array.isArray(metrics?.modules) ? metrics.modules : []
9050
- }
9051
- };
9052
- } else {
9053
- return void 0;
9054
- }
9055
- } catch (error) {
9056
- console.error("Failed to parse sniping config:", error);
9057
- return void 0;
9058
- }
9059
- }
9060
- __name(processSnipConfig, "processSnipConfig");
9061
-
9062
9080
  // src/core/sdk.ts
9063
- var POINT_KILL_TAG = /* @__PURE__ */ Symbol("__point_kill");
9064
9081
  var AppOTelSDK = class _AppOTelSDK {
9065
9082
  static {
9066
9083
  __name(this, "AppOTelSDK");
@@ -9068,9 +9085,13 @@ var AppOTelSDK = class _AppOTelSDK {
9068
9085
  sdk = null;
9069
9086
  logProcessor = null;
9070
9087
  spanProcessor = null;
9088
+ logger = null;
9089
+ isStarted = false;
9071
9090
  static CUSTOM_FORMAT_REGEX = /^[0-9a-fA-F]{32}-[0-9a-fA-F]{16}$/;
9072
9091
  static emptySpan = trace.wrapSpanContext(INVALID_SPAN_CONTEXT);
9073
9092
  start() {
9093
+ if (this.isStarted) return;
9094
+ this.isStarted = true;
9074
9095
  const resource = detectResources({
9075
9096
  detectors: [
9076
9097
  new PlatformDetector()
@@ -9094,6 +9115,7 @@ var AppOTelSDK = class _AppOTelSDK {
9094
9115
  spanProcessor: this.spanProcessor
9095
9116
  });
9096
9117
  this.sdk.start();
9118
+ this.logger = logs.getLogger("app-logger");
9097
9119
  }
9098
9120
  /**
9099
9121
  * [框架专用 API] 开启一个根 Span (Root Span)
@@ -9107,7 +9129,6 @@ var AppOTelSDK = class _AppOTelSDK {
9107
9129
  carrier["traceparent"] = `00-${customTraceId}-01`;
9108
9130
  }
9109
9131
  const activeContext = propagation.extract(context.active(), carrier);
9110
- activeContext.setValue(POINT_KILL_TAG, processSnipConfig(headers["x-force-observability-pointkill"]));
9111
9132
  const tracer = trace.getTracer("app-core");
9112
9133
  tracer.startActiveSpan(name, {}, activeContext, (span) => {
9113
9134
  fn(span);
@@ -9166,7 +9187,7 @@ var AppOTelSDK = class _AppOTelSDK {
9166
9187
  */
9167
9188
  log(level, message, extra = {}, spanAttr) {
9168
9189
  try {
9169
- const logger = logs.getLogger("app-logger");
9190
+ if (!this.logger) return;
9170
9191
  const severityNumber = mapSeverity(level);
9171
9192
  const severityText = mapSeverityText(level);
9172
9193
  const attributes = {
@@ -9187,7 +9208,7 @@ var AppOTelSDK = class _AppOTelSDK {
9187
9208
  } catch {
9188
9209
  logContext = context.active();
9189
9210
  }
9190
- logger.emit({
9211
+ this.logger.emit({
9191
9212
  body: message,
9192
9213
  severityNumber,
9193
9214
  severityText,