@lark-apaas/nestjs-logger 1.0.2-alpha.21 → 1.0.2-alpha.23

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.cjs CHANGED
@@ -65725,35 +65725,27 @@ var CustomExporter = class {
65725
65725
  uuid: idGenerator.generateTraceId(),
65726
65726
  app_env: isDev ? AppEnv.Dev : AppEnv.Prod
65727
65727
  };
65728
- const otlpLikeStructures = logs2.map((log) => {
65729
- const rawAttributes = {
65730
- ...defaultAttributes,
65731
- ...log.attributes ?? {}
65732
- };
65733
- const attributes = convertAttributesToString(rawAttributes);
65734
- return {
65735
- resource: {
65736
- attributes: {
65737
- ...defaultResourceAttr
65738
- }
65739
- },
65740
- logRecords: [
65741
- {
65742
- timeUnixNano: hrTimeToNanosNumber(log.hrTime),
65743
- observedTimeUnixNano: hrTimeToNanosNumber(log.hrTimeObserved),
65744
- severityNumber: log.severityNumber,
65745
- severityText: log.severityText,
65746
- body: log.body,
65747
- attributes,
65748
- traceId: log.spanContext?.traceId,
65749
- spanId: log.spanContext?.spanId
65750
- }
65751
- ]
65752
- };
65753
- });
65754
- otlpLikeStructures.forEach((structure) => {
65755
- console.log(this.logPrefix + JSON.stringify(structure) + this.logSuffix);
65756
- });
65728
+ const otlpLikeStructure = {
65729
+ resource: {
65730
+ attributes: {
65731
+ ...defaultResourceAttr
65732
+ }
65733
+ },
65734
+ logRecords: logs2.map((log) => ({
65735
+ timeUnixNano: hrTimeToNanosNumber(log.hrTime),
65736
+ observedTimeUnixNano: hrTimeToNanosNumber(log.hrTimeObserved),
65737
+ severityNumber: log.severityNumber,
65738
+ severityText: log.severityText,
65739
+ body: log.body,
65740
+ attributes: convertAttributesToString({
65741
+ ...defaultAttributes,
65742
+ ...log.attributes ?? {}
65743
+ }),
65744
+ traceID: log.spanContext?.traceId,
65745
+ spanID: log.spanContext?.spanId
65746
+ }))
65747
+ };
65748
+ console.log(this.logPrefix + JSON.stringify(otlpLikeStructure) + this.logSuffix);
65757
65749
  resultCallback({
65758
65750
  code: ExportResultCode.SUCCESS
65759
65751
  });
@@ -65810,60 +65802,38 @@ var CustomTraceExporter = class {
65810
65802
  }
65811
65803
  tracePrefix = "force-trace-prefix";
65812
65804
  traceSuffix = "force-trace-suffix";
