@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.js CHANGED
@@ -63856,60 +63856,38 @@ var CustomTraceExporter = class {
63856
63856
  }
63857
63857
  tracePrefix = "force-trace-prefix";
63858
63858
  traceSuffix = "force-trace-suffix";
63859
- requestContextService;
63860
- constructor(contextProvider) {
63861
- this.requestContextService = contextProvider;
63862
- }
63863
63859
  export(spans, resultCallback) {
63864
63860
  const isDev = process.env.NODE_ENV === "development";
63865
63861
  const defaultAttributes = {
63866
63862
  uuid: idGenerator.generateTraceId(),
63867
63863
  app_env: isDev ? AppEnv.Dev : AppEnv.Prod
63868
63864
  };
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
- });
63865
+ const otlpLikeStructure = {
63866
+ resource: {
63867
+ attributes: {
63868
+ ...defaultResourceAttr
63869
+ }
63870
+ },
63871
+ spans: spans.map((span) => {
63872
+ const rawAttributes = {
63873
+ ...defaultAttributes,
63874
+ ...span.attributes ?? {}
63875
+ };
63876
+ const attributes = convertAttributesToString(rawAttributes);
63877
+ return {
63878
+ startTimeUnixNano: hrTimeToNanosNumber(span.startTime),
63879
+ endTimeUnixNano: hrTimeToNanosNumber(span.endTime),
63880
+ name: span.name,
63881
+ attributes,
63882
+ traceId: span.spanContext().traceId,
63883
+ spanId: span.spanContext().spanId,
63884
+ parentSpanID: span.parentSpanContext?.spanId,
63885
+ status: span.status,
63886
+ spanKind: SpanKind.SERVER
63887
+ };
63888
+ })
63889
+ };
63890
+ console.log(this.tracePrefix + JSON.stringify(otlpLikeStructure) + this.traceSuffix);
63913
63891
  resultCallback({
63914
63892
  code: ExportResultCode.SUCCESS
63915
63893
  });
@@ -63928,9 +63906,6 @@ var AppOTelSDK = class _AppOTelSDK {
63928
63906
  logProcessor = null;
63929
63907
  spanProcessor = null;
63930
63908
  static CUSTOM_FORMAT_REGEX = /^[0-9a-fA-F]{32}-[0-9a-fA-F]{16}$/;
63931
- contextProvider = {
63932
- getContext: /* @__PURE__ */ __name2(() => ({}), "getContext")
63933
- };
63934
63909
  start() {
63935
63910
  const resource = detectResources({
63936
63911
  detectors: [
@@ -63940,12 +63915,12 @@ var AppOTelSDK = class _AppOTelSDK {
63940
63915
  const logExporter = new CustomExporter();
63941
63916
  this.logProcessor = new BatchLogRecordProcessor(logExporter, {
63942
63917
  scheduledDelayMillis: 2e3,
63943
- maxExportBatchSize: 10
63918
+ maxExportBatchSize: 2
63944
63919
  });
63945
- const traceExporter = new CustomTraceExporter(this.contextProvider);
63920
+ const traceExporter = new CustomTraceExporter();
63946
63921
  this.spanProcessor = new import_sdk_trace_node.BatchSpanProcessor(traceExporter, {
63947
- scheduledDelayMillis: 1e3,
63948
- maxExportBatchSize: 1
63922
+ scheduledDelayMillis: 2e3,
63923
+ maxExportBatchSize: 2
63949
63924
  });
63950
63925
  this.sdk = new import_sdk_node.NodeSDK({
63951
63926
  resource,
@@ -63964,8 +63939,6 @@ var AppOTelSDK = class _AppOTelSDK {
63964
63939
  const upperClass = headers["Rpc-Persist-Apaas-Observability-Trace"];
63965
63940
  if (customTraceId && _AppOTelSDK.CUSTOM_FORMAT_REGEX.test(customTraceId)) {
63966
63941
  carrier["traceparent"] = `00-${customTraceId}-01`;
63967
- } else {
63968
- carrier["traceparent"] = `00-${idGenerator.generateTraceId()}-${idGenerator.generateSpanId()}-01`;
63969
63942
  }
63970
63943
  const activeContext = propagation.extract(context.active(), carrier);
63971
63944
  const tracer = trace.getTracer("app-core");
@@ -64031,24 +64004,6 @@ var AppOTelSDK = class _AppOTelSDK {
64031
64004
  }
64032
64005
  };
64033
64006
  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
64007
 
64053
64008
  // ../nestjs-observable/dist/index.js
64054
64009
  import { CommonModule } from "@lark-apaas/nestjs-common";
@@ -64088,13 +64043,23 @@ var ObservableService = class {
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() {
@@ -64209,6 +64174,32 @@ var ObservableTraceMiddleware = class {
64209
64174
  ObservableTraceMiddleware = _ts_decorate3([
64210
64175
  Injectable2()
64211
64176
  ], ObservableTraceMiddleware);
64177
+ function Trace(name) {
64178
+ return function(target, propertyKey, descriptor) {
64179
+ const originalMethod = descriptor.value;
64180
+ if (!originalMethod) {
64181
+ return descriptor;
64182
+ }
64183
+ descriptor.value = async function(...args) {
64184
+ const spanName = name || `${target?.constructor?.name}.${String(propertyKey)}`;
64185
+ const contextAttributes = this.reqCtx?.getContext?.() || {};
64186
+ return await appSdk.trace(spanName, async (span) => {
64187
+ if (contextAttributes && typeof contextAttributes === "object") {
64188
+ span.setAttributes({
64189
+ module: "app_server",
64190
+ source_type: "user",
64191
+ ...contextAttributes
64192
+ });
64193
+ }
64194
+ const result = await originalMethod.apply(this, args);
64195
+ return result;
64196
+ });
64197
+ };
64198
+ return descriptor;
64199
+ };
64200
+ }
64201
+ __name(Trace, "Trace");
64202
+ __name3(Trace, "Trace");
64212
64203
 
64213
64204
  // src/service/app-logger.service.ts
64214
64205
  function _ts_decorate4(decorators, target, key, desc) {
@@ -64685,6 +64676,7 @@ var LoggingInterceptor = class {
64685
64676
  }
64686
64677
  this.appLogger.log(`HTTP request started
64687
64678
  url=${req.url}`, "HTTPTraceInterceptor");
64679
+ this.appLogger.log(`header: ${JSON.stringify(req.headers)}`, "HTTPHeaderInfo");
64688
64680
  this.traceLogger.logStructured("verbose", "HTTP request started", requestMeta, "HTTPTraceInterceptor");
64689
64681
  let logged = false;
64690
64682
  res.on("finish", () => {