@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.js CHANGED
@@ -63771,35 +63771,27 @@ var CustomExporter = class {
63771
63771
  uuid: idGenerator.generateTraceId(),
63772
63772
  app_env: isDev ? AppEnv.Dev : AppEnv.Prod
63773
63773
  };
63774
- const otlpLikeStructures = logs2.map((log) => {
63775
- const rawAttributes = {
63776
- ...defaultAttributes,
63777
- ...log.attributes ?? {}
63778
- };
63779
- const attributes = convertAttributesToString(rawAttributes);
63780
- return {
63781
- resource: {
63782
- attributes: {
63783
- ...defaultResourceAttr
63784
- }
63785
- },
63786
- logRecords: [
63787
- {
63788
- timeUnixNano: hrTimeToNanosNumber(log.hrTime),
63789
- observedTimeUnixNano: hrTimeToNanosNumber(log.hrTimeObserved),
63790
- severityNumber: log.severityNumber,
63791
- severityText: log.severityText,
63792
- body: log.body,
63793
- attributes,
63794
- traceId: log.spanContext?.traceId,
63795
- spanId: log.spanContext?.spanId
63796
- }
63797
- ]
63798
- };
63799
- });
63800
- otlpLikeStructures.forEach((structure) => {
63801
- console.log(this.logPrefix + JSON.stringify(structure) + this.logSuffix);
63802
- });
63774
+ const otlpLikeStructure = {
63775
+ resource: {
63776
+ attributes: {
63777
+ ...defaultResourceAttr
63778
+ }
63779
+ },
63780
+ logRecords: logs2.map((log) => ({
63781
+ timeUnixNano: hrTimeToNanosNumber(log.hrTime),
63782
+ observedTimeUnixNano: hrTimeToNanosNumber(log.hrTimeObserved),
63783
+ severityNumber: log.severityNumber,
63784
+ severityText: log.severityText,
63785
+ body: log.body,
63786
+ attributes: convertAttributesToString({
63787
+ ...defaultAttributes,
63788
+ ...log.attributes ?? {}
63789
+ }),
63790
+ traceID: log.spanContext?.traceId,
63791
+ spanID: log.spanContext?.spanId
63792
+ }))
63793
+ };
63794
+ console.log(this.logPrefix + JSON.stringify(otlpLikeStructure) + this.logSuffix);
63803
63795
  resultCallback({
63804
63796
  code: ExportResultCode.SUCCESS
63805
63797
  });
@@ -63856,60 +63848,38 @@ var CustomTraceExporter = class {
63856
63848
  }
63857
63849
  tracePrefix = "force-trace-prefix";
63858
63850
  traceSuffix = "force-trace-suffix";
63859
- requestContextService;
63860
- constructor(contextProvider) {
63861
- this.requestContextService = contextProvider;
63862
- }
63863
63851
  export(spans, resultCallback) {
63864
63852
  const isDev = process.env.NODE_ENV === "development";
63865
63853
  const defaultAttributes = {
63866
63854
  uuid: idGenerator.generateTraceId(),
63867
63855
  app_env: isDev ? AppEnv.Dev : AppEnv.Prod
63868
63856
  };
63869
- const ctx = this.requestContextService.getContext();
63870
- const contextAttributes = ctx ? {
63871
- user_id: ctx.userId,
63872
- tenant_id: ctx.tenantId,
63873
- app_id: ctx.appId,
63874
- method: ctx.method,
63875
- path: ctx.path
63876
- } : {
63877
- user_id: void 0,
63878
- tenant_id: void 0,
63879
- app_id: void 0
63880
- };
63881
- const otlpLikeStructures = spans.map((span) => {
63882
- const rawAttributes = {
63883
- ...defaultAttributes,
63884
- ...contextAttributes,
63885
- ...span.attributes ?? {}
63886
- };
63887
- const attributes = convertAttributesToString(rawAttributes);
63888
- return {
63889
- resource: {
63890
- // 合并 OTel Resource(此处暂留空,真实场景可由 sdk.resource 提供)
63891
- attributes: {
63892
- ...defaultResourceAttr
63893
- }
63894
- },
63895
- spans: [
63896
- {
63897
- startTimeUnixNano: hrTimeToNanosNumber(span.startTime),
63898
- endTimeUnixNano: hrTimeToNanosNumber(span.endTime),
63899
- name: span.name,
63900
- attributes,
63901
- traceId: span.spanContext().traceId,
63902
- spanId: span.spanContext().spanId,
63903
- parentSpanID: span.parentSpanContext?.spanId,
63904
- status: span.status,
63905
- spanKind: SpanKind.SERVER
63906
- }
63907
- ]
63908
- };
63909
- });
63910
- otlpLikeStructures.forEach((structure) => {
63911
- console.log(this.tracePrefix + JSON.stringify(structure) + this.traceSuffix);
63912
- });
63857
+ const otlpLikeStructure = {
63858
+ resource: {
63859
+ attributes: {
63860
+ ...defaultResourceAttr
63861
+ }
63862
+ },
63863
+ spans: spans.map((span) => {
63864
+ const rawAttributes = {
63865
+ ...defaultAttributes,
63866
+ ...span.attributes ?? {}
63867
+ };
63868
+ const attributes = convertAttributesToString(rawAttributes);
63869
+ return {
63870
+ startTimeUnixNano: hrTimeToNanosNumber(span.startTime),
63871
+ endTimeUnixNano: hrTimeToNanosNumber(span.endTime),
63872
+ name: span.name,
63873
+ attributes,
63874
+ traceID: span.spanContext().traceId,
63875
+ spanID: span.spanContext().spanId,
63876
+ parentSpanID: span.parentSpanContext?.spanId,
63877
+ status: span.status,
63878
+ kind: SpanKind.SERVER
63879
+ };
63880
+ })
63881
+ };
63882
+ console.log(this.tracePrefix + JSON.stringify(otlpLikeStructure) + this.traceSuffix);
63913
63883
  resultCallback({
63914
63884
  code: ExportResultCode.SUCCESS
63915
63885
  });
@@ -63928,9 +63898,6 @@ var AppOTelSDK = class _AppOTelSDK {
63928
63898
  logProcessor = null;
63929
63899
  spanProcessor = null;
63930
63900
  static CUSTOM_FORMAT_REGEX = /^[0-9a-fA-F]{32}-[0-9a-fA-F]{16}$/;
63931
- contextProvider = {
63932
- getContext: /* @__PURE__ */ __name2(() => ({}), "getContext")
63933
- };
63934
63901
  start() {
63935
63902
  const resource = detectResources({
63936
63903
  detectors: [
@@ -63940,12 +63907,12 @@ var AppOTelSDK = class _AppOTelSDK {
63940
63907
  const logExporter = new CustomExporter();
63941
63908
  this.logProcessor = new BatchLogRecordProcessor(logExporter, {
63942
63909
  scheduledDelayMillis: 2e3,
63943
- maxExportBatchSize: 10
63910
+ maxExportBatchSize: 2
63944
63911
  });
63945
- const traceExporter = new CustomTraceExporter(this.contextProvider);
63912
+ const traceExporter = new CustomTraceExporter();
63946
63913
  this.spanProcessor = new import_sdk_trace_node.BatchSpanProcessor(traceExporter, {
63947
- scheduledDelayMillis: 1e3,
63948
- maxExportBatchSize: 1
63914
+ scheduledDelayMillis: 2e3,
63915
+ maxExportBatchSize: 2
63949
63916
  });
63950
63917
  this.sdk = new import_sdk_node.NodeSDK({
63951
63918
  resource,
@@ -63961,17 +63928,12 @@ var AppOTelSDK = class _AppOTelSDK {
63961
63928
  startContext(headers, name, fn) {
63962
63929
  const carrier = {};
63963
63930
  const customTraceId = headers["rpc-persist-apaas-observability-trace"];
63964
- const upperClass = headers["Rpc-Persist-Apaas-Observability-Trace"];
63965
63931
  if (customTraceId && _AppOTelSDK.CUSTOM_FORMAT_REGEX.test(customTraceId)) {
63966
63932
  carrier["traceparent"] = `00-${customTraceId}-01`;
63967
- } else {
63968
- carrier["traceparent"] = `00-${idGenerator.generateTraceId()}-${idGenerator.generateSpanId()}-01`;
63969
63933
  }
63970
63934
  const activeContext = propagation.extract(context.active(), carrier);
63971
63935
  const tracer = trace.getTracer("app-core");
63972
63936
  tracer.startActiveSpan(name, {}, activeContext, (span) => {
63973
- span.setAttribute("upperClass", upperClass);
63974
- span.setAttribute("lowerClass", customTraceId);
63975
63937
  fn(span);
63976
63938
  });
63977
63939
  }
@@ -64031,24 +63993,6 @@ var AppOTelSDK = class _AppOTelSDK {
64031
63993
  }
64032
63994
  };
64033
63995
  var appSdk = new AppOTelSDK();
64034
- function Trace(name) {
64035
- return function(target, propertyKey, descriptor) {
64036
- const originalMethod = descriptor.value;
64037
- if (!originalMethod) {
64038
- return descriptor;
64039
- }
64040
- descriptor.value = async function(...args) {
64041
- const spanName = name || `${target?.constructor?.name}.${String(propertyKey)}`;
64042
- return await appSdk.trace(spanName, async (_) => {
64043
- const result = await originalMethod.apply(this, args);
64044
- return result;
64045
- });
64046
- };
64047
- return descriptor;
64048
- };
64049
- }
64050
- __name(Trace, "Trace");
64051
- __name2(Trace, "Trace");
64052
63996
 
64053
63997
  // ../nestjs-observable/dist/index.js
64054
63998
  import { CommonModule } from "@lark-apaas/nestjs-common";
@@ -64073,28 +64017,49 @@ function _ts_metadata(k, v) {
64073
64017
  }
64074
64018
  __name(_ts_metadata, "_ts_metadata");
64075
64019
  __name3(_ts_metadata, "_ts_metadata");
64076
- var ObservableService = class {
64020
+ var ObservableService = class _ObservableService {
64077
64021
  static {
64078
- __name(this, "ObservableService");
64022
+ __name(this, "_ObservableService");
64079
64023
  }
64080
64024
  static {
64081
64025
  __name3(this, "ObservableService");
64082
64026
  }
64083
64027
  reqCtx;
64028
+ static _instance = null;
64084
64029
  constructor(reqCtx) {
64085
64030
  this.reqCtx = reqCtx;
64031
+ if (!_ObservableService._instance) {
64032
+ _ObservableService._instance = this;
64033
+ }
64034
+ }
64035
+ /** 获取全局单例(需确保模块已初始化) */
64036
+ static getInstance() {
64037
+ if (!_ObservableService._instance) {
64038
+ throw new Error("[nestjs-observable] ObservableService \u5C1A\u672A\u521D\u59CB\u5316\uFF0C\u8BF7\u786E\u4FDD\u5728\u6839\u6A21\u5757\u5F15\u5165 NestjsObservableModule.forRoot()");
64039
+ }
64040
+ return _ObservableService._instance;
64086
64041
  }
64087
64042
  /** 记录日志(自动合并上下文) */
64088
64043
  log(level, message, attributes = {}) {
64089
64044
  const contextAttributes = this.reqCtx.getContext() || {};
64090
64045
  appSdk.log(level, message, {
64046
+ module: "app_server",
64047
+ source_type: "user",
64091
64048
  ...contextAttributes,
64092
64049
  ...attributes
64093
64050
  });
64094
64051
  }
64095
- /** 记录链路 */
64052
+ /** 记录链路 for user,供用户调用 */
64096
64053
  trace(name, fn) {
64097
- return appSdk.trace(name, fn);
64054
+ const contextAttributes = this.reqCtx.getContext() || {};
64055
+ return appSdk.trace(name, (span) => {
64056
+ span.setAttributes({
64057
+ module: "app_server",
64058
+ source_type: "user",
64059
+ ...contextAttributes
64060
+ });
64061
+ return fn(span);
64062
+ });
64098
64063
  }
64099
64064
  /** 手动刷新(FaaS/请求结束时可调用) */
64100
64065
  async flush() {
@@ -64121,6 +64086,15 @@ function _ts_metadata2(k, v) {
64121
64086
  }
64122
64087
  __name(_ts_metadata2, "_ts_metadata2");
64123
64088
  __name3(_ts_metadata2, "_ts_metadata");
64089
+ var OBSERVABLE_SINGLETON = null;
64090
+ function getObservableService() {
64091
+ if (!OBSERVABLE_SINGLETON) {
64092
+ throw new Error("[nestjs-observable] ObservableService \u5C1A\u672A\u521D\u59CB\u5316\uFF0C\u8BF7\u786E\u4FDD\u5728\u6839\u6A21\u5757\u5F15\u5165 NestjsObservableModule.forRoot()");
64093
+ }
64094
+ return OBSERVABLE_SINGLETON;
64095
+ }
64096
+ __name(getObservableService, "getObservableService");
64097
+ __name3(getObservableService, "getObservableService");
64124
64098
  var NestjsObservableModule = class _NestjsObservableModule {
64125
64099
  static {
64126
64100
  __name(this, "_NestjsObservableModule");
@@ -64128,7 +64102,9 @@ var NestjsObservableModule = class _NestjsObservableModule {
64128
64102
  static {
64129
64103
  __name3(this, "NestjsObservableModule");
64130
64104
  }
64131
- constructor() {
64105
+ observableService;
64106
+ constructor(observableService) {
64107
+ this.observableService = observableService;
64132
64108
  }
64133
64109
  static forRoot() {
64134
64110
  return {
@@ -64138,6 +64114,7 @@ var NestjsObservableModule = class _NestjsObservableModule {
64138
64114
  }
64139
64115
  onModuleInit() {
64140
64116
  appSdk.start();
64117
+ OBSERVABLE_SINGLETON = this.observableService;
64141
64118
  }
64142
64119
  };
64143
64120
  NestjsObservableModule = _ts_decorate2([
@@ -64154,7 +64131,9 @@ NestjsObservableModule = _ts_decorate2([
64154
64131
  ]
64155
64132
  }),
64156
64133
  _ts_metadata2("design:type", Function),
64157
- _ts_metadata2("design:paramtypes", [])
64134
+ _ts_metadata2("design:paramtypes", [
64135
+ typeof ObservableService === "undefined" ? Object : ObservableService
64136
+ ])
64158
64137
  ], NestjsObservableModule);
64159
64138
  var SpanStatusCode2;
64160
64139
  (function(SpanStatusCode22) {
@@ -64209,6 +64188,37 @@ var ObservableTraceMiddleware = class {
64209
64188
  ObservableTraceMiddleware = _ts_decorate3([
64210
64189
  Injectable2()
64211
64190
  ], ObservableTraceMiddleware);
64191
+ function Trace(name) {
64192
+ return function(target, propertyKey, descriptor) {
64193
+ const originalMethod = descriptor.value;
64194
+ if (!originalMethod) {
64195
+ return descriptor;
64196
+ }
64197
+ descriptor.value = async function(...args) {
64198
+ const spanName = name || `${target?.constructor?.name}.${String(propertyKey)}`;
64199
+ const context2 = this.reqCtx?.getContext?.() || {};
64200
+ const contextAttributes = {
64201
+ user_id: context2.userId || "",
64202
+ app_id: context2.appId || "",
64203
+ tenant_id: context2.tenantId || ""
64204
+ };
64205
+ return await appSdk.trace(spanName, async (span) => {
64206
+ if (contextAttributes && typeof contextAttributes === "object") {
64207
+ span.setAttributes({
64208
+ module: "app_server",
64209
+ source_type: "user",
64210
+ ...contextAttributes
64211
+ });
64212
+ }
64213
+ const result = await originalMethod.apply(this, args);
64214
+ return result;
64215
+ });
64216
+ };
64217
+ return descriptor;
64218
+ };
64219
+ }
64220
+ __name(Trace, "Trace");
64221
+ __name3(Trace, "Trace");
64212
64222
 
64213
64223
  // src/service/app-logger.service.ts
64214
64224
  function _ts_decorate4(decorators, target, key, desc) {