From 36ef2c3fd706bfae7b873081dd43b33a1ab00e5a Mon Sep 17 00:00:00 2001 From: D050513 Date: Wed, 8 Jan 2025 14:51:20 +0100 Subject: [PATCH] feat: support driver self-wrapping (for Dynatrace) --- hana/lib/drivers/dynatrace.js | 12 +++++++++--- hana/lib/drivers/hana-client.js | 4 ++-- hana/lib/drivers/hdb.js | 4 ++-- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/hana/lib/drivers/dynatrace.js b/hana/lib/drivers/dynatrace.js index 64451a3da..d506c2cdd 100644 --- a/hana/lib/drivers/dynatrace.js +++ b/hana/lib/drivers/dynatrace.js @@ -8,7 +8,7 @@ try { // If module was not required, do not do anything } -const isDynatraceEnabled = () => { +const _shall_wrap = () => { return dynatrace.sdk !== undefined && !process.env.CDS_SKIP_DYNATRACE } @@ -65,7 +65,7 @@ const _preparedStmtUsingDynatrace = function (client, prepareFn, dbInfo) { } } -const dynatraceClient = (client, credentials, tenant) => { +const _wrapped = (client, credentials, tenant) => { const dbInfo = { name: `SAPHANA${tenant ? `-${tenant}` : ''}`, vendor: dynatrace.sdk.DatabaseVendor.HANADB, @@ -88,4 +88,10 @@ const dynatraceClient = (client, credentials, tenant) => { return client } -module.exports = { dynatraceClient, isDynatraceEnabled } +module.exports = { + wrap_client: (client, credentials, tenant) => { + if (client.isDynatraceSupported) return client //> client will wrap itself + if (!_shall_wrap()) return client + return _wrapped(client, credentials, tenant) + } +} diff --git a/hana/lib/drivers/hana-client.js b/hana/lib/drivers/hana-client.js index d6291b753..ab669fbd3 100644 --- a/hana/lib/drivers/hana-client.js +++ b/hana/lib/drivers/hana-client.js @@ -3,7 +3,7 @@ const { Readable, Stream } = require('stream') const cds = require('@sap/cds') const hdb = require('@sap/hana-client') const { driver, prom, handleLevel } = require('./base') -const { isDynatraceEnabled: dt_sdk_is_present, dynatraceClient: wrap_client } = require('./dynatrace') +const { wrap_client } = require('./dynatrace') const LOG = cds.log('@sap/hana-client') if (process.env.NODE_ENV === 'production' && !process.env.HDB_NODEJS_THREADPOOL_SIZE && !process.env.UV_THREADPOOL_SIZE) LOG.warn("When using @sap/hana-client, it's strongly recommended to adjust its thread pool size with environment variable `HDB_NODEJS_THREADPOOL_SIZE`, otherwise it might lead to performance issues.\nLearn more: https://help.sap.com/docs/SAP_HANA_CLIENT/f1b440ded6144a54ada97ff95dac7adf/31a8c93a574b4f8fb6a8366d2c758f21.html") @@ -44,7 +44,7 @@ class HANAClientDriver extends driver { super(creds) this._native = hdb.createConnection(creds) - if (dt_sdk_is_present()) this._native = wrap_client(this._native, creds, creds.tenant) + this._native = wrap_client(this._native, creds, creds.tenant) this._native.setAutoCommit(false) } diff --git a/hana/lib/drivers/hdb.js b/hana/lib/drivers/hdb.js index 23d2d339d..39d7a9fb8 100644 --- a/hana/lib/drivers/hdb.js +++ b/hana/lib/drivers/hdb.js @@ -7,7 +7,7 @@ const hdb = require('hdb') const iconv = require('iconv-lite') const { driver, prom, handleLevel } = require('./base') -const { isDynatraceEnabled: dt_sdk_is_present, dynatraceClient: wrap_client } = require('./dynatrace') +const { wrap_client } = require('./dynatrace') if (cds.env.features.sql_simple_queries === 3) { // Make hdb return true / false @@ -44,7 +44,7 @@ class HDBDriver extends driver { super(creds) this._native = hdb.createClient(creds) - if (dt_sdk_is_present()) this._native = wrap_client(this._native, creds, creds.tenant) + this._native = wrap_client(this._native, creds, creds.tenant) this._native.setAutoCommit(false) this._native.on('close', () => this.destroy?.())