@lark-apaas/observable 1.0.4-alpha.3 → 1.0.4-alpha.5

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 { SpanKind, trace, INVALID_SPAN_CONTEXT, propagation, context, SpanStatusCode, TraceFlags } from '@opentelemetry/api';
5
+ import { context, SpanKind, trace, INVALID_SPAN_CONTEXT, propagation, 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 context2 = null;
521
+ var context4 = null;
522
522
  function errorContext(cb) {
523
523
  if (config_1.config.useDeprecatedSynchronousErrorHandling) {
524
- var isRoot = !context2;
524
+ var isRoot = !context4;
525
525
  if (isRoot) {
526
- context2 = {
526
+ context4 = {
527
527
  errorThrown: false,
528
528
  error: null
529
529
  };
530
530
  }
531
531
  cb();
532
532
  if (isRoot) {
533
- var _a = context2, errorThrown = _a.errorThrown, error = _a.error;
534
- context2 = null;
533
+ var _a = context4, errorThrown = _a.errorThrown, error = _a.error;
534
+ context4 = 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 && context2) {
547
- context2.errorThrown = true;
548
- context2.error = err;
546
+ if (config_1.config.useDeprecatedSynchronousErrorHandling && context4) {
547
+ context4.errorThrown = true;
548
+ context4.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.2"};
8845
+ version: "1.0.4-alpha.4"};
8846
8846
 
8847
8847
  // src/const.ts
8848
8848
  var AppEnv = /* @__PURE__ */ (function(AppEnv2) {
@@ -8874,57 +8874,6 @@ function convertAttributesToString(attributes) {
8874
8874
  }
8875
8875
  __name(convertAttributesToString, "convertAttributesToString");
8876
8876
 
8877
- // src/core/sniping-config.ts
8878
- var SnipingConfigManager = class _SnipingConfigManager {
8879
- static {
8880
- __name(this, "SnipingConfigManager");
8881
- }
8882
- static CONFIG_HEADER_NAME = "x-force-observability-pointkill";
8883
- static config = {
8884
- logs: {
8885
- modules: []
8886
- },
8887
- traces: {
8888
- modules: []
8889
- },
8890
- metrics: {
8891
- modules: []
8892
- }
8893
- };
8894
- static getConfig() {
8895
- return _SnipingConfigManager.config;
8896
- }
8897
- static setConfig(configStr) {
8898
- console.log("configStr", configStr);
8899
- if (!configStr) return;
8900
- try {
8901
- const res = JSON.parse(configStr);
8902
- const { logs: logs2, traces, metrics } = res ?? {};
8903
- if (res) {
8904
- _SnipingConfigManager.config = {
8905
- logs: {
8906
- modules: Array.isArray(logs2?.modules) ? logs2.modules : []
8907
- },
8908
- traces: {
8909
- modules: Array.isArray(traces?.modules) ? traces.modules : []
8910
- },
8911
- metrics: {
8912
- modules: Array.isArray(metrics?.modules) ? metrics.modules : []
8913
- }
8914
- };
8915
- }
8916
- } catch (error) {
8917
- console.error("Failed to parse sniping config:", error);
8918
- }
8919
- }
8920
- static updateFromHeaders(headers) {
8921
- const snipingConfig = headers[_SnipingConfigManager.CONFIG_HEADER_NAME] || headers[_SnipingConfigManager.CONFIG_HEADER_NAME.toLowerCase()];
8922
- if (snipingConfig) {
8923
- _SnipingConfigManager.setConfig(snipingConfig);
8924
- }
8925
- }
8926
- };
8927
-
8928
8877
  // src/core/log-exporter.ts
8929
8878
  var CustomExporter = class {
8930
8879
  static {
@@ -8935,19 +8884,12 @@ var CustomExporter = class {
8935
8884
  constructor() {
8936
8885
  }
8937
8886
  export(logs2, resultCallback) {
8938
- const snipingConfig = SnipingConfigManager.getConfig();
8939
- console.log("snipingConfig", snipingConfig);
8940
- if (snipingConfig.logs.modules.includes("*")) {
8941
- resultCallback({
8942
- code: ExportResultCode.SUCCESS
8943
- });
8944
- return;
8945
- }
8887
+ const pointKillConfig = context.active().getValue(POINT_KILL_TAG);
8946
8888
  const filteredLogs = logs2.filter((log) => {
8947
- const module = log.attributes?.module;
8948
- if (module && snipingConfig.logs.modules.includes(module)) {
8949
- return false;
8950
- }
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;
8951
8893
  return true;
8952
8894
  });
8953
8895
  if (filteredLogs.length === 0) {
@@ -8967,19 +8909,23 @@ var CustomExporter = class {
8967
8909
  ...defaultResourceAttr
8968
8910
  }
8969
8911
  },
8970
- logRecords: filteredLogs.map((log) => ({
8971
- timeUnixNano: hrTimeToNanosNumber(log.hrTime),
8972
- observedTimeUnixNano: hrTimeToNanosNumber(log.hrTimeObserved),
8973
- severityNumber: log.severityNumber,
8974
- severityText: log.severityText,
8975
- body: log.body,
8976
- attributes: convertAttributesToString({
8912
+ logRecords: filteredLogs.map((log) => {
8913
+ const rawAttributes = {
8977
8914
  ...defaultAttributes,
8978
8915
  ...log.attributes ?? {}
8979
- }),
8980
- traceID: log.spanContext?.traceId,
8981
- spanID: log.spanContext?.spanId
8982
- }))
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
+ })
8983
8929
  };
8984
8930
  console.log(this.logPrefix + JSON.stringify(otlpLikeStructure) + this.logSuffix);
8985
8931
  resultCallback({
@@ -9026,25 +8972,17 @@ var CustomTraceExporter = class {
9026
8972
  tracePrefix = "force-trace-prefix";
9027
8973
  traceSuffix = "force-trace-suffix";
9028
8974
  export(spans, resultCallback) {
9029
- const snipingConfig = SnipingConfigManager.getConfig();
9030
- console.log("snipingConfig", snipingConfig);
9031
- if (snipingConfig.traces.modules.includes("*")) {
9032
- resultCallback({
9033
- code: ExportResultCode.SUCCESS
9034
- });
9035
- return;
9036
- }
9037
8975
  const isDev = process.env.NODE_ENV === "development";
9038
8976
  const defaultAttributes = {
9039
8977
  uuid: idGenerator.generateTraceId(),
9040
8978
  app_env: isDev ? AppEnv.Dev : AppEnv.Prod
9041
8979
  };
9042
8980
  const finalSpans = spans.filter((span) => {
8981
+ const pointKillConfig = context.active().getValue(POINT_KILL_TAG);
9043
8982
  if ((span.attributes ?? {})[TraceDropToken]) return false;
9044
- const module = span.attributes?.module;
9045
- if (module && snipingConfig.traces.modules.includes(module)) {
9046
- return false;
9047
- }
8983
+ if (pointKillConfig?.traces?.modules?.includes("*")) return false;
8984
+ const moduleName = span.attributes?.module;
8985
+ if (moduleName && pointKillConfig?.traces?.modules?.includes(moduleName)) return false;
9048
8986
  return true;
9049
8987
  });
9050
8988
  if (!finalSpans.length) {
@@ -9093,7 +9031,36 @@ function isObservable(input) {
9093
9031
  }
9094
9032
  __name(isObservable, "isObservable");
9095
9033
 
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
+
9096
9062
  // src/core/sdk.ts
9063
+ var POINT_KILL_TAG = /* @__PURE__ */ Symbol("__point_kill");
9097
9064
  var AppOTelSDK = class _AppOTelSDK {
9098
9065
  static {
9099
9066
  __name(this, "AppOTelSDK");
@@ -9134,13 +9101,13 @@ var AppOTelSDK = class _AppOTelSDK {
9134
9101
  * wrapper 形式
9135
9102
  */
9136
9103
  startContext(headers, name, fn) {
9137
- SnipingConfigManager.updateFromHeaders(headers);
9138
9104
  const carrier = {};
9139
9105
  const customTraceId = headers["rpc-persist-apaas-observability-trace"];
9140
9106
  if (customTraceId && _AppOTelSDK.CUSTOM_FORMAT_REGEX.test(customTraceId)) {
9141
9107
  carrier["traceparent"] = `00-${customTraceId}-01`;
9142
9108
  }
9143
9109
  const activeContext = propagation.extract(context.active(), carrier);
9110
+ activeContext.setValue(POINT_KILL_TAG, processSnipConfig(headers["x-force-observability-pointkill"]));
9144
9111
  const tracer = trace.getTracer("app-core");
9145
9112
  tracer.startActiveSpan(name, {}, activeContext, (span) => {
9146
9113
  fn(span);