65813
- requestContextService;
65814
- constructor(contextProvider) {
65815
- this.requestContextService = contextProvider;
65816
- }
65817
65805
  export(spans, resultCallback) {
65818
65806
  const isDev = process.env.NODE_ENV === "development";
65819
65807
  const defaultAttributes = {
65820
65808
  uuid: idGenerator.generateTraceId(),
65821
65809
  app_env: isDev ? AppEnv.Dev : AppEnv.Prod
65822
65810
  };
65823
- const ctx = this.requestContextService.getContext();
65824
- const contextAttributes = ctx ? {
65825
- user_id: ctx.userId,
65826
- tenant_id: ctx.tenantId,
65827
- app_id: ctx.appId,
65828
- method: ctx.method,
65829
- path: ctx.path
65830
- } : {
65831
- user_id: void 0,
65832
- tenant_id: void 0,
65833
- app_id: void 0
65811
+ const otlpLikeStructure = {
65812
+ resource: {
65813
+ attributes: {
65814
+ ...defaultResourceAttr
65815
+ }
65816
+ },
65817
+ spans: spans.map((span) => {
65818
+ const rawAttributes = {
65819
+ ...defaultAttributes,
65820
+ ...span.attributes ?? {}
65821
+ };
65822
+ const attributes = convertAttributesToString(rawAttributes);
65823
+ return {
65824
+ startTimeUnixNano: hrTimeToNanosNumber(span.startTime),
65825
+ endTimeUnixNano: hrTimeToNanosNumber(span.endTime),
65826
+ name: span.name,
65827
+ attributes,
65828
+ traceID: span.spanContext().traceId,
65829
+ spanID: span.spanContext().spanId,
65830
+ parentSpanID: span.parentSpanContext?.spanId,
65831
+ status: span.status,
65832
+ kind: SpanKind.SERVER
65833
+ };
65834
+ })
65834
65835
  };
65835
- const otlpLikeStructures = spans.map((span) => {
65836
- const rawAttributes = {
65837
- ...defaultAttributes,
65838
- ...contextAttributes,
65839
- ...span.attributes ?? {}
65840
- };
65841
- const attributes = convertAttributesToString(rawAttributes);
65842
- return {
65843
- resource: {
65844
- // 合并 OTel Resource(此处暂留空,真实场景可由 sdk.resource 提供)
65845
- attributes: {
65846
- ...defaultResourceAttr
65847
- }
65848
- },
65849
- spans: [
65850
- {
65851
- startTimeUnixNano: hrTimeToNanosNumber(span.startTime),
65852
- endTimeUnixNano: hrTimeToNanosNumber(span.endTime),
65853
- name: span.name,
65854
- attributes,
65855
- traceId: span.spanContext().traceId,
65856
- spanId: span.spanContext().spanId,
65857
- parentSpanID: span.parentSpanContext?.spanId,
65858
- status: span.status,
65859
- spanKind: SpanKind.SERVER
65860
- }
65861
- ]
65862
- };
65863
- });
65864
- otlpLikeStructures.forEach((structure) => {
65865
- console.log(this.tracePrefix + JSON.stringify(structure) + this.traceSuffix);
65866
- });
65836
+ console.log(this.tracePrefix + JSON.stringify(otlpLikeStructure) + this.traceSuffix);
65867
65837
  resultCallback({
65868
65838
  code: ExportResultCode.SUCCESS
65869
65839
  });
@@ -65882,9 +65852,6 @@ var AppOTelSDK = class _AppOTelSDK {
65882
65852
  logProcessor = null;
65883
65853
  spanProcessor = null;
65884
65854
  static CUSTOM_FORMAT_REGEX = /^[0-9a-fA-F]{32}-[0-9a-fA-F]{16}$/;
65885
- contextProvider = {
65886
- getContext: /* @__PURE__ */ __name2(() => ({}), "getContext")
65887
- };
65888
65855
  start() {
65889
65856
  const resource = detectResources({
65890
65857
  detectors: [
@@ -65894,12 +65861,12 @@ var AppOTelSDK = class _AppOTelSDK {
65894
65861
  const logExporter = new CustomExporter();
65895
65862
  this.logProcessor = new BatchLogRecordProcessor(logExporter, {
65896
65863
  scheduledDelayMillis: 2e3,
65897
- maxExportBatchSize: 10
65864
+ maxExportBatchSize: 2
65898
65865
  });
65899
- const traceExporter = new CustomTraceExporter(this.contextProvider);
65866
+ const traceExporter = new CustomTraceExporter();
65900
65867
  this.spanProcessor = new import_sdk_trace_node.BatchSpanProcessor(traceExporter, {
65901
- scheduledDelayMillis: 1e3,
65902
- maxExportBatchSize: 1
65868
+ scheduledDelayMillis: 2e3,
65869
+ maxExportBatchSize: 2
65903
65870
  });
65904
65871
  this.sdk = new import_sdk_node.NodeSDK({
65905
65872
  resource,
@@ -65915,17 +65882,12 @@ var AppOTelSDK = class _AppOTelSDK {
65915
65882
  startContext(headers, name, fn) {
65916
65883
  const carrier = {};
65917
65884
  const customTraceId = headers["rpc-persist-apaas-observability-trace"];
65918
- const upperClass = headers["Rpc-Persist-Apaas-Observability-Trace"];
65919
65885
  if (customTraceId && _AppOTelSDK.CUSTOM_FORMAT_REGEX.test(customTraceId)) {
65920
65886
  carrier["traceparent"] = `00-${customTraceId}-01`;
65921
- } else {
65922
- carrier["traceparent"] = `00-${idGenerator.generateTraceId()}-${idGenerator.generateSpanId()}-01`;
65923
65887
  }
65924
65888
  const activeContext = propagation.extract(context.active(), carrier);
65925
65889
  const tracer = trace.getTracer("app-core");
65926
65890
  tracer.startActiveSpan(name, {}, activeContext, (span) => {
65927
- span.setAttribute("upperClass", upperClass);
65928
- span.setAttribute("lowerClass", customTraceId);
65929
65891
  fn(span);
65930
65892
  });
65931
65893
  }
@@ -65985,24 +65947,6 @@ var AppOTelSDK = class _AppOTelSDK {
65985
65947
  }
65986
65948
  };
65987
65949
  var appSdk = new AppOTelSDK();
65988
- function Trace(name) {
65989
- return function(target, propertyKey, descriptor) {
65990
- const originalMethod = descriptor.value;
65991
- if (!originalMethod) {
65992
- return descriptor;
65993
- }
65994
- descriptor.value = async function(...args) {
65995
- const spanName = name || `${target?.constructor?.name}.${String(propertyKey)}`;
65996
- return await appSdk.trace(spanName, async (_) => {
65997
- const result = await originalMethod.apply(this, args);
65998
- return result;
65999
- });
66000
- };
66001
- return descriptor;
66002
- };
66003
- }
66004
- __name(Trace, "Trace");
66005
- __name2(Trace, "Trace");
66006
65950
 
66007
65951
  // ../nestjs-observable/dist/index.js
66008
65952
  var import_nestjs_common = require("@lark-apaas/nestjs-common");
@@ -66027,28 +65971,49 @@ function _ts_metadata(k, v) {
66027
65971
  }
66028
65972
  __name(_ts_metadata, "_ts_metadata");
66029
65973
  __name3(_ts_metadata, "_ts_metadata");
66030
- var ObservableService = class {
65974
+ var ObservableService = class _ObservableService {
66031
65975
  static {
66032
- __name(this, "ObservableService");
65976
+ __name(this, "_ObservableService");
66033
65977
  }
66034
65978
  static {
66035
65979
  __name3(this, "ObservableService");
66036
65980
  }
66037
65981
  reqCtx;
65982
+ static _instance = null;
66038
65983
  constructor(reqCtx) {
66039
65984
  this.reqCtx = reqCtx;
65985
+ if (!_ObservableService._instance) {
65986
+ _ObservableService._instance = this;
65987
+ }
65988
+ }
65989
+ /** 获取全局单例(需确保模块已初始化) */
65990
+ static getInstance() {
65991
+ if (!_ObservableService._instance) {
65992
+ throw new Error("[nestjs-observable] ObservableService \u5C1A\u672A\u521D\u59CB\u5316\uFF0C\u8BF7\u786E\u4FDD\u5728\u6839\u6A21\u5757\u5F15\u5165 NestjsObservableModule.forRoot()");
65993
+ }
65994
+ return _ObservableService._instance;
66040
65995
  }
66041
65996
  /** 记录日志(自动合并上下文) */
66042
65997
  log(level, message, attributes = {}) {
66043
65998
  const contextAttributes = this.reqCtx.getContext() || {};
66044
65999
  appSdk.log(level, message, {
66000
+ module: "app_server",
66001
+ source_type: "user",
66045
66002
  ...contextAttributes,
66046
66003
  ...attributes
66047
66004
  });
66048
66005
  }
66049
- /** 记录链路 */
66006
+ /** 记录链路 for user,供用户调用 */
66050
66007
  trace(name, fn) {
66051
- return appSdk.trace(name, fn);
66008
+ const contextAttributes = this.reqCtx.getContext() || {};
66009
+ return appSdk.trace(name, (span) => {
66010
+ span.setAttributes({
66011
+ module: "app_server",
66012
+ source_type: "user",
66013
+ ...contextAttributes
66014
+ });
66015
+ return fn(span);
66016
+ });
66052
66017
  }
66053
66018
  /** 手动刷新(FaaS/请求结束时可调用) */
66054
66019
  async flush() {
@@ -66075,6 +66040,15 @@ function _ts_metadata2(k, v) {
66075
66040
  }
66076
66041
  __name(_ts_metadata2, "_ts_metadata2");
66077
66042
  __name3(_ts_metadata2, "_ts_metadata");
66043
+ var OBSERVABLE_SINGLETON = null;
66044
+ function getObservableService() {
66045
+ if (!OBSERVABLE_SINGLETON) {
66046
+ throw new Error("[nestjs-observable] ObservableService \u5C1A\u672A\u521D\u59CB\u5316\uFF0C\u8BF7\u786E\u4FDD\u5728\u6839\u6A21\u5757\u5F15\u5165 NestjsObservableModule.forRoot()");
66047
+ }
66048
+ return OBSERVABLE_SINGLETON;
66049
+ }
66050
+ __name(getObservableService, "getObservableService");
66051
+ __name3(getObservableService, "getObservableService");
66078
66052
  var NestjsObservableModule = class _NestjsObservableModule {
66079
66053
  static {
66080
66054
  __name(this, "_NestjsObservableModule");
@@ -66082,7 +66056,9 @@ var NestjsObservableModule = class _NestjsObservableModule {
66082
66056
  static {
66083
66057
  __name3(this, "NestjsObservableModule");
66084
66058
  }
66085
- constructor() {
66059
+ observableService;
66060
+ constructor(observableService) {
66061
+ this.observableService = observableService;
66086
66062
  }
66087
66063
  static forRoot() {
66088
66064
  return {
@@ -66092,6 +66068,7 @@ var NestjsObservableModule = class _NestjsObservableModule {
66092
66068
  }
66093
66069
  onModuleInit() {
66094
66070
  appSdk.start();
66071
+ OBSERVABLE_SINGLETON = this.observableService;
66095
66072
  }
66096
66073
  };
66097
66074
  NestjsObservableModule = _ts_decorate2([
@@ -66108,7 +66085,9 @@ NestjsObservableModule = _ts_decorate2([
66108
66085
  ]
66109
66086
  }),
66110
66087
  _ts_metadata2("design:type", Function),
66111
- _ts_metadata2("design:paramtypes", [])
66088
+ _ts_metadata2("design:paramtypes", [
66089
+ typeof ObservableService === "undefined" ? Object : ObservableService
66090
+ ])
66112
66091
  ], NestjsObservableModule);
66113
66092
  var SpanStatusCode2;
66114
66093
  (function(SpanStatusCode22) {
@@ -66163,6 +66142,37 @@ var ObservableTraceMiddleware = class {
66163
66142
  ObservableTraceMiddleware = _ts_decorate3([
66164
66143
  (0, import_common5.Injectable)()
66165
66144
  ], ObservableTraceMiddleware);
66145
+ function Trace(name) {
66146
+ return function(target, propertyKey, descriptor) {
66147
+ const originalMethod = descriptor.value;
66148
+ if (!originalMethod) {
66149
+ return descriptor;
66150
+ }
66151
+ descriptor.value = async function(...args) {
66152
+ const spanName = name || `${target?.constructor?.name}.${String(propertyKey)}`;
66153
+ const context2 = this.reqCtx?.getContext?.() || {};
66154
+ const contextAttributes = {
66155
+ user_id: context2.userId || "",
66156
+ app_id: context2.appId || "",
66157
+ tenant_id: context2.tenantId || ""
66158
+ };
66159
+ return await appSdk.trace(spanName, async (span) => {
66160
+ if (contextAttributes && typeof contextAttributes === "object") {
66161
+ span.setAttributes({
66162
+ module: "app_server",
66163
+ source_type: "user",
66164
+ ...contextAttributes
66165
+ });
66166
+ }
66167
+ const result = await originalMethod.apply(this, args);
66168
+ return result;
66169
+ });
66170
+ };
66171
+ return descriptor;
66172
+ };
66173
+ }
66174
+ __name(Trace, "Trace");
66175
+ __name3(Trace, "Trace");
66166
66176
 
66167
66177
  // src/service/app-logger.service.ts
66168
66178
  function _ts_decorate4(decorators, target, key, desc) {