plutin 1.7.17 → 1.8.0

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
@@ -459,7 +459,7 @@ __export(src_exports, {
459
459
  Metrics: () => Metrics,
460
460
  MetricsManager: () => MetricsManager,
461
461
  NullSpan: () => NullSpan,
462
- OTEL_ENABLED: () => OTEL_ENABLED2,
462
+ OTEL_ENABLED: () => OTEL_ENABLED,
463
463
  OtelManager: () => OtelManager,
464
464
  RepositoryInstrumentation: () => RepositoryInstrumentation,
465
465
  SpanManager: () => SpanManager,
@@ -495,12 +495,36 @@ var DependencyContainer = class {
495
495
  }
496
496
  static registry = /* @__PURE__ */ new Map();
497
497
  static singletons = /* @__PURE__ */ new Map();
498
- static register(token, myClass, options) {
499
- this.registry.set(token, {
500
- type: "class",
501
- myClass,
502
- singleton: options.singleton
503
- });
498
+ static isClass(value) {
499
+ return typeof value === "function" && value.prototype !== void 0 && value.prototype.constructor === value;
500
+ }
501
+ /**
502
+ * Registra uma classe ou instância no container
503
+ * @param token - Identificador único do registro
504
+ * @param classOrInstance - Classe (será instanciada) ou instância já criada
505
+ * @param options - Opções de registro
506
+ * @note Para instâncias: sempre retorna a mesma referência, mas apenas armazena
507
+ * no cache de singletons se `singleton: true` (para consistência)
508
+ */
509
+ static register(token, classOrInstance, options = {
510
+ singleton: true
511
+ }) {
512
+ if (this.isClass(classOrInstance)) {
513
+ this.registry.set(token, {
514
+ type: "class",
515
+ myClass: classOrInstance,
516
+ singleton: options.singleton
517
+ });
518
+ } else {
519
+ this.registry.set(token, {
520
+ type: "instance",
521
+ instance: classOrInstance,
522
+ singleton: options.singleton
523
+ });
524
+ if (options.singleton) {
525
+ this.singletons.set(token, classOrInstance);
526
+ }
527
+ }
504
528
  }
505
529
  static registerValue(token, value) {
506
530
  this.registry.set(token, {
@@ -530,6 +554,12 @@ var DependencyContainer = class {
530
554
  if (registration.type === "value") {
531
555
  return registration.value;
532
556
  }
557
+ if (registration.type === "instance") {
558
+ if (registration.singleton) {
559
+ return this.singletons.get(token);
560
+ }
561
+ return registration.instance;
562
+ }
533
563
  const { myClass, singleton } = registration;
534
564
  if (singleton) {
535
565
  if (!this.singletons.has(token)) {
@@ -1086,38 +1116,6 @@ var import_fastify = __toESM(require("fastify"), 1);
1086
1116
  var import_node_crypto2 = require("crypto");
1087
1117
  var import_qs = __toESM(require("qs"), 1);
1088
1118
 
1089
- // src/infra/adapters/observability/otel/span-manager.ts
1090
- var import_api = require("@opentelemetry/api");
1091
- var NullSpan = class NullSpan2 {
1092
- static {
1093
- __name(this, "NullSpan");
1094
- }
1095
- setAttributes() {
1096
- return;
1097
- }
1098
- setAttribute() {
1099
- return;
1100
- }
1101
- setStatus() {
1102
- return;
1103
- }
1104
- recordException() {
1105
- return;
1106
- }
1107
- };
1108
- var SpanManager = class SpanManager2 {
1109
- static {
1110
- __name(this, "SpanManager");
1111
- }
1112
- static getActiveSpan() {
1113
- if (process.env.OTEL_ENABLE === "false" || !process.env.OTEL_ENABLE) {
1114
- return new NullSpan();
1115
- }
1116
- const span = import_api.trace.getActiveSpan();
1117
- return span || new NullSpan();
1118
- }
1119
- };
1120
-
1121
1119
  // src/infra/adapters/http/validate-controller-metadata.ts
1122
1120
  var import_reflect_metadata3 = require("reflect-metadata");
1123
1121
  function validateControllerMetadata(controller) {
@@ -1159,10 +1157,12 @@ var FastifyAdapter = class {
1159
1157
  __name(this, "FastifyAdapter");
1160
1158
  }
1161
1159
  logger;
1160
+ span;
1162
1161
  metrics;
1163
1162
  instance;
1164
- constructor(logger, metrics2) {
1163
+ constructor(logger, span, metrics2) {
1165
1164
  this.logger = logger;
1165
+ this.span = span;
1166
1166
  this.metrics = metrics2;
1167
1167
  this.instance = (0, import_fastify.default)({
1168
1168
  bodyLimit: 10 * 1024 * 1024,
@@ -1173,8 +1173,8 @@ var FastifyAdapter = class {
1173
1173
  });
1174
1174
  this.instance.register(import_cors.default);
1175
1175
  this.instance.addHook("onRequest", async (request) => {
1176
- const span = SpanManager.getActiveSpan();
1177
- span.setAttributes({
1176
+ const span2 = this.span.getActiveSpan();
1177
+ span2.setAttributes({
1178
1178
  httpMethod: request.method,
1179
1179
  httpUrl: request.url,
1180
1180
  httpRoute: request.routeOptions.url || request.url,
@@ -1198,9 +1198,9 @@ var FastifyAdapter = class {
1198
1198
  });
1199
1199
  this.instance.addHook("onResponse", async (request, reply) => {
1200
1200
  const route = this.getNormalizedRoute(request);
1201
- const span = SpanManager.getActiveSpan();
1201
+ const span2 = this.span.getActiveSpan();
1202
1202
  const responseTime = reply.elapsedTime || 0;
1203
- span.setAttributes({
1203
+ span2.setAttributes({
1204
1204
  httpStatusCode: reply.statusCode
1205
1205
  });
1206
1206
  this.logger.info({
@@ -1241,7 +1241,7 @@ var FastifyAdapter = class {
1241
1241
  headers: request.headers,
1242
1242
  query: request.query
1243
1243
  };
1244
- const activeSpan = SpanManager.getActiveSpan();
1244
+ const activeSpan = this.span.getActiveSpan();
1245
1245
  try {
1246
1246
  activeSpan.setAttributes({
1247
1247
  controllerName: controllerClass.constructor.name,
@@ -1307,10 +1307,12 @@ var FastifyAdapter = class {
1307
1307
  };
1308
1308
  FastifyAdapter = _ts_decorate([
1309
1309
  _ts_param(0, Inject("Logger")),
1310
- _ts_param(1, Inject("Metrics")),
1310
+ _ts_param(1, Inject("Span")),
1311
+ _ts_param(2, Inject("Metrics")),
1311
1312
  _ts_metadata("design:type", Function),
1312
1313
  _ts_metadata("design:paramtypes", [
1313
1314
  typeof ILogger === "undefined" ? Object : ILogger,
1315
+ typeof ISpanManager === "undefined" ? Object : ISpanManager,
1314
1316
  typeof IMetricsManager === "undefined" ? Object : IMetricsManager
1315
1317
  ])
1316
1318
  ], FastifyAdapter);
@@ -1694,24 +1696,19 @@ var Logger = class {
1694
1696
  };
1695
1697
 
1696
1698
  // src/infra/adapters/observability/otel/metric.ts
1697
- var import_api2 = require("@opentelemetry/api");
1699
+ var import_api = require("@opentelemetry/api");
1698
1700
  var import_node_os = require("os");
1699
1701
  var import_node_perf_hooks = require("perf_hooks");
1700
1702
  var import_node_v8 = __toESM(require("v8"), 1);
1701
- var OTEL_ENABLED = process.env.OTEL_ENABLE === "true";
1702
1703
  var MetricsManager = class {
1703
1704
  static {
1704
1705
  __name(this, "MetricsManager");
1705
1706
  }
1707
+ env;
1706
1708
  meter = null;
1707
- constructor() {
1708
- console.log(`OTEL_ENABLED - ${OTEL_ENABLED} - ${typeof process.env.OTEL_ENABLE}`);
1709
- try {
1710
- this.meter = OTEL_ENABLED ? import_api2.metrics.getMeter(process.env.OTEL_SERVICE_NAME || "plutin-boilerplate-common", process.env.OTEL_SERVICE_VERSION || "1.0.0") : null;
1711
- console.log(`Meter - ${this.meter}`);
1712
- } catch (err) {
1713
- console.error("Error initializing metrics manager:", err);
1714
- }
1709
+ constructor(env) {
1710
+ this.env = env;
1711
+ this.meter = this.env.OTEL_ENABLE ? import_api.metrics.getMeter(this.env.OTEL_SERVICE_NAME || "plutin-boilerplate-common", this.env.OTEL_SERVICE_VERSION || "1.0.0") : null;
1715
1712
  }
1716
1713
  httpRequestsTotal = this.meter?.createCounter("http_requests_total", {
1717
1714
  description: "Total de requisi\xE7\xF5es HTTP",
@@ -1824,7 +1821,7 @@ var MetricsManager = class {
1824
1821
  collectionInterval;
1825
1822
  gcObserver;
1826
1823
  recordHttpRequest(params) {
1827
- if (!OTEL_ENABLED) {
1824
+ if (!this.env.OTEL_ENABLE) {
1828
1825
  return;
1829
1826
  }
1830
1827
  const { method, route, statusCode, durationSeconds, responseSizeBytes } = params;
@@ -1848,7 +1845,7 @@ var MetricsManager = class {
1848
1845
  }
1849
1846
  }
1850
1847
  recordDbQueryError(params) {
1851
- if (!OTEL_ENABLED) {
1848
+ if (!this.env.OTEL_ENABLE) {
1852
1849
  return;
1853
1850
  }
1854
1851
  if (!this.isValidDbQueryErrorParams(params)) {
@@ -1863,7 +1860,7 @@ var MetricsManager = class {
1863
1860
  });
1864
1861
  }
1865
1862
  recordDbQuery(params) {
1866
- if (!OTEL_ENABLED) {
1863
+ if (!this.env.OTEL_ENABLE) {
1867
1864
  return;
1868
1865
  }
1869
1866
  if (!this.isValidDbQueryParams(params)) {
@@ -1897,7 +1894,7 @@ var MetricsManager = class {
1897
1894
  return true;
1898
1895
  }
1899
1896
  recordDbTransaction(params) {
1900
- if (!OTEL_ENABLED) {
1897
+ if (!this.env.OTEL_ENABLE) {
1901
1898
  return;
1902
1899
  }
1903
1900
  const { operation, repository, durationSeconds } = params;
@@ -1913,7 +1910,7 @@ var MetricsManager = class {
1913
1910
  });
1914
1911
  }
1915
1912
  recordDbDeadlock(params) {
1916
- if (!OTEL_ENABLED) {
1913
+ if (!this.env.OTEL_ENABLE) {
1917
1914
  return;
1918
1915
  }
1919
1916
  const { operation, repository, errorMessage } = params;
@@ -1925,7 +1922,7 @@ var MetricsManager = class {
1925
1922
  });
1926
1923
  }
1927
1924
  recordHttpRequestBytes(bytes, attributes) {
1928
- if (!OTEL_ENABLED) {
1925
+ if (!this.env.OTEL_ENABLE) {
1929
1926
  return;
1930
1927
  }
1931
1928
  this.httpRequestBytesTotal?.add(bytes, {
@@ -1935,7 +1932,7 @@ var MetricsManager = class {
1935
1932
  });
1936
1933
  }
1937
1934
  recordProcessingDuration(params) {
1938
- if (!OTEL_ENABLED) {
1935
+ if (!this.env.OTEL_ENABLE) {
1939
1936
  return;
1940
1937
  }
1941
1938
  const { operation, durationSeconds } = params;
@@ -1945,7 +1942,7 @@ var MetricsManager = class {
1945
1942
  });
1946
1943
  }
1947
1944
  recordProcessingError(params) {
1948
- if (!OTEL_ENABLED) {
1945
+ if (!this.env.OTEL_ENABLE) {
1949
1946
  return;
1950
1947
  }
1951
1948
  const { operation, errorType } = params;
@@ -1956,7 +1953,7 @@ var MetricsManager = class {
1956
1953
  });
1957
1954
  }
1958
1955
  recordHttpClientRequest(params) {
1959
- if (!OTEL_ENABLED) {
1956
+ if (!this.env.OTEL_ENABLE) {
1960
1957
  return;
1961
1958
  }
1962
1959
  const { method, url, statusCode, durationSeconds, error, timeout } = params;
@@ -1980,7 +1977,7 @@ var MetricsManager = class {
1980
1977
  }
1981
1978
  }
1982
1979
  recordValidationError(params) {
1983
- if (!OTEL_ENABLED) {
1980
+ if (!this.env.OTEL_ENABLE) {
1984
1981
  return;
1985
1982
  }
1986
1983
  const { field, errorType } = params;
@@ -1991,8 +1988,8 @@ var MetricsManager = class {
1991
1988
  });
1992
1989
  }
1993
1990
  startSystemMetricsCollection(intervalMs = 5e3) {
1994
- console.log(`OTEL_ENABLED - ${!OTEL_ENABLED} - ${this.meter}`);
1995
- if (!OTEL_ENABLED || !this.meter) {
1991
+ console.log(`this.OTEL_ENABLED - ${!this.env.OTEL_ENABLE} - ${this.meter}`);
1992
+ if (!this.env.OTEL_ENABLE || !this.meter) {
1996
1993
  return;
1997
1994
  }
1998
1995
  this.eventLoopMonitor.enable();
@@ -2014,7 +2011,7 @@ var MetricsManager = class {
2014
2011
  this.collectPeriodicMetrics();
2015
2012
  }
2016
2013
  stopSystemMetricsCollection() {
2017
- if (!OTEL_ENABLED || !this.meter) {
2014
+ if (!this.env.OTEL_ENABLE || !this.meter) {
2018
2015
  return;
2019
2016
  }
2020
2017
  if (this.collectionInterval) {
@@ -2028,7 +2025,7 @@ var MetricsManager = class {
2028
2025
  }
2029
2026
  }
2030
2027
  collectSystemMetrics(observableResult) {
2031
- if (!OTEL_ENABLED) {
2028
+ if (!this.env.OTEL_ENABLE) {
2032
2029
  return;
2033
2030
  }
2034
2031
  const attributes = {
@@ -2069,7 +2066,7 @@ var MetricsManager = class {
2069
2066
  observableResult.observe(this.processUptimeSeconds, process.uptime(), attributes);
2070
2067
  }
2071
2068
  collectPeriodicMetrics() {
2072
- if (!OTEL_ENABLED) {
2069
+ if (!this.env.OTEL_ENABLE) {
2073
2070
  return;
2074
2071
  }
2075
2072
  const attributes = {
@@ -2093,7 +2090,7 @@ var MetricsManager = class {
2093
2090
  }
2094
2091
  }
2095
2092
  setupGCObserver() {
2096
- if (!OTEL_ENABLED) {
2093
+ if (!this.env.OTEL_ENABLE) {
2097
2094
  return;
2098
2095
  }
2099
2096
  try {
@@ -2279,6 +2276,42 @@ var OtelManager = class {
2279
2276
  }
2280
2277
  };
2281
2278
 
2279
+ // src/infra/adapters/observability/otel/span-manager.ts
2280
+ var import_api2 = require("@opentelemetry/api");
2281
+ var NullSpan = class NullSpan2 {
2282
+ static {
2283
+ __name(this, "NullSpan");
2284
+ }
2285
+ setAttributes() {
2286
+ return;
2287
+ }
2288
+ setAttribute() {
2289
+ return;
2290
+ }
2291
+ setStatus() {
2292
+ return;
2293
+ }
2294
+ recordException() {
2295
+ return;
2296
+ }
2297
+ };
2298
+ var SpanManager = class SpanManager2 {
2299
+ static {
2300
+ __name(this, "SpanManager");
2301
+ }
2302
+ env;
2303
+ constructor(env) {
2304
+ this.env = env;
2305
+ }
2306
+ getActiveSpan() {
2307
+ if (!this.env.OTEL_ENABLE) {
2308
+ return new NullSpan();
2309
+ }
2310
+ const span = import_api2.trace.getActiveSpan();
2311
+ return span || new NullSpan();
2312
+ }
2313
+ };
2314
+
2282
2315
  // src/infra/decorators/base-instrumentation-strategy.ts
2283
2316
  var import_api3 = require("@opentelemetry/api");
2284
2317
  var MILLISECONDS_TO_SECONDS = 1e3;
@@ -2292,7 +2325,7 @@ var EXCLUDED_METHODS = /* @__PURE__ */ new Set([
2292
2325
  ]);
2293
2326
  var TRACER_NAME = process.env.OTEL_SERVICE_NAME || "plutin-boilerplate-common";
2294
2327
  var TRACER_VERSION = process.env.OTEL_SERVICE_VERSION || "1.0.0";
2295
- var OTEL_ENABLED2 = process.env.OTEL_ENABLE === "true";
2328
+ var OTEL_ENABLED = process.env.OTEL_ENABLE === "true";
2296
2329
  var Tracer = class Tracer2 {
2297
2330
  static {
2298
2331
  __name(this, "Tracer");
@@ -2731,7 +2764,7 @@ var ProcessingInstrumentationStrategyFactory = class ProcessingInstrumentationSt
2731
2764
  __name(this, "ProcessingInstrumentationStrategyFactory");
2732
2765
  }
2733
2766
  static create(serviceName) {
2734
- if (!OTEL_ENABLED2) {
2767
+ if (!OTEL_ENABLED) {
2735
2768
  return new ProcessingLogsOnlyInstrumentationStrategy();
2736
2769
  }
2737
2770
  const tracer = Tracer.getTracer();
@@ -2906,7 +2939,7 @@ var RepositoryInstrumentationStrategyFactory = class RepositoryInstrumentationSt
2906
2939
  __name(this, "RepositoryInstrumentationStrategyFactory");
2907
2940
  }
2908
2941
  static create(dbSystem) {
2909
- if (!OTEL_ENABLED2) {
2942
+ if (!OTEL_ENABLED) {
2910
2943
  return new RepositoryLogsOnlyInstrumentationStrategy();
2911
2944
  }
2912
2945
  const tracer = Tracer.getTracer();