@use-tusk/drift-node-sdk 0.1.21 → 0.1.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
@@ -11150,13 +11150,23 @@ var HttpInstrumentation = class extends TdInstrumentationBase {
11150
11150
  let requestOptions;
11151
11151
  if (typeof args[0] === "string") {
11152
11152
  const url = new URL(args[0]);
11153
+ const additionalOptions = typeof args[1] === "function" ? void 0 : args[1];
11153
11154
  requestOptions = {
11154
11155
  protocol: url.protocol,
11155
11156
  hostname: url.hostname,
11156
11157
  port: url.port ? parseInt(url.port) : void 0,
11157
11158
  path: url.pathname + url.search,
11158
- method: args[1]?.method || "GET",
11159
- headers: args[1]?.headers || {}
11159
+ method: additionalOptions?.method || "GET",
11160
+ headers: additionalOptions?.headers || {}
11161
+ };
11162
+ } else if (self._isURLObject(args[0])) {
11163
+ const url = args[0];
11164
+ const additionalOptions = typeof args[1] === "function" ? void 0 : args[1];
11165
+ requestOptions = {
11166
+ ...self._urlToRequestOptions(url),
11167
+ method: additionalOptions?.method || "GET",
11168
+ headers: additionalOptions?.headers || {},
11169
+ ...additionalOptions
11160
11170
  };
11161
11171
  } else requestOptions = args[0] || {};
11162
11172
  const method = requestOptions.method || "GET";
@@ -11246,12 +11256,21 @@ var HttpInstrumentation = class extends TdInstrumentationBase {
11246
11256
  let requestOptions;
11247
11257
  if (typeof args[0] === "string") {
11248
11258
  const url = new URL(args[0]);
11259
+ const additionalOptions = typeof args[1] === "function" ? void 0 : args[1];
11249
11260
  requestOptions = {
11250
11261
  protocol: url.protocol,
11251
11262
  hostname: url.hostname,
11252
11263
  port: url.port ? parseInt(url.port) : void 0,
11253
11264
  path: url.pathname + url.search,
11254
- headers: args[1]?.headers || {}
11265
+ headers: additionalOptions?.headers || {}
11266
+ };
11267
+ } else if (self._isURLObject(args[0])) {
11268
+ const url = args[0];
11269
+ const additionalOptions = typeof args[1] === "function" ? void 0 : args[1];
11270
+ requestOptions = {
11271
+ ...self._urlToRequestOptions(url),
11272
+ headers: additionalOptions?.headers || {},
11273
+ ...additionalOptions
11255
11274
  };
11256
11275
  } else requestOptions = args[0] || {};
11257
11276
  const method = "GET";
@@ -11357,6 +11376,25 @@ var HttpInstrumentation = class extends TdInstrumentationBase {
11357
11376
  };
11358
11377
  };
11359
11378
  }
