@lark-apaas/observable 1.0.4-alpha.5 → 1.0.4-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.
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");
@@ -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.5"};
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,14 +9077,60 @@ 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 {
9080
+ // src/core/point-kill/index.ts
9081
+ var PointKillManager = class _PointKillManager {
9082
+ static {
9083
+ __name(this, "PointKillManager");
9084
+ }
9085
+ static instance;
9086
+ static CONFIG_HEADER_NAME = "x-force-observability-pointkill";
9087
+ config = {
9088
+ logs: {
9089
+ modules: []
9090
+ },
9091
+ traces: {
9092
+ modules: []
9093
+ },
9094
+ metrics: {
9095
+ modules: []
9096
+ }
9097
+ };
9098
+ constructor() {
9099
+ }
9100
+ static getInstance() {
9101
+ if (!_PointKillManager.instance) {
9102
+ _PointKillManager.instance = new _PointKillManager();
9103
+ }
9104
+ return _PointKillManager.instance;
9105
+ }
9106
+ getConfig() {
9107
+ return this.config;
9108
+ }
9109
+ updateConfig(newConfig) {
9110
+ this.config = {
9111
+ logs: {
9112
+ modules: [
9113
+ ...newConfig.logs.modules
9114
+ ]
9115
+ },
9116
+ traces: {
9117
+ modules: [
9118
+ ...newConfig.traces.modules
9119
+ ]
9120
+ },
9121
+ metrics: {
9122
+ modules: [
9123
+ ...newConfig.metrics.modules
9124
+ ]
9125
+ }
9126
+ };
9127
+ }
9128
+ setConfig(configStr) {
9129
+ if (!configStr) return;
9130
+ try {
9131
+ const res = JSON.parse(configStr);
9132
+ const { logs: logs2, traces, metrics } = res ?? {};
9133
+ this.updateConfig({
9042
9134
  logs: {
9043
9135
  modules: Array.isArray(logs2?.modules) ? logs2.modules : []
9044
9136
  },
@@ -9048,19 +9140,21 @@ function processSnipConfig(configStr) {
9048
9140
  metrics: {
9049
9141
  modules: Array.isArray(metrics?.modules) ? metrics.modules : []
9050
9142
  }
9051
- };
9052
- } else {
9053
- return void 0;
9143
+ });
9144
+ } catch (error) {
9145
+ console.error("Failed to parse sniping config:", error);
9054
9146
  }
9055
- } catch (error) {
9056
- console.error("Failed to parse sniping config:", error);
9057
- return void 0;
9058
9147
  }
9059
- }
9060
- __name(processSnipConfig, "processSnipConfig");
9148
+ updateFromHeaders(headers) {
9149
+ const snipingConfig = headers[_PointKillManager.CONFIG_HEADER_NAME] || headers[_PointKillManager.CONFIG_HEADER_NAME.toLowerCase()];
9150
+ if (snipingConfig) {
9151
+ this.setConfig(snipingConfig);
9152
+ }
9153
+ }
9154
+ };
9155
+ var pointKillManager = PointKillManager.getInstance();
9061
9156
 
9062
9157
  // src/core/sdk.ts
9063
- var POINT_KILL_TAG = /* @__PURE__ */ Symbol("__point_kill");
9064
9158
  var AppOTelSDK = class _AppOTelSDK {
9065
9159
  static {
9066
9160
  __name(this, "AppOTelSDK");
@@ -9101,13 +9195,13 @@ var AppOTelSDK = class _AppOTelSDK {
9101
9195
  * wrapper 形式
9102
9196
  */
9103
9197
  startContext(headers, name, fn) {
9198
+ pointKillManager.updateFromHeaders(headers);
9104
9199
  const carrier = {};
9105
9200
  const customTraceId = headers["rpc-persist-apaas-observability-trace"];
9106
9201
  if (customTraceId && _AppOTelSDK.CUSTOM_FORMAT_REGEX.test(customTraceId)) {
9107
9202
  carrier["traceparent"] = `00-${customTraceId}-01`;
9108
9203
  }
9109
9204
  const activeContext = propagation.extract(context.active(), carrier);
9110
- activeContext.setValue(POINT_KILL_TAG, processSnipConfig(headers["x-force-observability-pointkill"]));
9111
9205
  const tracer = trace.getTracer("app-core");
9112
9206
  tracer.startActiveSpan(name, {}, activeContext, (span) => {
9113
9207
  fn(span);