-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into no-false-unique-constraint-errors
- Loading branch information
Showing
6 changed files
with
115 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// COPIED AS IS (excluding unused code) FROM @sap/cds | ||
|
||
const dynatrace = {} | ||
try { | ||
dynatrace.sdk = require('@dynatrace/oneagent-sdk') | ||
dynatrace.api = dynatrace.sdk.createInstance() | ||
} catch { | ||
// If module was not required, do not do anything | ||
} | ||
|
||
const isDynatraceEnabled = () => { | ||
return dynatrace.sdk !== undefined && !process.env.CDS_SKIP_DYNATRACE | ||
} | ||
|
||
const _dynatraceResultCallback = function (tracer, cb) { | ||
return function (err, ...args) { | ||
const results = args.shift() | ||
if (err) { | ||
tracer.error(err) | ||
} else { | ||
tracer.setResultData({ | ||
rowsReturned: (results && results.length) || results | ||
}) | ||
} | ||
tracer.end(cb, err, results, ...args) | ||
} | ||
} | ||
|
||
const _execUsingDynatrace = (client, execFn, dbInfo) => { | ||
// args = [sql, options, callback] --> options is optional | ||
return function (...args) { | ||
const cb = args[args.length - 1] | ||
|
||
const tracer = dynatrace.api.traceSQLDatabaseRequest(dbInfo, { | ||
statement: args[0] | ||
}) | ||
|
||
tracer.startWithContext(execFn, client, ...args.slice(0, args.length - 1), _dynatraceResultCallback(tracer, cb)) | ||
} | ||
} | ||
|
||
const _preparedStmtUsingDynatrace = function (client, prepareFn, dbInfo) { | ||
// args = [sql, options, callback] --> options is optional | ||
return function (...args) { | ||
const cb = args[args.length - 1] | ||
|
||
const tracer = dynatrace.api.traceSQLDatabaseRequest(dbInfo, { | ||
statement: args[0] | ||
}) | ||
|
||
tracer.startWithContext(prepareFn, client, ...args.slice(0, args.length - 1), (err, stmt) => { | ||
if (err) { | ||
tracer.error(err) | ||
tracer.end(cb, err) | ||
} else { | ||
// same here. hana-client does not like decorating | ||
const originalExecFn = stmt.exec | ||
stmt.exec = function (...args) { | ||
const stmtCb = args[args.length - 1] | ||
originalExecFn.call(stmt, ...args.slice(0, args.length - 1), _dynatraceResultCallback(tracer, stmtCb)) | ||
} | ||
cb(null, stmt) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
const dynatraceClient = (client, credentials, tenant) => { | ||
const dbInfo = { | ||
name: `SAPHANA${tenant ? `-${tenant}` : ''}`, | ||
vendor: dynatrace.sdk.DatabaseVendor.HANADB, | ||
host: credentials.host, | ||
port: Number(credentials.port) | ||
} | ||
|
||
// hana-client does not like decorating. | ||
// because of that, we need to override the fn and pass the original fn for execution | ||
const originalExecFn = client.exec | ||
client.exec = _execUsingDynatrace(client, originalExecFn, dbInfo) | ||
const originalPrepareFn = client.prepare | ||
if (client.name === '@sap/hana-client') { | ||
// client.prepare = ... doesn't work for hana-client | ||
Object.defineProperty(client, 'prepare', { value: _preparedStmtUsingDynatrace(client, originalPrepareFn, dbInfo) }) | ||
} else { | ||
client.prepare = _preparedStmtUsingDynatrace(client, originalPrepareFn, dbInfo) | ||
} | ||
|
||
return client | ||
} | ||
|
||
module.exports = { dynatraceClient, isDynatraceEnabled } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters