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

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
@@ -65810,60 +65810,38 @@ var CustomTraceExporter = class {
65810
65810
  }
65811
65811
  tracePrefix = "force-trace-prefix";
65812
65812
  traceSuffix = "force-trace-suffix";
65813
- requestContextService;
65814
- constructor(contextProvider) {
65815
- this.requestContextService = contextProvider;
65816
- }
65817
65813
  export(spans, resultCallback) {
65818
65814
  const isDev = process.env.NODE_ENV === "development";
65819
65815
  const defaultAttributes = {
65820
65816
  uuid: idGenerator.generateTraceId(),
65821
65817
  app_env: isDev ? AppEnv.Dev : AppEnv.Prod
65822
65818
  };
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
65819
+ const otlpLikeStructure = {
65820
+ resource: {
65821
+ attributes: {
65822
+ ...defaultResourceAttr
65823
+ }
65824
+ },
65825
+ spans: spans.map((span) => {
65826
+ const rawAttributes = {
65827
+ ...defaultAttributes,
65828
+ ...span.attributes ?? {}
65829
+ };
65830
+ const attributes = convertAttributesToString(rawAttributes);
65831
+ return {
65832
+ startTimeUnixNano: hrTimeToNanosNumber(span.startTime),
65833
+ endTimeUnixNano: hrTimeToNanosNumber(span.endTime),
65834
+ name: span.name,
65835
+ attributes,
65836
+ traceId: span.spanContext().traceId,
65837
+ spanId: span.spanContext().spanId,
65838
+ parentSpanID: span.parentSpanContext?.spanId,
65839
+ status: span.status,
65840
+ spanKind: SpanKind.SERVER
65841
+ };
65842
+ })
65834
65843
  };
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
- });
65844
+ console.log(this.tracePrefix + JSON.stringify(otlpLikeStructure) + this.traceSuffix);
65867
65845
  resultCallback({
65868
65846
  code: ExportResultCode.SUCCESS
65869
65847
  });
@@ -65882,9 +65860,6 @@ var AppOTelSDK = class _AppOTelSDK {
65882
65860
  logProcessor = null;
65883
65861
  spanProcessor = null;
65884
65862
  static CUSTOM_FORMAT_REGEX = /^[0-9a-fA-F]{32}-[0-9a-fA-F]{16}$/;
65885
- contextProvider = {
65886
- getContext: /* @__PURE__ */ __name2(() => ({}), "getContext")
65887
- };
65888
65863
  start() {
65889
65864
  const resource = detectResources({
65890
65865
  detectors: [
@@ -65894,12 +65869,12 @@ var AppOTelSDK = class _AppOTelSDK {
65894
65869
  const logExporter = new CustomExporter();
65895
65870
  this.logProcessor = new BatchLogRecordProcessor(logExporter, {
65896
65871
  scheduledDelayMillis: 2e3,
65897
- maxExportBatchSize: 10
65872
+ maxExportBatchSize: 2
65898
65873
  });
65899
- const traceExporter = new CustomTraceExporter(this.contextProvider);
65874
+ const traceExporter = new CustomTraceExporter();
65900
65875
  this.spanProcessor = new import_sdk_trace_node.BatchSpanProcessor(traceExporter, {
65901
- scheduledDelayMillis: 1e3,
65902
- maxExportBatchSize: 1
65876
+ scheduledDelayMillis: 2e3,
65877
+ maxExportBatchSize: 2
65903
65878
  });
65904
65879
  this.sdk = new import_sdk_node.NodeSDK({
65905
65880
  resource,
@@ -65918,8 +65893,6 @@ var AppOTelSDK = class _AppOTelSDK {
65918
65893
  const upperClass = headers["Rpc-Persist-Apaas-Observability-Trace"];
65919
65894
  if (customTraceId && _AppOTelSDK.CUSTOM_FORMAT_REGEX.test(customTraceId)) {
65920
65895
  carrier["traceparent"] = `00-${customTraceId}-01`;
65921
- } else {
65922
- carrier["traceparent"] = `00-${idGenerator.generateTraceId()}-${idGenerator.generateSpanId()}-01`;
65923
65896
  }
65924
65897
  const activeContext = propagation.extract(context.active(), carrier);
65925
65898
  const tracer = trace.getTracer("app-core");
@@ -65985,24 +65958,6 @@ var AppOTelSDK = class _AppOTelSDK {
65985
65958
  }
65986
65959
  };
65987
65960
  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
65961
 
66007
65962
  // ../nestjs-observable/dist/index.js
66008
65963
  var import_nestjs_common = require("@lark-apaas/nestjs-common");
@@ -66042,13 +65997,23 @@ var ObservableService = class {
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() {
@@ -66163,6 +66128,32 @@ var ObservableTraceMiddleware = class {
66163
66128
  ObservableTraceMiddleware = _ts_decorate3([
66164
66129
  (0, import_common5.Injectable)()
66165
66130
  ], ObservableTraceMiddleware);
66131
+ function Trace(name) {
66132
+ return function(target, propertyKey, descriptor) {
66133
+ const originalMethod = descriptor.value;
66134
+ if (!originalMethod) {
66135
+ return descriptor;
66136
+ }
66137
+ descriptor.value = async function(...args) {
66138
+ const spanName = name || `${target?.constructor?.name}.${String(propertyKey)}`;
66139
+ const contextAttributes = this.reqCtx?.getContext?.() || {};
66140
+ return await appSdk.trace(spanName, async (span) => {
66141
+ if (contextAttributes && typeof contextAttributes === "object") {
66142
+ span.setAttributes({
66143
+ module: "app_server",
66144
+ source_type: "user",
66145
+ ...contextAttributes
66146
+ });
66147
+ }
66148
+ const result = await originalMethod.apply(this, args);
66149
+ return result;
66150
+ });
66151
+ };
66152
+ return descriptor;
66153
+ };
66154
+ }
66155
+ __name(Trace, "Trace");
66156
+ __name3(Trace, "Trace");
66166
66157
 
66167
66158
  // src/service/app-logger.service.ts
66168
66159
  function _ts_decorate4(decorators, target, key, desc) {
@@ -66639,6 +66630,7 @@ var LoggingInterceptor = class {
66639
66630
  }
66640
66631
  this.appLogger.log(`HTTP request started
66641
66632
  url=${req.url}`, "HTTPTraceInterceptor");
66633
+ this.appLogger.log(`header: ${JSON.stringify(req.headers)}`, "HTTPHeaderInfo");
66642
66634
  this.traceLogger.logStructured("verbose", "HTTP request started", requestMeta, "HTTPTraceInterceptor");
66643
66635
  let logged = false;
66644
66636
  res.on("finish", () => {