@lark-apaas/observable 1.0.4-alpha.4 → 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.3"};
8845
+ version: "1.0.4-alpha.4"};
8846
8846
 
8847
8847
  // src/const.ts
8848
8848
  var AppEnv = /* @__PURE__ */ (function(AppEnv2) {
@@ -8874,83 +8874,6 @@ function convertAttributesToString(attributes) {
8874
8874
  }
8875
8875
  __name(convertAttributesToString, "convertAttributesToString");
8876
8876
 
8877
- // src/core/point-kill/index.ts
8878
- var PointKillManager = class _PointKillManager {
8879
- static {
8880
- __name(this, "PointKillManager");
8881
- }
8882
- static instance;
8883
- static CONFIG_HEADER_NAME = "x-force-observability-pointkill";
8884
- config = {
8885
- logs: {
8886
- modules: []
8887
- },
8888
- traces: {
8889
- modules: []
8890
- },
8891
- metrics: {
8892
- modules: []
8893
- }
8894
- };
8895
- constructor() {
8896
- }
8897
- static getInstance() {
8898
- if (!_PointKillManager.instance) {
8899
- _PointKillManager.instance = new _PointKillManager();
8900
- }
8901
- return _PointKillManager.instance;
8902
- }
8903
- getConfig() {
8904
- return this.config;
8905
- }
8906
- updateConfig(newConfig) {
8907
- this.config = {
8908
- logs: {
8909
- modules: [
8910
- ...newConfig.logs.modules
8911
- ]
8912
- },
8913
- traces: {
8914
- modules: [
8915
- ...newConfig.traces.modules
8916
- ]
8917
- },
8918
- metrics: {
8919
- modules: [
8920
- ...newConfig.metrics.modules
8921
- ]
8922
- }
8923
- };
8924
- }
8925
- setConfig(configStr) {
8926
- if (!configStr) return;
8927
- try {
8928
- const res = JSON.parse(configStr);
8929
- const { logs: logs2, traces, metrics } = res ?? {};
8930
- this.updateConfig({
8931
- logs: {
8932
- modules: Array.isArray(logs2?.modules) ? logs2.modules : []
8933
- },
8934
- traces: {
8935
- modules: Array.isArray(traces?.modules) ? traces.modules : []
8936
- },
8937
- metrics: {
8938
- modules: Array.isArray(metrics?.modules) ? metrics.modules : []
8939
- }
8940
- });
8941
- } catch (error) {
8942
- console.error("Failed to parse sniping config:", error);
8943
- }
8944
- }
8945
- updateFromHeaders(headers) {
8946
- const snipingConfig = headers[_PointKillManager.CONFIG_HEADER_NAME] || headers[_PointKillManager.CONFIG_HEADER_NAME.toLowerCase()];
8947
- if (snipingConfig) {
8948
- this.setConfig(snipingConfig);
8949
- }
8950
- }
8951
- };
8952
- var pointKillManager = PointKillManager.getInstance();
8953
-
8954
8877
  // src/core/log-exporter.ts
8955
8878
  var CustomExporter = class {
8956
8879
  static {
@@ -8961,19 +8884,12 @@ var CustomExporter = class {
8961
8884
  constructor() {
8962
8885
  }
8963
8886
  export(logs2, resultCallback) {
8964
- const snipingConfig = pointKillManager.getConfig();
8965
- console.log("snipingConfig", snipingConfig);
8966
- if (snipingConfig.logs.modules.includes("*")) {
8967
- resultCallback({
8968
- code: ExportResultCode.SUCCESS
8969
- });
8970
- return;
8971
- }
8887
+ const pointKillConfig = context.active().getValue(POINT_KILL_TAG);
8972
8888
  const filteredLogs = logs2.filter((log) => {
8973
- const module = log.attributes?.module;
8974
- if (module && snipingConfig.logs.modules.includes(module)) {
8975
- return false;
8976
- }
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;
8977
8893
  return true;
8978
8894
  });
8979
8895
  if (filteredLogs.length === 0) {
@@ -8993,19 +8909,23 @@ var CustomExporter = class {
8993
8909
  ...defaultResourceAttr
8994
8910
  }
8995
8911
  },
8996
- logRecords: filteredLogs.map((log) => ({
8997
- timeUnixNano: hrTimeToNanosNumber(log.hrTime),
8998
- observedTimeUnixNano: hrTimeToNanosNumber(log.hrTimeObserved),
8999
- severityNumber: log.severityNumber,
9000
- severityText: log.severityText,
9001
- body: log.body,
9002
- attributes: convertAttributesToString({
8912
+ logRecords: filteredLogs.map((log) => {
8913
+ const rawAttributes = {
9003
8914
  ...defaultAttributes,
9004
8915
  ...log.attributes ?? {}
9005
- }),
9006
- traceID: log.spanContext?.traceId,
9007
- spanID: log.spanContext?.spanId
9008
- }))
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
+ })
9009
8929
  };
9010
8930
  console.log(this.logPrefix + JSON.stringify(otlpLikeStructure) + this.logSuffix);
9011
8931
  resultCallback({
@@ -9052,25 +8972,17 @@ var CustomTraceExporter = class {
9052
8972
  tracePrefix = "force-trace-prefix";
9053
8973
  traceSuffix = "force-trace-suffix";
9054
8974
  export(spans, resultCallback) {
9055
- const snipingConfig = pointKillManager.getConfig();
9056
- console.log("snipingConfig", snipingConfig);
9057
- if (snipingConfig.traces.modules.includes("*")) {
9058
- resultCallback({
9059
- code: ExportResultCode.SUCCESS
9060
- });
9061
- return;
9062
- }
9063
8975
  const isDev = process.env.NODE_ENV === "development";
9064
8976
  const defaultAttributes = {
9065
8977
  uuid: idGenerator.generateTraceId(),
9066
8978
  app_env: isDev ? AppEnv.Dev : AppEnv.Prod
9067
8979
  };
9068
8980
  const finalSpans = spans.filter((span) => {
8981
+ const pointKillConfig = context.active().getValue(POINT_KILL_TAG);
9069
8982
  if ((span.attributes ?? {})[TraceDropToken]) return false;
9070
- const module = span.attributes?.module;
9071
- if (module && snipingConfig.traces.modules.includes(module)) {
9072
- return false;
9073
- }
8983
+ if (pointKillConfig?.traces?.modules?.includes("*")) return false;
8984
+ const moduleName = span.attributes?.module;
8985
+ if (moduleName && pointKillConfig?.traces?.modules?.includes(moduleName)) return false;
9074
8986
  return true;
9075
8987
  });
9076
8988
  if (!finalSpans.length) {
@@ -9119,7 +9031,36 @@ function isObservable(input) {
9119
9031
  }
9120
9032
  __name(isObservable, "isObservable");
9121
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
+
9122
9062
  // src/core/sdk.ts
9063
+ var POINT_KILL_TAG = /* @__PURE__ */ Symbol("__point_kill");
9123
9064
  var AppOTelSDK = class _AppOTelSDK {
9124
9065
  static {
9125
9066
  __name(this, "AppOTelSDK");
@@ -9160,13 +9101,13 @@ var AppOTelSDK = class _AppOTelSDK {
9160
9101
  * wrapper 形式
9161
9102
  */
9162
9103
  startContext(headers, name, fn) {
9163
- pointKillManager.updateFromHeaders(headers);
9164
9104
  const carrier = {};
9165
9105
  const customTraceId = headers["rpc-persist-apaas-observability-trace"];
9166
9106
  if (customTraceId && _AppOTelSDK.CUSTOM_FORMAT_REGEX.test(customTraceId)) {
9167
9107
  carrier["traceparent"] = `00-${customTraceId}-01`;
9168
9108
  }
9169
9109
  const activeContext = propagation.extract(context.active(), carrier);
9110
+ activeContext.setValue(POINT_KILL_TAG, processSnipConfig(headers["x-force-observability-pointkill"]));
9170
9111
  const tracer = trace.getTracer("app-core");
9171
9112
  tracer.startActiveSpan(name, {}, activeContext, (span) => {
9172
9113
  fn(span);