11379
+ /**
11380
+ * Check if the input is a URL object (WHATWG URL API)
11381
+ * This is used to detect when a URL object is passed to http.get/request
11382
+ */
11383
+ _isURLObject(input) {
11384
+ return input instanceof URL || input && typeof input.href === "string" && typeof input.pathname === "string";
11385
+ }
11386
+ /**
11387
+ * Convert a URL object to RequestOptions
11388
+ * Similar to Node.js's internal urlToHttpOptions function
11389
+ */
11390
+ _urlToRequestOptions(url) {
11391
+ return {
11392
+ protocol: url.protocol,
11393
+ hostname: url.hostname,
11394
+ port: url.port ? parseInt(url.port) : void 0,
11395
+ path: url.pathname + (url.search || "")
11396
+ };
11397
+ }
11360
11398
  _normalizeProtocol(protocol, fallback) {
11361
11399
  if (!protocol) return fallback;
11362
11400
  const normalized = protocol.toLowerCase().replace(/:$/, "");
@@ -11734,6 +11772,7 @@ var PgInstrumentation = class extends TdInstrumentationBase {
11734
11772
  packageName,
11735
11773
  instrumentationName: self.INSTRUMENTATION_NAME,
11736
11774
  inputValue,
11775
+ inputSchemaMerges: { values: { matchImportance: 0 } },
11737
11776
  isPreAppStart: false
11738
11777
  }, (spanInfo) => {
11739
11778
  return self.handleReplayQuery(queryConfig, inputValue, spanInfo, stackTrace);
@@ -11753,6 +11792,7 @@ var PgInstrumentation = class extends TdInstrumentationBase {
11753
11792
  instrumentationName: self.INSTRUMENTATION_NAME,
11754
11793
  packageName,
11755
11794
  inputValue,
11795
+ inputSchemaMerges: { values: { matchImportance: 0 } },
11756
11796
  isPreAppStart
11757
11797
  }, (spanInfo) => {
11758
11798
  return self._handleRecordQueryInSpan(spanInfo, originalQuery, queryConfig, args, this);
@@ -11929,7 +11969,8 @@ var PgInstrumentation = class extends TdInstrumentationBase {
11929
11969
  kind: import_src$29.SpanKind.CLIENT,
11930
11970
  stackTrace
11931
11971
  },
11932
- tuskDrift: this.tuskDrift
11972
+ tuskDrift: this.tuskDrift,
11973
+ inputValueSchemaMerges: { values: { matchImportance: 0 } }
11933
11974
  });
11934
11975
  if (!mockData) {
11935
11976
  const queryText = queryConfig.text || inputValue.text || "UNKNOWN_QUERY";
@@ -12792,6 +12833,7 @@ var PostgresInstrumentation = class extends TdInstrumentationBase {
12792
12833
  packageName: "postgres",
12793
12834
  instrumentationName: self.INSTRUMENTATION_NAME,
12794
12835
  inputValue,
12836
+ inputSchemaMerges: { parameters: { matchImportance: 0 } },
12795
12837
  isPreAppStart
12796
12838
  }, (spanInfo) => {
12797
12839
  const wrappedOnFulfilled = (result) => {
@@ -12839,6 +12881,7 @@ var PostgresInstrumentation = class extends TdInstrumentationBase {
12839
12881
  packageName: "postgres",
12840
12882
  instrumentationName: self.INSTRUMENTATION_NAME,
12841
12883
  inputValue,
12884
+ inputSchemaMerges: { parameters: { matchImportance: 0 } },
12842
12885
  isPreAppStart: self.tuskDrift.isAppReady() ? false : true
12843
12886
  }, async (spanInfo) => {
12844
12887
  const mockedResult = await self._handleReplayQueryOperation({
@@ -13041,7 +13084,8 @@ var PostgresInstrumentation = class extends TdInstrumentationBase {
13041
13084
  kind: import_src$27.SpanKind.CLIENT,
13042
13085
  stackTrace
13043
13086
  },
13044
- tuskDrift: this.tuskDrift
13087
+ tuskDrift: this.tuskDrift,
13088
+ inputValueSchemaMerges: { parameters: { matchImportance: 0 } }
13045
13089
  });
13046
13090
  }
13047
13091
  /**
@@ -13095,6 +13139,7 @@ var PostgresInstrumentation = class extends TdInstrumentationBase {
13095
13139
  packageName: "postgres",
13096
13140
  instrumentationName: self.INSTRUMENTATION_NAME,
13097
13141
  inputValue,
13142
+ inputSchemaMerges: { parameters: { matchImportance: 0 } },
13098
13143
  isPreAppStart
13099
13144
  }, (spanInfo) => {
13100
13145
  return self._executeAndRecordCursorCallback({
@@ -13153,6 +13198,7 @@ var PostgresInstrumentation = class extends TdInstrumentationBase {
13153
13198
  packageName: "postgres",
13154
13199
  instrumentationName: self.INSTRUMENTATION_NAME,
13155
13200
  inputValue,
13201
+ inputSchemaMerges: { parameters: { matchImportance: 0 } },
13156
13202
  isPreAppStart: self.tuskDrift.isAppReady() ? false : true
13157
13203
  }, async (spanInfo) => {
13158
13204
  try {
@@ -13354,6 +13400,7 @@ var PostgresInstrumentation = class extends TdInstrumentationBase {
13354
13400
  packageName: "postgres",
13355
13401
  instrumentationName: self.INSTRUMENTATION_NAME,
13356
13402
  inputValue,
13403
+ inputSchemaMerges: { parameters: { matchImportance: 0 } },
13357
13404
  isPreAppStart
13358
13405
  }, async (spanInfo) => {
13359
13406
  const allRows = [];
@@ -13408,6 +13455,7 @@ var PostgresInstrumentation = class extends TdInstrumentationBase {
13408
13455
  packageName: "postgres",
13409
13456
  instrumentationName: self.INSTRUMENTATION_NAME,
13410
13457
  inputValue,
13458
+ inputSchemaMerges: { parameters: { matchImportance: 0 } },
13411
13459
  isPreAppStart: self.tuskDrift.isAppReady() ? false : true
13412
13460
  }, async (spanInfo) => {
13413
13461
  try {
@@ -13462,6 +13510,7 @@ var PostgresInstrumentation = class extends TdInstrumentationBase {
13462
13510
  packageName: "postgres",
13463
13511
  instrumentationName: self.INSTRUMENTATION_NAME,
13464
13512
  inputValue,
13513
+ inputSchemaMerges: { parameters: { matchImportance: 0 } },
13465
13514
  isPreAppStart
13466
13515
  }, (spanInfo) => {
13467
13516
  const wrappedOnFulfilled = (result) => {
@@ -13509,6 +13558,7 @@ var PostgresInstrumentation = class extends TdInstrumentationBase {
13509
13558
  packageName: "postgres",
13510
13559
  instrumentationName: self.INSTRUMENTATION_NAME,
13511
13560
  inputValue,
13561
+ inputSchemaMerges: { parameters: { matchImportance: 0 } },
13512
13562
  isPreAppStart: self.tuskDrift.isAppReady() ? false : true
13513
13563
  }, async (spanInfo) => {
13514
13564
  const mockedResult = await self._handleReplayQueryOperation({
@@ -13908,7 +13958,8 @@ var TdMysqlQueryMock = class {
13908
13958
  kind: import_src$26.SpanKind.CLIENT,
13909
13959
  stackTrace
13910
13960
  },
13911
- tuskDrift: this.tuskDrift
13961
+ tuskDrift: this.tuskDrift,
13962
+ inputValueSchemaMerges: { values: { matchImportance: 0 } }
13912
13963
  });
13913
13964
  }
13914
13965
  };
@@ -14164,6 +14215,7 @@ var MysqlInstrumentation = class extends TdInstrumentationBase {
14164
14215
  packageName: "mysql",
14165
14216
  instrumentationName: self.INSTRUMENTATION_NAME,
14166
14217
  inputValue,
14218
+ inputSchemaMerges: { values: { matchImportance: 0 } },
14167
14219
  isPreAppStart: false
14168
14220
  }, (spanInfo) => {
14169
14221
  const queryEmitter = self.queryMock.handleReplayQuery({
@@ -14190,6 +14242,7 @@ var MysqlInstrumentation = class extends TdInstrumentationBase {
14190
14242
  packageName: "mysql",
14191
14243
  instrumentationName: self.INSTRUMENTATION_NAME,
14192
14244
  inputValue,
14245
+ inputSchemaMerges: { values: { matchImportance: 0 } },
14193
14246
  isPreAppStart
14194
14247
  }, (spanInfo) => {
14195
14248
  return self._handleRecordQuery(spanInfo, originalQuery, this, args, callback, isEventEmitterMode);
@@ -14637,6 +14690,7 @@ var MysqlInstrumentation = class extends TdInstrumentationBase {
14637
14690
  packageName: "mysql",
14638
14691
  instrumentationName: self.INSTRUMENTATION_NAME,
14639
14692
  inputValue,
14693
+ inputSchemaMerges: { values: { matchImportance: 0 } },
14640
14694
  isPreAppStart: false
14641
14695
  }, (spanInfo) => {
14642
14696
  const queryEmitter = self.queryMock.handleReplayQuery({
@@ -14663,6 +14717,7 @@ var MysqlInstrumentation = class extends TdInstrumentationBase {
14663
14717
  packageName: "mysql",
14664
14718
  instrumentationName: self.INSTRUMENTATION_NAME,
14665
14719
  inputValue,
14720
+ inputSchemaMerges: { values: { matchImportance: 0 } },
14666
14721
  isPreAppStart
14667
14722
  }, (spanInfo) => {
14668
14723
  return self._handleRecordQuery(spanInfo, originalQuery, connection, args, callback, isEventEmitterMode);
@@ -15124,6 +15179,7 @@ var MysqlInstrumentation = class extends TdInstrumentationBase {
15124
15179
  packageName: "mysql",
15125
15180
  instrumentationName: self.INSTRUMENTATION_NAME,
15126
15181
  inputValue,
15182
+ inputSchemaMerges: { values: { matchImportance: 0 } },
15127
15183
  isPreAppStart: false
15128
15184
  }, (spanInfo) => {
15129
15185
  return self._handleReplayStream(inputValue, spanInfo, stackTrace, queryInstance);
@@ -15141,6 +15197,7 @@ var MysqlInstrumentation = class extends TdInstrumentationBase {
15141
15197
  packageName: "mysql",
15142
15198
  instrumentationName: self.INSTRUMENTATION_NAME,
15143
15199
  inputValue,
15200
+ inputSchemaMerges: { values: { matchImportance: 0 } },
15144
15201
  isPreAppStart
15145
15202
  }, (spanInfo) => {
15146
15203
  return self._handleRecordStream(spanInfo, originalStream, queryInstance, streamOptions);
@@ -15352,6 +15409,7 @@ var MysqlInstrumentation = class extends TdInstrumentationBase {
15352
15409
  packageName: "mysql",
15353
15410
  instrumentationName: self.INSTRUMENTATION_NAME,
15354
15411
  inputValue,
15412
+ inputSchemaMerges: { values: { matchImportance: 0 } },
15355
15413
  isPreAppStart: false
15356
15414
  }, (spanInfo) => {
15357
15415
  const queryEmitter = self.queryMock.handleReplayQuery({
@@ -15547,6 +15605,27 @@ var TdMysql2ConnectionMock = class extends events.EventEmitter {
15547
15605
  }
15548
15606
  return Promise.resolve();
15549
15607
  }
15608
+ prepare(sql, callback) {
15609
+ const sqlStr = typeof sql === "string" ? sql : sql.sql;
15610
+ const self = this;
15611
+ const mockStatement = {
15612
+ query: sqlStr,
15613
+ id: 1,
15614
+ columns: [],
15615
+ parameters: [],
15616
+ execute: (...args) => {
15617
+ const values = Array.isArray(args[0]) ? args[0] : [];
15618
+ const execCallback = typeof args[args.length - 1] === "function" ? args[args.length - 1] : void 0;
15619
+ return self.mysql2Instrumentation.handleNoOpReplayQuery({
15620
+ sql: sqlStr,
15621
+ values,
15622
+ callback: execCallback
15623
+ });
15624
+ },
15625
+ close: () => {}
15626
+ };
15627
+ if (callback) process.nextTick(() => callback(null, mockStatement));
15628
+ }
15550
15629
  pause() {}
15551
15630
  resume() {}
15552
15631
  escape(value) {
@@ -15603,6 +15682,10 @@ var TdMysql2QueryMock = class {
15603
15682
  });
15604
15683
  }).then(onResolve, onReject);
15605
15684
  };
15685
+ const self = this;
15686
+ emitter.stream = function(streamOptions) {
15687
+ return self._createReplayStreamForQuery(emitter, streamOptions);
15688
+ };
15606
15689
  process.nextTick(() => {
15607
15690
  const callback = queryConfig.callback;
15608
15691
  if (callback) callback(null, [], []);
@@ -15629,6 +15712,10 @@ var TdMysql2QueryMock = class {
15629
15712
  });
15630
15713
  }).then(onResolve, onReject);
15631
15714
  };
15715
+ const self = this;
15716
+ emitter.stream = function(streamOptions) {
15717
+ return self._createReplayStreamForQuery(emitter, streamOptions);
15718
+ };
15632
15719
  (async () => {
15633
15720
  try {
15634
15721
  const mockData = await this._fetchMockData(inputValue, spanInfo, spanName, submoduleName, stackTrace);
@@ -15672,8 +15759,46 @@ var TdMysql2QueryMock = class {
15672
15759
  kind: import_src$24.SpanKind.CLIENT,
15673
15760
  stackTrace
15674
15761
  },
15675
- tuskDrift: this.tuskDrift
15762
+ tuskDrift: this.tuskDrift,
15763
+ inputValueSchemaMerges: { values: { matchImportance: 0 } }
15764
+ });
15765
+ }
15766
+ /**
15767
+ * Create a replay stream for query.stream() calls
15768
+ * This is called when user calls query.stream() on a query object
15769
+ */
15770
+ _createReplayStreamForQuery(queryEmitter, streamOptions) {
15771
+ logger.debug(`[Mysql2Instrumentation] Creating replay stream for query.stream()`);
15772
+ const readableStream = new stream.Readable({
15773
+ objectMode: true,
15774
+ read() {}
15775
+ });
15776
+ queryEmitter.on("result", (row) => {
15777
+ readableStream.push(row);
15778
+ });
15779
+ queryEmitter.on("error", (err) => {
15780
+ readableStream.destroy(err);
15781
+ });
15782
+ queryEmitter.on("end", () => {
15783
+ readableStream.push(null);
15676
15784
  });
15785
+ return readableStream;
15786
+ }
15787
+ /**
15788
+ * Recursively restore Buffer objects from their JSON serialized form.
15789
+ * JSON.stringify converts Buffer to {"type":"Buffer","data":[...]}
15790
+ * This function converts them back to actual Buffer instances.
15791
+ */
15792
+ _restoreBuffers(obj) {
15793
+ if (obj === null || obj === void 0) return obj;
15794
+ if (typeof obj === "object" && obj.type === "Buffer" && Array.isArray(obj.data)) return Buffer.from(obj.data);
15795
+ if (Array.isArray(obj)) return obj.map((item) => this._restoreBuffers(item));
15796
+ if (typeof obj === "object") {
15797
+ const result = {};
15798
+ for (const key of Object.keys(obj)) result[key] = this._restoreBuffers(obj[key]);
15799
+ return result;
15800
+ }
15801
+ return obj;
15677
15802
  }
15678
15803
  /**
15679
15804
  * Convert stored MySQL2 values back to appropriate JavaScript types
@@ -15683,12 +15808,17 @@ var TdMysql2QueryMock = class {
15683
15808
  rows: [],
15684
15809
  fields: []
15685
15810
  };
15686
- if (result.rows !== void 0 && result.fields !== void 0) return {
15687
- rows: result.rows,
15688
- fields: result.fields
15811
+ const restoredResult = this._restoreBuffers(result);
15812
+ if (restoredResult.rows !== void 0 && restoredResult.fields !== void 0) return {
15813
+ rows: restoredResult.rows,
15814
+ fields: restoredResult.fields
15815
+ };
15816
+ if (restoredResult.affectedRows !== void 0) return {
15817
+ rows: restoredResult,
15818
+ fields: []
15689
15819
  };
15690
15820
  return {
15691
- rows: result,
15821
+ rows: restoredResult,
15692
15822
  fields: []
15693
15823
  };
15694
15824
  }
@@ -15833,6 +15963,18 @@ var Mysql2Instrumentation = class extends TdInstrumentationBase {
15833
15963
  logger.debug(`[Mysql2Instrumentation] Wrapped BaseConnection.prototype.end`);
15834
15964
  }
15835
15965
  }
15966
+ if (BaseConnectionClass.prototype && BaseConnectionClass.prototype.prepare) {
15967
+ if (!isWrapped$1(BaseConnectionClass.prototype.prepare)) {
15968
+ this._wrap(BaseConnectionClass.prototype, "prepare", this._getPreparePatchFn("connection"));
15969
+ logger.debug(`[Mysql2Instrumentation] Wrapped BaseConnection.prototype.prepare`);
15970
+ }
15971
+ }
15972
+ if (BaseConnectionClass.prototype && BaseConnectionClass.prototype.changeUser) {
15973
+ if (!isWrapped$1(BaseConnectionClass.prototype.changeUser)) {
15974
+ this._wrap(BaseConnectionClass.prototype, "changeUser", this._getChangeUserPatchFn("connection"));
15975
+ logger.debug(`[Mysql2Instrumentation] Wrapped BaseConnection.prototype.changeUser`);
15976
+ }
15977
+ }
15836
15978
  this.markModuleAsPatched(BaseConnectionClass);
15837
15979
  logger.debug(`[Mysql2Instrumentation] BaseConnection class patching complete`);
15838
15980
  return BaseConnectionClass;
@@ -15884,6 +16026,18 @@ var Mysql2Instrumentation = class extends TdInstrumentationBase {
15884
16026
  logger.debug(`[Mysql2Instrumentation] Wrapped Connection.prototype.end`);
15885
16027
  }
15886
16028
  }
16029
+ if (ConnectionClass.prototype && ConnectionClass.prototype.prepare) {
16030
+ if (!isWrapped$1(ConnectionClass.prototype.prepare)) {
16031
+ this._wrap(ConnectionClass.prototype, "prepare", this._getPreparePatchFn("connection"));
16032
+ logger.debug(`[Mysql2Instrumentation] Wrapped Connection.prototype.prepare`);
16033
+ }
16034
+ }
16035
+ if (ConnectionClass.prototype && ConnectionClass.prototype.changeUser) {
16036
+ if (!isWrapped$1(ConnectionClass.prototype.changeUser)) {
16037
+ this._wrap(ConnectionClass.prototype, "changeUser", this._getChangeUserPatchFn("connection"));
16038
+ logger.debug(`[Mysql2Instrumentation] Wrapped Connection.prototype.changeUser`);
16039
+ }
16040
+ }
15887
16041
  }
15888
16042
  _patchPoolV2(PoolClass) {
15889
16043
  logger.debug(`[Mysql2Instrumentation] Patching Pool class (v2)`);
@@ -15977,6 +16131,7 @@ var Mysql2Instrumentation = class extends TdInstrumentationBase {
15977
16131
  packageName: "mysql2",
15978
16132
  instrumentationName: self.INSTRUMENTATION_NAME,
15979
16133
  inputValue,
16134
+ inputSchemaMerges: { values: { matchImportance: 0 } },
15980
16135
  isPreAppStart: false
15981
16136
  }, (spanInfo) => {
15982
16137
  return self.handleReplayQuery(queryConfig, inputValue, spanInfo, "query", stackTrace);
@@ -15995,6 +16150,7 @@ var Mysql2Instrumentation = class extends TdInstrumentationBase {
15995
16150
  instrumentationName: self.INSTRUMENTATION_NAME,
15996
16151
  packageName: "mysql2",
15997
16152
  inputValue,
16153
+ inputSchemaMerges: { values: { matchImportance: 0 } },
15998
16154
  isPreAppStart
15999
16155
  }, (spanInfo) => {
16000
16156
  return self._handleRecordQueryInSpan(spanInfo, originalQuery, queryConfig, args, this);
@@ -16042,6 +16198,7 @@ var Mysql2Instrumentation = class extends TdInstrumentationBase {
16042
16198
  packageName: "mysql2",
16043
16199
  instrumentationName: self.INSTRUMENTATION_NAME,
16044
16200
  inputValue,
16201
+ inputSchemaMerges: { values: { matchImportance: 0 } },
16045
16202
  isPreAppStart: false
16046
16203
  }, (spanInfo) => {
16047
16204
  return self.handleReplayQuery(queryConfig, inputValue, spanInfo, "execute", stackTrace);
@@ -16060,6 +16217,7 @@ var Mysql2Instrumentation = class extends TdInstrumentationBase {
16060
16217
  instrumentationName: self.INSTRUMENTATION_NAME,
16061
16218
  packageName: "mysql2",
16062
16219
  inputValue,
16220
+ inputSchemaMerges: { values: { matchImportance: 0 } },
16063
16221
  isPreAppStart
16064
16222
  }, (spanInfo) => {
16065
16223
  return self._handleRecordQueryInSpan(spanInfo, originalExecute, queryConfig, args, this);
@@ -16283,6 +16441,251 @@ var Mysql2Instrumentation = class extends TdInstrumentationBase {
16283
16441
  };
16284
16442
  };
16285
16443
  }
16444
+ _getChangeUserPatchFn(clientType) {
16445
+ const self = this;
16446
+ return (originalChangeUser) => {
16447
+ return function changeUser(options, callback) {
16448
+ if (typeof options === "function") {
16449
+ callback = options;
16450
+ options = {};
16451
+ }
16452
+ const inputValue = { clientType };
16453
+ if (self.mode === TuskDriftMode.REPLAY) return handleReplayMode({
16454
+ noOpRequestHandler: () => {
16455
+ if (callback) {
16456
+ process.nextTick(() => callback(null));
16457
+ return;
16458
+ }
16459
+ return Promise.resolve();
16460
+ },
16461
+ isServerRequest: false,
16462
+ replayModeHandler: () => {
16463
+ return SpanUtils.createAndExecuteSpan(self.mode, () => originalChangeUser.apply(this, [options, callback]), {
16464
+ name: `mysql2.${clientType}.changeUser`,
16465
+ kind: import_src$22.SpanKind.CLIENT,
16466
+ submodule: "changeUser",
16467
+ packageName: "mysql2",
16468
+ packageType: PackageType.MYSQL,
16469
+ instrumentationName: self.INSTRUMENTATION_NAME,
16470
+ inputValue,
16471
+ isPreAppStart: false
16472
+ }, (spanInfo) => {
16473
+ if (callback) {
16474
+ process.nextTick(() => callback(null));
16475
+ return;
16476
+ }
16477
+ return Promise.resolve();
16478
+ });
16479
+ }
16480
+ });
16481
+ else if (self.mode === TuskDriftMode.RECORD) return handleRecordMode({
16482
+ originalFunctionCall: () => originalChangeUser.apply(this, [options, callback]),
16483
+ recordModeHandler: ({ isPreAppStart }) => {
16484
+ return SpanUtils.createAndExecuteSpan(self.mode, () => originalChangeUser.apply(this, [options, callback]), {
16485
+ name: `mysql2.${clientType}.changeUser`,
16486
+ kind: import_src$22.SpanKind.CLIENT,
16487
+ submodule: "changeUser",
16488
+ packageName: "mysql2",
16489
+ packageType: PackageType.MYSQL,
16490
+ instrumentationName: self.INSTRUMENTATION_NAME,
16491
+ inputValue,
16492
+ isPreAppStart
16493
+ }, (spanInfo) => {
16494
+ return self._handleSimpleCallbackMethod(spanInfo, originalChangeUser.bind(this, options), callback, this);
16495
+ });
16496
+ },
16497
+ spanKind: import_src$22.SpanKind.CLIENT
16498
+ });
16499
+ else return originalChangeUser.apply(this, [options, callback]);
16500
+ };
16501
+ };
16502
+ }
16503
+ _getPreparePatchFn(clientType) {
16504
+ const self = this;
16505
+ return (originalPrepare) => {
16506
+ return function prepare(...args) {
16507
+ const firstArg = args[0];
16508
+ const sql = typeof firstArg === "string" ? firstArg : firstArg?.sql;
16509
+ const originalCallback = typeof args[args.length - 1] === "function" ? args[args.length - 1] : void 0;
16510
+ if (self.mode === TuskDriftMode.REPLAY) return handleReplayMode({
16511
+ noOpRequestHandler: () => self._handleNoOpReplayPrepare(sql, originalCallback),
16512
+ isServerRequest: false,
16513
+ replayModeHandler: () => self._handleReplayPrepare(sql, originalCallback, clientType)
16514
+ });
16515
+ else if (self.mode === TuskDriftMode.RECORD) return handleRecordMode({
16516
+ originalFunctionCall: () => originalPrepare.apply(this, args),
16517
+ recordModeHandler: ({ isPreAppStart }) => self._handleRecordPrepare(originalPrepare, args, sql, originalCallback, this, clientType, isPreAppStart),
16518
+ spanKind: import_src$22.SpanKind.CLIENT
16519
+ });
16520
+ return originalPrepare.apply(this, args);
16521
+ };
16522
+ };
16523
+ }
16524
+ _handleRecordPrepare(originalPrepare, args, sql, originalCallback, context$6, clientType, isPreAppStart) {
16525
+ const self = this;
16526
+ return SpanUtils.createAndExecuteSpan(this.mode, () => originalPrepare.apply(context$6, args), {
16527
+ name: `mysql2.${clientType}.prepare`,
16528
+ kind: import_src$22.SpanKind.CLIENT,
16529
+ submodule: "prepare",
16530
+ packageType: PackageType.MYSQL,
16531
+ packageName: "mysql2",
16532
+ instrumentationName: this.INSTRUMENTATION_NAME,
16533
+ inputValue: {
16534
+ sql,
16535
+ clientType
16536
+ },
16537
+ isPreAppStart
16538
+ }, (spanInfo) => {
16539
+ const wrappedCallback = (err, statement) => {
16540
+ if (err) {
16541
+ logger.debug(`[Mysql2Instrumentation] MySQL2 prepare error: ${err.message} (${SpanUtils.getTraceInfo()})`);
16542
+ try {
16543
+ SpanUtils.endSpan(spanInfo.span, {
16544
+ code: import_src$22.SpanStatusCode.ERROR,
16545
+ message: err.message
16546
+ });
16547
+ } catch (error) {
16548
+ logger.error(`[Mysql2Instrumentation] error ending span:`, error);
16549
+ }
16550
+ } else {
16551
+ logger.debug(`[Mysql2Instrumentation] MySQL2 prepare completed successfully (${SpanUtils.getTraceInfo()})`);
16552
+ try {
16553
+ SpanUtils.addSpanAttributes(spanInfo.span, { outputValue: {
16554
+ prepared: true,
16555
+ statementId: statement?.id
16556
+ } });
16557
+ SpanUtils.endSpan(spanInfo.span, { code: import_src$22.SpanStatusCode.OK });
16558
+ } catch (error) {
16559
+ logger.error(`[Mysql2Instrumentation] error processing prepare response:`, error);
16560
+ }
16561
+ if (statement && statement.execute) {
16562
+ const originalExecute = statement.execute.bind(statement);
16563
+ statement.execute = self._getPreparedStatementExecuteWrapper(originalExecute, sql, clientType);
16564
+ }
16565
+ }
16566
+ if (originalCallback) originalCallback(err, statement);
16567
+ };
16568
+ const newArgs = [...args];
16569
+ const callbackIndex = newArgs.findIndex((a) => typeof a === "function");
16570
+ if (callbackIndex >= 0) newArgs[callbackIndex] = wrappedCallback;
16571
+ else newArgs.push(wrappedCallback);
16572
+ return originalPrepare.apply(context$6, newArgs);
16573
+ });
16574
+ }
16575
+ _getPreparedStatementExecuteWrapper(originalExecute, sql, clientType) {
16576
+ const self = this;
16577
+ return function execute(...args) {
16578
+ let values = [];
16579
+ let callback;
16580
+ for (const arg of args) if (typeof arg === "function") callback = arg;
16581
+ else if (Array.isArray(arg)) values = arg;
16582
+ else if (arg !== void 0) values = [arg];
16583
+ const inputValue = {
16584
+ sql,
16585
+ values,
16586
+ clientType
16587
+ };
16588
+ const queryConfig = {
16589
+ sql,
16590
+ values,
16591
+ callback
16592
+ };
16593
+ const stackTrace = captureStackTrace(["Mysql2Instrumentation"]);
16594
+ if (self.mode === TuskDriftMode.REPLAY) return handleReplayMode({
16595
+ noOpRequestHandler: () => self.queryMock.handleNoOpReplayQuery(queryConfig),
16596
+ isServerRequest: false,
16597
+ replayModeHandler: () => {
16598
+ const spanName = `mysql2.${clientType}.preparedExecute`;
16599
+ return SpanUtils.createAndExecuteSpan(self.mode, () => originalExecute(...args), {
16600
+ name: spanName,
16601
+ kind: import_src$22.SpanKind.CLIENT,
16602
+ submodule: "preparedExecute",
16603
+ packageType: PackageType.MYSQL,
16604
+ packageName: "mysql2",
16605
+ instrumentationName: self.INSTRUMENTATION_NAME,
16606
+ inputValue,
16607
+ inputSchemaMerges: { values: { matchImportance: 0 } },
16608
+ isPreAppStart: false
16609
+ }, (spanInfo) => {
16610
+ return self.handleReplayQuery(queryConfig, inputValue, spanInfo, "preparedExecute", stackTrace);
16611
+ });
16612
+ }
16613
+ });
16614
+ else if (self.mode === TuskDriftMode.RECORD) return handleRecordMode({
16615
+ originalFunctionCall: () => originalExecute(...args),
16616
+ recordModeHandler: ({ isPreAppStart }) => {
16617
+ const spanName = `mysql2.${clientType}.preparedExecute`;
16618
+ return SpanUtils.createAndExecuteSpan(self.mode, () => originalExecute(...args), {
16619
+ name: spanName,
16620
+ kind: import_src$22.SpanKind.CLIENT,
16621
+ submodule: "preparedExecute",
16622
+ packageType: PackageType.MYSQL,
16623
+ packageName: "mysql2",
16624
+ instrumentationName: self.INSTRUMENTATION_NAME,
16625
+ inputValue,
16626
+ inputSchemaMerges: { values: { matchImportance: 0 } },
16627
+ isPreAppStart
16628
+ }, (spanInfo) => {
16629
+ return self._handleRecordQueryInSpan(spanInfo, originalExecute, queryConfig, args, void 0);
16630
+ });
16631
+ },
16632
+ spanKind: import_src$22.SpanKind.CLIENT
16633
+ });
16634
+ return originalExecute(...args);
16635
+ };
16636
+ }
16637
+ _handleReplayPrepare(sql, originalCallback, clientType) {
16638
+ const self = this;
16639
+ return SpanUtils.createAndExecuteSpan(this.mode, () => {}, {
16640
+ name: `mysql2.${clientType}.prepare`,
16641
+ kind: import_src$22.SpanKind.CLIENT,
16642
+ submodule: "prepare",
16643
+ packageType: PackageType.MYSQL,
16644
+ packageName: "mysql2",
16645
+ instrumentationName: this.INSTRUMENTATION_NAME,
16646
+ inputValue: {
16647
+ sql,
16648
+ clientType
16649
+ },
16650
+ isPreAppStart: false
16651
+ }, (spanInfo) => {
16652
+ const mockStatement = {
16653
+ query: sql,
16654
+ id: 1,
16655
+ columns: [],
16656
+ parameters: [],
16657
+ execute: self._getPreparedStatementExecuteWrapper(() => {}, sql, clientType),
16658
+ close: () => {}
16659
+ };
16660
+ try {
16661
+ SpanUtils.addSpanAttributes(spanInfo.span, { outputValue: { prepared: true } });
16662
+ SpanUtils.endSpan(spanInfo.span, { code: import_src$22.SpanStatusCode.OK });
16663
+ } catch (error) {
16664
+ logger.error(`[Mysql2Instrumentation] error ending prepare span:`, error);
16665
+ }
16666
+ if (originalCallback) process.nextTick(() => originalCallback(null, mockStatement));
16667
+ });
16668
+ }
16669
+ _handleNoOpReplayPrepare(sql, originalCallback) {
16670
+ const self = this;
16671
+ const mockStatement = {
16672
+ query: sql,
16673
+ id: 1,
16674
+ columns: [],
16675
+ parameters: [],
16676
+ execute: (...args) => {
16677
+ const values = Array.isArray(args[0]) ? args[0] : [];
16678
+ const callback = typeof args[args.length - 1] === "function" ? args[args.length - 1] : void 0;
16679
+ return self.queryMock.handleNoOpReplayQuery({
16680
+ sql,
16681
+ values,
16682
+ callback
16683
+ });
16684
+ },
16685
+ close: () => {}
16686
+ };
16687
+ if (originalCallback) process.nextTick(() => originalCallback(null, mockStatement));
16688
+ }
16286
16689
  _handleSimpleCallbackMethod(spanInfo, originalMethod, callback, context$6) {
16287
16690
  if (callback) {
16288
16691
  const wrappedCallback = (error) => {
@@ -16354,15 +16757,29 @@ var Mysql2Instrumentation = class extends TdInstrumentationBase {
16354
16757
  else if (args.length > 1 && typeof args[1] !== "function") config.values = [args[1]];
16355
16758
  return config;
16356
16759
  }
16357
- if (typeof firstArg === "object" && firstArg.sql) return {
16358
- sql: firstArg.sql,
16359
- values: firstArg.values,
16360
- callback: firstArg.callback || (typeof args[1] === "function" ? args[1] : void 0)
16361
- };
16760
+ if (typeof firstArg === "object" && firstArg.sql) {
16761
+ let values = firstArg.values;
16762
+ let callback = firstArg.callback;
16763
+ if (typeof args[1] === "function") callback = args[1];
16764
+ else {
16765
+ if (args[1] !== void 0) values = Array.isArray(args[1]) ? args[1] : [args[1]];
16766
+ if (typeof args[2] === "function") callback = args[2];
16767
+ }
16768
+ return {
16769
+ sql: firstArg.sql,
16770
+ values,
16771
+ callback
16772
+ };
16773
+ }
16362
16774
  return null;
16363
16775
  }
16364
16776
  _handleRecordQueryInSpan(spanInfo, originalQuery, queryConfig, args, context$6) {
16365
- if (!!queryConfig.callback) {
16777
+ const hasCallback = !!queryConfig.callback;
16778
+ const invokeOriginal = (invokeArgs) => {
16779
+ return context$6 ? originalQuery.apply(context$6, invokeArgs) : originalQuery(...invokeArgs);
16780
+ };
16781
+ if (hasCallback) {
16782
+ const parentContext = import_src$22.context.active();
16366
16783
  const originalCallback = queryConfig.callback;
16367
16784
  const wrappedCallback = (error, results, fields) => {
16368
16785
  if (error) {
@@ -16384,7 +16801,7 @@ var Mysql2Instrumentation = class extends TdInstrumentationBase {
16384
16801
  logger.error(`[Mysql2Instrumentation] error processing response:`, error$1);
16385
16802
  }
16386
16803
  }
16387
- return originalCallback(error, results, fields);
16804
+ return import_src$22.context.with(parentContext, () => originalCallback(error, results, fields));
16388
16805
  };
16389
16806
  try {
16390
16807
  const firstArg = args[0];
@@ -16399,9 +16816,9 @@ var Mysql2Instrumentation = class extends TdInstrumentationBase {
16399
16816
  } catch (error) {
16400
16817
  logger.error(`[Mysql2Instrumentation] error replacing callback:`, error, args);
16401
16818
  }
16402
- return originalQuery.apply(context$6, args);
16819
+ return invokeOriginal(args);
16403
16820
  } else {
16404
- const result = originalQuery.apply(context$6, args);
16821
+ const result = invokeOriginal(args);
16405
16822
  if (result && typeof result.on === "function") {
16406
16823
  const streamResults = [];
16407
16824
  let streamFields = null;
@@ -16607,6 +17024,34 @@ var Mysql2Instrumentation = class extends TdInstrumentationBase {
16607
17024
  if (event === "error" && !this._isConnectOrErrorEmitted) return this;
16608
17025
  return super.on(event, listener);
16609
17026
  }
17027
+ once(event, listener) {
17028
+ if (!this._connectEventMock) return super.once(event, listener);
17029
+ if (event === "connect" && !this._isConnectOrErrorEmitted) {
17030
+ this._connectEventMock.getReplayedConnectionEvent(inputValue).then(({ output }) => {
17031
+ if (output !== void 0) process.nextTick(() => {
17032
+ listener.call(this, output);
17033
+ this._isConnectOrErrorEmitted = true;
17034
+ });
17035
+ }).catch((err) => {
17036
+ logger.error(`[Mysql2Instrumentation] Error replaying connection event:`, err);
17037
+ });
17038
+ return this;
17039
+ }
17040
+ if (event === "error" && !this._isConnectOrErrorEmitted) return this;
17041
+ return super.once(event, listener);
17042
+ }
17043
+ connect(callback) {
17044
+ if (!callback) return;
17045
+ this._connectEventMock.getReplayedConnectionEvent(inputValue).then(({ output }) => {
17046
+ if (output !== void 0) process.nextTick(() => {
17047
+ this._isConnectOrErrorEmitted = true;
17048
+ callback(null);
17049
+ });
17050
+ }).catch((err) => {
17051
+ logger.error(`[Mysql2Instrumentation] Error replaying connection event in connect():`, err);
17052
+ process.nextTick(() => callback(err));
17053
+ });
17054
+ }
16610
17055
  }
16611
17056
  const mockConnection = new MockConnection(...args);
16612
17057
  mockConnection.addListener("error", (_err) => {});
@@ -16634,9 +17079,13 @@ var Mysql2Instrumentation = class extends TdInstrumentationBase {
16634
17079
  fields: fields || []
16635
17080
  };
16636
17081
  else if (result.affectedRows !== void 0) outputValue = {
17082
+ fieldCount: result.fieldCount,
16637
17083
  affectedRows: result.affectedRows,
16638
17084
  insertId: result.insertId,
16639
- warningCount: result.warningCount
17085
+ info: result.info ?? "",
17086
+ serverStatus: result.serverStatus ?? 0,
17087
+ warningStatus: result.warningStatus ?? 0,
17088
+ changedRows: result.changedRows ?? 0
16640
17089
  };
16641
17090
  else outputValue = result;
16642
17091
  SpanUtils.addSpanAttributes(spanInfo.span, { outputValue });
@@ -18864,16 +19313,28 @@ var GrpcInstrumentation = class GrpcInstrumentation extends TdInstrumentationBas
18864
19313
  /**
18865
19314
  * Helper method to parse optional unary response arguments
18866
19315
  *
18867
- * Handles the following cases:
18868
- * 1. (metadata: Metadata, callback: Function) - no options
18869
- * 2. (metadata: Metadata, options: Object, callback: Function) - with options
19316
+ * Handles the following cases (matching grpc-node's checkOptionalUnaryResponseArguments):
19317
+ * 1. (callback: Function) - callback only, no metadata or options
19318
+ * 2. (metadata: Metadata, callback: Function) - metadata + callback, no options
19319
+ * 3. (options: Object, callback: Function) - options + callback, no metadata
19320
+ * 4. (metadata: Metadata, options: Object, callback: Function) - full signature
18870
19321
  */
18871
19322
  parseUnaryCallArguments(MetadataConstructor, arg1, arg2, arg3) {
19323
+ if (typeof arg1 === "function") return {
19324
+ metadata: new MetadataConstructor(),
19325
+ options: {},
19326
+ callback: arg1
19327
+ };
18872
19328
  if (arg1 instanceof MetadataConstructor && typeof arg2 === "function") return {
18873
19329
  metadata: arg1,
18874
19330
  options: {},
18875
19331
  callback: arg2
18876
19332
  };
19333
+ if (!(arg1 instanceof MetadataConstructor) && typeof arg1 === "object" && arg1 !== null && typeof arg2 === "function") return {
19334
+ metadata: new MetadataConstructor(),
19335
+ options: arg1,
19336
+ callback: arg2
19337
+ };
18877
19338
  if (arg1 instanceof MetadataConstructor && arg2 instanceof Object && typeof arg3 === "function") return {
18878
19339
  metadata: arg1,
18879
19340
  options: arg2,
@@ -25815,15 +26276,13 @@ var TdSpanExporter = class {
25815
26276
  logger.debug("All adapters cleared");
25816
26277
  }
25817
26278
  /**
25818
- * Set the mode for determining which adapters to run
25819
- */
25820
- setMode(mode) {
25821
- this.mode = mode;
25822
- }
25823
- /**
25824
26279
  * Export spans using all configured adapters
25825
26280
  */
25826
26281
  export(spans, resultCallback) {
26282
+ if (this.mode !== TuskDriftMode.RECORD) {
26283
+ resultCallback({ code: import_src$5.ExportResultCode.SUCCESS });
26284
+ return;
26285
+ }
25827
26286
  logger.debug(`TdSpanExporter.export() called with ${spans.length} span(s)`);
25828
26287
  const traceBlockingManager = TraceBlockingManager.getInstance();
25829
26288
  const filteredSpansBasedOnLibraryName = spans.filter((span) => {
@@ -25862,21 +26321,16 @@ var TdSpanExporter = class {
25862
26321
  resultCallback({ code: import_src$5.ExportResultCode.SUCCESS });
25863
26322
  return;
25864
26323
  }
25865
- const activeAdapters = this.getActiveAdapters();
25866
- if (activeAdapters.length === 0) {
26324
+ if (this.adapters.length === 0) {
25867
26325
  logger.debug(`No active adapters for mode: ${this.mode}`);
25868
26326
  resultCallback({ code: import_src$5.ExportResultCode.SUCCESS });
25869
26327
  return;
25870
26328
  }
25871
- Promise.all(activeAdapters.map((adapter) => adapter.exportSpans(cleanSpans))).then(() => resultCallback({ code: import_src$5.ExportResultCode.SUCCESS })).catch((error) => resultCallback({
26329
+ Promise.all(this.adapters.map((adapter) => adapter.exportSpans(cleanSpans))).then(() => resultCallback({ code: import_src$5.ExportResultCode.SUCCESS })).catch((error) => resultCallback({
25872
26330
  code: import_src$5.ExportResultCode.FAILED,
25873
26331
  error
25874
26332
  }));
25875
26333
  }
25876
- getActiveAdapters() {
25877
- if (this.mode !== TuskDriftMode.RECORD) return this.adapters.filter((adapter) => adapter.name === "in-memory" || adapter.name === "callback");
25878
- return this.adapters;
25879
- }
25880
26334
  /**
25881
26335
  * Shutdown all adapters
25882
26336
  */
@@ -33007,7 +33461,7 @@ var require_src = /* @__PURE__ */ __commonJS({ "node_modules/@opentelemetry/sdk-
33007
33461
  //#endregion
33008
33462
  //#region package.json
33009
33463
  var import_src$1 = /* @__PURE__ */ __toESM(require_src(), 1);
33010
- var version = "0.1.21";
33464
+ var version = "0.1.22";
33011
33465
 
33012
33466
  //#endregion
33013
33467
  //#region src/version.ts