@lark-apaas/nestjs-logger 1.0.2-alpha.20 → 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
@@ -63657,10 +63657,10 @@ var METRICS_LOGGER = Symbol("METRICS_LOGGER");
63657
63657
  // src/service/app-logger.service.ts
63658
63658
  import { RequestContextService as RequestContextService2, OBSERVABLE_SERVICE_TOKEN } from "@lark-apaas/nestjs-common";
63659
63659
 
63660
- // ../../../node_modules/@lark-apaas/nestjs-observable/dist/index.js
63660
+ // ../nestjs-observable/dist/index.js
63661
63661
  import { Global, Module } from "@nestjs/common";
63662
63662
 
63663
- // ../../../node_modules/@lark-apaas/nestjs-observable/node_modules/@lark-apaas/observable/dist/index.js
63663
+ // ../observable-server/dist/index.js
63664
63664
  var import_sdk_node = __toESM(require_src12(), 1);
63665
63665
  init_esm6();
63666
63666
  var import_sdk_trace_node = __toESM(require_src2(), 1);
@@ -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,
@@ -63960,15 +63935,16 @@ var AppOTelSDK = class _AppOTelSDK {
63960
63935
  */
63961
63936
  startContext(headers, name, fn) {
63962
63937
  const carrier = {};
63963
- const customTraceId = headers["x-apaas-observability"];
63938
+ const customTraceId = headers["rpc-persist-apaas-observability-trace"];
63939
+ const upperClass = headers["Rpc-Persist-Apaas-Observability-Trace"];
63964
63940
  if (customTraceId && _AppOTelSDK.CUSTOM_FORMAT_REGEX.test(customTraceId)) {
63965
63941
  carrier["traceparent"] = `00-${customTraceId}-01`;
63966
- } else {
63967
- carrier["traceparent"] = `00-${idGenerator.generateTraceId()}-${idGenerator.generateSpanId()}-01`;
63968
63942
  }
63969
63943
  const activeContext = propagation.extract(context.active(), carrier);
63970
63944
  const tracer = trace.getTracer("app-core");
63971
63945
  tracer.startActiveSpan(name, {}, activeContext, (span) => {
63946
+ span.setAttribute("upperClass", upperClass);
63947
+ span.setAttribute("lowerClass", customTraceId);
63972
63948
  fn(span);
63973
63949
  });
63974
63950
  }
@@ -64028,26 +64004,8 @@ var AppOTelSDK = class _AppOTelSDK {
64028
64004
  }
64029
64005
  };
64030
64006
  var appSdk = new AppOTelSDK();
64031
- function Trace(name) {
64032
- return function(target, propertyKey, descriptor) {
64033
- const originalMethod = descriptor.value;
64034
- if (!originalMethod) {
64035
- return descriptor;
64036
- }
64037
- descriptor.value = async function(...args) {
64038
- const spanName = name || `${target?.constructor?.name}.${String(propertyKey)}`;
64039
- return await appSdk.trace(spanName, async (_) => {
64040
- const result = await originalMethod.apply(this, args);
64041
- return result;
64042
- });
64043
- };
64044
- return descriptor;
64045
- };
64046
- }
64047
- __name(Trace, "Trace");
64048
- __name2(Trace, "Trace");
64049
64007
 
64050
- // ../../../node_modules/@lark-apaas/nestjs-observable/dist/index.js
64008
+ // ../nestjs-observable/dist/index.js
64051
64009
  import { CommonModule } from "@lark-apaas/nestjs-common";
64052
64010
  import { Injectable } from "@nestjs/common";
64053
64011
  import { RequestContextService } from "@lark-apaas/nestjs-common";
@@ -64085,13 +64043,23 @@ var ObservableService = class {
64085
64043
  log(level, message, attributes = {}) {
64086
64044
  const contextAttributes = this.reqCtx.getContext() || {};
64087
64045
  appSdk.log(level, message, {
64046
+ module: "app_server",
64047
+ source_type: "user",
64088
64048
  ...contextAttributes,
64089
64049
  ...attributes
64090
64050
  });
64091
64051
  }
64092
- /** 记录链路 */
64052
+ /** 记录链路 for user,供用户调用 */
64093
64053
  trace(name, fn) {
64094
- 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
+ });
64095
64063
  }
64096
64064
  /** 手动刷新(FaaS/请求结束时可调用) */
64097
64065
  async flush() {
@@ -64206,6 +64174,32 @@ var ObservableTraceMiddleware = class {
64206
64174
  ObservableTraceMiddleware = _ts_decorate3([
64207
64175
  Injectable2()
64208
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");
64209
64203
 
64210
64204
  // src/service/app-logger.service.ts
64211
64205
  function _ts_decorate4(decorators, target, key, desc) {